orm 添加外键注解

pull/330/head
AriaLyy 7 years ago
parent cd587dd212
commit 9f6c0d727a
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/TaskManager.java
  2. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  3. 25
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  4. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java
  6. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  7. 36
      Aria/src/main/java/com/arialyy/aria/orm/Foreign.java
  8. 36
      Aria/src/main/java/com/arialyy/aria/orm/PrimaryAndForeign.java
  9. 34
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  10. 47
      Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java
  11. 3
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  12. 12
      AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java
  13. 4
      app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java
  14. 4
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java
  15. 14
      app/src/main/res/layout/dialog_sub_task_handler.xml

@ -77,7 +77,7 @@ public class TaskManager {
/** /**
* 通过key获取任务 * 通过key获取任务
* *
* @return 入梅找不到任务返回null否则返回key对应的任务 * @return 如果找不到任务返回null否则返回key对应的任务
*/ */
public AbsTask getTask(String key) { public AbsTask getTask(String key) {
return map.get(CommonUtil.keyToHashKey(key)); return map.get(CommonUtil.keyToHashKey(key));

@ -21,6 +21,7 @@ import android.os.Parcelable;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.Foreign;
import com.arialyy.aria.orm.Primary; import com.arialyy.aria.orm.Primary;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -34,7 +35,12 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
/** /**
* 所属任务组 * 所属任务组
*/ */
private String groupName = ""; @Foreign(table = DownloadGroupEntity.class, column = "groupName") private String groupName = "";
/**
* 下载任务实体的key
*/
@Foreign(table = DownloadTaskEntity.class, column = "key") private String taskKey = "";
/** /**
* 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息如果服务器没有返回则不会设置该信息 * 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息如果服务器没有返回则不会设置该信息

@ -168,11 +168,26 @@ public abstract class AbsGroupUtil implements IUtil {
* *
* @param url 子任务下载地址 * @param url 子任务下载地址
*/ */
public void cancelSunTask(String url) { public void cancelSubTask(String url) {
Downloader d = getDownloader(url, false); //List<String> urls = mTaskEntity.entity.getUrls();
if (d != null) { //if (urls != null && !urls.isEmpty() && urls.contains(url)) {
d.cancel(); // urls.remove(url);
} // DownloadTaskEntity det =
// DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url);
// if (det != null) {
// mTotalLen -= det.getEntity().getFileSize();
// mGroupSize--;
// if (mGroupSize == 0) {
// closeTimer(false);
// mListener.onCancel();
// }
// }
// mTaskEntity.getEntity().update();
//}
//Downloader d = getDownloader(url, false);
//if (d != null) {
// d.cancel();
//}
} }
/** /**

@ -17,6 +17,8 @@ package com.arialyy.aria.core.inf;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.orm.Foreign;
import com.arialyy.aria.orm.NormalList; import com.arialyy.aria.orm.NormalList;
import com.arialyy.aria.orm.Primary; import com.arialyy.aria.orm.Primary;
import java.util.ArrayList; import java.util.ArrayList;
@ -29,7 +31,8 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
/** /**
* 组名组名为任务地址相加的urlMd5 * 组名组名为任务地址相加的urlMd5
*/ */
@Primary protected String groupName = ""; @Primary @Foreign(table = DownloadGroupTaskEntity.class, column = "key") protected String
groupName = "";
/** /**
* 任务组别名 * 任务组别名

@ -59,7 +59,7 @@ public abstract class AbsGroupTask<TASK_ENTITY extends AbsGroupTaskEntity>
*/ */
public void cancelSubTask(String url) { public void cancelSubTask(String url) {
if (mUtil != null) { if (mUtil != null) {
mUtil.cancelSunTask(url); mUtil.cancelSubTask(url);
} }
} }
} }

@ -18,17 +18,17 @@ package com.arialyy.aria.core.upload;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.orm.Foreign;
import com.arialyy.aria.orm.Ignore; import com.arialyy.aria.orm.Ignore;
import com.arialyy.aria.orm.Primary; import com.arialyy.aria.orm.Primary;
import com.arialyy.aria.orm.PrimaryAndForeign;
/** /**
* Created by lyy on 2017/2/9. * Created by lyy on 2017/2/9.
* 上传文件实体 * 上传文件实体
*/ */
public class UploadEntity extends AbsNormalEntity implements Parcelable { public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Primary @Primary @Foreign(table = UploadTaskEntity.class, column = "key") private String filePath; //文件路径
private String filePath; //文件路径
public String getFilePath() { public String getFilePath() {
return filePath; return filePath;

@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by AriaL on 2017/7/4.
* 外键约束
*/
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Foreign {
/**
* 关联的表
*/
Class<? extends DbEntity> table();
/**
* 关联的列
*/
String column();
}

@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by AriaL on 2017/7/4.
* 主键和外键约束
*/
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface PrimaryAndForeign {
/**
* 关联的表
*/
Class<? extends DbEntity> table();
/**
* 关联的列
*/
String column();
}

@ -251,6 +251,18 @@ final class SqlHelper extends SQLiteOpenHelper {
String... expression) { String... expression) {
db = checkDb(db); db = checkDb(db);
CheckUtil.checkSqlExpression(expression); CheckUtil.checkSqlExpression(expression);
//List<Field> fields = CommonUtil.getAllFields(clazz);
//for (Field field : fields) {
// if (SqlUtil.isOneToOne(field)) {
// OneToOne oto = field.getAnnotation(OneToOne.class);
// delData(db, oto.table(), oto.key() + "=?", );
// } else if (SqlUtil.isOneToMany(field)) {
// OneToMany otm = field.getAnnotation(OneToMany.class);
// delData(db, otm.table(), otm.key() + "=?", otm.key());
// }
//}
String sql = "DELETE FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " "; String sql = "DELETE FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " ";
sql = sql.replace("?", "%s"); sql = sql.replace("?", "%s");
Object[] params = new String[expression.length - 1]; Object[] params = new String[expression.length - 1];
@ -422,11 +434,8 @@ final class SqlHelper extends SQLiteOpenHelper {
} }
Class<?> type = field.getType(); Class<?> type = field.getType();
sb.append(field.getName()); sb.append(field.getName());
if (type == String.class if (type == String.class || type == Map.class || type == List.class || SqlUtil.isOneToOne(
|| type == Map.class field) || type.isEnum()) {
|| type == List.class
|| SqlUtil.isOneToOne(field)
|| type.isEnum()) {
sb.append(" varchar"); sb.append(" varchar");
} else if (type == int.class || type == Integer.class) { } else if (type == int.class || type == Integer.class) {
sb.append(" interger"); sb.append(" interger");
@ -446,8 +455,17 @@ final class SqlHelper extends SQLiteOpenHelper {
continue; continue;
} }
if (SqlUtil.isPrimary(field)) { if (SqlUtil.isPrimary(field)) {
//sb.append(" PRIMARY KEY"); sb.append(" PRIMARY KEY");
sb.append(" NOT NULL"); }
if (SqlUtil.isForeign(field)) {
Foreign foreign = field.getAnnotation(Foreign.class);
sb.append(",FOREIGN KEY (")
.append(field.getName())
.append(") REFERENCES ")
.append(foreign.table())
.append("(")
.append(foreign.column())
.append(")");
} }
sb.append(","); sb.append(",");
} }
@ -587,7 +605,7 @@ final class SqlHelper extends SQLiteOpenHelper {
return findData(db, params[0], params[1] + "=?", primary); return findData(db, params[0], params[1] + "=?", primary);
} }
private static void closeCursor(Cursor cursor){ private static void closeCursor(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) { if (cursor != null && !cursor.isClosed()) {
try { try {
cursor.close(); cursor.close();

@ -15,9 +15,7 @@
*/ */
package com.arialyy.aria.orm; package com.arialyy.aria.orm;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -136,8 +134,6 @@ final class SqlUtil {
return TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1); return TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1);
} }
/** /**
* @return true 忽略该字段 * @return true 忽略该字段
*/ */
@ -146,13 +142,9 @@ final class SqlUtil {
Ignore ignore = field.getAnnotation(Ignore.class); Ignore ignore = field.getAnnotation(Ignore.class);
int modifiers = field.getModifiers(); int modifiers = field.getModifiers();
String fieldName = field.getName(); String fieldName = field.getName();
return (ignore != null && ignore.value()) return (ignore != null && ignore.value()) || fieldName.equals("rowID") || fieldName.equals(
|| fieldName.equals("rowID") "shadow$_klass_") || fieldName.equals("shadow$_monitor_") || field.isSynthetic() || Modifier
|| fieldName.equals("shadow$_klass_") .isStatic(modifiers) || Modifier.isFinal(modifiers);
|| fieldName.equals("shadow$_monitor_")
|| field.isSynthetic()
|| Modifier.isStatic(modifiers)
|| Modifier.isFinal(modifiers);
} }
/** /**
@ -172,27 +164,48 @@ final class SqlUtil {
} }
/** /**
* 判断是否是主键 * 判断是否是主键约束
*
* @return {@code true}主键约束
*/ */
static boolean isPrimary(Field field) { static boolean isPrimary(Field field) {
Primary pk = field.getAnnotation(Primary.class); Primary pk = field.getAnnotation(Primary.class);
return pk != null; return pk != null;
} }
/**
* 判断是否是外键约束
*
* @return {@code true}外键约束
*/
static boolean isForeign(Field field) {
Foreign fk = field.getAnnotation(Foreign.class);
return fk != null;
}
/**
* 判断是否是主外键约束
*
* @return {@code true}主外键约束
*/
static boolean isPrimaryAndForeign(Field field) {
PrimaryAndForeign fk = field.getAnnotation(PrimaryAndForeign.class);
return fk != null;
}
private static Object checkData(String type, String data) { private static Object checkData(String type, String data) {
if (type.equalsIgnoreCase("java.lang.String")){ if (type.equalsIgnoreCase("java.lang.String")) {
return data; return data;
}else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")){ } else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")) {
return Integer.parseInt(data); return Integer.parseInt(data);
}else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")){ } else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")) {
return Double.parseDouble(data); return Double.parseDouble(data);
}else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")){ } else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")) {
return Float.parseFloat(data); return Float.parseFloat(data);
} }
return null; return null;
} }
/** /**
* 查找class的主键字段 * 查找class的主键字段
* *

@ -301,7 +301,8 @@ public class CommonUtil {
if (config.exists()) { if (config.exists()) {
config.delete(); config.delete();
} }
dEntity.deleteData(); //dEntity.deleteData();
//tEntity.deleteData();
tEntity.deleteData(); tEntity.deleteData();
} }

@ -128,12 +128,12 @@ import java.lang.annotation.Target;
String[] value() default { AriaConstance.NO_URL }; String[] value() default { AriaConstance.NO_URL };
} }
///** /**
// * 任务组子任务删除的注解 * 任务组子任务删除的注解
// */ */
//@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel {
// String[] value() default { AriaConstance.NO_URL }; String[] value() default { AriaConstance.NO_URL };
//} }
/** /**
* 任务组子任务失败的注解 * 任务组子任务失败的注解

@ -18,6 +18,7 @@ package com.arialyy.simple.download.group;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -109,6 +110,7 @@ import java.util.List;
//@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask, //@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask,
// DownloadEntity subEntity) { // DownloadEntity subEntity) {
// Log.d(TAG, "new Size: " + groupTask.getConvertFileSize());
// mSub.setText("子任务:" + mChildName + ",状态:取消下载"); // mSub.setText("子任务:" + mChildName + ",状态:取消下载");
//} //}
@ -129,7 +131,7 @@ import java.util.List;
return R.layout.dialog_sub_task_handler; return R.layout.dialog_sub_task_handler;
} }
@OnClick({ R.id.start, R.id.stop }) void onClick(View view) { @OnClick({ R.id.start, R.id.stop, R.id.cancel }) void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl()); Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl());

@ -66,8 +66,8 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
dialog.show(getSupportFragmentManager(), "download_num"); dialog.show(getSupportFragmentManager(), "download_num");
break; break;
case R.id.stop_all: case R.id.stop_all:
Aria.download(this).stopAllTask(); //Aria.download(this).stopAllTask();
//Aria.download(this).removeAllTask(false); Aria.download(this).removeAllTask(false);
break; break;
case R.id.turn: case R.id.turn:
startActivity(new Intent(this, MultiDownloadActivity.class)); startActivity(new Intent(this, MultiDownloadActivity.class));

@ -62,13 +62,13 @@
/> />
<!--<Button--> <!--<Button-->
<!--android:id="@+id/cancel"--> <!--android:id="@+id/cancel"-->
<!--android:layout_width="wrap_content"--> <!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"--> <!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"--> <!--android:layout_weight="1"-->
<!--android:text="删除"--> <!--android:text="删除"-->
<!--style="?buttonBarButtonStyle"--> <!--style="?buttonBarButtonStyle"-->
<!--/>--> <!--/>-->
</LinearLayout> </LinearLayout>
</RelativeLayout> </RelativeLayout>

Loading…
Cancel
Save