* database
* @throws Exception on any error
*/
public void dumpTable(PrintWriter writer, JDBCConnectionImpl conx, char quoteChar, String[] tables) throws Exception {
Connection jdbcConnection = conx.getConnection();
DatabaseMetaData dbMetaData = jdbcConnection.getMetaData();
if (tables == null) {
ResultSet rs = dbMetaData.getTables(null, null, null, null);
try {
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
String tableType = rs.getString("TABLE_TYPE");
if (tableType.equalsIgnoreCase("TABLE")) {
dumpTable(writer, conx, quoteChar, new String[] { tableName });
}
}
} finally {
rs.close();
}
} else {
for (int i = 0; i < tables.length; i++) {
String tableName = tables[i];
log.info("Dumping table creation for " + tableName);
writer.println("CREATE TABLE " + tableName + " (");
boolean first = true;
// Columns
ResultSet rs2 = dbMetaData.getColumns(null, null, tableName, "%");
try {
while (rs2.next()) {
if (first) {
first = false;
} else {
writer.println(",");
}
String columnName = rs2.getString("COLUMN_NAME");
String columnType = rs2.getString("TYPE_NAME");
int columnSize = rs2.getInt("COLUMN_SIZE");
String nullable = rs2.getString("IS_NULLABLE");
String nullString = "NULL";
if ("NO".equalsIgnoreCase(nullable)) {
nullString = "NOT NULL";
}
writer.print(" " + columnName + " " + columnType);
if (columnSize != 0) {
if (columnType.equalsIgnoreCase("varchar") && columnSize > 255) {
columnSize = 255;
}
writer.print(" (" + columnSize + ")");
}
writer.print(" " + nullString);
}
} finally {
rs2.close();
}
// Keys
try {
rs2 = dbMetaData.getPrimaryKeys(null, null, tableName);
String primaryKeyName = null;
StringBuffer primaryKeyColumns = new StringBuffer();
while (rs2.next()) {
String thisKeyName = rs2.getString("PK_NAME");
if ((thisKeyName != null && primaryKeyName == null) || (thisKeyName == null && primaryKeyName != null)