|
1 | 1 | package io.github.cotide.dapper.query; |
2 | 2 |
|
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 | + } |
4 | 106 | } |
0 commit comments