Skip to content

Commit 4820bfd

Browse files
committed
additional test cases & improve metadata reading & formatting
1 parent 24fcc05 commit 4820bfd

File tree

3 files changed

+118
-55
lines changed

3 files changed

+118
-55
lines changed

src/main/java/org/sqlite/core/CoreDatabaseMetaData.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ protected String escape(final String val) {
186186

187187
/**
188188
* Returns line without changes or with escaped schema prefix
189+
*
189190
* @param schema schema name
190191
* @param line of text to prepend to
191192
* @return The SQL escaped schema name with dot or empty string
@@ -200,6 +201,7 @@ protected String prependSchemaPrefix(String schema, String line) {
200201

201202
/**
202203
* Adds line without changes or with escaped schema prefix
204+
*
203205
* @param sql String builder for sql request
204206
* @param schema schema name
205207
* @param line line to prepend schema prefix to
@@ -212,7 +214,6 @@ protected void prependSchemaPrefix(StringBuilder sql, String schema, String line
212214
}
213215
}
214216

215-
216217
// inner classes
217218

218219
/** Pattern used to extract column order for an unnamed primary key. */

src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java

+73-41
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,9 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
915915
checkOpen();
916916

917917
StringBuilder sql = new StringBuilder(700);
918-
sql.append("select null as TABLE_CAT, ").append(quote(s)).append(" as TABLE_SCHEM, tblname as TABLE_NAME, ")
918+
sql.append("select null as TABLE_CAT, ")
919+
.append(quote(s == null ? "main" : s))
920+
.append(" as TABLE_SCHEM, tblname as TABLE_NAME, ")
919921
.append(
920922
"cn as COLUMN_NAME, ct as DATA_TYPE, tn as TYPE_NAME, colSize as COLUMN_SIZE, ")
921923
.append(
@@ -948,10 +950,13 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
948950
ResultSet rsColAutoinc = null;
949951
try {
950952
statColAutoinc = conn.createStatement();
951-
rsColAutoinc = statColAutoinc.executeQuery(
952-
"SELECT LIKE('%autoincrement%', LOWER(sql)) FROM " + prependSchemaPrefix(s,
953-
"sqlite_master WHERE LOWER(name) = LOWER('") + escape(tableName)
954-
+ "') AND TYPE IN ('table', 'view')");
953+
rsColAutoinc =
954+
statColAutoinc.executeQuery(
955+
"SELECT LIKE('%autoincrement%', LOWER(sql)) FROM "
956+
+ prependSchemaPrefix(
957+
s, "sqlite_master WHERE LOWER(name) = LOWER('")
958+
+ escape(tableName)
959+
+ "') AND TYPE IN ('table', 'view')");
955960
rsColAutoinc.next();
956961
isAutoIncrement = rsColAutoinc.getInt(1) == 1;
957962
} finally {
@@ -972,8 +977,10 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
972977
}
973978

974979
// For each table, get the column info and build into overall SQL
975-
String pragmaStatement = "PRAGMA " + prependSchemaPrefix(s, "table_xinfo('" + escape(tableName) +
976-
"')");
980+
String pragmaStatement =
981+
"PRAGMA "
982+
+ prependSchemaPrefix(
983+
s, "table_xinfo('" + escape(tableName) + "')");
977984
try (Statement colstat = conn.createStatement();
978985
ResultSet rscol = colstat.executeQuery(pragmaStatement)) {
979986

@@ -1172,8 +1179,9 @@ public ResultSet getCrossReference(
11721179
/** @see java.sql.DatabaseMetaData#getSchemas() */
11731180
public ResultSet getSchemas() throws SQLException {
11741181
if (getSchemas == null) {
1175-
getSchemas = conn.prepareStatement(
1176-
"select name as TABLE_SCHEM, null as TABLE_CATALOG from pragma_database_list;");
1182+
getSchemas =
1183+
conn.prepareStatement(
1184+
"select name as TABLE_SCHEM, null as TABLE_CATALOG from pragma_database_list;");
11771185
}
11781186

11791187
return getSchemas.executeQuery();
@@ -1198,7 +1206,9 @@ public ResultSet getPrimaryKeys(String c, String s, String table) throws SQLExce
11981206

11991207
Statement stat = conn.createStatement();
12001208
StringBuilder sql = new StringBuilder(512);
1201-
sql.append("select null as TABLE_CAT, ").append(quote(s)).append(" as TABLE_SCHEM, '")
1209+
sql.append("select null as TABLE_CAT, ")
1210+
.append(quote(s == null ? "main" : s))
1211+
.append(" as TABLE_SCHEM, '")
12021212
.append(escape(table))
12031213
.append("' as TABLE_NAME, cn as COLUMN_NAME, ks as KEY_SEQ, pk as PK_NAME from (");
12041214

@@ -1249,7 +1259,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
12491259

12501260
catalog = (catalog != null) ? quote(catalog) : null;
12511261

1252-
String quotedSchema = (schema != null) ? quote(schema) : null;
1262+
String quotedSchema = (schema != null) ? quote(schema) : quote("main");
12531263

12541264
StringBuilder exportedKeysQuery = new StringBuilder(512);
12551265

@@ -1258,11 +1268,11 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
12581268
if (pkColumns != null) {
12591269
// retrieve table list
12601270
ArrayList<String> tableList;
1261-
try (
1262-
ResultSet rs = stat.executeQuery(
1263-
"select name from " + prependSchemaPrefix(schema, "sqlite_master where type = " +
1264-
"'table'"))
1265-
) {
1271+
try (ResultSet rs =
1272+
stat.executeQuery(
1273+
"select name from "
1274+
+ prependSchemaPrefix(
1275+
schema, "sqlite_master where type = " + "'table'"))) {
12661276
tableList = new ArrayList<>();
12671277

12681278
while (rs.next()) {
@@ -1406,12 +1416,12 @@ public ResultSet getImportedKeys(String catalog, String schema, String table)
14061416
sql.append("select ")
14071417
.append(quote(catalog))
14081418
.append(" as PKTABLE_CAT, ")
1409-
.append(quote(schema))
1419+
.append(quote(schema == null ? "main" : schema))
14101420
.append(" as PKTABLE_SCHEM, ")
14111421
.append("ptn as PKTABLE_NAME, pcn as PKCOLUMN_NAME, ")
14121422
.append(quote(catalog))
14131423
.append(" as FKTABLE_CAT, ")
1414-
.append(quote(schema))
1424+
.append(quote(schema == null ? "main" : schema))
14151425
.append(" as FKTABLE_SCHEM, ")
14161426
.append(quote(table))
14171427
.append(" as FKTABLE_NAME, ")
@@ -1526,7 +1536,7 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
15261536
// define the column header
15271537
// this is from the JDBC spec, it is part of the driver protocol
15281538
sql.append("select null as TABLE_CAT,")
1529-
.append(quote(s))
1539+
.append(quote(s == null ? "main" : s))
15301540
.append(" as TABLE_SCHEM, '")
15311541
.append(escape(table))
15321542
.append(
@@ -1537,7 +1547,9 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
15371547
"cn as COLUMN_NAME, null as ASC_OR_DESC, 0 as CARDINALITY, 0 as PAGES, null as FILTER_CONDITION from (");
15381548

15391549
// this always returns a result set now, previously threw exception
1540-
rs = stat.executeQuery("pragma " + prependSchemaPrefix(s, "index_list('" + escape(table) + "');"));
1550+
rs =
1551+
stat.executeQuery(
1552+
"pragma " + prependSchemaPrefix(s, "index_list('" + escape(table) + "');"));
15411553

15421554
ArrayList<ArrayList<Object>> indexList = new ArrayList<>();
15431555
while (rs.next()) {
@@ -1561,7 +1573,11 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
15611573
while (indexIterator.hasNext()) {
15621574
currentIndex = indexIterator.next();
15631575
String indexName = currentIndex.get(0).toString();
1564-
rs = stat.executeQuery("pragma " + prependSchemaPrefix(s, "index_info('" + escape(indexName) + "');"));
1576+
rs =
1577+
stat.executeQuery(
1578+
"pragma "
1579+
+ prependSchemaPrefix(
1580+
s, "index_info('" + escape(indexName) + "');"));
15651581

15661582
while (rs.next()) {
15671583

@@ -1689,7 +1705,10 @@ public synchronized ResultSet getTables(
16891705
StringBuilder sql = new StringBuilder();
16901706
sql.append("SELECT").append("\n");
16911707
sql.append(" NULL AS TABLE_CAT,").append("\n");
1692-
sql.append(" NULL AS TABLE_SCHEM,").append("\n");
1708+
sql.append(" ")
1709+
.append(quote(s == null ? "main" : s))
1710+
.append(" AS TABLE_SCHEM,")
1711+
.append("\n");
16931712
sql.append(" NAME AS TABLE_NAME,").append("\n");
16941713
sql.append(" TYPE AS TABLE_TYPE,").append("\n");
16951714
sql.append(" NULL AS REMARKS,").append("\n");
@@ -1985,7 +2004,7 @@ public PrimaryKeyFinder(String table) throws SQLException {
19852004
/**
19862005
* Constructor.
19872006
*
1988-
* @param table The table for which to get find a primary key.
2007+
* @param table The table for which to get find a primary key.
19892008
* @param schema Schema in which table is located
19902009
* @throws SQLException
19912010
*/
@@ -2000,10 +2019,13 @@ public PrimaryKeyFinder(String table, String schema) throws SQLException {
20002019
// read create SQL script for table
20012020
ResultSet rs =
20022021
stat.executeQuery(
2003-
"select sql from " + prependSchemaPrefix(schema, "sqlite_master where"
2004-
+ " lower(name) = lower('"
2005-
+ escape(table)
2006-
+ "') and type in ('table', 'view')"))) {
2022+
"select sql from "
2023+
+ prependSchemaPrefix(
2024+
schema,
2025+
"sqlite_master where"
2026+
+ " lower(name) = lower('"
2027+
+ escape(table)
2028+
+ "') and type in ('table', 'view')"))) {
20072029

20082030
if (!rs.next()) throw new SQLException("Table not found: '" + table + "'");
20092031

@@ -2019,10 +2041,12 @@ public PrimaryKeyFinder(String table, String schema) throws SQLException {
20192041
}
20202042

20212043
if (pkColumns == null) {
2022-
try (
2023-
ResultSet rs2 = stat.executeQuery(
2024-
"pragma " + prependSchemaPrefix(schema, "table_info('" + escape(table) + "');"))
2025-
) {
2044+
try (ResultSet rs2 =
2045+
stat.executeQuery(
2046+
"pragma "
2047+
+ prependSchemaPrefix(
2048+
schema,
2049+
"table_info('" + escape(table) + "');"))) {
20262050
while (rs2.next()) {
20272051
if (rs2.getBoolean(6)) pkColumns = new String[] {rs2.getString(2)};
20282052
}
@@ -2073,13 +2097,15 @@ public ImportedKeyFinder(String table, String schema) throws SQLException {
20732097

20742098
List<String> fkNames = getForeignKeyNames(this.fkTableName, schema);
20752099

2076-
try (
2077-
Statement stat = conn.createStatement();
2078-
ResultSet rs = stat.executeQuery("pragma " + prependSchemaPrefix(
2079-
schema,
2080-
"foreign_key_list('" + escape(this.fkTableName.toLowerCase()) + "')"
2081-
))
2082-
) {
2100+
try (Statement stat = conn.createStatement();
2101+
ResultSet rs =
2102+
stat.executeQuery(
2103+
"pragma "
2104+
+ prependSchemaPrefix(
2105+
schema,
2106+
"foreign_key_list('"
2107+
+ escape(this.fkTableName.toLowerCase())
2108+
+ "')"))) {
20832109

20842110
int prevFkId = -1;
20852111
int count = 0;
@@ -2122,9 +2148,15 @@ private List<String> getForeignKeyNames(String tbl, String schema) throws SQLExc
21222148
return fkNames;
21232149
}
21242150
try (Statement stat2 = conn.createStatement();
2125-
ResultSet rs = stat2.executeQuery("select sql from " + prependSchemaPrefix(schema,
2126-
"sqlite_master where" + " lower(name) = lower('" + escape(tbl) + "')"
2127-
))) {
2151+
ResultSet rs =
2152+
stat2.executeQuery(
2153+
"select sql from "
2154+
+ prependSchemaPrefix(
2155+
schema,
2156+
"sqlite_master where"
2157+
+ " lower(name) = lower('"
2158+
+ escape(tbl)
2159+
+ "')"))) {
21282160
if (rs.next()) {
21292161
Matcher matcher = FK_NAMED_PATTERN.matcher(rs.getString(1));
21302162

0 commit comments

Comments
 (0)