From 5de6054f2719499b23e9a6aeb1ed7912cdf33113 Mon Sep 17 00:00:00 2001 From: kenstott <128912107+kenstott@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:05:20 -0400 Subject: [PATCH] Downgraded all Java 21 dependent code to Java 11 --- calcite-rs-jni/calcite | 2 +- .../main/java/org/kenstott/CalciteQuery.java | 112 +++--- .../jni/src/main/java/org/kenstott/Main.java | 15 +- .../java/org/kenstott/SQLiteSqlDialect.java | 6 +- .../org/kenstott/SQLiteSqlDialectFactory.java | 357 +++++++++++++----- .../java/org/kenstott/StatementPreparer.java | 11 +- 6 files changed, 335 insertions(+), 168 deletions(-) diff --git a/calcite-rs-jni/calcite b/calcite-rs-jni/calcite index 2c5de27..5a693c7 160000 --- a/calcite-rs-jni/calcite +++ b/calcite-rs-jni/calcite @@ -1 +1 @@ -Subproject commit 2c5de27ec4084221ca33570c7eca79007ad389cd +Subproject commit 5a693c7769d3bfd7995b3ac6657063304608fa7d diff --git a/calcite-rs-jni/jni/src/main/java/org/kenstott/CalciteQuery.java b/calcite-rs-jni/jni/src/main/java/org/kenstott/CalciteQuery.java index a7d7139..1fae021 100644 --- a/calcite-rs-jni/jni/src/main/java/org/kenstott/CalciteQuery.java +++ b/calcite-rs-jni/jni/src/main/java/org/kenstott/CalciteQuery.java @@ -192,7 +192,7 @@ private Collection getTables() { tableTypeList.add(tableType); } } - } catch (SQLException e) { + } catch (Throwable e) { logger.error(e.toString()); throw new RuntimeException(e); } @@ -240,11 +240,11 @@ private Collection getTables() { } catch (SQLException e) { /* ignore */ } list.add(new TableMetadata(catalog, schemaName, tableName, remarks, primaryKeys, exportedKeys, localCatalogName, localSchemaName)); } - } catch (Exception e) { + } catch (Throwable e) { span.setAttribute("Error", e.toString()); } } - } catch(Exception e) { + } catch (Throwable e) { System.err.println(e.toString()); } } @@ -280,54 +280,54 @@ private Map getTableColumnInfo(TableMetadata table) { String description = columnsSet.getString("REMARKS"); String dataTypeName = columnsSet.getString("TYPE_NAME"); boolean nullable = columnsSet.getBoolean("NULLABLE"); - Map remapTypes = Map.ofEntries( - entry("CHAR", "CHAR"), - entry("CHAR(1)", "VARCHAR"), - entry("VARCHAR", "VARCHAR"), - entry("VARCHAR(65536)", "VARCHAR"), - entry("VARCHAR(65536) NOT NULL", "VARCHAR"), - entry("VARCHAR NOT NULL", "VARCHAR"), - entry("JavaType(class java.util.ArrayList)", "LIST"), - entry("JavaType(class org.apache.calcite.adapter.file.ComparableArrayList)", "LIST"), - entry("ANY ARRAY", "LIST"), - entry("VARCHAR NOT NULL ARRAY", "LIST"), - entry("JavaType(class java.util.LinkedHashMap)", "MAP"), - entry("JavaType(class org.apache.calcite.adapter.file.ComparableLinkedHashMap)", "MAP"), - entry("JavaType(class java.lang.String)", "VARCHAR"), - entry("JavaType(class java.lang.Integer)", "INTEGER"), - entry("INTEGER NOT NULL", "INTEGER"), - entry("INTEGER", "INTEGER"), - entry("JSON", "JSON"), - entry("JSONB", "JSON"), - entry("SMALLINT NOT NULL", "INTEGER"), - entry("SMALLINT", "INTEGER"), - entry("TINYINT NOT NULL", "INTEGER"), - entry("TINYINT", "INTEGER"), - entry("BIGINT NOT NULL", "INTEGER"), - entry("BIGINT", "INTEGER"), - entry("FLOAT NOT NULL", "FLOAT"), - entry("FLOAT", "FLOAT"), - entry("DOUBLE NOT NULL", "DOUBLE"), - entry("DOUBLE", "DOUBLE"), - entry("BOOLEAN NOT NULL", "BOOLEAN"), - entry("BOOLEAN", "BOOLEAN"), - entry("VARBINARY NOT NULL", "VARBINARY"), - entry("VARBINARY", "VARBINARY"), - entry("BINARY NOT NULL", "BINARY"), - entry("BINARY", "BINARY"), - entry("DATE NOT NULL", "DATE"), - entry("DATE", "DATE"), - entry("TIME(0) NOT NULL", "TIME"), - entry("TIME(0)", "TIME"), - entry("TIMESTAMP(0) NOT NULL", "TIMESTAMP"), - entry("TIMESTAMP(0)", "TIMESTAMP"), - entry("TIMESTAMP(3) NOT NULL", "TIMESTAMP"), - entry("TIMESTAMP(3)", "TIMESTAMP"), - entry("TIMESTAMP NOT NULL", "TIMESTAMPTZ"), - entry("TIMESTAMP", "TIMESTAMPTZ"), - entry("DECIMAL(10,2)", "FLOAT"), - entry("DECIMAL(12,2)", "FLOAT") - ); + Map remapTypes = new HashMap<>(); + remapTypes.put("CHAR", "CHAR"); + remapTypes.put("CHAR(1)", "VARCHAR"); + remapTypes.put("VARCHAR", "VARCHAR"); + remapTypes.put("VARCHAR(65536)", "VARCHAR"); + remapTypes.put("VARCHAR(65536) NOT NULL", "VARCHAR"); + remapTypes.put("VARCHAR NOT NULL", "VARCHAR"); + remapTypes.put("JavaType(class java.util.ArrayList)", "LIST"); + remapTypes.put("JavaType(class org.apache.calcite.adapter.file.ComparableArrayList)", "LIST"); + remapTypes.put("ANY ARRAY", "LIST"); + remapTypes.put("VARCHAR NOT NULL ARRAY", "LIST"); + remapTypes.put("JavaType(class java.util.LinkedHashMap)", "MAP"); + remapTypes.put("JavaType(class org.apache.calcite.adapter.file.ComparableLinkedHashMap)", "MAP"); + remapTypes.put("JavaType(class java.lang.String)", "VARCHAR"); + remapTypes.put("JavaType(class java.lang.Integer)", "INTEGER"); + remapTypes.put("INTEGER NOT NULL", "INTEGER"); + remapTypes.put("INTEGER", "INTEGER"); + remapTypes.put("JSON", "JSON"); + remapTypes.put("JSONB", "JSON"); + remapTypes.put("SMALLINT NOT NULL", "INTEGER"); + remapTypes.put("SMALLINT", "INTEGER"); + remapTypes.put("TINYINT NOT NULL", "INTEGER"); + remapTypes.put("TINYINT", "INTEGER"); + remapTypes.put("BIGINT NOT NULL", "INTEGER"); + remapTypes.put("BIGINT", "INTEGER"); + remapTypes.put("FLOAT NOT NULL", "FLOAT"); + remapTypes.put("FLOAT", "FLOAT"); + remapTypes.put("DOUBLE NOT NULL", "DOUBLE"); + remapTypes.put("DOUBLE", "DOUBLE"); + remapTypes.put("BOOLEAN NOT NULL", "BOOLEAN"); + remapTypes.put("BOOLEAN", "BOOLEAN"); + remapTypes.put("VARBINARY NOT NULL", "VARBINARY"); + remapTypes.put("VARBINARY", "VARBINARY"); + remapTypes.put("BINARY NOT NULL", "BINARY"); + remapTypes.put("BINARY", "BINARY"); + remapTypes.put("DATE NOT NULL", "DATE"); + remapTypes.put("DATE", "DATE"); + remapTypes.put("TIME(0) NOT NULL", "TIME"); + remapTypes.put("TIME(0)", "TIME"); + remapTypes.put("TIMESTAMP(0) NOT NULL", "TIMESTAMP"); + remapTypes.put("TIMESTAMP(0)", "TIMESTAMP"); + remapTypes.put("TIMESTAMP(3) NOT NULL", "TIMESTAMP"); + remapTypes.put("TIMESTAMP(3)", "TIMESTAMP"); + remapTypes.put("TIMESTAMP NOT NULL", "TIMESTAMPTZ"); + remapTypes.put("TIMESTAMP", "TIMESTAMPTZ"); + remapTypes.put("DECIMAL(10,2)", "FLOAT"); + remapTypes.put("DECIMAL(12,2)", "FLOAT"); + String mappedType = remapTypes.get(dataTypeName); if (mappedType == null) { if (dataTypeName.toLowerCase().contains("varchar") && !dataTypeName.toLowerCase().endsWith("map")) { @@ -474,13 +474,13 @@ public String queryModels(String query, String parentTraceId, String parentSpanI Object value = resultSet.getObject(i); // handling Dates and Timestamps - if (value instanceof java.sql.Date sqlDate) { + if (value instanceof java.sql.Date) { + java.sql.Date sqlDate = (java.sql.Date) value; java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); String rfcDateString = rfcDateFormat.format(utilDate.toInstant()); columns.put(metaData.getColumnLabel(i), rfcDateString); - } - else if (value instanceof java.sql.Timestamp sqlTimestamp) { - // convert to java.util.Date first + } else if (value instanceof java.sql.Timestamp) { + java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp) value; java.util.Date utilDate = new java.util.Date(sqlTimestamp.getTime()); String rfcDateString = rfcFormat.format(utilDate.toInstant()); columns.put(metaData.getColumnLabel(i), rfcDateString); @@ -492,7 +492,7 @@ else if (value instanceof java.sql.Timestamp sqlTimestamp) { } rows.add(columns); } - } catch(Exception e) { + } catch(Throwable e) { e.printStackTrace(); } finally { resultSet.close(); diff --git a/calcite-rs-jni/jni/src/main/java/org/kenstott/Main.java b/calcite-rs-jni/jni/src/main/java/org/kenstott/Main.java index ab2881f..cf83427 100644 --- a/calcite-rs-jni/jni/src/main/java/org/kenstott/Main.java +++ b/calcite-rs-jni/jni/src/main/java/org/kenstott/Main.java @@ -14,13 +14,7 @@ public class Main { public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException { -// String jdbcUrl = "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443?ProjectId=MyProject26996&OAuthType=0&OAuthServiceAcctEmail=kenneth.stott@gmail.com"; -// String encodedUrl = URLEncoder.encode(url, StandardCharsets.UTF_8.toString()); -// System.out.println(encodedUrl); -// Class.forName("com.simba.googlebigquery.jdbc42.Driver"); -// Connection conn = DriverManager.getConnection(jdbcUrl); - - String modelPath = "../../adapters/databricks/model.json"; + String modelPath = "../../adapters/file/model.json"; String username = ""; String password = ""; Connection calciteConnection = null; @@ -30,12 +24,7 @@ public static void main(String[] args) throws IOException, SQLException, ClassNo calciteConnection = query.createCalciteConnection(modelPath); String x = query.getModels(); System.out.println(x); - String q1 = """ - SELECT * from "lineitem" - - OFFSET 2 ROWS - FETCH NEXT 5 ROWS ONLY - """; + String q1 = "SELECT * from \"DEPTS\""; String z1 = query.queryModels(q1); System.out.println(z1); // String z2 = query.queryModels(""" diff --git a/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialect.java b/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialect.java index 95d4d06..4f07675 100644 --- a/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialect.java +++ b/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialect.java @@ -101,8 +101,10 @@ public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightP String phrasesList = String.join(", ", selectItems); // Create the new SQL query - String newSqlQuery = String.format(""" - SELECT %s FROM "%s" WHERE "%s" IN (%s)""", phrasesList, tableName, idColumn, valuesList); + String newSqlQuery = String.format( + "\n SELECT %s FROM \"%s\" WHERE \"%s\" IN (%s)", + phrasesList, tableName, idColumn, valuesList + ); writer.reset(); writer.print(newSqlQuery); } else { diff --git a/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialectFactory.java b/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialectFactory.java index bf2dee4..55752ca 100644 --- a/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialectFactory.java +++ b/calcite-rs-jni/jni/src/main/java/org/kenstott/SQLiteSqlDialectFactory.java @@ -7,7 +7,6 @@ import org.apache.calcite.sql.SqlDialect; import org.apache.calcite.sql.SqlDialectFactory; import org.apache.calcite.sql.dialect.JethroDataSqlDialect; -import org.apache.calcite.sql.fun.SqlLibrary; import org.apache.calcite.sql.validate.SqlConformance; import org.apache.calcite.sql.validate.SqlConformanceEnum; import org.checkerframework.checker.nullness.qual.Nullable; @@ -20,100 +19,274 @@ */ public class SQLiteSqlDialectFactory implements SqlDialectFactory { - private record ContextImpl(SqlDialect.DatabaseProduct databaseProduct, @Nullable String databaseProductName, - @Nullable String databaseVersion, int databaseMajorVersion, int databaseMinorVersion, - String literalQuoteString, String literalEscapedQuoteString, - @Nullable String identifierQuoteString, @Nullable String identifierEscapedQuoteString, - Casing quotedCasing, Casing unquotedCasing, boolean caseSensitive, - SqlConformance conformance, NullCollation nullCollation, - RelDataTypeSystem dataTypeSystem, - JethroDataSqlDialect.JethroInfo jethroInfo) implements SqlDialect.Context { - private ContextImpl(SqlDialect.DatabaseProduct databaseProduct, @Nullable String databaseProductName, @Nullable String databaseVersion, int databaseMajorVersion, int databaseMinorVersion, String literalQuoteString, String literalEscapedQuoteString, @Nullable String identifierQuoteString, @Nullable String identifierEscapedQuoteString, Casing quotedCasing, Casing unquotedCasing, boolean caseSensitive, SqlConformance conformance, NullCollation nullCollation, RelDataTypeSystem dataTypeSystem, JethroDataSqlDialect.JethroInfo jethroInfo) { - this.databaseProduct = Objects.requireNonNull(databaseProduct, "databaseProduct"); - this.databaseProductName = databaseProductName; - this.databaseVersion = databaseVersion; - this.databaseMajorVersion = databaseMajorVersion; - this.databaseMinorVersion = databaseMinorVersion; - this.literalQuoteString = literalQuoteString; - this.literalEscapedQuoteString = literalEscapedQuoteString; - this.identifierQuoteString = identifierQuoteString; - this.identifierEscapedQuoteString = identifierEscapedQuoteString; - this.quotedCasing = Objects.requireNonNull(quotedCasing, "quotedCasing"); - this.unquotedCasing = Objects.requireNonNull(unquotedCasing, "unquotedCasing"); - this.caseSensitive = caseSensitive; - this.conformance = Objects.requireNonNull(conformance, "conformance"); - this.nullCollation = Objects.requireNonNull(nullCollation, "nullCollation"); - this.dataTypeSystem = Objects.requireNonNull(dataTypeSystem, "dataTypeSystem"); - this.jethroInfo = Objects.requireNonNull(jethroInfo, "jethroInfo"); - } - - public SqlDialect.Context withDatabaseProduct(SqlDialect.DatabaseProduct databaseProduct) { - return new ContextImpl(databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withDatabaseProductName(String databaseProductName) { - return new ContextImpl(this.databaseProduct, databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withDatabaseVersion(String databaseVersion) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withDatabaseMajorVersion(int databaseMajorVersion) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withDatabaseMinorVersion(int databaseMinorVersion) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withLiteralQuoteString(String literalQuoteString) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withLiteralEscapedQuoteString(String literalEscapedQuoteString) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withIdentifierQuoteString(@Nullable String identifierQuoteString) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withIdentifierEscapedQuoteString(@Nullable String identifierEscapedQuoteString) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withUnquotedCasing(Casing unquotedCasing) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withQuotedCasing(Casing quotedCasing) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withCaseSensitive(boolean caseSensitive) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withConformance(SqlConformance conformance) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withNullCollation(NullCollation nullCollation) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, nullCollation, this.dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withDataTypeSystem(RelDataTypeSystem dataTypeSystem) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, dataTypeSystem, this.jethroInfo); - } - - public SqlDialect.Context withJethroInfo(JethroDataSqlDialect.JethroInfo jethroInfo) { - return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, jethroInfo); - } + private static class ContextImpl implements SqlDialect.Context { + private final SqlDialect.DatabaseProduct databaseProduct; + private final @Nullable String databaseProductName; + private final @Nullable String databaseVersion; + private final int databaseMajorVersion; + private final int databaseMinorVersion; + private final String literalQuoteString; + private final String literalEscapedQuoteString; + private final @Nullable String identifierQuoteString; + private final @Nullable String identifierEscapedQuoteString; + private final Casing quotedCasing; + private final Casing unquotedCasing; + private final boolean caseSensitive; + private final SqlConformance conformance; + private final NullCollation nullCollation; + private final RelDataTypeSystem dataTypeSystem; + private final JethroDataSqlDialect.JethroInfo jethroInfo; + + private ContextImpl(SqlDialect.DatabaseProduct databaseProduct, @Nullable String databaseProductName, + @Nullable String databaseVersion, int databaseMajorVersion, int databaseMinorVersion, + String literalQuoteString, String literalEscapedQuoteString, + @Nullable String identifierQuoteString, @Nullable String identifierEscapedQuoteString, + Casing quotedCasing, Casing unquotedCasing, boolean caseSensitive, + SqlConformance conformance, NullCollation nullCollation, + RelDataTypeSystem dataTypeSystem, + JethroDataSqlDialect.JethroInfo jethroInfo) { + this.databaseProduct = Objects.requireNonNull(databaseProduct, "databaseProduct"); + this.databaseProductName = databaseProductName; + this.databaseVersion = databaseVersion; + this.databaseMajorVersion = databaseMajorVersion; + this.databaseMinorVersion = databaseMinorVersion; + this.literalQuoteString = literalQuoteString; + this.literalEscapedQuoteString = literalEscapedQuoteString; + this.identifierQuoteString = identifierQuoteString; + this.identifierEscapedQuoteString = identifierEscapedQuoteString; + this.quotedCasing = Objects.requireNonNull(quotedCasing, "quotedCasing"); + this.unquotedCasing = Objects.requireNonNull(unquotedCasing, "unquotedCasing"); + this.caseSensitive = caseSensitive; + this.conformance = Objects.requireNonNull(conformance, "conformance"); + this.nullCollation = Objects.requireNonNull(nullCollation, "nullCollation"); + this.dataTypeSystem = Objects.requireNonNull(dataTypeSystem, "dataTypeSystem"); + this.jethroInfo = Objects.requireNonNull(jethroInfo, "jethroInfo"); + } + + @Override + public SqlDialect.DatabaseProduct databaseProduct() { + return databaseProduct; + } + + @Override + public @Nullable String databaseProductName() { + return databaseProductName; + } + + @Override + public @Nullable String databaseVersion() { + return databaseVersion; + } + + @Override + public int databaseMajorVersion() { + return databaseMajorVersion; + } + + @Override + public int databaseMinorVersion() { + return databaseMinorVersion; + } + + @Override + public String literalQuoteString() { + return literalQuoteString; + } + + @Override + public String literalEscapedQuoteString() { + return literalEscapedQuoteString; + } + + @Override + public @Nullable String identifierQuoteString() { + return identifierQuoteString; + } + + @Override + public @Nullable String identifierEscapedQuoteString() { + return identifierEscapedQuoteString; + } + + @Override + public Casing unquotedCasing() { + return unquotedCasing; + } + + @Override + public Casing quotedCasing() { + return quotedCasing; + } + + @Override + public boolean caseSensitive() { + return caseSensitive; + } + + @Override + public SqlConformance conformance() { + return conformance; + } + + @Override + public NullCollation nullCollation() { + return nullCollation; + } + + @Override + public RelDataTypeSystem dataTypeSystem() { + return dataTypeSystem; + } + + @Override + public JethroDataSqlDialect.JethroInfo jethroInfo() { + return jethroInfo; + } + + public SqlDialect.Context withDatabaseProduct(SqlDialect.DatabaseProduct databaseProduct) { + return new ContextImpl(databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withDatabaseProductName(String databaseProductName) { + return new ContextImpl(this.databaseProduct, databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withDatabaseVersion(String databaseVersion) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withDatabaseMajorVersion(int databaseMajorVersion) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withDatabaseMinorVersion(int databaseMinorVersion) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withLiteralQuoteString(String literalQuoteString) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withLiteralEscapedQuoteString(String literalEscapedQuoteString) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withIdentifierQuoteString(@Nullable String identifierQuoteString) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); } + public SqlDialect.Context withIdentifierEscapedQuoteString(@Nullable String identifierEscapedQuoteString) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withUnquotedCasing(Casing unquotedCasing) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withQuotedCasing(Casing quotedCasing) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withCaseSensitive(boolean caseSensitive) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withConformance(SqlConformance conformance) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, conformance, this.nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withNullCollation(NullCollation nullCollation) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, nullCollation, this.dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withDataTypeSystem(RelDataTypeSystem dataTypeSystem) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, dataTypeSystem, this.jethroInfo); + } + + public SqlDialect.Context withJethroInfo(JethroDataSqlDialect.JethroInfo jethroInfo) { + return new ContextImpl(this.databaseProduct, this.databaseProductName, this.databaseVersion, this.databaseMajorVersion, this.databaseMinorVersion, this.literalQuoteString, this.literalEscapedQuoteString, this.identifierQuoteString, this.identifierEscapedQuoteString, this.quotedCasing, this.unquotedCasing, this.caseSensitive, this.conformance, this.nullCollation, this.dataTypeSystem, jethroInfo); + } + + + public @Nullable String getDatabaseProductName() { + return databaseProductName; + } + + + public @Nullable String getDatabaseVersion() { + return databaseVersion; + } + + + public int getDatabaseMajorVersion() { + return databaseMajorVersion; + } + + + public int getDatabaseMinorVersion() { + return databaseMinorVersion; + } + + + public String getLiteralQuoteString() { + return literalQuoteString; + } + + + public String getLiteralEscapedQuoteString() { + return literalEscapedQuoteString; + } + + + public @Nullable String getIdentifierQuoteString() { + return identifierQuoteString; + } + + + public @Nullable String getIdentifierEscapedQuoteString() { + return identifierEscapedQuoteString; + } + + + public Casing getUnquotedCasing() { + return unquotedCasing; + } + + + public Casing getQuotedCasing() { + return quotedCasing; + } + + + public boolean isCaseSensitive() { + return caseSensitive; + } + + + public SqlConformance getConformance() { + return conformance; + } + + + public NullCollation getNullCollation() { + return nullCollation; + } + + + public RelDataTypeSystem getDataTypeSystem() { + return dataTypeSystem; + } + + + public JethroDataSqlDialect.JethroInfo getJethroInfo() { + return jethroInfo; + } + } + private static SqlDialect.Context context() { - return new ContextImpl(SqlDialect.DatabaseProduct.MYSQL, null, null, -1, -1, "'", "''", "\"", null, Casing.UNCHANGED, Casing.TO_UPPER, true, SqlConformanceEnum.DEFAULT, NullCollation.HIGH, RelDataTypeSystemImpl.DEFAULT, JethroDataSqlDialect.JethroInfo.EMPTY); + return new ContextImpl(SqlDialect.DatabaseProduct.MYSQL, null, null, -1, -1, "'", "''", "\"", null, + Casing.UNCHANGED, Casing.TO_UPPER, true, SqlConformanceEnum.DEFAULT, + NullCollation.HIGH, RelDataTypeSystemImpl.DEFAULT, JethroDataSqlDialect.JethroInfo.EMPTY); } @Override diff --git a/calcite-rs-jni/jni/src/main/java/org/kenstott/StatementPreparer.java b/calcite-rs-jni/jni/src/main/java/org/kenstott/StatementPreparer.java index f45d22d..a6a28f5 100644 --- a/calcite-rs-jni/jni/src/main/java/org/kenstott/StatementPreparer.java +++ b/calcite-rs-jni/jni/src/main/java/org/kenstott/StatementPreparer.java @@ -43,12 +43,15 @@ public static PreparedStatement prepare(String input, Connection connection) thr private static String findParams(String input, ArrayList extractedStrings, ArrayList params) { Pattern pattern = Pattern.compile("\\?(\\d+)\\?"); Matcher matcher = pattern.matcher(input); - return matcher.replaceAll(match -> { - String value = match.group().replaceAll("\\?", ""); + StringBuffer sb = new StringBuffer(); + while (matcher.find()) { + String value = matcher.group().replaceAll("\\?", ""); int index = Integer.parseInt(value); params.add(extractedStrings.get(index)); - return PARAM_MARKER; - }); + matcher.appendReplacement(sb, PARAM_MARKER); + } + matcher.appendTail(sb); + return sb.toString(); } private static ArrayList extractMarkedUpStrings(String input) {