8000 Server:新增支持全局默认@database · coder-caicai/APIJSON@e63c066 · GitHub
[go: up one dir, main page]

Skip to content

Commit e63c066

Browse files
committed
Server:新增支持全局默认@database
1 parent 694810b commit e63c066

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class DemoSQLExecutor extends AbstractSQLExecutor {
4242
} catch (ClassNotFoundException e) {
4343
e.printStackTrace();
4444
}
45-
45+
4646
try { //加载驱动程序
4747
Class.forName("org.postgresql.Driver");
4848
Log.d(TAG, "成功加载 PostgresSQL 驱动!");
@@ -65,6 +65,8 @@ public int executeUpdate(@NotNull SQLConfig config) throws Exception {
6565
}
6666

6767

68+
//TODO 根据不同数据库来分组存 connection 和 statement,例如 Map<database, connection>,
69+
// 解决一次请求中有2个以上不同数据库类型导致后面的查询都用第一个的数据库类型
6870
private Connection connection = null;
6971
private PreparedStatement statement = null;
7072
/**
@@ -76,7 +78,6 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
7678
if (connection == null || connection.isClosed()) {
7779
Log.i(TAG, "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
7880

79-
8081
if (DemoSQLConfig.DATABASE_POSTGRESQL.equalsIgnoreCase(config.getDatabase())) { //PostgreSQL 不允许 cross-database
8182
connection = DriverManager.getConnection(config.getDBUri() + "/" + config.getSchema(), config.getDBAccount(), config.getDBPassword());
8283
}
@@ -85,14 +86,14 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
8586
+ config.getDBAccount() + "&password=" + config.getDBPassword());
8687
}
8788
}
88-
89+
8990
statement = connection.prepareStatement(config.getSQL(config.isPrepared())); //创建Statement对象
9091
List<Object> valueList = config.isPrepared() ? config.getPreparedValueList() : null;
91-
92+
9293
if (valueList != null && valueList.isEmpty() == false) {
93-
94+
9495
for (int i = 0; i < valueList.size(); i++) {
95-
96+
9697
if (DemoSQLConfig.DATABASE_POSTGRESQL.equalsIgnoreCase(config.getDatabase())) {
9798
statement.setObject(i + 1, valueList.get(i)); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错
9899
}

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public AbstractParser setGlobleRole(RequestRole globleRole) {
131131
this.globleRole = globleRole;
132132
return this;
133133
}
134+
protected String globleDatabase;
135+
public AbstractParser setGlobleDatabase(String globleDatabase) {
136+
this.globleDatabase = globleDatabase;
137+
return this;
138+
}
134139

135140
@Override
136141
public boolean isNoVerify() {
@@ -257,6 +262,13 @@ public JSONObject parseResponse(JSONObject request) {
257262
return extendErrorResult(requestObject, e);
258263
}
259264
}
265+
266+
try {
267+
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
268+
requestObject.remove(JSONRequest.KEY_DATABASE);
269+
} catch (Exception e) {
270+
return extendErrorResult(requestObject, e);
271+
}
260272

261273
final String requestString = JSON.toJSONString(request);//request传进去解析后已经变了
262274

@@ -1096,6 +1108,11 @@ public synchronized JSONObject executeSQL(SQLConfig config) throws Exception {
10961108
}
10971109
verifier.verify(config);
10981110
}
1111+
1112+
if (config.getDatabase() == null && globleDatabase != null) {
1113+
config.setDatabase(globleDatabase);
1114+
}
1115+
10991116
return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config));
11001117
}
11011118

0 commit comments

Comments
 (0)
0