Review Board 1.7.22


SQOOP-945 Sqoop2: Integration: Auxiliary methods that will support export functionality

Review Request #11528 - Created May 29, 2013 and submitted

Jarek Cecho
SQOOP-945
Reviewers
Sqoop
sqoop-sqoop2
I've added couple of methods to support basic export functionality and one export test case.
New integration test seems to be passing.
test/src/main/java/org/apache/sqoop/test/db/DatabaseProvider.java
Revision 72d1a95429418547efbd3566bd8143f8d759d044 New Change
[20] 19 lines
[+20]
20
import org.apache.commons.lang.StringUtils;
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
21
import org.apache.log4j.Logger;
22

    
   
22

   
23
import java.sql.Connection;
23
import java.sql.Connection;
24
import java.sql.DriverManager;
24
import java.sql.DriverManager;

    
   
25
import java.sql.ResultSet;
25
import java.sql.SQLException;
26
import java.sql.SQLException;
26
import java.sql.Statement;
27
import java.sql.Statement;
27
import java.util.LinkedList;
28
import java.util.LinkedList;
28
import java.util.List;
29
import java.util.List;
29

    
   
30

   
[+20] [20] 163 lines
[+20] [+] public void executeUpdate(String query) {
193
      }
194
      }
194
    }
195
    }
195
  }
196
  }
196

    
   
197

   
197
  /**
198
  /**

    
   
199
   * Execute given query in a new statement object and return corresponding

    
   
200
   * result set. Caller is responsible for closing both ResultSet and Statement

    
   
201
   * object!

    
   
202
   *

    
   
203
   * @param query Query to execute

    
   
204
   * @return Generated ResultSet

    
   
205
   */

    
   
206
  public ResultSet executeQuery(String query) {

    
   
207
    LOG.info("Executing query: " + query);

    
   
208
    Statement stmt = null;

    
   
209

   

    
   
210
    try {

    
   
211
      stmt = connection.createStatement();

    
   
212
      return stmt.executeQuery(query);

    
   
213
    } catch (SQLException e) {

    
   
214
      LOG.error("Error in executing query", e);

    
   
215
      throw new RuntimeException("Error in executing query", e);

    
   
216
    }

    
   
217
  }

    
   
218

   

    
   
219
  /**
198
   * Create new table.
220
   * Create new table.
199
   *
221
   *
200
   * @param name Table name
222
   * @param name Table name
201
   * @param primaryKey Primary key column(0) or null if table should not have any
223
   * @param primaryKey Primary key column(0) or null if table should not have any
202
   * @param columns List of double values column name and value for example ... "id", "varchar(50)"...
224
   * @param columns List of double values column name and value for example ... "id", "varchar(50)"...
[+20] [20] 38 lines
[+20] [+] public void insertRow(String tableName, Object ...values) {
241
    sb.append(escapeTableName(tableName));
263
    sb.append(escapeTableName(tableName));
242
    sb.append(" VALUES (");
264
    sb.append(" VALUES (");
243

    
   
265

   
244
    List<String> valueList = new LinkedList<String>();
266
    List<String> valueList = new LinkedList<String>();
245
    for(Object value : values) {
267
    for(Object value : values) {
246
      if(value == null) {
268
      valueList.add(convertObjectToQueryString(value));
247
        valueList.add(nullConstant());

   
248
      } else if(value.getClass() == String.class) {

   
249
        valueList.add(escapeValueString((String)value));

   
250
      } else {

   
251
        valueList.add(value.toString());

   
252
      }

   
253
    }
269
    }
254

    
   
270

   
255
    sb.append(StringUtils.join(valueList, ", "));
271
    sb.append(StringUtils.join(valueList, ", "));
256
    sb.append(")");
272
    sb.append(")");
257

    
   
273

   
258
    executeUpdate(sb.toString());
274
    executeUpdate(sb.toString());
259
  }
275
  }
260

    
   
276

   
261
  /**
277
  /**

    
   
278
   * Return rows that match given conditions.

    
   
279
   *

    
   
280
   * @param tableName Table name

    
   
281
   * @param conditions Conditions in form of double values - column name and value, for example: "id", 1 or "last_update_date", null

    
   
282
   * @return ResultSet with given criteria

    
   
283
   */

    
   
284
  public ResultSet getRows(String tableName, Object []conditions) {

    
   
285
    // Columns are in form of two strings - name and value

    
   
286
    if(conditions.length % 2 != 0) {

    
   
287
      throw new RuntimeException("Incorrect number of parameters.");

    
   
288
    }

    
   
289

   

    
   
290
    StringBuilder sb = new StringBuilder("SELECT * FROM ");

    
   
291
    sb.append(escapeTableName(tableName));

    
   
292

   

    
   
293
    List<String> conditionList = new LinkedList<String>();

    
   
294
    for(int i = 0; i < conditions.length; i += 2) {

    
   
295
      Object columnName = conditions[i];

    
   
296
      Object value = conditions[i + 1];

    
   
297

   

    
   
298
      if( !(columnName instanceof String)) {

    
   
299
        throw new RuntimeException("Each odd item should be a string with column name.");

    
   
300
      }

    
   
301

   

    
   
302
      if(value == null) {

    
   
303
        conditionList.add(escapeColumnName((String) columnName) + " IS NULL");

    
   
304
      } else {

    
   
305
        conditionList.add(escapeColumnName((String) columnName) + " = " + convertObjectToQueryString(value));

    
   
306
      }

    
   
307
    }

    
   
308

   

    
   
309
    if(conditionList.size() != 0) {

    
   
310
      sb.append(" WHERE ").append(StringUtils.join(conditionList, " AND "));

    
   
311
    }

    
   
312

   

    
   
313
    return executeQuery(sb.toString());

    
   
314
  }

    
   
315

   

    
   
316
  /**

    
   
317
   * Convert given object to it's representation that can be safely used inside

    
   
318
   * query.

    
   
319
   *

    
   
320
   * @param value Value to convert

    
   
321
   * @return Query safe string representation

    
   
322
   */

    
   
323
  public String convertObjectToQueryString(Object value) {

    
   
324
    if(value == null) {

    
   
325
      return nullConstant();

    
   
326
    } else if(value.getClass() == String.class) {

    
   
327
      return escapeValueString((String)value);

    
   
328
    } else {

    
   
329
      return value.toString();

    
   
330
    }

    
   
331
  }

    
   
332

   

    
   
333
  /**
262
   * Drop table.
334
   * Drop table.
263
   *
335
   *
264
   * Any exceptions will be ignored.
336
   * Any exceptions will be ignored.
265
   *
337
   *
266
   * @param tableName
338
   * @param tableName
[+20] [20] 8 lines
[+20] [+] public void dropTable(String tableName) {
275
      LOG.info("Ignoring exception: " + e);
347
      LOG.info("Ignoring exception: " + e);
276
    }
348
    }
277
  }
349
  }
278

    
   
350

   
279
  /**
351
  /**

    
   
352
   * Return number of rows from given table.

    
   
353
   *

    
   
354
   * @param tableName Table name

    
   
355
   * @return Number of rows

    
   
356
   */

    
   
357
  public long rowCount(String tableName) {

    
   
358
    StringBuilder sb = new StringBuilder("SELECT COUNT(*) FROM ");

    
   
359
    sb.append(escapeTableName(tableName));

    
   
360

   

    
   
361
    ResultSet rs = null;

    
   
362
    try {

    
   
363
      rs = executeQuery(sb.toString());

    
   
364
      if(!rs.next()) {

    
   
365
        throw new RuntimeException("Row count query did not returned any rows.");

    
   
366
      }

    
   
367

   

    
   
368
      return rs.getLong(1);

    
   
369
    } catch (SQLException e) {

    
   
370
      LOG.error("Can't get number of rows: ", e);

    
   
371
      throw new RuntimeException("Can't get number of rows: ", e);

    
   
372
    } finally {

    
   
373
      closeResultSetWithStatement(rs);

    
   
374
    }

    
   
375
  }

    
   
376

   

    
   
377
  /**

    
   
378
   * Close given result set (if not null) and associated statement.

    
   
379
   *

    
   
380
   * @param rs ResultSet to close.

    
   
381
   */

    
   
382
  public void closeResultSetWithStatement(ResultSet rs) {

    
   
383
    if(rs != null) {

    
   
384
      try {

    
   
385
        Statement stmt = rs.getStatement();

    
   
386
        rs.close();

    
   
387
        stmt.close();

    
   
388
      } catch (SQLException e) {

    
   
389
        LOG.info("Ignoring exception: ", e);

    
   
390
      }

    
   
391
    }

    
   
392
  }

    
   
393

   

    
   
394
  /**
280
   * Load class.
395
   * Load class.
281
   *
396
   *
282
   * @param className Class name
397
   * @param className Class name
283
   */
398
   */
284
  public void loadClass(String className) {
399
  public void loadClass(String className) {
285
    try {
400
    try {
286
      Class.forName(className);
401
      Class.forName(className);
287
    } catch (ClassNotFoundException e) {
402
    } catch (ClassNotFoundException e) {
288
      throw new RuntimeException("Class not found: " + className, e);
403
      throw new RuntimeException("Class not found: " + className, e);
289
    }
404
    }
290
  }
405
  }
291
}
406
}
test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java
Revision 6eb31845fd9c000ec3667581c58754934ba9d2b8 New Change
 
test/src/test/java/org/apache/sqoop/integration/connector/ConnectorTestCase.java
Revision cdc3bd295e6dd041d6a17bc6dc8df90b8a2fc768 New Change
 
test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
New File
 
  1. test/src/main/java/org/apache/sqoop/test/db/DatabaseProvider.java: Loading...
  2. test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java: Loading...
  3. test/src/test/java/org/apache/sqoop/integration/connector/ConnectorTestCase.java: Loading...
  4. test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java: Loading...