11
11
import java .sql .ResultSet ;
12
12
import java .sql .SQLException ;
13
13
import java .sql .Statement ;
14
+ import java .sql .Timestamp ;
14
15
import java .util .ArrayList ;
15
16
import java .util .Iterator ;
16
17
import java .util .List ;
40
41
41
42
/**
42
43
* Access method for a JDBC database. Exposes data as TupleBatches.
43
- *
44
- *
44
+ *
45
+ *
45
46
*/
46
47
public final class JdbcAccessMethod extends AccessMethod {
47
48
@@ -54,7 +55,7 @@ public final class JdbcAccessMethod extends AccessMethod {
54
55
55
56
/**
56
57
* The constructor. Creates an object and connects with the database
57
- *
58
+ *
58
59
* @param jdbcInfo connection information
59
60
* @param readOnly whether read-only connection or not
60
61
* @throws DbException if there is an error making the connection.
@@ -106,7 +107,7 @@ public void setReadOnly(final Boolean readOnly) throws DbException {
106
107
107
108
/**
108
109
* Helper function to copy data into PostgreSQL using the COPY command.
109
- *
110
+ *
110
111
* @param relationKey the destination relation
111
112
* @param schema the schema of the relation
112
113
* @param tupleBatch the tuples to be inserted.
@@ -168,9 +169,34 @@ public void tupleBatchInsert(final RelationKey relationKey, final Schema schema,
168
169
/* Set up and execute the query */
169
170
final PreparedStatement statement =
170
171
jdbcConnection .prepareStatement (insertStatementFromSchema (schema , relationKey ));
171
- tupleBatch .getIntoJdbc (statement );
172
- // TODO make it also independent. should be getIntoJdbc(statement,
173
- // tupleBatch)
172
+ for (int row = 0 ; row < tupleBatch .numTuples (); ++row ) {
173
+ for (int col = 0 ; col < tupleBatch .numColumns (); ++col ) {
174
+ switch (schema .getColumnType (col )) {
175
+ case BOOLEAN_TYPE :
176
+ statement .setBoolean (col + 1 , tupleBatch .getBoolean (col , row ));
177
+ break ;
178
+ case DATETIME_TYPE :
179
+ statement .setTimestamp (col + 1 , new Timestamp (tupleBatch .getDateTime (col , row ).getMillis ()));
180
+ break ;
181
+ case DOUBLE_TYPE :
182
+ statement .setDouble (col + 1 , tupleBatch .getDouble (col , row ));
183
+ break ;
184
+ case FLOAT_TYPE :
185
+ statement .setFloat (col + 1 , tupleBatch .getFloat (col , row ));
186
+ break ;
187
+ case INT_TYPE :
188
+ statement .setInt (col + 1 , tupleBatch .getInt (col , row ));
189
+ break ;
190
+ case LONG_TYPE :
191
+ statement .setLong (col + 1 , tupleBatch .getLong (col , row ));
192
+ break ;
193
+ case STRING_TYPE :
194
+ statement .setString (col + 1 , tupleBatch .getString (col , row ));
195
+ break ;
196
+ }
197
+ }
198
+ statement .addBatch ();
199
+ }
174
200
statement .executeBatch ();
175
201
statement .close ();
176
202
} catch (final SQLException e ) {
@@ -253,7 +279,7 @@ public void createTableIfNotExists(final RelationKey relationKey, final Schema s
253
279
254
280
/**
255
281
* Create an unlogged table.
256
- *
282
+ *
257
283
* @param relationKey the relation name
258
284
* @param schema the relation schema
259
285
* @throws DbException if anything goes wrong
@@ -333,7 +359,7 @@ private String createIfNotExistsStatementFromSchema(final Schema schema, final R
333
359
334
360
/**
335
361
* Helper utility for creating JDBC CREATE TABLE statements.
336
- *
362
+ *
337
363
* @param type a Myria column type.
338
364
* @param dbms the description of the DBMS, e.g., "mysql".
339
365
* @return the name of the DBMS type that matches the given Myria type.
@@ -494,7 +520,7 @@ public void createIndexIfNotExists(final RelationKey relationKey, final Schema s
494
520
495
521
/**
496
522
* Create an index in postgres if no index with the same name already exists.
497
- *
523
+ *
498
524
* @param relationKey the table on which the indexes will be created.
499
525
* @param schema the Schema of the data in the table.
500
526
* @param index the index to be created; each entry is a list of column indices.
@@ -519,7 +545,7 @@ public void createIndexIfNotExistPostgres(final RelationKey relationKey, final S
519
545
520
546
/**
521
547
* Wraps a JDBC ResultSet in a Iterator<TupleBatch>.
522
- *
548
+ *
523
549
* Implementation based on org.apache.commons.dbutils.ResultSetIterator. Requires ResultSet.isLast() to be implemented.
524
550
*/
525
551
class JdbcTupleBatchIterator implements Iterator <TupleBatch > {
@@ -534,7 +560,7 @@ class JdbcTupleBatchIterator implements Iterator<TupleBatch> {
534
560
535
561
/**
536
562
* Constructs a JdbcTupleBatchIterator from the given ResultSet and Schema objects.
537
- *
563
+ *
538
564
* @param resultSet the JDBC ResultSet containing the results.
539
565
* @param schema the Schema of the generated TupleBatch objects.
540
566
*/
0 commit comments