@@ -915,7 +915,9 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
915
915
checkOpen ();
916
916
917
917
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, " )
919
921
.append (
920
922
"cn as COLUMN_NAME, ct as DATA_TYPE, tn as TYPE_NAME, colSize as COLUMN_SIZE, " )
921
923
.append (
@@ -948,10 +950,13 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
948
950
ResultSet rsColAutoinc = null ;
949
951
try {
950
952
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')" );
955
960
rsColAutoinc .next ();
956
961
isAutoIncrement = rsColAutoinc .getInt (1 ) == 1 ;
957
962
} finally {
@@ -972,8 +977,10 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
972
977
}
973
978
974
979
// 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 ) + "')" );
977
984
try (Statement colstat = conn .createStatement ();
978
985
ResultSet rscol = colstat .executeQuery (pragmaStatement )) {
979
986
@@ -1172,8 +1179,9 @@ public ResultSet getCrossReference(
1172
1179
/** @see java.sql.DatabaseMetaData#getSchemas() */
1173
1180
public ResultSet getSchemas () throws SQLException {
1174
1181
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;" );
1177
1185
}
1178
1186
1179
1187
return getSchemas .executeQuery ();
@@ -1198,7 +1206,9 @@ public ResultSet getPrimaryKeys(String c, String s, String table) throws SQLExce
1198
1206
1199
1207
Statement stat = conn .createStatement ();
1200
1208
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, '" )
1202
1212
.append (escape (table ))
1203
1213
.append ("' as TABLE_NAME, cn as COLUMN_NAME, ks as KEY_SEQ, pk as PK_NAME from (" );
1204
1214
@@ -1249,7 +1259,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
1249
1259
1250
1260
catalog = (catalog != null ) ? quote (catalog ) : null ;
1251
1261
1252
- String quotedSchema = (schema != null ) ? quote (schema ) : null ;
1262
+ String quotedSchema = (schema != null ) ? quote (schema ) : quote ( "main" ) ;
1253
1263
1254
1264
StringBuilder exportedKeysQuery = new StringBuilder (512 );
1255
1265
@@ -1258,11 +1268,11 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
1258
1268
if (pkColumns != null ) {
1259
1269
// retrieve table list
1260
1270
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'" )) ) {
1266
1276
tableList = new ArrayList <>();
1267
1277
1268
1278
while (rs .next ()) {
@@ -1406,12 +1416,12 @@ public ResultSet getImportedKeys(String catalog, String schema, String table)
1406
1416
sql .append ("select " )
1407
1417
.append (quote (catalog ))
1408
1418
.append (" as PKTABLE_CAT, " )
1409
- .append (quote (schema ))
1419
+ .append (quote (schema == null ? "main" : schema ))
1410
1420
.append (" as PKTABLE_SCHEM, " )
1411
1421
.append ("ptn as PKTABLE_NAME, pcn as PKCOLUMN_NAME, " )
1412
1422
.append (quote (catalog ))
1413
1423
.append (" as FKTABLE_CAT, " )
1414
- .append (quote (schema ))
1424
+ .append (quote (schema == null ? "main" : schema ))
1415
1425
.append (" as FKTABLE_SCHEM, " )
1416
1426
.append (quote (table ))
1417
1427
.append (" as FKTABLE_NAME, " )
@@ -1526,7 +1536,7 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
1526
1536
// define the column header
1527
1537
// this is from the JDBC spec, it is part of the driver protocol
1528
1538
sql .append ("select null as TABLE_CAT," )
1529
- .append (quote (s ))
1539
+ .append (quote (s == null ? "main" : s ))
1530
1540
.append (" as TABLE_SCHEM, '" )
1531
1541
.append (escape (table ))
1532
1542
.append (
@@ -1537,7 +1547,9 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
1537
1547
"cn as COLUMN_NAME, null as ASC_OR_DESC, 0 as CARDINALITY, 0 as PAGES, null as FILTER_CONDITION from (" );
1538
1548
1539
1549
// 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 ) + "');" ));
1541
1553
1542
1554
ArrayList <ArrayList <Object >> indexList = new ArrayList <>();
1543
1555
while (rs .next ()) {
@@ -1561,7 +1573,11 @@ public ResultSet getIndexInfo(String c, String s, String table, boolean u, boole
1561
1573
while (indexIterator .hasNext ()) {
1562
1574
currentIndex = indexIterator .next ();
1563
1575
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 ) + "');" ));
1565
1581
1566
1582
while (rs .next ()) {
1567
1583
@@ -1689,7 +1705,10 @@ public synchronized ResultSet getTables(
1689
1705
StringBuilder sql = new StringBuilder ();
1690
1706
sql .append ("SELECT" ).append ("\n " );
1691
1707
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 " );
1693
1712
sql .append (" NAME AS TABLE_NAME," ).append ("\n " );
1694
1713
sql .append (" TYPE AS TABLE_TYPE," ).append ("\n " );
1695
1714
sql .append (" NULL AS REMARKS," ).append ("\n " );
@@ -1985,7 +2004,7 @@ public PrimaryKeyFinder(String table) throws SQLException {
1985
2004
/**
1986
2005
* Constructor.
1987
2006
*
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.
1989
2008
* @param schema Schema in which table is located
1990
2009
* @throws SQLException
1991
2010
*/
@@ -2000,10 +2019,13 @@ public PrimaryKeyFinder(String table, String schema) throws SQLException {
2000
2019
// read create SQL script for table
2001
2020
ResultSet rs =
2002
2021
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')" ))) {
2007
2029
2008
2030
if (!rs .next ()) throw new SQLException ("Table not found: '" + table + "'" );
2009
2031
@@ -2019,10 +2041,12 @@ public PrimaryKeyFinder(String table, String schema) throws SQLException {
2019
2041
}
2020
2042
2021
2043
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 ) + "');" ))) {
2026
2050
while (rs2 .next ()) {
2027
2051
if (rs2 .getBoolean (6 )) pkColumns = new String [] {rs2 .getString (2 )};
2028
2052
}
@@ -2073,13 +2097,15 @@ public ImportedKeyFinder(String table, String schema) throws SQLException {
2073
2097
2074
2098
List <String > fkNames = getForeignKeyNames (this .fkTableName , schema );
2075
2099
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
+ + "')" ))) {
2083
2109
2084
2110
int prevFkId = -1 ;
2085
2111
int count = 0 ;
@@ -2122,9 +2148,15 @@ private List<String> getForeignKeyNames(String tbl, String schema) throws SQLExc
2122
2148
return fkNames ;
2123
2149
}
2124
2150
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
+ + "')" ))) {
2128
2160
if (rs .next ()) {
2129
2161
Matcher matcher = FK_NAMED_PATTERN .matcher (rs .getString (1 ));
2130
2162
0 commit comments