📄 transferdb.java
字号:
Vector getCatalog() throws DataAccessPointException { Vector ret = new Vector(); ResultSet result = null; if (databaseToConvert != null && databaseToConvert.length() > 0) { ret.addElement(databaseToConvert); return (ret); } try { result = meta.getCatalogs(); } catch (SQLException e) { result = null; } try { if (result != null) { while (result.next()) { ret.addElement(result.getString(1)); } result.close(); } } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } return (ret); } void setCatalog(String sCatalog) throws DataAccessPointException { if (sCatalog != null && sCatalog.length() > 0) { try { conn.setCatalog(sCatalog); } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } } } Vector getTables(String sCatalog, String[] sSchemas) throws DataAccessPointException { Vector tTable = new Vector(); ResultSet result = null; tracer.trace("Reading source tables"); int nbloops = 1; if (sSchemas != null) { nbloops = sSchemas.length; } try {// variations return null or emtpy result sets with informix JDBC driver 2.2 for (int SchemaIdx = 0; SchemaIdx < nbloops; SchemaIdx++) { if (sSchemas != null && sSchemas[SchemaIdx] != null) { result = meta.getTables(sCatalog, sSchemas[SchemaIdx], null, null); } else { try { result = meta.getTables(sCatalog, "", null, null); } catch (SQLException e) { result = meta.getTables(sCatalog, null, null, null); } } while (result.next()) { String name = result.getString(3); String type = result.getString(4); String schema = ""; if (sSchemas != null && sSchemas[SchemaIdx] != null) { schema = sSchemas[SchemaIdx]; } /* ** we ignore the following table types: ** "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY" ** "ALIAS", "SYNONYM" */ if ((type.compareTo("TABLE") == 0) || (type.compareTo("VIEW") == 0)) { TransferTable t = new TransferTable(this, name, schema, type, tracer); tTable.addElement(t); } else { tracer.trace("Found table of type :" + type + " - this type is ignored"); } } } } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } finally { if (result != null) { try { result.close(); } catch (SQLException e) {} } } return (tTable); } void getTableStructure(TransferTable TTable, DataAccessPoint Dest) throws DataAccessPointException { String create = "CREATE " + TTable.Stmts.sType + " " + Dest.helper.formatName(TTable.Stmts.sDestTable); String insert = ""; ResultSet ImportedKeys = null; boolean importedkeys = false; String alterCreate = new String(""); String alterDrop = new String(""); String ConstraintName = new String(""); String RefTableName = new String(""); String foreignKeyName = new String(""); String columnName = new String(""); TTable.Stmts.sDestDrop = "DROP " + TTable.Stmts.sType + " " + Dest.helper.formatName(TTable.Stmts.sDestTable) + ";"; if (TTable.Stmts.sType.compareTo("TABLE") == 0) { TTable.Stmts.sDestDelete = "DELETE FROM " + Dest.helper.formatName(TTable.Stmts.sDestTable) + ";"; create += "("; } else if (TTable.Stmts.sType.compareTo("VIEW") == 0) { TTable.Stmts.bDelete = false; TTable.Stmts.sDestDelete = ""; create += " AS SELECT "; } if (TTable.Stmts.sType.compareTo("TABLE") == 0) { insert = "INSERT INTO " + Dest.helper.formatName(TTable.Stmts.sDestTable) + " VALUES("; } else if (TTable.Stmts.sType.compareTo("VIEW") == 0) { TTable.Stmts.bInsert = false; insert = ""; } if (TTable.Stmts.sType.compareTo("VIEW") == 0) { /* ** Don't know how to retrieve the underlying select so we leave here. ** The user will have to edit the rest of the create statement. */ TTable.Stmts.bTransfer = false; TTable.Stmts.bCreate = true; TTable.Stmts.bDelete = false; TTable.Stmts.bDrop = true; TTable.Stmts.bCreateIndex = false; TTable.Stmts.bDropIndex = false; TTable.Stmts.bInsert = false; TTable.Stmts.bAlter = false; return; } ImportedKeys = null; try { ImportedKeys = meta.getImportedKeys(TTable.Stmts.sDatabaseToConvert, TTable.Stmts.sSchema, TTable.Stmts.sSourceTable); } catch (SQLException e) { ImportedKeys = null; } try { if (ImportedKeys != null) { while (ImportedKeys.next()) { importedkeys = true; if (!ImportedKeys.getString(12).equals(ConstraintName)) { if (!ConstraintName.equals("")) { alterCreate += Dest.helper .formatIdentifier(columnName .substring(0, columnName .length() - 1)) + ") REFERENCES " + Dest.helper .formatName(RefTableName); if (foreignKeyName.length() > 0) { alterCreate += " (" + Dest.helper.formatIdentifier( foreignKeyName.substring( 0, foreignKeyName.length() - 1)) + ")"; } alterCreate += ";"; alterDrop = alterDrop.substring(0, alterDrop.length() - 1) + ";"; foreignKeyName = ""; columnName = ""; } RefTableName = ImportedKeys.getString(3); ConstraintName = ImportedKeys.getString(12); alterCreate += "ALTER TABLE " + Dest.helper.formatName(TTable.Stmts.sDestTable) + " ADD CONSTRAINT "; if ((TTable.Stmts.bFKForced) && (!ConstraintName.startsWith("FK_"))) { alterCreate += Dest.helper.formatIdentifier( "FK_" + ConstraintName) + " "; } else { alterCreate += Dest.helper.formatIdentifier(ConstraintName) + " "; } alterCreate += "FOREIGN KEY ("; alterDrop += "ALTER TABLE " + Dest.helper.formatName(TTable.Stmts.sDestTable) + " DROP CONSTRAINT "; if ((TTable.Stmts.bFKForced) && (!ConstraintName.startsWith("FK_"))) { alterDrop += Dest.helper.formatIdentifier( "FK_" + ConstraintName) + " "; } else { alterDrop += Dest.helper.formatIdentifier(ConstraintName) + " "; } } columnName += ImportedKeys.getString(8) + ","; foreignKeyName += ImportedKeys.getString(4) + ","; } ImportedKeys.close(); } if (importedkeys) { alterCreate += columnName.substring(0, columnName.length() - 1) + ") REFERENCES " + Dest.helper.formatName(RefTableName); if (foreignKeyName.length() > 0) { alterCreate += " (" + Dest.helper.formatIdentifier( foreignKeyName.substring( 0, foreignKeyName.length() - 1)) + ")"; } alterCreate += ";"; alterDrop = alterDrop.substring(0, alterDrop.length() - 1) + ";"; TTable.Stmts.sDestDrop = alterDrop + TTable.Stmts.sDestDrop; } } catch (SQLException e) { throw new DataAccessPointException(e.getMessage()); } boolean primarykeys = false; String PrimaryKeysConstraint = new String(); PrimaryKeysConstraint = ""; ResultSet PrimaryKeys = null; try { PrimaryKeys = meta.getPrimaryKeys(TTable.Stmts.sDatabaseToConvert, TTable.Stmts.sSchema, TTable.Stmts.sSourceTable); } catch (SQLException e) { PrimaryKeys = null; } try { if (PrimaryKeys != null) { while (PrimaryKeys.next()) { if (primarykeys) { PrimaryKeysConstraint += ", "; } else { if (PrimaryKeys.getString(6) != null) { PrimaryKeysConstraint = " CONSTRAINT " + Dest.helper.formatIdentifier( PrimaryKeys.getString(6)); } PrimaryKeysConstraint += " PRIMARY KEY ("; } PrimaryKeysConstraint += Dest.helper.formatIdentifier( PrimaryKeys.getString(4)); primarykeys = true; } PrimaryKeys.close(); if (primarykeys) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -