26
26
import apijson .orm .AbstractSQLConfig ;
27
27
28
28
29
- /**SQL配置
29
+ /**SQL 配置
30
30
* TiDB 用法和 MySQL 一致
31
31
* @author Lemon
32
32
*/
@@ -40,7 +40,7 @@ public DemoSQLConfig(RequestMethod method, String table) {
40
40
}
41
41
42
42
static {
43
- DEFAULT_DATABASE = DATABASE_MYSQL ; //TODO 默认数据库类型,改成你自己的
43
+ DEFAULT_DATABASE = DATABASE_MYSQL ; //TODO 默认数据库类型,改成你自己的。TiDB, MariaDB, OceanBase 这类兼容 MySQL 的可当做 MySQL 使用
44
44
DEFAULT_SCHEMA = "sys" ; //TODO 默认数据库名/模式,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: public, SQL Server: dbo, Oracle:
45
45
46
46
//表名和数据库不一致的,需要配置映射关系。只使用 APIJSONORM 时才需要;
@@ -63,7 +63,7 @@ public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, Str
63
63
64
64
//取消注释来实现自定义各个表的主键名
65
65
// @Override
66
- // public String getIdKey(String database, String schema, String table) {
66
+ // public String getIdKey(String database, String schema, String datasource, String table) {
67
67
// return StringUtil.firstCase(table + "Id"); // userId, comemntId ...
68
68
// // return StringUtil.toLowerCase(t) + "_id"; // user_id, comemnt_id ...
69
69
// // return StringUtil.toUpperCase(t) + "_ID"; // USER_ID, COMMENT_ID ...
@@ -76,19 +76,32 @@ public String getUserIdKey(String database, String schema, String datasource, St
76
76
77
77
//取消注释来实现数据库自增 id
78
78
// @Override
79
- // public Object newId(RequestMethod method, String database, String schema, String table) {
79
+ // public Object newId(RequestMethod method, String database, String schema, String datasource, String table) {
80
80
// return null; // return null 则不生成 id,一般用于数据库自增 id
81
81
// }
82
-
83
- // @Override
84
- // public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
85
- //// super.onMissingKey4Combine(name, request, combine, item, key);
86
- // }
82
+
83
+ // @Override
84
+ // public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
85
+ //// super.onMissingKey4Combine(name, request, combine, item, key);
86
+ // }
87
87
};
88
88
89
+ // 自定义原始 SQL 片段,其它功能满足不了时才用它,只有 RAW_MAP 配置了的 key 才允许前端传
90
+ RAW_MAP .put ("`to`.`id`" , "" ); // 空字符串 "" 表示用 key 的值 `to`.`id`
91
+ RAW_MAP .put ("to.momentId" , "`to`.`momentId`" ); // 最终以 `to`.`userId` 拼接 SQL,相比以上写法可以让前端写起来更简单
92
+ RAW_MAP .put ("(`Comment`.`userId`=`to`.`userId`)" , "" ); // 已经是一个条件表达式了,用 () 包裹是为了避免 JSON 中的 key 拼接在前面导致 SQL 出错
93
+ RAW_MAP .put ("sum(if(userId%2=0,1,0))" , "" ); // 超过单个函数的 SQL 表达式
94
+ RAW_MAP .put ("sumUserIdIsEven" , "sum(if(`userId`%2=0,1,0)) AS sumUserIdIsEven" ); // 简化前端传参
95
+ RAW_MAP .put ("substring_index(substring_index(content,',',1),',',-1)" , "" ); // APIAuto 不支持 ',可以用 Postman 测
96
+ RAW_MAP .put ("substring_index(substring_index(content,'.',1),'.',-1) AS subContent" , "" ); // APIAuto 不支持 ',可以用 Postman 测
97
+ RAW_MAP .put ("commentWhereItem1" ,"(`Comment`.`userId` = 38710 AND `Comment`.`momentId` = 470)" );
98
+ RAW_MAP .put ("to_days(now())-to_days(`date`)<=7" ,"" ); // 给 @having 使用
99
+
89
100
}
90
101
91
102
103
+ // 如果 DemoSQLExecutor.getConnection 能拿到连接池的有效 Connection,则这里不需要配置 dbVersion, dbUri, dbAccount, dbPassword
104
+
92
105
@ Override
93
106
public String getDBVersion () {
94
107
if (isMySQL ()) {
@@ -108,15 +121,17 @@ public String getDBVersion() {
108
121
}
109
122
return null ;
110
123
}
111
-
124
+
112
125
@ JSONField (serialize = false ) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
113
126
@ Override
114
127
public String getDBUri () {
115
128
if (isMySQL ()) {
116
- return "jdbc:mysql://localhost:3306" ; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
129
+ // 这个是 MySQL 8.0 及以上,要加 userSSL=false return "jdbc:mysql://localhost:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
130
+ // 以下是 MySQL 5.7 及以下
131
+ return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8" ; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
117
132
}
118
133
if (isPostgreSQL ()) {
119
- return "jdbc:postgresql://localhost:5432/postgres" ; //TODO 改成你自己的
134
+ return "jdbc:postgresql://localhost:5432/postgres?stringtype=unspecified " ; //TODO 改成你自己的
120
135
}
121
136
if (isSQLServer ()) {
122
137
return "jdbc:jtds:sqlserver://localhost:1433/pubs;instance=SQLEXPRESS" ; //TODO 改成你自己的
@@ -129,7 +144,7 @@ public String getDBUri() {
129
144
}
130
145
return null ;
131
146
}
132
-
147
+
133
148
@ JSONField (serialize = false ) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
134
149
@ Override
135
150
public String getDBAccount () {
@@ -150,7 +165,7 @@ public String getDBAccount() {
150
165
}
151
166
return null ;
152
167
}
153
-
168
+
154
169
@ JSONField (serialize = false ) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
155
170
@ Override
156
171
public String getDBPassword () {
@@ -171,8 +186,12 @@ public String getDBPassword() {
171
186
}
172
187
return null ;
173
188
}
174
-
175
- //取消注释后,默认的 APIJSON 配置表会由业务表所在数据库模式 schema 改为自定义的
189
+
190
+ //取消注释后,默认的 APIJSON 配置表会由业务表所在 数据库类型 database 和 数据库模式 schema 改为自定义的
191
+ // @Override
192
+ // public String getConfigDatabase() {
193
+ // return DATABASE_POSTGRESQL;
194
+ // }
176
195
// @Override
177
196
// public String getConfigSchema() {
178
197
// return "apijson";
@@ -207,4 +226,35 @@ public String getDBPassword() {
207
226
// return false;
208
227
// }
209
228
229
+
230
+ // 取消注释支持 !key 反选字段 和 字段名映射,需要先依赖插件 https://github.com/APIJSON/apijson-column
231
+ // @Override
232
+ // public AbstractSQLConfig setColumn(List<String> column) {
233
+ // return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), getMethod()));
234
+ // }
235
+ // @Override
236
+ // public String getKey(String key) {
237
+ // return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod()));
238
+ // }
239
+
240
+ // 取消注释来兼容 Oracle DATETIME, TIMESTAMP 等日期时间类型的值来写库
241
+ // public Object getValue(@NotNull Object value) {
242
+ // if (isOracle() && RequestMethod.isQueryMethod(getMethod()) == false && value instanceof String) {
243
+ // try {
244
+ // SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
245
+ // parser.parse((String) value);
246
+ // if (isPrepared()) {
247
+ // preparedValueList.add(value);
248
+ // }
249
+ // return "to_date(" + (isPrepared() ? "?" : getSQLValue(value)) + ",'yyyy-mm-dd hh24:mi:ss')";
250
+ // }
251
+ // catch (Throwable e) {
252
+ // if (Log.DEBUG) {
253
+ // e.printStackTrace();
254
+ // }
255
+ // }
256
+ // }
257
+ // return super.getValue(value);
258
+ // }
259
+
210
260
}
0 commit comments