SqlDialect get(DataSource dataSource) { Connection connection = null; try { connection = dataSource.getConnection(); DatabaseMetaData metaData = connection.getMetaData(); String productName = metaData.getDatabaseProductName(); String productVersion = metaData.getDatabaseProductVersion(); List key = Arrays.asList(productName, productVersion); SqlDialect dialect = map.get(key); if (dialect == null) { final SqlDialect.DatabaseProduct product = SqlDialect.getProduct(productName, productVersion); dialect = new SqlDialect(product, productName, metaData.getIdentifierQuoteString()); map.put(key, dialect); } connection.close(); connection = null; return dialect; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { // ignore } } } }
@Signature public Memory getMetaData() throws SQLException { ArrayMemory r = new ArrayMemory(); r.refOfIndex("userName").assign(metaData.getUserName()); r.refOfIndex("driverName").assign(metaData.getDriverName()); r.refOfIndex("driverVersion").assign(metaData.getDriverVersion()); r.refOfIndex("databaseName").assign(metaData.getDatabaseProductName()); r.refOfIndex("databaseVersion").assign(metaData.getDatabaseProductVersion()); r.refOfIndex("catalogSeparator").assign(metaData.getCatalogSeparator()); r.refOfIndex("catalogTerm").assign(metaData.getCatalogTerm()); r.refOfIndex("schemaTerm").assign(metaData.getSchemaTerm()); r.refOfIndex("procedureTerm").assign(metaData.getProcedureTerm()); r.refOfIndex("searchStringEscape").assign(metaData.getSearchStringEscape()); r.refOfIndex("numericFunctions").assign(metaData.getNumericFunctions()); r.refOfIndex("stringFunctions").assign(metaData.getStringFunctions()); r.refOfIndex("timeDateFunctions").assign(metaData.getTimeDateFunctions()); r.refOfIndex("systemFunctions").assign(metaData.getSystemFunctions()); r.refOfIndex("defaultTransactionIsolation").assign(metaData.getDefaultTransactionIsolation()); r.refOfIndex("identifierQuoteString").assign(metaData.getIdentifierQuoteString()); r.refOfIndex("maxBinaryLiteralLength").assign(metaData.getMaxBinaryLiteralLength()); r.refOfIndex("maxCatalogNameLength").assign(metaData.getMaxCatalogNameLength()); r.refOfIndex("maxCharLiteralLength").assign(metaData.getMaxCharLiteralLength()); r.refOfIndex("maxConnections").assign(metaData.getMaxConnections()); r.refOfIndex("maxColumnNameLength").assign(metaData.getMaxColumnNameLength()); r.refOfIndex("maxColumnsInGroupBy").assign(metaData.getMaxColumnsInGroupBy()); r.refOfIndex("maxColumnsInIndex").assign(metaData.getMaxColumnsInIndex()); r.refOfIndex("maxColumnsInOrderBy").assign(metaData.getMaxColumnsInOrderBy()); r.refOfIndex("maxColumnsInSelect").assign(metaData.getMaxColumnsInSelect()); r.refOfIndex("maxColumnsInTable").assign(metaData.getMaxColumnsInTable()); r.refOfIndex("maxCursorNameLength").assign(metaData.getMaxCursorNameLength()); r.refOfIndex("maxIndexLength").assign(metaData.getMaxIndexLength()); r.refOfIndex("maxProcedureNameLength").assign(metaData.getMaxProcedureNameLength()); r.refOfIndex("maxRowSize").assign(metaData.getMaxRowSize()); r.refOfIndex("maxSchemaNameLength").assign(metaData.getMaxSchemaNameLength()); r.refOfIndex("maxStatementLength").assign(metaData.getMaxStatementLength()); r.refOfIndex("maxTableNameLength").assign(metaData.getMaxTableNameLength()); r.refOfIndex("maxTablesInSelect").assign(metaData.getMaxTablesInSelect()); return r.toConstant(); }
public void testMetaData() { String ddl0 = "DROP TABLE ADDRESSBOOK IF EXISTS; DROP TABLE ADDRESSBOOK_CATEGORY IF EXISTS; DROP TABLE USER IF EXISTS;"; String ddl1 = "CREATE TABLE USER(USER_ID INTEGER NOT NULL PRIMARY KEY,LOGIN_ID VARCHAR(128) NOT NULL,USER_NAME VARCHAR(254) DEFAULT ' ' NOT NULL,CREATE_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,UPDATE_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,LAST_ACCESS_DATE TIMESTAMP,CONSTRAINT IXUQ_LOGIN_ID0 UNIQUE(LOGIN_ID))"; String ddl2 = "CREATE TABLE ADDRESSBOOK_CATEGORY(USER_ID INTEGER NOT NULL,CATEGORY_ID INTEGER DEFAULT 0 NOT NULL,CATEGORY_NAME VARCHAR(60) DEFAULT '' NOT NULL,CONSTRAINT SYS_PK_ADDRESSBOOK_CATEGORY PRIMARY KEY(USER_ID,CATEGORY_ID),CONSTRAINT FK_ADRBKCAT1 FOREIGN KEY(USER_ID) REFERENCES USER(USER_ID) ON DELETE CASCADE)"; String ddl3 = "CREATE TABLE ADDRESSBOOK(USER_ID INTEGER NOT NULL,ADDRESSBOOK_ID INTEGER NOT NULL,CATEGORY_ID INTEGER DEFAULT 0 NOT NULL,FIRST VARCHAR(64) DEFAULT '' NOT NULL,LAST VARCHAR(64) DEFAULT '' NOT NULL,NOTE VARCHAR(128) DEFAULT '' NOT NULL,CONSTRAINT SYS_PK_ADDRESSBOOK PRIMARY KEY(USER_ID,ADDRESSBOOK_ID),CONSTRAINT FK_ADRBOOK1 FOREIGN KEY(USER_ID,CATEGORY_ID) REFERENCES ADDRESSBOOK_CATEGORY(USER_ID,CATEGORY_ID) ON DELETE CASCADE)"; String result1 = "1"; String result2 = "2"; String result3 = "3"; String result4 = "4"; String result5 = "5"; try { stmnt.execute(ddl0); stmnt.execute(ddl1); stmnt.execute(ddl2); stmnt.execute(ddl3); DatabaseMetaData md = connection.getMetaData(); { // System.out.println(md.getDatabaseMajorVersion()); // System.out.println(md.getDatabaseMinorVersion()); System.out.println(md.getDatabaseProductName()); System.out.println(md.getDatabaseProductVersion()); System.out.println(md.getDefaultTransactionIsolation()); System.out.println(md.getDriverMajorVersion()); System.out.println(md.getDriverMinorVersion()); System.out.println(md.getDriverName()); System.out.println(md.getDriverVersion()); System.out.println(md.getExtraNameCharacters()); System.out.println(md.getIdentifierQuoteString()); // System.out.println(md.getJDBCMajorVersion()); // System.out.println(md.getJDBCMinorVersion()); System.out.println(md.getMaxBinaryLiteralLength()); System.out.println(md.getMaxCatalogNameLength()); System.out.println(md.getMaxColumnsInGroupBy()); System.out.println(md.getMaxColumnsInIndex()); System.out.println(md.getMaxColumnsInOrderBy()); System.out.println(md.getMaxColumnsInSelect()); System.out.println(md.getMaxColumnsInTable()); System.out.println(md.getMaxConnections()); System.out.println(md.getMaxCursorNameLength()); System.out.println(md.getMaxIndexLength()); System.out.println(md.getMaxProcedureNameLength()); System.out.println(md.getMaxRowSize()); System.out.println(md.getMaxSchemaNameLength()); System.out.println(md.getMaxStatementLength()); System.out.println(md.getMaxStatements()); System.out.println(md.getMaxTableNameLength()); System.out.println(md.getMaxUserNameLength()); System.out.println(md.getNumericFunctions()); System.out.println(md.getProcedureTerm()); // System.out.println(md.getResultSetHoldability()); System.out.println(md.getSchemaTerm()); System.out.println(md.getSearchStringEscape()); System.out.println(md.getSQLKeywords()); // System.out.println(md.getSQLStateType()); System.out.println(md.getStringFunctions()); System.out.println(md.getSystemFunctions()); System.out.println(md.getTimeDateFunctions()); System.out.println(md.getURL()); System.out.println(md.getUserName()); System.out.println(md.importedKeyCascade); System.out.println(md.isCatalogAtStart()); System.out.println(md.isReadOnly()); ResultSet rs; rs = md.getPrimaryKeys(null, null, "USER"); ResultSetMetaData rsmd = rs.getMetaData(); String result0 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result0 += rs.getString(i + 1) + ":"; } result0 += "\n"; } rs.close(); System.out.println(result0); } { ResultSet rs; rs = md.getBestRowIdentifier(null, null, "USER", 0, true); ResultSetMetaData rsmd = rs.getMetaData(); String result0 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result0 += rs.getString(i + 1) + ":"; } result0 += "\n"; } rs.close(); System.out.println(result0); } { ResultSet rs = md.getImportedKeys(null, null, "ADDRESSBOOK"); ResultSetMetaData rsmd = rs.getMetaData(); result1 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result1 += rs.getString(i + 1) + ":"; } result1 += "\n"; } rs.close(); System.out.println(result1); } { ResultSet rs = md.getCrossReference(null, null, "ADDRESSBOOK_CATEGORY", null, null, "ADDRESSBOOK"); ResultSetMetaData rsmd = rs.getMetaData(); result2 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result2 += rs.getString(i + 1) + ":"; } result2 += "\n"; } rs.close(); System.out.println(result2); } { ResultSet rs = md.getExportedKeys(null, null, "USER"); ResultSetMetaData rsmd = rs.getMetaData(); result3 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result3 += rs.getString(i + 1) + ":"; } result3 += "\n"; } rs.close(); System.out.println(result3); } { ResultSet rs = md.getCrossReference(null, null, "USER", null, null, "ADDRESSBOOK_CATEGORY"); ResultSetMetaData rsmd = rs.getMetaData(); result4 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result4 += rs.getString(i + 1) + ":"; } result4 += "\n"; } rs.close(); System.out.println(result4); } { stmnt.execute("DROP TABLE T IF EXISTS;"); stmnt.executeQuery("CREATE TABLE T (I IDENTITY, A CHAR, B CHAR);"); stmnt.executeQuery( "INSERT INTO T VALUES (NULL, 'get_column_name', '" + getColumnName + "');"); ResultSet rs = stmnt.executeQuery("SELECT I, A, B, A \"aliasA\", B \"aliasB\" FROM T;"); ResultSetMetaData rsmd = rs.getMetaData(); result5 = ""; for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result5 += rsmd.getColumnName(i + 1) + ":" + rs.getString(i + 1) + ":"; } result5 += "\n"; } rs.close(); rs = stmnt.executeQuery("SELECT I, A, B, A \"aliasA\", B \"aliasB\" FROM T;"); ; rsmd = rs.getMetaData(); for (; rs.next(); ) { for (int i = 0; i < rsmd.getColumnCount(); i++) { result5 += rsmd.getColumnLabel(i + 1) + ":" + rs.getString(i + 1) + ":"; } result5 += "\n"; } System.out.println(result5); System.out.println("first column identity: " + rsmd.isAutoIncrement(1)); rsmd.isCaseSensitive(1); rsmd.isCurrency(1); rsmd.isDefinitelyWritable(1); rsmd.isNullable(1); rsmd.isReadOnly(1); rsmd.isSearchable(1); rsmd.isSigned(1); rsmd.isWritable(1); rs.close(); // test identity with PreparedStatement pstmnt = connection.prepareStatement("INSERT INTO T VALUES (?,?,?)"); pstmnt.setString(1, null); pstmnt.setString(2, "test"); pstmnt.setString(3, "test2"); pstmnt.executeUpdate(); pstmnt = connection.prepareStatement("call identity()"); ResultSet rsi = pstmnt.executeQuery(); rsi.next(); int identity = rsi.getInt(1); System.out.println("call identity(): " + identity); rsi.close(); } } catch (SQLException e) { fail(e.getMessage()); } System.out.println("testMetaData complete"); // assert equality of exported and imported with xref assertEquals(result1, result2); assertEquals(result3, result4); }