Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Destrolaric committed Feb 14, 2023
1 parent b0eab66 commit 1313587
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
if (i != schemasNames.size() - 1) {
sql.append(" UNION ALL ");
} else {
sql.append("\n) order by TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION;");
sql.append("\n) order by TABLE_NAME, TABLE_SCHEM, ORDINAL_POSITION;");
}
}
if (schemasNames.size() == 0) {
Expand Down Expand Up @@ -1228,7 +1228,8 @@ public ResultSet getSchemas() throws SQLException {
if (getSchemas == null) {
getSchemas =
conn.prepareStatement(
"select name as TABLE_SCHEM, null as TABLE_CATALOG from pragma_database_list;");
"select name as TABLE_SCHEM, null as TABLE_CATALOG from pragma_database_list order by "
+ "TABLE_SCHEM;");
}

return getSchemas.executeQuery();
Expand All @@ -1244,7 +1245,8 @@ public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLExce
}
Statement stat = conn.createStatement();
String sql = "select name as TABLE_SCHEM, null as TABLE_CATALOG from pragma_database_list\n"
+ "where TABLE_SCHEM like '" + schemaPattern + "' escape '" + getSearchStringEscape() + "';";
+ "where TABLE_SCHEM like '" + schemaPattern + "' escape '" + getSearchStringEscape() + "' order by "
+ "TABLE_SCHEM;";
return stat.executeQuery(sql);
}

Expand Down Expand Up @@ -1527,7 +1529,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table)
throws SQLException {
ResultSet rs;
Statement stat = conn.createStatement();
StringBuilder sql = new StringBuilder(700);
StringBuilder sql = new StringBuilder(1024);
ResultSet schemas = getSchemas(null, escapeWildcards(schema));
List<String> schemasNames = getSchemasNames(schemas);
for (int i = 0; i < schemasNames.size(); i++) {
Expand Down
125 changes: 120 additions & 5 deletions src/test/java/org/sqlite/DBMetaDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1708,10 +1708,10 @@ public void readSchemaWithAttachedDatabases() throws SQLException {
ResultSetMetaData rsMeta = rs.getMetaData();
assertThat(rsMeta.getColumnCount()).isEqualTo(2);
assertThat(rsMeta.getColumnName(1)).isEqualTo("TABLE_SCHEM");
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("main");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db2");
assertThat(rsMeta.getColumnName(2)).isEqualTo("TABLE_CATALOG");
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db2");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("main");
}

@Test
Expand Down Expand Up @@ -1767,23 +1767,66 @@ public void getTablesForAttachedDatabase() throws SQLException {

@Test
public void testDynamicDatabaseAttachment() throws IOException, SQLException {
ResultSet schemas = meta.getSchemas();
schemas.close();
ResultSet schemas;
File thirdDB = Files.createTempFile("db3", ".sqlite").toFile();
stat.executeUpdate("attach database \"" + thirdDB.toURI().toURL() + "\" as db3;");
try {
schemas = meta.getSchemas();
boolean schemaFound = false;
while (schemas.next()) {
schemaFound = "db3".equals(schemas.getString("TABLE_SCHEM"));
if (schemaFound) {
break;
}
}
assertThat(schemaFound).isTrue();

} finally {
stat.executeUpdate("detach database db3;");
thirdDB.deleteOnExit();
}
}

@Test
public void testGetSchemasForAttachedDatabases() throws SQLException, IOException {
ResultSet rs;
rs = meta.getSchemas(null, "db2");
assertThat(rs).isNotNull();
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db2");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
rs.close();
File thirdDB = Files.createTempFile("db3", ".sqlite").toFile();
thirdDB.deleteOnExit();
stat.executeUpdate("attach database \"" + thirdDB.toURI().toURL() + "\" as db3;");
try {
rs = meta.getSchemas(null, "db_");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db2");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db3");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
rs.close();
rs = meta.getSchemas(null, "%");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db2");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("db3");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_SCHEM")).isEqualTo("main");
assertThat(rs.getString("TABLE_CATALOG")).isNull();
rs.close();
rs = meta.getSchemas(null, "\\%");
assertThat(rs.next()).isFalse();

} finally {
stat.executeUpdate("detach database db3;");
}
}

@Test
public void getColumnsForAttachedDatabaseTables() throws SQLException {
ResultSet rs = meta.getColumns(null, "db2", "test2", "id");
Expand All @@ -1797,6 +1840,7 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getInt("DECIMAL_DIGITS")).isEqualTo(0);
assertThat(rs.getString("IS_AUTOINCREMENT")).isEqualTo("NO");
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "fn");
assertThat(rs.next()).isTrue();
Expand All @@ -1808,6 +1852,7 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getInt("DECIMAL_DIGITS")).isEqualTo(10);
assertThat(rs.getString("IS_AUTOINCREMENT")).isEqualTo("NO");
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "sn");
assertThat(rs.next()).isTrue();
Expand All @@ -1817,6 +1862,7 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getInt("DECIMAL_DIGITS")).isEqualTo(10);
assertThat(rs.getString("COLUMN_DEF")).isNull();
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "intvalue");
assertThat(rs.next()).isTrue();
Expand All @@ -1827,6 +1873,7 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getInt("DECIMAL_DIGITS")).isEqualTo(0);
assertThat(rs.getString("COLUMN_DEF")).isNull();
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "realvalue");
assertThat(rs.next()).isTrue();
Expand All @@ -1837,6 +1884,7 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getInt("DECIMAL_DIGITS")).isEqualTo(3);
assertThat(rs.getString("COLUMN_DEF")).isNull();
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "%");
assertThat(rs.next()).isTrue();
Expand All @@ -1850,13 +1898,15 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test2", "%n");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("fn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("sn");
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "test%", "%");
// TABLE "test2"
Expand Down Expand Up @@ -1892,6 +1942,8 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.getString("TABLE_NAME")).isEqualTo("testView2");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
assertThat(rs.next()).isFalse();
rs.close();


rs = meta.getColumns(null, "db2", "%", "%");
// TABLE "test2"
Expand Down Expand Up @@ -1919,10 +1971,73 @@ public void getColumnsForAttachedDatabaseTables() throws SQLException {
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
assertThat(rs.next()).isFalse();
rs.close();

rs = meta.getColumns(null, "db2", "doesnotexist", "%");
assertThat(rs.next()).isFalse();
assertThat(rs.getMetaData().getColumnCount()).isEqualTo(24);
rs.close();


rs = meta.getColumns(null, null, "%", "%");
assertReadsAllColumns(rs);
rs = meta.getColumns(null, "%", "%", "%");
assertReadsAllColumns(rs);
rs = meta.getColumns(null, "\\%", "%", "%");
assertThat(rs.next()).isFalse();
}

private void assertReadsAllColumns(ResultSet rs) throws SQLException {
// TABLE "test"
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_NAME")).isEqualTo("test");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("id");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("fn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("sn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("intvalue");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
// TABLE "test2"
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_NAME")).isEqualTo("test2");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("id");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("fn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("sn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("intvalue");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
// VIEW "testView"
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_NAME")).isEqualTo("testView");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("id");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("fn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("sn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("intvalue");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
// VIEW "testView2"
assertThat(rs.next()).isTrue();
assertThat(rs.getString("TABLE_NAME")).isEqualTo("testView2");
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("id");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("fn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("sn");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("intvalue");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("realvalue");
assertThat(rs.next()).isFalse();
rs.close();
}

@Test
Expand Down

0 comments on commit 1313587

Please sign in to comment.