Skip to content

Commit d0fc632

Browse files
committed
👶 update to sql2o-plus 1.0.3-SNAPSHOT
新增where or 的支持
1 parent 5b109c5 commit d0fc632

6 files changed

Lines changed: 186 additions & 12 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,33 @@ Sql sql = Sql.builder()
282282
// select user_id,user_name
283283
// FROM user_info
284284
// where user_name = :p0 and user_id in (:p1,:p2)
285-
// order by create_time DESC
285+
// order by create_time desc
286286
/*** [参数值] ***/
287287
// [Test],[1],[2]
288288
```
289289

290+
## Sql or
291+
292+
```java
293+
Sql sql = Sql.builder().select().from(UserInfo.class)
294+
.whereLike(UserInfo::getName,"Test_2")
295+
.or()
296+
.whereLike(UserInfo::getName,"Test_2")
297+
.orderBy(UserInfo::getId);
298+
299+
/*** [Sql语句] ***/
300+
// select *
301+
// from user_info
302+
// where user_name like :p0
303+
// or
304+
// user_name like :p1
305+
// order by user_id asc
306+
/*** [参数值] ***/
307+
// [Test_2],[Test_2]
308+
```
309+
310+
311+
290312

291313
## Dto Mapper
292314

src/main/java/io/github/cotide/dapper/query/Sql.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public Sql append(String sql, Object... params) {
5858
}
5959

6060

61+
public SqlWhereClause or()
62+
{
63+
return whereClause(where("or"));
64+
}
6165

6266
public <T extends Entity,R> Sql whereGt(TypeFunction<T, R> function,Object param) {
6367
return where(Sql2oUtils.getLambdaColumnName(function)+" > ? ",param);
@@ -79,7 +83,7 @@ public <T extends Entity,R> Sql whereLte(TypeFunction<T, R> function,Object par
7983

8084
public Sql where(String sql, Object... params) {
8185
Guard.isNotNullOrEmpty(sql,"where sql");
82-
return append(new Sql("where " + sql, params));
86+
return whereClause().where(sql,params);
8387
}
8488

8589

@@ -144,8 +148,7 @@ public <T extends Entity,R> Sql whereLike(TypeFunction<T, R> function, String
144148
}
145149

146150
public Sql whereLike(String column, String parm) throws SqlBuildException {
147-
where(column+" like ? ","%"+parm+"%");
148-
return this;
151+
return whereClause().whereLike(column,parm);
149152
}
150153

151154
public SelectClause select(String columns) {
@@ -211,7 +214,7 @@ public Sql orderBy(String columns) {
211214

212215
public Sql orderBy(String columns, OrderBy orderBy) {
213216
Guard.isNotNullOrEmpty(columns,"orderBy columns");
214-
return append(new Sql("order by " + columns + " "+ orderBy.toString()));
217+
return append(new Sql("order by " + columns + " "+ orderBy.toString().toLowerCase()));
215218
}
216219

217220
public <T extends Entity,R> Sql orderBy(String asName,TypeFunction<T, R> function) {
@@ -220,7 +223,7 @@ public <T extends Entity,R> Sql orderBy(String asName,TypeFunction<T, R> functi
220223

221224
public <T extends Entity,R> Sql orderBy(String asName,TypeFunction<T, R> function,OrderBy orderBy) {
222225
String columnName = Sql2oUtils.getLambdaColumnName(function);
223-
return orderBy((asName!=null&&!asName.isEmpty()?asName+".":"")+columnName,orderBy);
226+
return orderBy((asName!=null&&!asName.isEmpty()?asName+".":"") + columnName,orderBy);
224227
}
225228

226229

@@ -365,6 +368,17 @@ public <T extends Entity> SqlJoinClause joinDb(String dbName,Class<T> modelClass
365368

366369
//#region Helper
367370

371+
private SqlWhereClause whereClause()
372+
{
373+
return new SqlWhereClause(this);
374+
}
375+
376+
377+
private SqlWhereClause whereClause(Sql sql)
378+
{
379+
return new SqlWhereClause(sql);
380+
}
381+
368382
private SqlJoinClause joinClause(String joinType,String table)
369383
{
370384
return new SqlJoinClause(append(new Sql(joinType+table)));
@@ -406,7 +420,17 @@ private void build(StringBuilder sb, List<Object> args, Sql lhs) {
406420
String sql = _sql;
407421
if (is(lhs, "where ") && is(this, "where "))
408422
{
409-
sql = "and " + sql.substring(6);
423+
if(is(this, "where or"))
424+
{
425+
sql = "or " + sql.substring(8);
426+
427+
}else if(is(lhs,"where or"))
428+
{
429+
sql = sql.substring(6);
430+
}
431+
else {
432+
sql = "and " + sql.substring(6);
433+
}
410434
}
411435
if (is(lhs, "order by ") && is(this, "order by "))
412436
{
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package io.github.cotide.dapper.query;
22

3-
public class SqlWhereClause extends BaseClause {
3+
public class SqlOrClause extends BaseClause {
44

5-
public SqlWhereClause(Sql sql) {
5+
public SqlOrClause(Sql sql) {
66
super(sql);
77
}
88

99

10-
10+
public SqlWhereClause or()
11+
{
12+
return new SqlWhereClause(_sql);
13+
}
14+
1115
}
Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,106 @@
11
package io.github.cotide.dapper.query;
22

3-
public class SqlWhereClause {
3+
import io.github.cotide.dapper.basic.domain.Entity;
4+
import io.github.cotide.dapper.core.functions.TypeFunction;
5+
import io.github.cotide.dapper.core.unit.Sql2oUtils;
6+
import io.github.cotide.dapper.core.utility.Guard;
7+
import io.github.cotide.dapper.exceptions.SqlBuildException;
8+
9+
10+
public class SqlWhereClause extends BaseClause {
11+
12+
13+
public SqlWhereClause(Sql sql) {
14+
super(sql);
15+
}
16+
17+
public <T extends Entity,R> Sql whereGt(TypeFunction<T, R> function, Object param) {
18+
return where(Sql2oUtils.getLambdaColumnName(function)+" > ? ",param);
19+
}
20+
21+
public <T extends Entity,R> Sql whereLt(TypeFunction<T, R> function,Object param) {
22+
return where(Sql2oUtils.getLambdaColumnName(function)+" < ? ",param);
23+
}
24+
25+
26+
public <T extends Entity,R> Sql whereGte(TypeFunction<T, R> function,Object param) {
27+
return where(Sql2oUtils.getLambdaColumnName(function)+" >= ? ",param);
28+
}
29+
30+
public <T extends Entity,R> Sql whereLte(TypeFunction<T, R> function,Object param) {
31+
return where(Sql2oUtils.getLambdaColumnName(function)+" <= ? ",param);
32+
}
33+
34+
35+
public Sql where(String sql, Object... params) {
36+
Guard.isNotNullOrEmpty(sql,"where sql");
37+
return _sql.append(new Sql("where " + sql, params));
38+
39+
}
40+
41+
42+
43+
public <T extends Entity,R> Sql where(String asName,TypeFunction<T, R> function,Object param) {
44+
return where((asName!=null&&!asName.isEmpty()?asName+".":"")+Sql2oUtils.getLambdaColumnName(function)+" = ? ",param);
45+
}
46+
47+
public <T extends Entity,R> Sql where(TypeFunction<T, R> function,Object param) {
48+
return where(Sql2oUtils.getLambdaColumnName(function)+" = ? ",param);
49+
}
50+
51+
public <T extends Entity,R> Sql whereIn(String asName,TypeFunction<T, R> function, Object... paras) throws SqlBuildException {
52+
53+
return whereIn((asName!=null&&!asName.isEmpty()?asName+".":"")+Sql2oUtils.getLambdaColumnName(function),paras);
54+
}
55+
56+
public <T extends Entity,R> Sql whereIn(TypeFunction<T, R> function, Object... paras) throws SqlBuildException {
57+
58+
return whereIn(Sql2oUtils.getLambdaColumnName(function),paras);
59+
}
60+
61+
public Sql whereIn(String column, Object... paras) throws SqlBuildException {
62+
if (column == null || column.length() == 0){
63+
throw new SqlBuildException("query error: must have 'in' word");
64+
}
65+
if (paras == null || paras.length == 0)
66+
{
67+
throw new SqlBuildException("paras error");
68+
}
69+
if(paras.getClass().isArray())
70+
{
71+
StringBuffer appendValue = new StringBuffer();
72+
73+
for (int i=0 ;i<paras.length;i++)
74+
{
75+
appendValue.append("@"+i);
76+
if(i != paras.length-1)
77+
{
78+
appendValue.append(",");
79+
}
80+
}
81+
82+
String appendSql = String.format("%s in (%s)",column,appendValue);
83+
where(appendSql,paras);
84+
}else{
85+
where(String.format("%s in (?)",column,paras));
86+
}
87+
88+
return _sql;
89+
}
90+
91+
92+
public <T extends Entity,R> Sql whereLike(String asName,TypeFunction<T, R> function, String parm) throws SqlBuildException {
93+
94+
return whereLike((asName!=null&&!asName.isEmpty()?asName+".":"")+Sql2oUtils.getLambdaColumnName(function),parm);
95+
}
96+
97+
public <T extends Entity,R> Sql whereLike(TypeFunction<T, R> function, String parm) throws SqlBuildException {
98+
99+
return whereLike(Sql2oUtils.getLambdaColumnName(function),parm);
100+
}
101+
102+
public Sql whereLike(String column, String parm) throws SqlBuildException {
103+
return where(column+" like ? ","%"+parm+"%");
104+
105+
}
4106
}

src/main/java/sql2o/Query.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ public <T> ResultSetIterable<T> executeAndFetchLazy(final ResultSetHandler<T> re
592592

593593
private static <T> ResultSetHandlerFactory<T> newResultSetHandlerFactory(final ResultSetHandler<T> resultSetHandler) {
594594
return new ResultSetHandlerFactory<T>() {
595-
public ResultSetHandler<T> newResultSetHandler(ResultSetMetaData resultSetMetaData) throws SQLException {
595+
@Override
596+
public ResultSetHandler<T> newResultSetHandler(ResultSetMetaData resultSetMetaData) {
596597
return resultSetHandler;
597598
}
598599
};

src/test/java/com/sqltest/db/SelectTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515
public class SelectTest extends BaseTest {
1616

1717

18+
@Test
19+
public void or()
20+
{
21+
Database db = getDatabase();
22+
IRepository<UserInfo> userInfoIRepository = db.getRepository(UserInfo.class);
23+
Sql sql = Sql.builder().select().from(UserInfo.class)
24+
.whereLike(UserInfo::getName,"Test_2")
25+
.or()
26+
.whereLike(UserInfo::getName,"Test")
27+
.orderBy(UserInfo::getId);
28+
29+
List<UserInfo> result = userInfoIRepository.getList(sql);
30+
for (UserInfo item : result) {
31+
System.out.println("id:" + item.getId());
32+
System.out.println("user_Name:" + item.getName());
33+
System.out.println("login:" + item.getLogin());
34+
}
35+
}
36+
37+
1838
@Test
1939
public void getWhereIn()
2040
{
@@ -32,6 +52,7 @@ public void getWhereIn()
3252
}
3353

3454

55+
3556
@Test
3657
public void getById()
3758
{

0 commit comments

Comments
 (0)