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/05 01:25:38 UTC
[skywalking] branch master updated: Use prepareStatement in H2SQLExecutor#getByIDs (#9561)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 5ad3782231 Use prepareStatement in H2SQLExecutor#getByIDs (#9561)
5ad3782231 is described below
commit 5ad3782231ddb2433122a9036c91a9681dc3d40d
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Sep 5 09:25:27 2022 +0800
Use prepareStatement in H2SQLExecutor#getByIDs (#9561)
---
docs/en/changes/changes.md | 1 +
.../storage/plugin/jdbc/ArrayParamBuilder.java | 32 -------------
.../storage/plugin/jdbc/h2/dao/H2SQLExecutor.java | 53 ++++++++++++----------
.../plugin/jdbc/ArrayParamBuilderTestCase.java | 43 ------------------
4 files changed, 30 insertions(+), 99 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/ArrayParamBuilder.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java
deleted file mode 100644
index 1dd8a56ea8..0000000000
--- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.skywalking.oap.server.storage.plugin.jdbc;
-
-public class ArrayParamBuilder {
-
- public static String build(String[] values) {
- StringBuilder param = new StringBuilder();
- for (int i = 0; i < values.length; i++) {
- param.append("'").append(values[i]).append("'");
- if (i < values.length - 1) {
- param.append(",");
- }
- }
- return param.toString();
- }
-}
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..741aee55b8 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<>(ids.length);
+ 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.toArray(new Object[0]))) {
+ 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(",");
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java
deleted file mode 100644
index 2af41c040a..0000000000
--- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.skywalking.oap.server.storage.plugin.jdbc;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class ArrayParamBuilderTestCase {
-
- @Test
- public void testBuild() {
- String param = ArrayParamBuilder.build(new String[] {"1"});
- Assert.assertEquals("'1'", param);
-
- param = ArrayParamBuilder.build(new String[] {
- "1",
- "2"
- });
- Assert.assertEquals("'1','2'", param);
-
- param = ArrayParamBuilder.build(new String[] {
- "1",
- "2",
- "3"
- });
- Assert.assertEquals("'1','2','3'", param);
- }
-}