1 23 24 29 30 package com.sun.jdo.spi.persistence.generator.database; 31 32 import java.io.*; 33 import java.util.*; 34 import java.sql.*; 35 36 import org.netbeans.modules.dbschema.*; 37 38 43 48 public class DDLGenerator { 49 50 51 private static final char SPACE = ' '; 53 54 private static final char START = '('; 56 57 private static final char END = ')'; 59 60 private static final String COLUMN_SEPARATOR = ", "; 62 63 private static final String NOT_NULL_STRING = "NOT NULL"; 65 66 private static final String NULL_STRING = "NULL"; 68 69 72 104 public static void generateDDL(SchemaElement schema, String dbVendorName, 111 OutputStream createDDLSql, OutputStream dropDDLSql, 112 OutputStream dropDDLJdbc, OutputStream createDDLJdbc, 113 OutputStream dbStream, boolean dropAndCreateTbl) 114 throws DBException, SQLException, IOException { 115 116 if (schema != null) { 117 MappingPolicy mappingPolicy = MappingPolicy.getMappingPolicy( 118 dbVendorName); 119 DDLTemplateFormatter.reset(mappingPolicy); 120 String schemaName = schema.getName().getName(); 121 List createAllTblDDL = new ArrayList(); 122 List alterAddConstraintsDDL = new ArrayList(); 123 List alterDropConstraintsDDL = new ArrayList(); 124 List dropAllTblDDL = new ArrayList(); 125 TableElement[] tables = schema.getTables(); 126 127 if (tables != null) { 128 for (int ii = 0; ii < tables.length; ii++) { 129 TableElement table = tables[ii]; 130 131 createAllTblDDL.add( 132 createCreateTableDDL(table, mappingPolicy)); 133 alterAddConstraintsDDL.addAll( 134 createAddConstraintsDDL(table)); 135 alterDropConstraintsDDL.addAll( 136 createDropConstraintsDDL(table)); 137 dropAllTblDDL.add( 138 createDropTableDDL(table)); 139 } 140 } 141 String stmtSeparator = mappingPolicy.getStatementSeparator(); 142 generateSQL(createDDLSql, dropDDLSql, dropDDLJdbc, createDDLJdbc, 143 (DatabaseOutputStream) dbStream, createAllTblDDL, 144 alterAddConstraintsDDL, alterDropConstraintsDDL, dropAllTblDDL, 145 stmtSeparator, dropAndCreateTbl); 146 } 147 } 148 149 165 private static void generateSQL(OutputStream createSql, 169 OutputStream dropSql, OutputStream dropTxt, OutputStream createTxt, 170 DatabaseOutputStream dbStream, List createAllTblDDL, 171 List alterAddConstraintsDDL, List alterDropConstraintsDDL, 172 List dropAllTblDDL, String stmtSeparator, boolean dropAndCreateTbl) 173 throws DBException, SQLException { 174 175 PrintStream workStream = null; 176 PrintStream txtStream = null; 177 178 try { 179 workStream = new PrintStream(dropSql); 181 if (dropTxt != null) { 182 txtStream = new PrintStream(dropTxt); 183 } 184 for (int i = 0; i < alterDropConstraintsDDL.size(); i++) { 185 String dropConstStmt = (String ) alterDropConstraintsDDL.get(i); 186 writeDDL(workStream, txtStream, stmtSeparator, dropConstStmt); 187 if ((dbStream != null) && dropAndCreateTbl) { 188 dbStream.write(dropConstStmt); 189 } 190 } 191 192 for (int i = 0; i < dropAllTblDDL.size(); i++) { 194 String dropTblStmt = (String ) dropAllTblDDL.get(i); 195 writeDDL(workStream, txtStream, stmtSeparator, dropTblStmt); 196 if ((dbStream != null) && dropAndCreateTbl) { 197 dbStream.write(dropTblStmt); 198 } 199 } 200 workStream.close(); 201 if (txtStream != null) { 202 txtStream.close(); 203 } 204 205 workStream = new PrintStream(createSql); 207 if (createTxt != null) { 208 txtStream = new PrintStream(createTxt); 209 } 210 211 for (int i = 0; i < createAllTblDDL.size(); i++) { 213 StringBuffer createStmt = new StringBuffer (); 214 String [] createTbl = (String []) createAllTblDDL.get(i); 215 216 for (int j = 0; j < createTbl.length; j++) { 217 workStream.println(createTbl[j]); 218 createStmt.append(createTbl[j]); 219 } 220 workStream.println(stmtSeparator); 221 if (txtStream != null) { 222 txtStream.println(createStmt); 223 } 224 if (dbStream != null) { 225 dbStream.write(createStmt.toString()); 226 } 227 } 228 229 for (int i = 0; i < alterAddConstraintsDDL.size(); i++) { 231 String stmt = (String )alterAddConstraintsDDL.get(i); 232 writeDDL(workStream, txtStream, stmtSeparator, stmt); 233 if (dbStream != null) { 234 dbStream.write(stmt); 235 } 236 } 237 238 workStream.close(); 239 if (txtStream != null) { 240 txtStream.close(); 241 } 242 243 if (dbStream != null) { 244 dbStream.flush(); 245 } 246 } finally { 247 if (workStream != null) { 248 workStream.close(); 249 } 250 if (txtStream != null) { 251 txtStream.close(); 252 } 253 } 254 } 255 256 264 private static void writeDDL(PrintStream sql, PrintStream txt, 265 String stmtSeparator, String stmt) { 266 267 sql.println(stmt); 268 sql.println(stmtSeparator); 269 270 if (txt != null) { 272 txt.println(stmt); 273 } 274 } 275 276 279 294 private static String [] createCreateTableDDL(TableElement table, 295 MappingPolicy mappingPolicy) { 296 297 List createTblList = new ArrayList(); 298 String [] oneParam = { table.getName().getName() }; 299 300 createTblList.add( 301 DDLTemplateFormatter.formatCreateTable(oneParam)); 302 303 ColumnElement[] columns = table.getColumns(); 305 String constraint = createPrimaryKeyConstraint(table); 306 int size = columns.length; 307 308 for (int i = 0; i < size; i++) { 309 StringBuffer columnContent = new StringBuffer (); 310 columnContent.append(getColumnDef(columns[i], mappingPolicy)); 311 312 if ((i < size - 1) || ((i == size - 1) && (constraint != null))) { 315 columnContent.append(COLUMN_SEPARATOR); 316 } 317 createTblList.add(columnContent.toString()); 318 } 319 320 if (constraint != null) { 321 createTblList.add(constraint); 322 } 323 createTblList.add(mappingPolicy.getCreateTableEnd()); 324 325 return (String []) createTblList.toArray( 326 new String [createTblList.size()]); 327 } 328 329 338 private static String createDropTableDDL(TableElement table) { 339 String [] oneParam = { table.getName().getName() }; 340 return DDLTemplateFormatter.formatDropTable(oneParam); 341 } 342 343 352 private static String createPrimaryKeyConstraint(TableElement table) { 353 String rc = null; 354 UniqueKeyElement pk = table.getPrimaryKey(); 355 356 if (pk != null) { 357 String [] twoParams = new String [2]; 358 twoParams[0] = pk.getName().getName(); 359 twoParams[1] = getColumnNames(pk.getColumns()); 360 rc = DDLTemplateFormatter.formatPKConstraint(twoParams); 361 } 362 return rc; 363 } 364 365 376 private static List createAddConstraintsDDL(TableElement table) { 377 List alterTblDDL = new ArrayList(); 378 String [] fourParams = new String [4]; 379 String [] oneParam = { table.getName().getName() }; 380 381 ForeignKeyElement[] fkeys = table.getForeignKeys(); 383 if (fkeys != null) { 384 String alterTblString = 385 DDLTemplateFormatter.formatAlterTableAddConstraint(oneParam); 386 387 for (int jj=0; jj < fkeys.length; jj++) { 388 ForeignKeyElement fkey = fkeys[jj]; 389 fourParams[0] = fkey.getName().getName(); 390 fourParams[1] = getColumnNames(fkey.getLocalColumns()); 391 fourParams[2] = fkey.getReferencedTable().getName().getName(); 392 fourParams[3] = getColumnNames(fkey.getReferencedColumns()); 393 394 StringBuffer alterTblDDLString = new StringBuffer ( 395 alterTblString); 396 alterTblDDLString.append(SPACE); 397 alterTblDDLString.append( 398 DDLTemplateFormatter.formatFKConstraint(fourParams)); 399 alterTblDDL.add(alterTblDDLString.toString()); 400 } 401 } 402 return alterTblDDL; 403 } 404 405 413 private static List createDropConstraintsDDL(TableElement table) { 414 List alterTbls = new ArrayList(); 415 String [] twoParams = new String [2]; 416 twoParams[0] = table.getName().getName(); 417 ForeignKeyElement[] fkeys = table.getForeignKeys(); 418 419 if (fkeys != null) { 420 for (int i = 0; i < fkeys.length; i++) { 421 twoParams[1] = fkeys[i].getName().getName(); 422 String alterTblString = 423 DDLTemplateFormatter.formatAlterTableDropConstraint( 424 twoParams); 425 alterTbls.add(alterTblString); 426 } 427 } 428 return alterTbls; 429 } 430 431 444 private static String getColumnDef(ColumnElement column, 445 MappingPolicy mappingPolicy) { 446 447 int jdbcType = column.getType(); 448 Integer scale = column.getScale(); 449 Integer precision = column.getPrecision(); 450 Integer length = column.getLength(); 451 String sqlType = mappingPolicy.getSQLTypeName(jdbcType); 452 StringBuffer columnContent = new StringBuffer (); 453 454 columnContent.append(column.getName().getName()); 455 columnContent.append(SPACE); 456 columnContent.append(sqlType); 457 if (column.isCharacterType() && length != null) { 458 columnContent.append(START); 459 columnContent.append(length.toString()); 460 columnContent.append(END); 461 462 } else if (column.isNumericType() && precision != null) { 463 columnContent.append(START); 464 columnContent.append(precision.toString()); 465 if (scale != null) { 466 columnContent.append(COLUMN_SEPARATOR); 467 columnContent.append(scale.toString()); 468 } 469 columnContent.append(END); 470 471 } else if (column.isBlobType() && length != null) { 472 columnContent.append(START); 473 columnContent.append(length.toString()); 474 columnContent.append(END); 475 } 476 477 if (jdbcType == Types.BLOB || jdbcType == Types.CLOB) { 479 String lobText = mappingPolicy.getLobLogging(); 480 if (lobText != null && lobText.length() > 0) { 481 columnContent.append(SPACE).append(lobText); 482 } 483 } 484 485 if (column.isNullable()) { 487 String nullText = mappingPolicy.getColumnNullability(); 488 if (nullText != null && nullText.length() > 0) { 489 columnContent.append(SPACE).append(nullText); 490 } 491 } else { 492 columnContent.append(SPACE).append(NOT_NULL_STRING); 493 } 494 495 return columnContent.toString(); 496 } 497 498 504 private static String getColumnNames(ColumnElement[] columns) { 505 StringBuffer columnNames = new StringBuffer (); 506 for (int i = 0; i < columns.length; i++) { 507 if (i > 0) { 508 columnNames.append(COLUMN_SEPARATOR); 509 } 510 columnNames.append(columns[i].getName().getName()); 511 } 512 return columnNames.toString(); 513 } 514 } 515 | Popular Tags |