You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/09/04 16:12:24 UTC

[skywalking] branch sql-db created (now 9d83029fe5)

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

wusheng pushed a change to branch sql-db
in repository https://gitbox.apache.org/repos/asf/skywalking.git


      at 9d83029fe5 Use prepareStatement in H2SQLExecutor#getByIDs

This branch includes the following new commits:

     new 9d83029fe5 Use prepareStatement in H2SQLExecutor#getByIDs

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[skywalking] 01/01: Use prepareStatement in H2SQLExecutor#getByIDs

Posted by wu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch sql-db
in repository https://gitbox.apache.org/repos/asf/skywalking.git

commit 9d83029fe5e112d1caf67a3b7ccfef4db25aa8fe
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Sep 5 00:12:09 2022 +0800

    Use prepareStatement in H2SQLExecutor#getByIDs
---
 docs/en/changes/changes.md                         |  1 +
 .../storage/plugin/jdbc/h2/dao/H2SQLExecutor.java  | 53 ++++++++++++----------
 2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index 290dd8ee54..85a81c6407 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -6,6 +6,7 @@
 #### OAP Server
 
 * Add component ID(133) for impala JDBC Java agent plugin and component ID(134) for impala server
+* Use prepareStatement in H2SQLExecutor#getByIDs.(No function change).
 
 #### UI
 
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java
index 39500d2a23..47b3f2ec7e 100644
--- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java
@@ -38,7 +38,6 @@ import org.apache.skywalking.oap.server.core.storage.type.StorageDataComplexObje
 import org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException;
 import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient;
 import org.apache.skywalking.oap.server.library.util.CollectionUtils;
-import org.apache.skywalking.oap.server.storage.plugin.jdbc.ArrayParamBuilder;
 import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLBuilder;
 import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor;
 import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo;
@@ -49,26 +48,32 @@ public class H2SQLExecutor {
                                                                  String modelName,
                                                                  String[] ids,
                                                                  StorageBuilder<T> storageBuilder) throws IOException {
-        /*
-         * Although H2 database or other database support createArrayOf and setArray operate,
-         * Mysql 5.1.44 driver doesn't.
-         */
-        String param = ArrayParamBuilder.build(ids);
 
-        try (Connection connection = h2Client.getConnection();
-             ResultSet rs = h2Client.executeQuery(
-                 connection, "SELECT * FROM " + modelName + " WHERE id in (" + param + ")")) {
-            List<StorageData> storageDataList = new ArrayList<>();
-            StorageData storageData;
-            do {
-                storageData = toStorageData(rs, modelName, storageBuilder);
-                if (storageData != null) {
-                    storageDataList.add(storageData);
+        try (Connection connection = h2Client.getConnection()) {
+            SQLBuilder sql = new SQLBuilder("SELECT * FROM " + modelName + " WHERE id in (");
+            List<Object> parameters = new ArrayList<>();
+            for (int i = 0; i < ids.length; i++) {
+                if (i == 0) {
+                    sql.append("?");
+                } else {
+                    sql.append(",?");
                 }
+                parameters.add(ids[i]);
             }
-            while (storageData != null);
+            sql.append(")");
+            try (ResultSet rs = h2Client.executeQuery(connection, sql.toString(), parameters)) {
+                StorageData storageData;
+                List<StorageData> storageDataList = new ArrayList<>();
+                do {
+                    storageData = toStorageData(rs, modelName, storageBuilder);
+                    if (storageData != null) {
+                        storageDataList.add(storageData);
+                    }
+                }
+                while (storageData != null);
 
-            return storageDataList;
+                return storageDataList;
+            }
         } catch (SQLException | JDBCClientException e) {
             throw new IOException(e.getMessage(), e);
         }
@@ -148,14 +153,14 @@ public class H2SQLExecutor {
         param.add(metrics.id());
         for (int i = 0; i < columns.size(); i++) {
             ModelColumn column = columns.get(i);
-                sqlBuilder.append("?");
+            sqlBuilder.append("?");
 
-                Object value = objectMap.get(column.getColumnName().getName());
-                if (value instanceof StorageDataComplexObject) {
-                    param.add(((StorageDataComplexObject) value).toStorageData());
-                } else {
-                    param.add(value);
-                }
+            Object value = objectMap.get(column.getColumnName().getName());
+            if (value instanceof StorageDataComplexObject) {
+                param.add(((StorageDataComplexObject) value).toStorageData());
+            } else {
+                param.add(value);
+            }
 
             if (i != columns.size() - 1) {
                 sqlBuilder.append(",");