提交 73f1e9b4 authored 作者: Niklas Mehner's avatar Niklas Mehner

Implement MetaData.getColumns() for columns.

上级 1972c244
...@@ -282,7 +282,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -282,7 +282,7 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+quote(columnNamePattern)+");"); +quote(columnNamePattern)+");");
} }
checkClosed(); checkClosed();
PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT " String tableSql = "SELECT "
+ "TABLE_CATALOG TABLE_CAT, " + "TABLE_CATALOG TABLE_CAT, "
+ "TABLE_SCHEMA TABLE_SCHEM, " + "TABLE_SCHEMA TABLE_SCHEM, "
+ "TABLE_NAME, " + "TABLE_NAME, "
...@@ -313,7 +313,67 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -313,7 +313,67 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+ "AND TABLE_SCHEMA LIKE ? ESCAPE ? " + "AND TABLE_SCHEMA LIKE ? ESCAPE ? "
+ "AND TABLE_NAME LIKE ? ESCAPE ? " + "AND TABLE_NAME LIKE ? ESCAPE ? "
+ "AND COLUMN_NAME LIKE ? ESCAPE ? " + "AND COLUMN_NAME LIKE ? ESCAPE ? "
+ "ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION"); + "ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION";
String synonymSql = "SELECT "
+ "s.SYNONYM_CATALOG TABLE_CAT, "
+ "s.SYNONYM_SCHEMA TABLE_SCHEM, "
+ "s.SYNONYM_NAME TABLE_NAME, "
+ "c.COLUMN_NAME, "
+ "c.DATA_TYPE, "
+ "c.TYPE_NAME, "
+ "c.CHARACTER_MAXIMUM_LENGTH COLUMN_SIZE, "
+ "c.CHARACTER_MAXIMUM_LENGTH BUFFER_LENGTH, "
+ "c.NUMERIC_SCALE DECIMAL_DIGITS, "
+ "c.NUMERIC_PRECISION_RADIX NUM_PREC_RADIX, "
+ "c.NULLABLE, "
+ "c.REMARKS, "
+ "c.COLUMN_DEFAULT COLUMN_DEF, "
+ "c.DATA_TYPE SQL_DATA_TYPE, "
+ "ZERO() SQL_DATETIME_SUB, "
+ "c.CHARACTER_OCTET_LENGTH CHAR_OCTET_LENGTH, "
+ "c.ORDINAL_POSITION, "
+ "c.IS_NULLABLE IS_NULLABLE, "
+ "CAST(c.SOURCE_DATA_TYPE AS VARCHAR) SCOPE_CATALOG, "
+ "CAST(c.SOURCE_DATA_TYPE AS VARCHAR) SCOPE_SCHEMA, "
+ "CAST(c.SOURCE_DATA_TYPE AS VARCHAR) SCOPE_TABLE, "
+ "c.SOURCE_DATA_TYPE, "
+ "CASE WHEN c.SEQUENCE_NAME IS NULL THEN "
+ "CAST(? AS VARCHAR) ELSE CAST(? AS VARCHAR) END IS_AUTOINCREMENT, "
+ "CAST(c.SOURCE_DATA_TYPE AS VARCHAR) SCOPE_CATLOG "
+ "FROM INFORMATION_SCHEMA.COLUMNS c JOIN INFORMATION_SCHEMA.SYNONYMS s ON "
+ "s.SYNONYM_FOR = c.TABLE_NAME "
+ "AND s.SYNONYM_FOR_SCHEMA = c.TABLE_SCHEMA "
+ "WHERE s.SYNONYM_CATALOG LIKE ? ESCAPE ? "
+ "AND s.SYNONYM_SCHEMA LIKE ? ESCAPE ? "
+ "AND s.SYNONYM_NAME LIKE ? ESCAPE ? "
+ "AND c.COLUMN_NAME LIKE ? ESCAPE ? ";
PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT "
+ "TABLE_CAT, "
+ "TABLE_SCHEM, "
+ "TABLE_NAME, "
+ "COLUMN_NAME, "
+ "DATA_TYPE, "
+ "TYPE_NAME, "
+ "COLUMN_SIZE, "
+ "BUFFER_LENGTH, "
+ "DECIMAL_DIGITS, "
+ "NUM_PREC_RADIX, "
+ "NULLABLE, "
+ "REMARKS, "
+ "COLUMN_DEF, "
+ "SQL_DATA_TYPE, "
+ "SQL_DATETIME_SUB, "
+ "CHAR_OCTET_LENGTH, "
+ "ORDINAL_POSITION, "
+ "IS_NULLABLE, "
+ "SCOPE_CATALOG, "
+ "SCOPE_SCHEMA, "
+ "SCOPE_TABLE, "
+ "SOURCE_DATA_TYPE, "
+ "IS_AUTOINCREMENT, "
+ "SCOPE_CATLOG "
+ "FROM ((" + tableSql + ") UNION (" + synonymSql
+ ")) ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION");
prep.setString(1, "NO"); prep.setString(1, "NO");
prep.setString(2, "YES"); prep.setString(2, "YES");
prep.setString(3, getCatalogPattern(catalogPattern)); prep.setString(3, getCatalogPattern(catalogPattern));
...@@ -324,6 +384,16 @@ public class JdbcDatabaseMetaData extends TraceObject implements ...@@ -324,6 +384,16 @@ public class JdbcDatabaseMetaData extends TraceObject implements
prep.setString(8, "\\"); prep.setString(8, "\\");
prep.setString(9, getPattern(columnNamePattern)); prep.setString(9, getPattern(columnNamePattern));
prep.setString(10, "\\"); prep.setString(10, "\\");
prep.setString(11, "NO");
prep.setString(12, "YES");
prep.setString(13, getCatalogPattern(catalogPattern));
prep.setString(14, "\\");
prep.setString(15, getSchemaPattern(schemaPattern));
prep.setString(16, "\\");
prep.setString(17, getPattern(tableNamePattern));
prep.setString(18, "\\");
prep.setString(19, getPattern(columnNamePattern));
prep.setString(20, "\\");
return prep.executeQuery(); return prep.executeQuery();
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
......
...@@ -546,6 +546,7 @@ public class MetaTable extends Table { ...@@ -546,6 +546,7 @@ public class MetaTable extends Table {
"SYNONYM_SCHEMA", "SYNONYM_SCHEMA",
"SYNONYM_NAME", "SYNONYM_NAME",
"SYNONYM_FOR", "SYNONYM_FOR",
"SYNONYM_FOR_SCHEMA",
"TYPE_NAME", "TYPE_NAME",
"STATUS", "STATUS",
"REMARKS", "REMARKS",
...@@ -1889,6 +1890,8 @@ public class MetaTable extends Table { ...@@ -1889,6 +1890,8 @@ public class MetaTable extends Table {
identifier(synonym.getName()), identifier(synonym.getName()),
// SYNONYM_FOR // SYNONYM_FOR
synonym.getSynonymForName(), synonym.getSynonymForName(),
// SYNONYM_FOR_SCHEMA
synonym.getSynonymForSchema().getName(),
// TYPE NAME // TYPE NAME
"SYNONYM", "SYNONYM",
// STATUS // STATUS
......
...@@ -9,6 +9,7 @@ import org.h2.command.ddl.CreateSynonymData; ...@@ -9,6 +9,7 @@ import org.h2.command.ddl.CreateSynonymData;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.schema.Schema;
import org.h2.schema.SchemaObjectBase; import org.h2.schema.SchemaObjectBase;
/** /**
...@@ -73,6 +74,10 @@ public class TableSynonym extends SchemaObjectBase { ...@@ -73,6 +74,10 @@ public class TableSynonym extends SchemaObjectBase {
return data.synonymFor; return data.synonymFor;
} }
public Schema getSynonymForSchema() {
return data.synonymForSchema;
}
public boolean isInvalid() { public boolean isInvalid() {
return synonymFor.isValid(); return synonymFor.isValid();
} }
......
...@@ -171,6 +171,12 @@ public class TestSynonymForTable extends TestBase { ...@@ -171,6 +171,12 @@ public class TestSynonymForTable extends TestBase {
assertEquals(tables.getString("TABLE_TYPE"), "SYNONYM"); assertEquals(tables.getString("TABLE_TYPE"), "SYNONYM");
assertFalse(tables.next()); assertFalse(tables.next());
ResultSet columns = conn.getMetaData().getColumns(null, Constants.SCHEMA_MAIN, "TESTSYNONYM", null);
assertTrue(columns.next());
assertEquals(columns.getString("TABLE_NAME"), "TESTSYNONYM");
assertEquals(columns.getString("COLUMN_NAME"), "ID");
assertFalse(columns.next());
ResultSet synonyms = conn.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYNONYMS"); ResultSet synonyms = conn.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYNONYMS");
assertTrue(synonyms.next()); assertTrue(synonyms.next());
assertEquals("SYNONYM", synonyms.getString("SYNONYM_CATALOG")); assertEquals("SYNONYM", synonyms.getString("SYNONYM_CATALOG"));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论