|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse; |
| 17 | + |
| 18 | +import com.clickhouse.jdbc.ClickHouseConnection; |
| 19 | +import com.clickhouse.jdbc.ClickHouseStatement; |
| 20 | +import com.navercorp.pinpoint.bootstrap.context.DatabaseInfo; |
| 21 | +import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2; |
| 22 | +import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations; |
| 23 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier; |
| 24 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder; |
| 25 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties; |
| 26 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCApi; |
| 27 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass; |
| 28 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.template.DriverManagerDataSource; |
| 29 | +import com.navercorp.pinpoint.plugin.jdbc.clickhouse.ClickHouseConstants; |
| 30 | +import org.apache.logging.log4j.LogManager; |
| 31 | +import org.apache.logging.log4j.Logger; |
| 32 | + |
| 33 | +import javax.sql.DataSource; |
| 34 | +import java.lang.reflect.Method; |
| 35 | +import java.sql.Driver; |
| 36 | +import java.sql.DriverManager; |
| 37 | +import java.sql.ResultSet; |
| 38 | +import java.sql.SQLException; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author intr3p1d |
| 42 | + */ |
| 43 | +public class ClickHouseITBase { |
| 44 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 45 | + static final String TABLE_NAME = "jdbc_example_basic"; |
| 46 | + |
| 47 | + protected DriverProperties driverProperties; |
| 48 | + protected ClickHouseITHelper clickHouseITHelper; |
| 49 | + |
| 50 | + protected String DB_TYPE = ClickHouseConstants.CLICK_HOUSE.getName(); |
| 51 | + protected String DB_EXECUTE_QUERY = ClickHouseConstants.CLICK_HOUSE_EXECUTE_QUERY.getName(); |
| 52 | + |
| 53 | + protected String jdbcUrl; |
| 54 | + protected String databaseId; |
| 55 | + protected String databaseIdPassword; |
| 56 | + protected String databaseAddress; |
| 57 | + protected String databaseName; |
| 58 | + protected JDBCDriverClass jdbcDriverClass; |
| 59 | + protected JDBCApi jdbcApi; |
| 60 | + private DataSource dataSource; |
| 61 | + |
| 62 | + public void setup( |
| 63 | + DriverProperties driverProperties, |
| 64 | + JdbcUrlParserV2 jdbcUrlParser, |
| 65 | + JDBCDriverClass jdbcDriverClass, |
| 66 | + JDBCApi jdbcApi) { |
| 67 | + this.driverProperties = driverProperties; |
| 68 | + this.clickHouseITHelper = new ClickHouseITHelper(driverProperties); |
| 69 | + |
| 70 | + this.jdbcUrl = driverProperties.getUrl(); |
| 71 | + |
| 72 | + DatabaseInfo databaseInfo = jdbcUrlParser.parse(jdbcUrl); |
| 73 | + |
| 74 | + this.databaseAddress = databaseInfo.getHost().get(0); |
| 75 | + this.databaseName = databaseInfo.getDatabaseId(); |
| 76 | + |
| 77 | + this.databaseId = driverProperties.getUser(); |
| 78 | + this.databaseIdPassword = driverProperties.getPassword(); |
| 79 | + this.dataSource = new DriverManagerDataSource(jdbcUrl, databaseId, databaseIdPassword); |
| 80 | + |
| 81 | + this.jdbcDriverClass = jdbcDriverClass; |
| 82 | + this.jdbcApi = jdbcApi; |
| 83 | + |
| 84 | + try { |
| 85 | + Driver driver = jdbcDriverClass.getDriver().newInstance(); |
| 86 | + DriverManager.registerDriver(driver); |
| 87 | + } catch (Exception e) { |
| 88 | + throw new RuntimeException("driver register error", e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private ClickHouseConnection getConnection() throws SQLException { |
| 93 | + return clickHouseITHelper.getConnection(); |
| 94 | + } |
| 95 | + |
| 96 | + public void executeQueries() throws SQLException { |
| 97 | + |
| 98 | + PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); |
| 99 | + verifier.printCache(); |
| 100 | + |
| 101 | + ClickHouseConnection conn = getConnection(); |
| 102 | + String dropAndCreateQuery = String.format( |
| 103 | + "drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory", |
| 104 | + TABLE_NAME); |
| 105 | + |
| 106 | + int count; |
| 107 | + try (ClickHouseStatement stmt = conn.createStatement()) { |
| 108 | + // multi-statement query is supported by default |
| 109 | + // session will be created automatically during execution |
| 110 | + stmt.execute(dropAndCreateQuery); |
| 111 | + count = stmt.getUpdateCount(); |
| 112 | + logger.info(count); |
| 113 | + } |
| 114 | + |
| 115 | + String sql = "select * from " + TABLE_NAME; |
| 116 | + try (ClickHouseStatement stmt = conn.createStatement()) { |
| 117 | + // set max_result_rows = 3, result_overflow_mode = 'break' |
| 118 | + // or simply discard rows after the first 3 in read-only mode |
| 119 | + stmt.setMaxRows(3); |
| 120 | + count = 0; |
| 121 | + try (ResultSet rs = stmt.executeQuery(sql)) { |
| 122 | + while (rs.next()) { |
| 123 | + count++; |
| 124 | + } |
| 125 | + } |
| 126 | + logger.info(count); |
| 127 | + } |
| 128 | + |
| 129 | + Method connect = jdbcApi.getDriver().getConnect(); |
| 130 | + verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl))); |
| 131 | + |
| 132 | + Method execute = jdbcApi.getPreparedStatement().getExecute(); |
| 133 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName, Expectations.sql(dropAndCreateQuery, null))); |
| 134 | + |
| 135 | + Method executeQuery = jdbcApi.getPreparedStatement().getExecuteQuery(); |
| 136 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeQuery, null, databaseAddress, databaseName, Expectations.sql(sql, null))); |
| 137 | + |
| 138 | + } |
| 139 | +} |
0 commit comments