You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2024/08/15 17:57:19 UTC

(sis) 02/05: Add a constant for the "INSERT INTO" string, for the same reason as "SELECT": make a little bit easier to identify where we build a SQL insert statement. We do not use it for `toString()` implementations, as they are not really SQL.

This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit d72d2efd1f07933d961d74edcd2d1735c0061a70
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Aug 15 11:18:01 2024 +0200

    Add a constant for the "INSERT INTO" string, for the same reason as "SELECT":
    make a little bit easier to identify where we build a SQL insert statement.
    We do not use it for `toString()` implementations, as they are not really SQL.
---
 .../main/org/apache/sis/metadata/sql/MetadataWriter.java           | 4 ++--
 .../main/org/apache/sis/metadata/sql/privy/SQLBuilder.java         | 7 +++++++
 .../main/org/apache/sis/metadata/sql/privy/ScriptRunner.java       | 2 +-
 .../main/org/apache/sis/storage/sql/postgis/ExtentEstimator.java   | 2 +-
 .../sis/referencing/factory/sql/epsg/DataScriptFormatter.java      | 3 ++-
 5 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/MetadataWriter.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/MetadataWriter.java
index 54a7bc5a76..7e8f29807e 100644
--- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/MetadataWriter.java
+++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/MetadataWriter.java
@@ -511,7 +511,7 @@ public class MetadataWriter extends MetadataSource {
         /*
          * Create the SQL statement which will insert the data.
          */
-        helper.clear().append("INSERT INTO ").appendIdentifier(schema(), table).append(" (").appendIdentifier(ID_COLUMN);
+        helper.clear().append(SQLBuilder.INSERT).appendIdentifier(schema(), table).append(" (").appendIdentifier(ID_COLUMN);
         for (final String column : asSingletons.keySet()) {
             helper.append(", ").appendIdentifier(column);
         }
@@ -665,7 +665,7 @@ public class MetadataWriter extends MetadataSource {
             exists = rs.next();
         }
         if (!exists) {
-            final String sql = helper().clear().append("INSERT INTO ").appendIdentifier(schema(), table)
+            final String sql = helper().clear().append(SQLBuilder.INSERT).appendIdentifier(schema(), table)
                     .append(" (").appendIdentifier(CODE_COLUMN).append(") VALUES (").appendValue(identifier)
                     .append(')').toString();
             if (stmt.executeUpdate(sql) != 1) {
diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/SQLBuilder.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/SQLBuilder.java
index 50bc4fa242..e3b3dac0a7 100644
--- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/SQLBuilder.java
+++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/SQLBuilder.java
@@ -36,6 +36,13 @@ public class SQLBuilder extends Syntax {
      */
     public static final String SELECT = "SELECT ";
 
+    /**
+     * The {@value} keyword (with a trailing space).
+     * Defined as a convenience for identifying locations in the Java code
+     * where we start to write a SQL statement using a builder.
+     */
+    public static final String INSERT = "INSERT INTO ";
+
     /**
      * The buffer where the SQL query is created.
      */
diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/ScriptRunner.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/ScriptRunner.java
index 94b5216564..486268f6d6 100644
--- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/ScriptRunner.java
+++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/metadata/sql/privy/ScriptRunner.java
@@ -742,7 +742,7 @@ parseLine:  while (pos < length) {
          * The scripts usually do not contain any SELECT statement. One exception is the creation
          * of geometry columns in a PostGIS database, which use "SELECT AddGeometryColumn(…)".
          */
-        if (subSQL.startsWith("SELECT ")) {
+        if (subSQL.startsWith(SQLBuilder.SELECT)) {
             statement.executeQuery(subSQL).close();
         } else {
             if (maxRowsPerInsert != Integer.MAX_VALUE && subSQL.startsWith("INSERT INTO")) {
diff --git a/endorsed/src/org.apache.sis.storage.sql/main/org/apache/sis/storage/sql/postgis/ExtentEstimator.java b/endorsed/src/org.apache.sis.storage.sql/main/org/apache/sis/storage/sql/postgis/ExtentEstimator.java
index ee88f50814..97367f75a6 100644
--- a/endorsed/src/org.apache.sis.storage.sql/main/org/apache/sis/storage/sql/postgis/ExtentEstimator.java
+++ b/endorsed/src/org.apache.sis.storage.sql/main/org/apache/sis/storage/sql/postgis/ExtentEstimator.java
@@ -120,7 +120,7 @@ final class ExtentEstimator {
     private void query(final Statement statement) throws SQLException {
         for (final Column column : columns) {
             if (column.getGeometryType().isPresent()) {
-                database.appendFunctionCall(builder.append("SELECT "), "ST_EstimatedExtent");
+                database.appendFunctionCall(builder.append(SQLBuilder.SELECT), "ST_EstimatedExtent");
                 builder.append('(');
                 if (table.schema != null) {
                     builder.appendValue(table.schema).append(", ");
diff --git a/optional/src/org.apache.sis.referencing.epsg/test/org/apache/sis/referencing/factory/sql/epsg/DataScriptFormatter.java b/optional/src/org.apache.sis.referencing.epsg/test/org/apache/sis/referencing/factory/sql/epsg/DataScriptFormatter.java
index a5bb34d6b7..d09221a312 100644
--- a/optional/src/org.apache.sis.referencing.epsg/test/org/apache/sis/referencing/factory/sql/epsg/DataScriptFormatter.java
+++ b/optional/src/org.apache.sis.referencing.epsg/test/org/apache/sis/referencing/factory/sql/epsg/DataScriptFormatter.java
@@ -34,6 +34,7 @@ import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Workaround;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.privy.URLs;
+import org.apache.sis.metadata.sql.privy.SQLBuilder;
 import org.apache.sis.metadata.sql.privy.ScriptRunner;
 
 // Test dependencies
@@ -228,7 +229,7 @@ public final class DataScriptFormatter extends ScriptRunner {
         }
         if (CharSequences.regionMatches(sql, ++lower, oldValue)) {
             final int s = CharSequences.skipLeadingWhitespaces(sql, 0, lower);
-            if (CharSequences.regionMatches(sql, s, "INSERT INTO " + table + " VALUES")) {
+            if (CharSequences.regionMatches(sql, s, SQLBuilder.INSERT + table + " VALUES")) {
                 if (--upper - lower != oldValue.length()) {
                     throw new AssertionError("Unexpected length");
                 }