许鹏程
2023-06-30 3bbfaa3d7d416afbd154576453c8ee9e7e2f8899
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.product.data.center.entity;
 
import com.product.common.lang.StringUtils;
import com.product.core.entity.DataTableEntity;
import com.product.core.exception.BaseException;
import com.product.data.center.config.ErrorCode;
import com.product.data.center.utils.BatchUtils;
import com.product.data.center.utils.CallBackReturnValue;
import com.product.datasource.config.DataBaseType;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
/**
 * 查询实体类
 *
 * @author cheng
 * @date 2022年07月14日10:26:22
 */
public class QueryDataConfigEntity {
    /**
     * 查询表名
     */
    private String queryTable;
    /**
     * 需要查询的字段集合
     */
    private Set<String> queryFieldSet;
    /**
     * 自动递增主键字段
     */
    private String autoIncrementPrimaryField;
    /**
     * 查询条件
     * 支持占位符
     */
    private String queryFilter;
    /**
     * 占位符参数
     */
    private Object[] params;
    /**
     * 数据库类型
     */
    private DataBaseType dbt;
 
    /**
     * @param queryTable
     * @param queryFilter
     * @param params
     */
    public QueryDataConfigEntity(String queryTable, String queryFilter, String autoIncrementPrimaryField, Object[] params) {
        this.queryTable = queryTable;
        this.queryFilter = queryFilter;
        this.params = params;
        this.autoIncrementPrimaryField = autoIncrementPrimaryField;
    }
 
    /**
     * @param queryTable
     * @param queryFilter
     */
    public QueryDataConfigEntity(String queryTable, String queryFilter, String autoIncrementPrimaryField) {
        this.queryTable = queryTable;
        this.queryFilter = queryFilter;
        this.autoIncrementPrimaryField = autoIncrementPrimaryField;
    }
 
    /**
     * @param queryTable
     * @param queryFieldSet
     * @param queryFilter
     * @param params
     */
    public QueryDataConfigEntity(String queryTable, Set<String> queryFieldSet, String autoIncrementPrimaryField, String queryFilter, Object[] params) {
        this.queryTable = queryTable;
        this.queryFieldSet = queryFieldSet;
        this.queryFilter = queryFilter;
        this.params = params;
        this.autoIncrementPrimaryField = autoIncrementPrimaryField;
    }
 
    /**
     * 添加查询字段
     *
     * @param fieldName
     */
    public void addQueryField(String... fieldName) {
        if (fieldName != null) {
            if (this.queryFieldSet == null) {
                this.queryFieldSet = new HashSet<>();
            }
            this.queryFieldSet.addAll(Arrays.asList(fieldName));
        }
    }
 
    /**
     * 删除查询字段
     *
     * @param fieldName
     */
    public void removeQueryField(String... fieldName) {
        this.querySqlSuccess = false;
        if (fieldName != null && this.queryFieldSet != null && this.queryFieldSet.size() > 0) {
            for (String field : fieldName) {
                this.queryFieldSet.remove(field);
            }
        }
    }
 
 
    public String getQueryTable() {
        return queryTable;
    }
 
    public void setQueryTable(String queryTable) {
        this.querySqlSuccess = false;
        this.queryTable = queryTable;
    }
 
    public Set<String> getQueryFieldSet() {
        return queryFieldSet;
    }
 
    public void setQueryFieldSet(Set<String> queryFieldSet) {
        this.querySqlSuccess = false;
        this.queryFieldSet = queryFieldSet;
    }
 
    public String getQueryFilter() {
        return queryFilter;
    }
 
    public void setQueryFilter(String queryFilter) {
        this.querySqlSuccess = false;
        this.queryFilter = queryFilter;
    }
 
    public Object[] getParams() {
        return params;
    }
 
    public void setParams(Object[] params) {
        this.params = params;
    }
 
    public DataBaseType getDbt() {
        return dbt;
    }
 
    public void setDbt(DataBaseType dbt) {
        this.querySqlSuccess = false;
        this.dbt = dbt;
    }
 
    public String getAutoIncrementPrimaryField() {
        return autoIncrementPrimaryField;
    }
 
    public void setAutoIncrementPrimaryField(String autoIncrementPrimaryField) {
        this.autoIncrementPrimaryField = autoIncrementPrimaryField;
    }
 
    /**
     * 查询sql状态 是否初始化模板完成
     */
    private boolean querySqlSuccess = false;
 
    private String sqlTemplate;
 
    /**
     * 获取查询sql
     *
     * @param currentPage 当前页数
     * @param pageSize    每页条数
     * @return
     */
    public String getQuerySql(int currentPage, int pageSize) throws BaseException {
        if (!this.querySqlSuccess) {
            this.initQuerySqlTemplate();
        }
        int first = -1;
        int second = -1;
        if (DataBaseType.ORACLE.equals(this.dbt)) {
            first = currentPage * pageSize;
            second = (currentPage - 1) * pageSize;
        } else if (DataBaseType.MYSQL.equals(this.dbt)) {
            if (currentPage == 1) {
                first = 0;
            } else {
                first = (currentPage - 1) * pageSize;
            }
            second = pageSize;
        } else if (DataBaseType.SQLSERVER.equals(this.dbt)) {
            first = pageSize;
            second = (currentPage - 1) * pageSize;
        } else if (DataBaseType.INFORMIX.equals(this.dbt)) {
            first = (currentPage - 1) * pageSize;
            second = pageSize;
        } else if (DataBaseType.PSQL.equals(this.dbt)) {
            first = pageSize;
            second = pageSize * (currentPage - 1);
        } else {
            throw new BaseException(ErrorCode.NONSUPPORT_DATABASE_TYPE);
        }
 
        return sqlTemplate.replace("<<~1~>>", first + "").replace("<<~2~>>", second + "");
    }
 
 
    public String getQueryTotalCountSql() {
        StringBuilder sql = new StringBuilder("SELECT ");
        sql.append("count(").append(this.autoIncrementPrimaryField).append(") as total_count FROM ");
        sql.append(this.queryTable);
        if (!StringUtils.isEmpty(this.queryFilter)) {
            sql.append(" WHERE ( ").append(this.queryFilter).append(" ) ");
        }
        return sql.toString();
    }
 
    /**
     * 初始化查询sql
     */
    private void initQuerySqlTemplate() {
        if (this.queryFieldSet == null || this.queryFieldSet.size() <= 0) {
            throw new BaseException(ErrorCode.BATCH_EXECUTE_QUERY_FIELD_CAN_NOT_EMPTY);
        }
        StringBuilder queryTemplate = new StringBuilder("SELECT ");
        if (DataBaseType.INFORMIX.equals(this.dbt)) {
            queryTemplate.append(" SKIP <<~1~>> FIRST <<~2~>> ");
        } else if (DataBaseType.SQLSERVER.equals(this.dbt)) {
//            queryTemplate.append(" TOP <<~1~>> ");
            // TODO: 2022/7/25 暂未实现sqlserver查询
        }
 
        String[] fields = this.queryFieldSet.stream().toArray(String[]::new);
        for (int i = 0; i < fields.length; i++) {
            if (i > 0) {
                queryTemplate.append(" , ");
            }
            queryTemplate.append(BatchUtils.getFieldName(fields[i], this.dbt));
        }
        if (!this.queryFieldSet.contains(autoIncrementPrimaryField)) {
            queryTemplate.append(",").append(BatchUtils.getFieldName(this.autoIncrementPrimaryField, this.dbt));
        }
        //ORACLE 特殊查询出序号用于分页
        if (DataBaseType.ORACLE.equals(this.dbt)) {
            queryTemplate.append("");
        }
        queryTemplate.append(" FROM ").append(this.queryTable);
        //是否拼接了WHERE 关键字
        boolean isWhere = false;
        if (!StringUtils.isEmpty(this.queryFilter)) {
            isWhere = true;
            queryTemplate.append(" WHERE ").append("(").append(this.queryFilter).append(") ");
        }
        if (DataBaseType.ORACLE.equals(this.dbt)) {
 
            queryTemplate.append(" order by ").append(BatchUtils.getFieldName(this.autoIncrementPrimaryField, this.dbt)).append(" asc ");
            queryTemplate.insert(0, "SELECT aa.*,ROWNUM as row_num  FROM ( ");
 
            queryTemplate.append(" ) aa ");
 
            queryTemplate.append(" WHERE  ( ").append(" ROWNUM<=<<~1~>> ) ");
 
            queryTemplate.insert(0, " SELECT * FROM (");
            queryTemplate.append(" ) A where (row_num> <<~2~>> )");
        } else {
 
            queryTemplate.append(" order by ").append(BatchUtils.getFieldName(this.autoIncrementPrimaryField, this.dbt)).append(" asc ");
        }
        if (this.dbt.equals(DataBaseType.MYSQL)) {
            queryTemplate.append(" limit <<~1~>>,<<~2~>>");
        }
        this.sqlTemplate = queryTemplate.toString();
        this.querySqlSuccess = true;
    }
 
    public static void main(String[] args) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT *,ROWNNUM from TABLE where 1=1");
        sql.append(" order by xxx");
        sql.insert(0, " SELECT *,ROWNUM as row_num  FROM (");
        sql.append(" ) A ");
//        sql.append(") a ORDER BY XXX");
        sql.insert(0, " SELECT * FROM (");
        sql.append(") A ");
        System.out.println(sql);
    }
}