Skip to content

Commit 7d33b02

Browse files
committed
Server:MySQL preparedStatement 严格按照类型调用方法,避免隐式转换提高性能
1 parent 6743103 commit 7d33b02

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/DemoSQLExecutor.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,23 @@ public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exceptio
109109
Object v;
110110
for (int i = 0; i < valueList.size(); i++) {
111111
v = valueList.get(i); //JSON.isBooleanOrNumberOrString(v) 解决 PostgreSQL: Can't infer the SQL type to use for an instance of com.alibaba.fastjson.JSONArray
112-
113-
if (DemoSQLConfig.DATABASE_POSTGRESQL.equals(config.getDatabase())) {
114-
if (JSON.isBooleanOrNumberOrString(v)) {
115-
statement.setObject(i + 1, v); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错
116-
}
117-
else {
112+
113+
if (JSON.isBooleanOrNumberOrString(v)) {
114+
statement.setObject(i + 1, v); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错
115+
}
116+
else {
117+
if (DemoSQLConfig.DATABASE_POSTGRESQL.equals(config.getDatabase())) {
118+
118119
PGobject o = new PGobject();
119120
o.setType("jsonb");
120121
o.setValue(v == null ? null : v.toString());
121122
statement.setObject(i + 1, o); //PostgreSQL 除了基本类型,其它的必须通过 PGobject 设置进去,否则 jsonb = varchar 等报错
122123
}
124+
else {
125+
statement.setString(i + 1, v == null ? null : v.toString()); //MySQL setObject 不支持 JSON 类型
126+
}
123127
}
124-
else {
125-
statement.setString(i + 1, v == null ? null : v.toString()); //MySQL setObject 不支持 JSON 类型
126-
}
128+
127129
}
128130
}
129131
// statement.close();
@@ -135,12 +137,12 @@ public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exceptio
135137
protected Object getValue(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
136138
JSONObject table, int columnIndex, Map<String, JSONObject> childMap) throws Exception {
137139
Object value = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, childMap);
138-
140+
139141
if (value instanceof Blob) { //FIXME 存的是 abcde,取出来直接就是 [97, 98, 99, 100, 101] 这种 byte[] 类型,没有经过以下处理,但最终序列化后又变成了字符串 YWJjZGU=
140142
value = new String(((Blob) value).getBytes(1, (int) ((Blob) value).length()), "UTF-8");
141143
}
142144
else if (value instanceof Clob) {
143-
145+
144146
StringBuffer sb = new StringBuffer();
145147
BufferedReader br = new BufferedReader(((Clob) value).getCharacterStream());
146148
String s = br.readLine();
@@ -166,9 +168,9 @@ public void close() {
166168
if (connectionMap == null) {
167169
return;
168170
}
169-
171+
170172
Collection<Connection> connections = connectionMap.values();
171-
173+
172174
if (connections != null) {
173175
for (Connection connection : connections) {
174176
try {

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,8 +1341,10 @@ private Object getValue(@NotNull Object value) {
13411341
preparedValueList.add(value);
13421342
return "?";
13431343
}
1344-
return (value instanceof Number || value instanceof Boolean) && DATABASE_POSTGRESQL.equals(getDatabase()) ? value : "'" + value + "'";
1344+
// return (value instanceof Number || value instanceof Boolean) && DATABASE_POSTGRESQL.equals(getDatabase()) ? value : "'" + value + "'";
1345+
return (value instanceof Number || value instanceof Boolean) ? value : "'" + value + "'"; //MySQL 隐式转换用不了索引
13451346
}
1347+
13461348
@Override
13471349
public List<Object> getPreparedValueList() {
13481350
return preparedValueList;

0 commit comments

Comments
 (0)