forked from tikv/client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiDBJDBCClient.java
More file actions
156 lines (139 loc) · 5.37 KB
/
Copy pathTiDBJDBCClient.java
File metadata and controls
156 lines (139 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2021 TiKV Project Authors.
*
* Licensed 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.tikv.common;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TiDBJDBCClient implements AutoCloseable {
private static final String UNLOCK_TABLES_SQL = "unlock tables";
private static final String SELECT_TIDB_CONFIG_SQL = "select @@tidb_config";
private static final String ENABLE_TABLE_LOCK_KEY = "enable-table-lock";
private static final Boolean ENABLE_TABLE_LOCK_DEFAULT = false;
private static final String DELAY_CLEAN_TABLE_LOCK = "delay-clean-table-lock";
private static final int DELAY_CLEAN_TABLE_LOCK_DEFAULT = 0;
private static final String TIDB_ROW_FORMAT_VERSION_SQL = "select @@tidb_row_format_version";
private static final int TIDB_ROW_FORMAT_VERSION_DEFAULT = 1;
private final Logger logger = LoggerFactory.getLogger(getClass().getName());
private final Connection connection;
public TiDBJDBCClient(Connection connection) {
this.connection = connection;
}
public boolean isEnableTableLock() throws IOException, SQLException {
Map<String, Object> configMap = readConfMapFromTiDB();
Object enableTableLock =
configMap.getOrDefault(ENABLE_TABLE_LOCK_KEY, ENABLE_TABLE_LOCK_DEFAULT);
return (Boolean) enableTableLock;
}
/**
* get enable-table-lock config from tidb
*
* @return Milliseconds
* @throws IOException
* @throws SQLException
*/
public int getDelayCleanTableLock() throws IOException, SQLException {
Map<String, Object> configMap = readConfMapFromTiDB();
Object enableTableLock =
configMap.getOrDefault(DELAY_CLEAN_TABLE_LOCK, DELAY_CLEAN_TABLE_LOCK_DEFAULT);
return (int) enableTableLock;
}
/**
* get row format version from tidb
*
* @return 1 if should not encode and write with new row format.(default) 2 if encode and write
* with new row format.(default on v4.0.0 cluster)
*/
public int getRowFormatVersion() {
try {
List<List<Object>> result = queryTiDBViaJDBC(TIDB_ROW_FORMAT_VERSION_SQL);
if (result.isEmpty()) {
// default set to 1
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
} else {
Object version = result.get(0).get(0);
if (version instanceof String) {
return Integer.parseInt((String) version);
} else if (version instanceof Number) {
return ((Number) version).intValue();
} else {
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
}
}
} catch (Exception ignored) {
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
}
}
public boolean lockTableWriteLocal(String databaseName, String tableName) throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
String sql = "lock tables `" + databaseName + "`.`" + tableName + "` write local";
int result = tidbStmt.executeUpdate(sql);
return result == 0;
}
}
public boolean unlockTables() throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
int result = tidbStmt.executeUpdate(UNLOCK_TABLES_SQL);
return result == 0;
}
}
public boolean dropTable(String databaseName, String tableName) throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
String sql = "drop table if exists `" + databaseName + "`.`" + tableName + "`";
return tidbStmt.execute(sql);
}
}
private Map<String, Object> readConfMapFromTiDB() throws SQLException, IOException {
String configJSON = (String) queryTiDBViaJDBC(SELECT_TIDB_CONFIG_SQL).get(0).get(0);
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> typeRef =
new TypeReference<HashMap<String, Object>>() {};
return objectMapper.readValue(configJSON, typeRef);
}
public boolean isClosed() throws SQLException {
return connection.isClosed();
}
@Override
public void close() throws Exception {
connection.close();
}
private List<List<Object>> queryTiDBViaJDBC(String query) throws SQLException {
ArrayList<List<Object>> result = new ArrayList<>();
try (Statement tidbStmt = connection.createStatement()) {
ResultSet resultSet = tidbStmt.executeQuery(query);
ResultSetMetaData rsMetaData = resultSet.getMetaData();
while (resultSet.next()) {
ArrayList<Object> row = new ArrayList<>();
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
row.add(resultSet.getObject(i));
}
result.add(row);
}
}
return result;
}
}