Skip to content

Commit

Permalink
[add]whereImple,make it more sexy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayin committed Jun 5, 2014
1 parent 8210074 commit fa34be4
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 122 deletions.
42 changes: 6 additions & 36 deletions library/src/com/annotation/core/Deletor.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,29 @@
package com.annotation.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.annotation.entity.Sqlable;
import com.annotation.entity.Wherable;
import com.annotation.entity.WhereImpl;
import com.annotation.utils.ReflectionUtils;

public class Deletor implements Sqlable ,Wherable<Deletor>{
public class Deletor extends WhereImpl<Deletor>{

private String _talbe;
private StringBuffer _where;

public Deletor() {
_where = new StringBuffer();
super();
}

public Deletor from(Class<?> cls) {
_talbe = ReflectionUtils.getTableName(cls);
return this;
}

public Deletor where(String column, String operation, String value) {
_where.append(column).append(" ");
_where.append(operation).append(" ");
_where.append("\"").append(value.replaceAll("\\\"", "\\\\\"")).append("\"").append(" ");
return this;
}

//只支持单条例如:id = 1 不能: id = 1 and age=18
public Deletor where(String expression){
Pattern p = Pattern.compile("(\\S+)\\s*([!<>=]+)\\s*(\\S+)");
Matcher m = p.matcher(expression);
if(m.matches()){
return where(m.group(1),m.group(2),m.group(3));
}
return this;
}

public Deletor and() {
_where.append("and").append(" ");
return this;
}

public Deletor or() {
_where.append("or").append(" ");
return this;
}

@Override
public String build() {
StringBuffer builder = new StringBuffer();
builder.append("Delete").append(" ");
builder.append("From").append(" ").append(_talbe).append(" ");
builder.append("Where").append(" ").append(_where);
if (hasWhere()){
builder.append("Where").append(" ").append(getWhere());
}
return builder.toString();
}

Expand Down
43 changes: 6 additions & 37 deletions library/src/com/annotation/core/Selector.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.annotation.core;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.annotation.entity.Sqlable;
import com.annotation.entity.Wherable;
import com.annotation.entity.WhereImpl;
import com.annotation.utils.ReflectionUtils;

public class Selector implements Sqlable, Wherable<Selector> {
public class Selector extends WhereImpl<Selector>{
Class<?> _entity;
String _table;
int __limit, __offset;
boolean _distinct, _all;
StringBuffer _where, _groupBy, _orderBy, _resultColumn;
StringBuffer _groupBy, _orderBy, _resultColumn;

public Selector(String... resultColumn) {
super();
_resultColumn = new StringBuffer();
_where = new StringBuffer();
_groupBy = new StringBuffer();
_orderBy = new StringBuffer();
__limit = -1;
Expand Down Expand Up @@ -49,34 +46,6 @@ public Selector all() {
return this;
}

public Selector where(String column, String operation, String value) {
_where.append(column).append(" ");
_where.append(operation).append(" ");
_where.append("\"").append(value.replaceAll("\\\"", "\\\\\""))
.append("\"").append(" ");
return this;
}

//只支持单条例如:id = 1 不能: id = 1 and age=18
public Selector where(String expression){
Pattern p = Pattern.compile("(\\S+)\\s*([!<>=]+)\\s*(\\S+)");
Matcher m = p.matcher(expression);
if(m.matches()){
return where(m.group(1),m.group(2),m.group(3));
}
return this;
}

public Selector and() {
_where.append("and").append(" ");
return this;
}

public Selector or() {
_where.append("or").append(" ");
return this;
}

public Selector groupBy(String... columns) {
for (String c : columns) {
_groupBy.append(c).append(",");
Expand Down Expand Up @@ -128,8 +97,8 @@ public String build() {
builder.append(_resultColumn).append(" ");

builder.append("From").append(" ").append(_table).append(" ");
if (_where.length() > 0)
builder.append("Where").append(" ").append(_where).append(" ");
if(hasWhere())
builder.append("Where").append(" ").append(getWhere()).append(" ");

if (_groupBy.length() > 0)
builder.append("Group By").append(" ").append(_groupBy).append(" ");
Expand Down
42 changes: 5 additions & 37 deletions library/src/com/annotation/core/Updater.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.annotation.core;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.annotation.entity.Sqlable;
import com.annotation.entity.Wherable;
import com.annotation.entity.WhereImpl;
import com.annotation.utils.ReflectionUtils;

public class Updater implements Sqlable, Wherable<Updater> {
public class Updater extends WhereImpl<Updater> {
String _table;
StringBuffer _where, _targetColumn;
StringBuffer _targetColumn;

public Updater() {
_where = new StringBuffer();
_targetColumn = new StringBuffer();
}

Expand All @@ -33,43 +29,15 @@ public Updater update(Object obj) {
return this;
}

@Override
public Updater where(String column, String operation, String value) {
_where.append(column).append(" ");
_where.append(operation).append(" ");
_where.append("\"").append(value.replaceAll("\\\"", "\\\\\"")).append("\"").append(" ");
return this;
}

//只支持单条例如:id = 1 不能: id = 1 and age=18
public Updater where(String expression){
Pattern p = Pattern.compile("(\\S+)\\s*([!<>=]+)\\s*(\\S+)");
Matcher m = p.matcher(expression);
if(m.matches()){
return where(m.group(1),m.group(2),m.group(3));
}
return this;
}

@Override
public Updater and() {
_where.append("and").append(" ");
return this;
}

@Override
public Updater or() {
_where.append("or").append(" ");
return this;
}

@Override
public String build() {
StringBuffer builder = new StringBuffer();
builder.append("Update").append(" ");
builder.append(_table).append(" ");
builder.append("Set").append(" ").append(_targetColumn).append(" ");
builder.append("Where").append(" ").append(_where);
if(hasWhere())
builder.append("Where").append(" ").append(getWhere());
return builder.toString();
}
}
4 changes: 3 additions & 1 deletion library/src/com/annotation/entity/Wherable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.annotation.entity;

public interface Wherable<T> {
public interface Wherable<T> extends Sqlable{
public T where(String column, String operation, String value);

public T where(String expression);

public T and();

Expand Down
54 changes: 54 additions & 0 deletions library/src/com/annotation/entity/WhereImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.annotation.entity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("unchecked")
public abstract class WhereImpl<T> implements Wherable<T> {

private StringBuffer _where;

public WhereImpl() {
_where = new StringBuffer();
}

@Override
public T where(String column, String operation, String value) {
_where.append(column).append(" ");
_where.append(operation).append(" ");
_where.append("\"").append(value.replaceAll("\\\"", "\\\\\""))
.append("\"").append(" ");
return (T) this;
}

// 只支持单条例如:id = 1 不能: id = 1 and age=18
@Override
public T where(String expression) {
Pattern p = Pattern.compile("(\\S+)\\s*([!<>=]+)\\s*(\\S+)");
Matcher m = p.matcher(expression);
if (m.matches()) {
return where(m.group(1), m.group(2), m.group(3));
}
return (T) this;
}

@Override
public T and() {
_where.append("and").append(" ");
return (T) this;
}

@Override
public T or() {
_where.append("or").append(" ");
return (T) this;
}

public StringBuffer getWhere() {
return this._where;
}

public boolean hasWhere() {
return this._where.length() > 0;
}
}
21 changes: 11 additions & 10 deletions test/src/io/github/jayin/test/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@

public class QueryTest extends AndroidTestCase {

public void insert(){
User u =new User();
public void insert() {
User u = new User();
u.setAge(18);
u.setName("Mars");
u.setUserid(1);
u.save(getContext());

u.setName("Jack");
u.setUserid(2);
u.save(getContext());

u.setName("Mark");
u.setUserid(3);
u.save(getContext());

u.setName("Waton");
u.setUserid(4);
u.save(getContext());
}
public void query1(){

public void query1() {
Selector selector = new Selector().from(User.class);
List<User> users = new Query(selector).excute(getContext());
_.d(users.toString());
}

public void query2(){
Selector selector = new Selector().from(User.class).where("userid", "=", "1");

public void query2() {
Selector selector = new Selector().from(User.class).where("userid",
"=", "1");
List<User> users = new Query(selector).excute(getContext());
_.d(users.toString());
}
Expand Down
8 changes: 7 additions & 1 deletion test/src/io/github/jayin/test/issue/Issue1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public class Issue1Test extends AndroidTestCase {
public void deletorTest() {
String sql = new Deletor().from(Admin.class).where("id = 1").and()
.where("name = Jayin").build();
// System.out.println(sql);
_.d(sql);
sql = new Deletor().from(Admin.class).build();
_.d(sql);
}

public void selectorTest() {
String sql = new Selector().from(Admin.class).where("id = 1").and()
.where("name = Jayin").build();
_.d(sql);
sql = new Selector().from(Admin.class).build();
_.d(sql);
}

public void UpdaterTest() {
Expand All @@ -30,6 +33,9 @@ public void UpdaterTest() {
String sql = new Updater().update(a).where("id = 1").and()
.where("name = Jayin").build();
_.d(sql);

sql = new Updater().update(a).build();
_.d(sql);
}

public void test_all() {
Expand Down

0 comments on commit fa34be4

Please sign in to comment.