ResultSet oRSet;
ResultSetMetaData oRData;
DBColumn oCol;
String sCol;
int iCols;
ListIterator oColIterator;
String sColName;
short iSQLType;
String sTypeName;
int iPrecision;
int iDigits;
int iNullable;
int iColPos;
int iDBMS;
String sGetAllCols = "";
String sSetPKCols = "";
String sSetAllCols = "";
String sSetNoPKCols = "";
oColumns = new LinkedList<DBColumn>();
oIndexes = new LinkedList<DBIndex>();
oPrimaryKeys = new LinkedList<String>();
if (DebugFile.trace)
{
DebugFile.writeln("Begin DBTable.readColumns([DatabaseMetaData])" );
DebugFile.incIdent();
DebugFile.writeln("DatabaseMetaData.getColumns(" + sCatalog + "," + sSchema + "," + sName + ",%)");
}
if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL"))
iDBMS = JDCConnection.DBMS_POSTGRESQL;
else if (oConn.getMetaData().getDatabaseProductName().equals("Oracle"))
iDBMS = JDCConnection.DBMS_ORACLE;
else if (oConn.getMetaData().getDatabaseProductName().equals("MySQL"))
iDBMS = JDCConnection.DBMS_MYSQL;
else if (oConn.getMetaData().getDatabaseProductName().equals("ACCESS"))
iDBMS = JDCConnection.DBMS_ACCESS;
else if (oConn.getMetaData().getDatabaseProductName().equals("Microsoft SQL Server"))
iDBMS = JDCConnection.DBMS_MSSQL;
else if (oConn.getMetaData().getDatabaseProductName().equals("SQLite"))
iDBMS = JDCConnection.DBMS_SQLITE;
else
iDBMS = 0;
oStmt = oConn.createStatement();
try {
if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT * FROM " + sName + " WHERE 1=0)");
oRSet = oStmt.executeQuery("SELECT * FROM " + sName + " WHERE 1=0");
iErrCode = 0;
}
catch (SQLException sqle) {
// Patch for Oracle. DatabaseMetadata.getTables() returns table names
// that later cannot be SELECTed, so this catch ignore these system tables
oStmt.close();
oRSet = null;
if (DebugFile.trace) DebugFile.writeln("SQLException " + sName + " " + sqle.getMessage());
iErrCode = sqle.getErrorCode();
if (iErrCode==0) iErrCode=-1;
if (!sqle.getSQLState().equals("42000"))
throw new SQLException(sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode());
}
if (0==iErrCode) {
if (DebugFile.trace) DebugFile.writeln("ResultSet.getMetaData()");
oRData= oRSet.getMetaData();
iCols = oRData.getColumnCount();
if (DebugFile.trace) DebugFile.writeln("table has " + String.valueOf(iCols) + " columns");
for (int c=1; c<=iCols; c++) {
sColName = oRData.getColumnName(c).toLowerCase();
sTypeName = oRData.getColumnTypeName(c);
iSQLType = (short) oRData.getColumnType(c);
if (iDBMS==JDCConnection.DBMS_POSTGRESQL)
switch (iSQLType) {
case Types.CHAR:
case Types.VARCHAR:
iPrecision = oRData.getColumnDisplaySize(c);
break;
default:
iPrecision = oRData.getPrecision(c);
}
else {
// New for v2.0, solves bug SF887614
if (iSQLType==Types.BLOB || iSQLType==Types.CLOB)
iPrecision = 2147483647;
// end v2.0
else
iPrecision = oRData.getPrecision(c);
}
iDigits = oRData.getScale(c);
iNullable = oRData.isNullable(c);
iColPos = c;
if (5==iDBMS && iSQLType==Types.NUMERIC && iPrecision<=6 && iDigits==0) {
// Workaround for an Oracle 9i bug witch is unable to convert from Short to NUMERIC but does understand SMALLINT
oCol = new DBColumn (sName,sColName,(short) Types.SMALLINT, sTypeName, iPrecision, iDigits, iNullable,iColPos);
}
else {
oCol = new DBColumn (sName,sColName,iSQLType,sTypeName,iPrecision,iDigits,iNullable,iColPos);
}
// Establecer el comportamiento de no tocar en ningún caso los campos dt_created
// quitar este if si se desea asignarlos manualmente al insertar cada registro
if (!sColName.equals(DB.dt_created))
oColumns.add(oCol);
} // next
if (DebugFile.trace) DebugFile.writeln("ResultSet.close()");
oRSet.close();
oRSet = null;
oStmt.close();
oStmt = null;
if (5==iDBMS) /* Oracle */ {
/* getPrimaryKeys() not working properly on some Oracle versions,
use non portable implementation instead. Sergio 12-01-2004.
Code until v1.1.4:
if (DebugFile.trace)
DebugFile.writeln("DatabaseMetaData.getPrimaryKeys(null, " + sSchema.toUpperCase() + ", " + sName.toUpperCase() + ")");
oRSet = oMData.getPrimaryKeys(sCatalog, sSchema.toUpperCase(), sName.toUpperCase());
*/
oStmt = oConn.createStatement();
if (DebugFile.trace) {
if (null==sSchema)
DebugFile.writeln("Statement.executeQuery(SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "')");
else
DebugFile.writeln("Statement.executeQuery(SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.OWNER='" + sSchema.toUpperCase() + "' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "')");
}
if (null==sSchema)
oRSet = oStmt.executeQuery("SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "'");
else
oRSet = oStmt.executeQuery("SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.OWNER='" + sSchema.toUpperCase() + "' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "'");
// End new code v1.2.0
}
else if (10==iDBMS) { // Microsoft Access
oRSet=null;
}
else {
if (DebugFile.trace)
DebugFile.writeln("DatabaseMetaData.getPrimaryKeys(" + sCatalog + "," + sSchema + "," + sName + ")");
oRSet = oMData.getPrimaryKeys(sCatalog, sSchema, sName);
} // fi (iDBMS)
if (oRSet!=null) {
while (oRSet.next()) {
oPrimaryKeys.add(oRSet.getString(4).toLowerCase());
sSetPKCols += oRSet.getString(4) + "=? AND ";
} // wend
if (DebugFile.trace) DebugFile.writeln("pk cols " + sSetPKCols);
if (sSetPKCols.length()>7)
sSetPKCols = sSetPKCols.substring(0, sSetPKCols.length()-5);
if (DebugFile.trace) DebugFile.writeln("ResultSet.close()");
oRSet.close();
oRSet = null;
} // fi (oRSet)
if (null!=oStmt) { oStmt.close(); oStmt = null; }
oColIterator = oColumns.listIterator();
while (oColIterator.hasNext()) {
sCol = ((DBColumn) oColIterator.next()).getName();
sGetAllCols += sCol + ",";
sSetAllCols += "?,";
if (!oPrimaryKeys.contains(sCol) && !sCol.equalsIgnoreCase(DB.dt_created))