Skip to content

Commit 02e6be3

Browse files
committed
Server:支持通过PgAttribute和PgClass联表查PostgreSQL的字段属性
1 parent 791f19b commit 02e6be3

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
import zuo.biao.apijson.StringUtil;
6060
import zuo.biao.apijson.server.exception.NotExistException;
6161
import zuo.biao.apijson.server.model.Column;
62+
import zuo.biao.apijson.server.model.PgAttribute;
63+
import zuo.biao.apijson.server.model.PgClass;
6264
import zuo.biao.apijson.server.model.Table;
6365

6466
/**config sql for JSON Request
@@ -75,8 +77,10 @@ public abstract class AbstractSQLConfig implements SQLConfig {
7577
public static final Map<String, String> TABLE_KEY_MAP;
7678
static {
7779
TABLE_KEY_MAP = new HashMap<String, String>();
78-
TABLE_KEY_MAP.put(Table.class.getSimpleName(), Table.TAG);
79-
TABLE_KEY_MAP.put(Column.class.getSimpleName(), Column.TAG);
80+
TABLE_KEY_MAP.put(Table.class.getSimpleName(), Table.TABLE_NAME);
81+
TABLE_KEY_MAP.put(Column.class.getSimpleName(), Column.TABLE_NAME);
82+
TABLE_KEY_MAP.put(PgAttribute.class.getSimpleName(), PgAttribute.TABLE_NAME);
83+
TABLE_KEY_MAP.put(PgClass.class.getSimpleName(), PgClass.TABLE_NAME);
8084
}
8185

8286
@NotNull
@@ -219,7 +223,7 @@ public AbstractSQLConfig setSchema(String schema) {
219223
if (schema != null) {
220224
String quote = getQuote();
221225
String s = schema.startsWith(quote) && schema.endsWith(quote) ? schema.substring(1, schema.length() - 1) : schema;
222-
if (StringUtil.isName(s) == false) {
226+
if (StringUtil.isEmpty(s, true) == false && StringUtil.isName(s) == false) {
223227
throw new IllegalArgumentException("@schema:value 中value必须是1个单词!");
224228
}
225229
}
@@ -252,15 +256,19 @@ public String getTablePath() {
252256

253257
String sqlTable = getSQLTable();
254258
String sch = getSchema();
255-
if (StringUtil.isEmpty(sch, true)) {
256-
if ((Table.TAG.equals(sqlTable) || Column.TAG.equals(sqlTable)) ) {
259+
if (sch == null) { //PostgreSQL 的 pg_class 和 pg_attribute 表好像不属于任何 Schema StringUtil.isEmpty(sch, true)) {
260+
if ((Table.TABLE_NAME.equals(sqlTable) || Column.TABLE_NAME.equals(sqlTable)) ) {
257261
sch = SCHEMA_INFORMATION;
258-
} else {
262+
}
263+
else if ((PgAttribute.TABLE_NAME.equals(sqlTable) || PgClass.TABLE_NAME.equals(sqlTable)) ) {
264+
sch = "";
265+
}
266+
else {
259267
sch = DEFAULT_SCHEMA;
260268
}
261269
}
262270

263-
return q + sch + q + "." + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
271+
return (StringUtil.isEmpty(sch, true) ? "" : q + sch + q + ".") + q + sqlTable + q + ( isKeyPrefix() ? " AS " + getAlias() : "");
264272
}
265273
@Override
266274
public AbstractSQLConfig setTable(String table) { //Table已经在Parser中校验,所以这里不用防SQL注入

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import zuo.biao.apijson.server.model.Column;
4848
import zuo.biao.apijson.server.model.Document;
4949
import zuo.biao.apijson.server.model.Function;
50+
import zuo.biao.apijson.server.model.PgAttribute;
51+
import zuo.biao.apijson.server.model.PgClass;
5052
import zuo.biao.apijson.server.model.Request;
5153
import zuo.biao.apijson.server.model.Response;
5254
import zuo.biao.apijson.server.model.Table;
@@ -70,6 +72,9 @@ public abstract class AbstractVerifier<T> implements Verifier<T> {
7072
if (Log.DEBUG) {
7173
ACCESS_MAP.put(Table.class.getSimpleName(), getAccessMap(Table.class.getAnnotation(MethodAccess.class)));
7274
ACCESS_MAP.put(Column.class.getSimpleName(), getAccessMap(Column.class.getAnnotation(MethodAccess.class)));
75+
ACCESS_MAP.put(PgAttribute.class.getSimpleName(), getAccessMap(PgAttribute.class.getAnnotation(MethodAccess.class)));
76+
ACCESS_MAP.put(PgClass.class.getSimpleName(), getAccessMap(PgClass.class.getAnnotation(MethodAccess.class)));
77+
7378
ACCESS_MAP.put(Test.class.getSimpleName(), getAccessMap(Test.class.getAnnotation(MethodAccess.class)));
7479
ACCESS_MAP.put(Request.class.getSimpleName(), getAccessMap(Request.class.getAnnotation(MethodAccess.class)));
7580
ACCESS_MAP.put(Response.class.getSimpleName(), getAccessMap(Response.class.getAnnotation(MethodAccess.class)));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
*/
2222
@MethodAccess(POST = {}, PUT = {}, DELETE = {})
2323
public class Column {
24-
public static final String TAG = "columns";
24+
public static final String TABLE_NAME = "columns";
2525

2626
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package zuo.biao.apijson.server.model;
16+
17+
import zuo.biao.apijson.MethodAccess;
18+
19+
/**评论
20+
* @author Lemon
21+
*/
22+
@MethodAccess
23+
public class PgAttribute {
24+
public static final String TABLE_NAME = "pg_attribute";
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package zuo.biao.apijson.server.model;
16+
17+
import zuo.biao.apijson.MethodAccess;
18+
19+
/**评论
20+
* @author Lemon
21+
*/
22+
@MethodAccess
23+
public class PgClass {
24+
public static final String TABLE_NAME = "pg_class";
25+
26+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
*/
2222
@MethodAccess(POST = {}, PUT = {}, DELETE = {})
2323
public class Table {
24-
public static final String TAG = "tables";
24+
public static final String TABLE_NAME = "tables";
2525

2626
}

0 commit comments

Comments
 (0)