Skip to content

Commit

Permalink
修缮api,加入SearchMajorKey方便根据主键查询数据
Browse files Browse the repository at this point in the history
  • Loading branch information
陈浪 committed May 26, 2020
1 parent a84bef3 commit 5221030
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/ellen/dhcsqlite/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class Student {

//主键
@MajorKey
@MajorKey(isAutoIncrement = true)
private int id;
@DhcSqlFieldName("my_name") //映射数据库中字段名字为my_name
private String name;
Expand All @@ -30,6 +30,7 @@ public class Student {
@DhcSqlFieldName("your_father")
private Father father;
@DataStructure //表示这个属性是数据类型属性,需要用注解区分,才能正确的进行json映射,否则会报错
@DhcSqlFieldName("爸爸们")
private Father[] fathers;

public Student(int id, String name, int age, String phoneNumber, String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

public class AutoOperateProxy implements InvocationHandler {

private ZxyTable zxyReflectionTable;
private ZxyTable zxyTable;

public AutoOperateProxy(ZxyTable zxyTable) {
this.zxyReflectionTable = zxyTable;
this.zxyTable = zxyTable;
}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand Down Expand Up @@ -69,23 +69,23 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
if(orderSql != null && orderSql.equals("")){
orderSql = null;
}
return zxyReflectionTable.search(newSql(whereSql, method, args), orderSql);
return zxyTable.search(newSql(whereSql, method, args), orderSql);
}
Delete delete = method.getAnnotation(Delete.class);
if (delete != null) {
String deleteSql = delete.value();
zxyReflectionTable.delete(newSql(deleteSql, method, args));
zxyTable.delete(newSql(deleteSql, method, args));
return null;
}
TotalSearchSql totalSearchSql = method.getAnnotation(TotalSearchSql.class);
if (totalSearchSql != null) {
String totalSearchSqlString = totalSearchSql.value();
return zxyReflectionTable.searchDataBySql(newSql(totalSearchSqlString, method, args));
return zxyTable.searchDataBySql(newSql(totalSearchSqlString, method, args));
}
TotalSql totalSql = method.getAnnotation(TotalSql.class);
if (totalSql != null) {
String totalSqlString = totalSql.value();
zxyReflectionTable.exeSQL(newSql(totalSqlString, method, args));
zxyTable.exeSQL(newSql(totalSqlString, method, args));
return null;
}
Update update = method.getAnnotation(Update.class);
Expand All @@ -94,21 +94,21 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
String whereSql = update.whereSql();
valueSql = newSql(valueSql,method,args);
whereSql = newSql(whereSql,method,args);
StringBuilder stringBuilder = new StringBuilder("UPDATE "+zxyReflectionTable.getTableName()+" SET ");
StringBuilder stringBuilder = new StringBuilder("UPDATE "+zxyTable.getTableName()+" SET ");
stringBuilder.append(valueSql);
stringBuilder.append(" WHERE ");
stringBuilder.append(whereSql);
stringBuilder.append(";");
String updateSql = stringBuilder.toString();
zxyReflectionTable.exeSQL(updateSql);
zxyTable.exeSQL(updateSql);
return null;
}
TotalUpdateSql totalUpdateSql = method.getAnnotation(TotalUpdateSql.class);
if(totalUpdateSql != null){
String totalUpdateSqlString = totalUpdateSql.value();
String updateSql =newSql(totalUpdateSqlString,method,args);
Log.e("Ellen2018","update语句:"+updateSql);
zxyReflectionTable.exeSQL(updateSql);
zxyTable.exeSQL(updateSql);
return null;
}
SearchByMajorKey searchByMajorKey = method.getAnnotation(SearchByMajorKey.class);
Expand All @@ -118,17 +118,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
if(orderSql != null && orderSql.equals("")){
orderSql = null;
}
String majorKeyName = zxyReflectionTable.getMajorKeyName();
String majorKeyName = zxyTable.getMajorKeyName();
if(!TextUtils.isEmpty(majorKeyName)) {
if(whereSql.contains("{}")) {
whereSql = whereSql.replace("{}", zxyReflectionTable.getMajorKeyName());
whereSql = whereSql.replace("{}", zxyTable.getMajorKeyName());
}else {
whereSql = zxyReflectionTable.getMajorKeyName() + " "+whereSql;
whereSql = zxyTable.getMajorKeyName() + " "+whereSql;
}
}else {
throw new NoPrimaryKeyException("没有主键,无法根据主键查询数据!");
}
return zxyReflectionTable.search(newSql(whereSql,method,args),orderSql);
return zxyTable.search(newSql(whereSql,method,args),orderSql);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MajorKey{
boolean isAutoIncrement() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ private void getFields() {
SQLField sqlField = null;
if (majorKeykey != null) {
//这里是主键
sqlField = SQLField.getPrimaryKeyField(fieldName, fieldType, false);
boolean isAutoIncrement = majorKeykey.isAutoIncrement();
if(isAutoIncrement){
sqlField = SQLField.getPrimaryKeyField(fieldName, fieldType, true);
}else {
sqlField = SQLField.getPrimaryKeyField(fieldName, fieldType, false);
}
majorKeyField = field;
majorKeySqlField = sqlField;
} else {
Expand Down

0 comments on commit 5221030

Please sign in to comment.