From 858cb2c8441f35e6ba0ace85a9523695dfc8e0ab Mon Sep 17 00:00:00 2001 From: Serge Rider Date: Fri, 1 Nov 2024 06:33:30 +0100 Subject: [PATCH] dbeaver/dbeaver#23361 Columns for all tables read --- .../driver/libsql/LibSqlDatabaseMetaData.java | 29 ++++++++++--------- .../driver/libsql/client/LibSqlClient.java | 3 ++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java index e99c455..6350438 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java @@ -108,18 +108,22 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam @Override public ResultSet getColumns(String catalog, String schemaPattern, String tableName, String columnNamePattern) throws SQLException { verifySchemaParameters(catalog, schemaPattern); - try (PreparedStatement dbStat = connection.prepareStatement( - "SELECT NULL as TABLE_CAT, NULL AS TABLE_SCHEM,'" + tableName + "' AS TABLE_NAME," + - "name as COLUMN_NAME," + Types.VARCHAR + " AS DATA_TYPE, type AS TYPE_NAME, 0 AS COLUMN_SIZE," + - "NULL AS REMARKS,cid AS ORDINAL_POSITION " + - "FROM PRAGMA_TABLE_INFO('" + tableName + "')") - ) { - return dbStat.executeQuery(); + if (CommonUtils.isEmpty(tableName) || "%".equals(tableName)) { + tableName = null; } + return executeQuery( + "WITH all_tables AS (SELECT name AS tn FROM sqlite_master WHERE type = 'table'" + + (tableName == null ? "" : " and name=" + LibSqlUtils.escape(tableName)) + ") \n" + + "SELECT NULL as TABLE_CAT, NULL AS TABLE_SCHEM, at.tn as TABLE_NAME,\n" + + "pti.name as COLUMN_NAME," + Types.VARCHAR + " AS DATA_TYPE, pti.type AS TYPE_NAME, 0 AS COLUMN_SIZE," + + "NULL AS REMARKS,pti.cid AS ORDINAL_POSITION " + + "FROM all_tables at INNER JOIN pragma_table_info(at.tn) pti\n" + + "ORDER BY TABLE_NAME"); } @Override - public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { + public ResultSet getPrimaryKeys(String catalog, String schema, String tableName) throws SQLException { + String table = tableName; PrimaryKeyFinder pkFinder = new PrimaryKeyFinder(connection, table); String[] columns = pkFinder.getColumns(); @@ -141,12 +145,9 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr for (int i = 0; i < columns.length; i++) { if (i > 0) sql.append(" union "); sql.append("select ") - .append(pkName) - .append(" as pk, '") - .append(LibSqlUtils.escape(LibSqlUtils.unquote(columns[i]))) - .append("' as cn, ") - .append(i + 1) - .append(" as ks"); + .append(pkName).append(" as pk, '") + .append(LibSqlUtils.escape(LibSqlUtils.unquote(columns[i]))).append("' as cn, ") + .append(i + 1).append(" as ks"); } sql.append(") order by cn;"); diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java index 0fced4e..a0a5a34 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java @@ -58,6 +58,9 @@ public LibSqlExecutionResult[] executeBatch( try { HttpURLConnection conn = openConnection(); conn.setRequestMethod("POST"); + conn.setRequestProperty("User-Agent", + LibSqlConstants.DRIVER_INFO + " " + + LibSqlConstants.DRIVER_VERSION_MAJOR + "." + LibSqlConstants.DRIVER_VERSION_MINOR); conn.setDoOutput(true); try (OutputStream os = conn.getOutputStream()) {