From 7392eba667f48a2262ce87dd16803d7496c53c71 Mon Sep 17 00:00:00 2001 From: Equalzys <215766856@qq.com> Date: Thu, 12 Aug 2021 13:36:13 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=97=B6=E5=8E=BB=E5=90=88=E5=B9=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E9=94=99=E8=AF=AF=202=E3=80=81=E6=8D=95?= =?UTF-8?q?=E8=8E=B7sql=E6=9F=A5=E8=AF=A2=E5=A4=B1=E8=B4=A5=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=203=E3=80=81=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=95=BF=E5=BA=A6=E8=80=8C=E5=AF=BC=E8=87=B4=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/ftp/upload/FtpULoaderUtil.java | 4 +- .../aria/http/upload/HttpULoaderUtil.java | 4 +- .../core/loader/NormalThreadStateManager.java | 10 +- .../core/loader/UploadThreadStateManager.java | 275 ++++ .../com/arialyy/aria/orm/DelegateFind.java | 1107 +++++++++-------- .../aria/sftp/upload/SFtpULoaderUtil.java | 4 +- 6 files changed, 849 insertions(+), 555 deletions(-) create mode 100644 PublicComponent/src/main/java/com/arialyy/aria/core/loader/UploadThreadStateManager.java diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java index 757fdb38..3a54d64b 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; +import com.arialyy.aria.core.loader.UploadThreadStateManager; import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.ftp.FtpTaskOption; @@ -44,7 +45,8 @@ public final class FtpULoaderUtil extends AbsNormalLoaderUtil { @Override public LoaderStructure BuildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); structure.addComponent(new FtpURecordHandler((UTaskWrapper) getTaskWrapper())) - .addComponent(new NormalThreadStateManager(getListener())) +// .addComponent(new NormalThreadStateManager(getListener())) + .addComponent(new UploadThreadStateManager(getListener())) .addComponent(new FtpUFileInfoTask((UTaskWrapper) getTaskWrapper())) .addComponent(new NormalTTBuilder(getTaskWrapper(), new AbsNormalTTBuilderAdapter() { @Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java index f7802d7a..addeeed4 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; +import com.arialyy.aria.core.loader.UploadThreadStateManager; import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.http.HttpRecordHandler; @@ -45,7 +46,8 @@ public final class HttpULoaderUtil extends AbsNormalLoaderUtil { @Override public LoaderStructure BuildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); structure.addComponent(new HttpRecordHandler(getTaskWrapper())) - .addComponent(new NormalThreadStateManager(getListener())) +// .addComponent(new NormalThreadStateManager(getListener())) + .addComponent(new UploadThreadStateManager(getListener())) .addComponent(new NormalTTBuilder(getTaskWrapper(), new AbsNormalTTBuilderAdapter() { @Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) { return new HttpUThreadTaskAdapter(config); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java index 6ef8a0eb..4a8a6bbd 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalThreadStateManager.java @@ -249,8 +249,14 @@ public class NormalThreadStateManager implements IThreadStateManager { private boolean mergeFile() { if (mTaskRecord.threadNum == 1) { File targetFile = new File(mTaskRecord.filePath); - if (targetFile.exists() && targetFile.length() == mTaskRecord.fileLength){ - return true; + if (targetFile.exists()){ + //没有获得文件长度:不支持断点续传 + if (mTaskRecord.fileLength == 0 && targetFile.length() != 0) { + return true; + } + if (targetFile.length() != 0 && targetFile.length() == mTaskRecord.fileLength) { + return true; + } } FileUtil.deleteFile(targetFile); File partFile = new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0)); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/UploadThreadStateManager.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/UploadThreadStateManager.java new file mode 100644 index 00000000..00afc259 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/UploadThreadStateManager.java @@ -0,0 +1,275 @@ +/* + * 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.core.loader; + +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.inf.IThreadStateManager; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.exception.AriaException; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.FileUtil; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * 线程任务管理器,用于处理多线程下载时任务的状态回调 + */ +public class UploadThreadStateManager implements IThreadStateManager { + private final String TAG = CommonUtil.getClassName(this); + + /** + * 任务状态回调 + */ + private IEventListener mListener; + private int mThreadNum; // 启动的线程总数 + private AtomicInteger mCancelNum = new AtomicInteger(0); // 已经取消的线程的数 + private AtomicInteger mStopNum = new AtomicInteger(0); // 已经停止的线程数 + private AtomicInteger mFailNum = new AtomicInteger(0); // 失败的线程数 + private AtomicInteger mCompleteNum = new AtomicInteger(0); // 完成的线程数 + private long mProgress; //当前总进度 + private TaskRecord mTaskRecord; // 任务记录 + private Looper mLooper; + + /** + * @param listener 任务事件 + */ + public UploadThreadStateManager(IEventListener listener) { + mListener = listener; + } + + @Override public void setLooper(TaskRecord taskRecord, Looper looper) { + mTaskRecord = taskRecord; + mThreadNum = mTaskRecord.threadNum; + mLooper = looper; + } + + private void checkLooper() { + if (mTaskRecord == null) { + throw new NullPointerException("任务记录为空"); + } + if (mLooper == null) { + throw new NullPointerException("Looper为空"); + } + } + + private Handler.Callback callback = new Handler.Callback() { + @Override public boolean handleMessage(Message msg) { + checkLooper(); + switch (msg.what) { + case STATE_STOP: + mStopNum.getAndIncrement(); + if (isStop()) { + quitLooper(); + } + break; + case STATE_CANCEL: + mCancelNum.getAndIncrement(); + if (isCancel()) { + quitLooper(); + } + break; + case STATE_FAIL: + mFailNum.getAndIncrement(); + if (isFail()) { + Bundle b = msg.getData(); + mListener.onFail(b.getBoolean(DATA_RETRY, false), + (AriaException) b.getSerializable(DATA_ERROR_INFO)); + quitLooper(); + } + break; + case STATE_COMPLETE: + mCompleteNum.getAndIncrement(); + if (isComplete()) { + ALog.d(TAG, "isComplete, completeNum = " + mCompleteNum); + //上传文件不需要合并文件 + mListener.onComplete(); + quitLooper(); + } + break; + case STATE_RUNNING: + Bundle b = msg.getData(); + if (b != null) { + long len = b.getLong(IThreadStateManager.DATA_ADD_LEN, 0); + mProgress += len; + } + + break; + case STATE_UPDATE_PROGRESS: + if (msg.obj == null) { + mProgress = updateBlockProgress(); + } else if (msg.obj instanceof Long) { + mProgress = (long) msg.obj; + } + break; + } + return false; + } + }; + + @Override public void updateCurrentProgress(long currentProgress) { + mProgress = currentProgress; + } + + /** + * 退出looper循环 + */ + private void quitLooper() { + mLooper.quit(); + } + + /** + * 获取当前任务下载进度 + * + * @return 当前任务下载进度 + */ + @Override + public long getCurrentProgress() { + return mProgress; + } + + @Override public Handler.Callback getHandlerCallback() { + return callback; + } + + /** + * 所有子线程是否都已经停止 + */ + public boolean isStop() { + //ALog.d(TAG, + // String.format("isStop; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum, + // mCancelNum, mFailNum, mCompleteNum)); + return mStopNum.get() == mThreadNum || mStopNum.get() + mCompleteNum.get() == mThreadNum; + } + + /** + * 所有子线程是否都已经失败 + */ + @Override + public boolean isFail() { + //ALog.d(TAG, + // String.format("isFail; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum, + // mCancelNum, mFailNum, mCompleteNum)); + return mCompleteNum.get() != mThreadNum + && (mFailNum.get() == mThreadNum || mFailNum.get() + mCompleteNum.get() == mThreadNum); + } + + /** + * 所有子线程是否都已经完成 + */ + @Override + public boolean isComplete() { + //ALog.d(TAG, + // String.format("isComplete; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", + // mStopNum, + // mCancelNum, mFailNum, mCompleteNum)); + return mCompleteNum.get() == mThreadNum; + } + + /** + * 所有子线程是否都已经取消 + */ + public boolean isCancel() { + //ALog.d(TAG, String.format("isCancel; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", + // mStopNum, + // mCancelNum, mFailNum, mCompleteNum)); + return mCancelNum.get() == mThreadNum; + } + + /** + * 更新分块任务s的真实进度 + */ + private long updateBlockProgress() { + long size = 0; + for (int i = 0, len = mTaskRecord.threadRecords.size(); i < len; i++) { + File temp = new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, i)); + if (temp.exists()) { + size += temp.length(); + } + } + return size; + } + + /** + * 合并sftp的分块 + */ + private boolean mergerSFtp() { + if (mTaskRecord.threadNum == 1) { + File partFile = new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0)); + return partFile.renameTo(new File(mTaskRecord.filePath)); + } + + List partPath = new ArrayList<>(); + for (int i = 0, len = mTaskRecord.threadNum; i < len; i++) { + partPath.add(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, i)); + } + FileUtil.mergeSFtpFile(mTaskRecord.filePath, partPath, mTaskRecord.fileLength); + for (String pp : partPath) { + FileUtil.deleteFile(pp); + } + return true; + } + + /** + * 合并文件 + * + * @return {@code true} 合并成功,{@code false}合并失败 + */ + private boolean mergeFile() { + if (mTaskRecord.threadNum == 1) { + File targetFile = new File(mTaskRecord.filePath); + if (targetFile.exists() && targetFile.length() == mTaskRecord.fileLength){ + return true; + } + FileUtil.deleteFile(targetFile); + File partFile = new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0)); + return partFile.renameTo(targetFile); + } + + List partPath = new ArrayList<>(); + for (int i = 0, len = mTaskRecord.threadNum; i < len; i++) { + partPath.add(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, i)); + } + boolean isSuccess = FileUtil.mergeFile(mTaskRecord.filePath, partPath); + if (isSuccess) { + for (String pp : partPath) { + FileUtil.deleteFile(pp); + } + File targetFile = new File(mTaskRecord.filePath); + if (targetFile.exists() && targetFile.length() > mTaskRecord.fileLength) { + ALog.e(TAG, String.format("任务【%s】分块文件合并失败,下载长度超出文件真实长度,downloadLen: %s,fileSize: %s", + targetFile.getName(), targetFile.length(), mTaskRecord.fileLength)); + return false; + } + return true; + } else { + ALog.e(TAG, "合并失败"); + return false; + } + } + + @Override public void accept(ILoaderVisitor visitor) { + visitor.addComponent(this); + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java index 7209315f..7d8de8de 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -19,11 +19,13 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import android.util.SparseArray; + import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; import com.arialyy.aria.orm.annotation.Wrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; + import java.lang.reflect.Field; import java.net.URLDecoder; import java.net.URLEncoder; @@ -37,598 +39,603 @@ import java.util.Map; * 查询数据 */ class DelegateFind extends AbsDelegate { - private final String PARENT_COLUMN_ALIAS = "p"; - private final String CHILD_COLUMN_ALIAS = "c"; - - private DelegateFind() { - } - - /** - * 获取{@link One}和{@link Many}注解的字段 - * - * @return 返回[OneField, ManyField] ,如果注解依赖错误返回null - */ - private Field[] getOneAndManyField(Class clazz) { - Field[] om = new Field[2]; - Field[] fields = clazz.getDeclaredFields(); - Field one = null, many = null; - boolean hasOne = false, hasMany = false; - for (Field field : fields) { - if (SqlUtil.isOne(field)) { - if (hasOne) { - ALog.w(TAG, "查询数据失败,实体中有多个@One 注解"); - return null; + private final String PARENT_COLUMN_ALIAS = "p"; + private final String CHILD_COLUMN_ALIAS = "c"; + + private DelegateFind() { + } + + /** + * 获取{@link One}和{@link Many}注解的字段 + * + * @return 返回[OneField, ManyField] ,如果注解依赖错误返回null + */ + private Field[] getOneAndManyField(Class clazz) { + Field[] om = new Field[2]; + Field[] fields = clazz.getDeclaredFields(); + Field one = null, many = null; + boolean hasOne = false, hasMany = false; + for (Field field : fields) { + if (SqlUtil.isOne(field)) { + if (hasOne) { + ALog.w(TAG, "查询数据失败,实体中有多个@One 注解"); + return null; + } + hasOne = true; + one = field; + } + if (SqlUtil.isMany(field)) { + if (hasMany) { + ALog.w(TAG, "查询数据失败,实体中有多个@Many 注解"); + return null; + } + if (!field.getType().isAssignableFrom(List.class)) { + ALog.w(TAG, "查询数据失败,@Many 注解的类型不是List"); + return null; + } + hasMany = true; + many = field; + } } - hasOne = true; - one = field; - } - if (SqlUtil.isMany(field)) { - if (hasMany) { - ALog.w(TAG, "查询数据失败,实体中有多个@Many 注解"); - return null; + + if (one == null || many == null) { + ALog.w(TAG, "查询数据失败,实体中没有@One或@Many注解"); + return null; } - if (!field.getType().isAssignableFrom(List.class)) { - ALog.w(TAG, "查询数据失败,@Many 注解的类型不是List"); - return null; + + if (many.getType() != List.class) { + ALog.w(TAG, "查询数据失败,@Many注解的字段必须是List"); + return null; } - hasMany = true; - many = field; - } + om[0] = one; + om[1] = many; + return om; } - if (one == null || many == null) { - ALog.w(TAG, "查询数据失败,实体中没有@One或@Many注解"); - return null; + /** + * 查找一对多的关联数据 + * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null + * 如果实体中没有{@link One}或{@link Many}注解,将返回null + * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul + * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} + */ + List findRelationData(SQLiteDatabase db, Class clazz, + String... expression) { + return exeRelationSql(db, clazz, 1, Integer.MAX_VALUE, expression); } - if (many.getType() != List.class) { - ALog.w(TAG, "查询数据失败,@Many注解的字段必须是List"); - return null; - } - om[0] = one; - om[1] = many; - return om; - } - - /** - * 查找一对多的关联数据 - * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null - * 如果实体中没有{@link One}或{@link Many}注解,将返回null - * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul - * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} - */ - List findRelationData(SQLiteDatabase db, Class clazz, - String... expression) { - return exeRelationSql(db, clazz, 1, Integer.MAX_VALUE, expression); - } - - /** - * 查找一对多的关联数据 - * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null - * 如果实体中没有{@link One}或{@link Many}注解,将返回null - * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul - * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} - */ - List findRelationData(SQLiteDatabase db, Class clazz, - int page, int num, String... expression) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page,num 小于1"); - return null; - } - return exeRelationSql(db, clazz, page, num, expression); - } - - /** - * 执行关联查询,如果不需要分页,page和num传-1 - * - * @param page 当前页 - * @param num 一页的数量 - */ - private List exeRelationSql(SQLiteDatabase db, Class wrapperClazz, - int page, int num, String... expression) { - db = checkDb(db); - if (SqlUtil.isWrapper(wrapperClazz)) { - Field[] om = getOneAndManyField(wrapperClazz); - if (om == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - Field one = om[0], many = om[1]; - try { - Many m = many.getAnnotation(Many.class); - Class parentClazz = Class.forName(one.getType().getName()); - Class childClazz = Class.forName(CommonUtil.getListParamType(many).getName()); - // 检查表 - SqlUtil.checkOrCreateTable(db, parentClazz); - SqlUtil.checkOrCreateTable(db, childClazz); - final String pTableName = parentClazz.getSimpleName(); - final String cTableName = childClazz.getSimpleName(); - List pColumn = SqlUtil.getAllNotIgnoreField(parentClazz); - List cColumn = SqlUtil.getAllNotIgnoreField(childClazz); - StringBuilder pSb = new StringBuilder(); - StringBuilder cSb = new StringBuilder(); - - if (pColumn != null) { - pSb.append(pTableName.concat(".rowid AS ").concat(PARENT_COLUMN_ALIAS).concat("rowid,")); - for (Field f : pColumn) { - String temp = PARENT_COLUMN_ALIAS.concat(f.getName()); - pSb.append(pTableName.concat(".").concat(f.getName())) - .append(" AS ") - .append(temp) - .append(","); - } + /** + * 查找一对多的关联数据 + * 如果查找不到数据或实体没有被{@link Wrapper}注解,将返回null + * 如果实体中没有{@link One}或{@link Many}注解,将返回null + * 如果实体中有多个{@link One}或{@link Many}注解,将返回nul + * {@link One} 的注解对象必须是{@link DbEntity},{@link Many}的注解对象必须是List,并且List中的类型必须是{@link DbEntity} + */ + List findRelationData(SQLiteDatabase db, Class clazz, + int page, int num, String... expression) { + if (page < 1 || num < 1) { + ALog.w(TAG, "page,num 小于1"); + return null; } + return exeRelationSql(db, clazz, page, num, expression); + } - if (cColumn != null) { - pSb.append(cTableName.concat(".rowid AS ").concat(CHILD_COLUMN_ALIAS).concat("rowid,")); - for (Field f : cColumn) { - String temp = CHILD_COLUMN_ALIAS.concat(f.getName()); - cSb.append(cTableName.concat(".").concat(f.getName())) - .append(" AS ") - .append(temp) - .append(","); - } + /** + * 执行关联查询,如果不需要分页,page和num传-1 + * + * @param page 当前页 + * @param num 一页的数量 + */ + private List exeRelationSql(SQLiteDatabase db, Class wrapperClazz, + int page, int num, String... expression) { + db = checkDb(db); + if (SqlUtil.isWrapper(wrapperClazz)) { + Field[] om = getOneAndManyField(wrapperClazz); + if (om == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + Field one = om[0], many = om[1]; + try { + Many m = many.getAnnotation(Many.class); + Class parentClazz = Class.forName(one.getType().getName()); + Class childClazz = Class.forName(CommonUtil.getListParamType(many).getName()); + // 检查表 + SqlUtil.checkOrCreateTable(db, parentClazz); + SqlUtil.checkOrCreateTable(db, childClazz); + final String pTableName = parentClazz.getSimpleName(); + final String cTableName = childClazz.getSimpleName(); + List pColumn = SqlUtil.getAllNotIgnoreField(parentClazz); + List cColumn = SqlUtil.getAllNotIgnoreField(childClazz); + StringBuilder pSb = new StringBuilder(); + StringBuilder cSb = new StringBuilder(); + + if (pColumn != null) { + pSb.append(pTableName.concat(".rowid AS ").concat(PARENT_COLUMN_ALIAS).concat("rowid,")); + for (Field f : pColumn) { + String temp = PARENT_COLUMN_ALIAS.concat(f.getName()); + pSb.append(pTableName.concat(".").concat(f.getName())) + .append(" AS ") + .append(temp) + .append(","); + } + } + + if (cColumn != null) { + pSb.append(cTableName.concat(".rowid AS ").concat(CHILD_COLUMN_ALIAS).concat("rowid,")); + for (Field f : cColumn) { + String temp = CHILD_COLUMN_ALIAS.concat(f.getName()); + cSb.append(cTableName.concat(".").concat(f.getName())) + .append(" AS ") + .append(temp) + .append(","); + } + } + + String pColumnAlia = pSb.toString(); + String cColumnAlia = cSb.toString(); + if (!TextUtils.isEmpty(pColumnAlia)) { + pColumnAlia = pColumnAlia.substring(0, pColumnAlia.length() - 1); + } + + if (!TextUtils.isEmpty(cColumnAlia)) { + cColumnAlia = cColumnAlia.substring(0, cColumnAlia.length() - 1); + } + + sb.append("SELECT "); + + if (!TextUtils.isEmpty(pColumnAlia)) { + sb.append(pColumnAlia).append(","); + } + if (!TextUtils.isEmpty(cColumnAlia)) { + sb.append(cColumnAlia); + } + if (TextUtils.isEmpty(pColumnAlia) && TextUtils.isEmpty(cColumnAlia)) { + sb.append(" * "); + } + + sb.append(" FROM ") + .append(pTableName) + .append(" INNER JOIN ") + .append(cTableName) + .append(" ON ") + .append(pTableName.concat(".").concat(m.parentColumn())) + .append(" = ") + .append(cTableName.concat(".").concat(m.entityColumn())); + String sql; + if (expression != null && expression.length > 0) { + if (!CommonUtil.checkSqlExpression(expression)) { + return null; + } + sb.append(" WHERE ").append(expression[0]).append(" "); + sql = sb.toString(); + sql = sql.replace("?", "%s"); + Object[] params = new String[expression.length - 1]; + for (int i = 0, len = params.length; i < len; i++) { + params[i] = String.format("'%s'", SqlUtil.encodeStr(expression[i + 1])); + } + sql = String.format(sql, params); + } else { + sql = sb.toString(); + } + boolean paged = false; + if (page != -1 && num != -1) { + paged = true; + sql = sql.concat(String.format(" Group by %s LIMIT %s,%s", + pTableName.concat(".").concat(m.parentColumn()), (page - 1) * num, num)); + } + Cursor cursor = db.rawQuery(sql, null); + List data = + newInstanceEntity(wrapperClazz, parentClazz, childClazz, cursor, pColumn, cColumn, + paged, db, m.entityColumn(), m.parentColumn()); + + closeCursor(cursor); + return data; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } else { + ALog.e(TAG, "查询数据失败,实体类没有使用@Wrapper 注解"); + return null; } + return null; + } - String pColumnAlia = pSb.toString(); - String cColumnAlia = cSb.toString(); - if (!TextUtils.isEmpty(pColumnAlia)) { - pColumnAlia = pColumnAlia.substring(0, pColumnAlia.length() - 1); - } + /** + * 创建关联查询的数据 + * + * @param pColumn 父表的所有字段 + * @param cColumn 字表的所有字段 + */ + private synchronized List newInstanceEntity( + Class wrapperClazz, Class

parentClazz, + Class childClazz, + Cursor cursor, + List pColumn, List cColumn, boolean paged, SQLiteDatabase db, + String entityColumn, String parentColumn) { + List wrappers = new ArrayList<>(); + SparseArray> childs = new SparseArray<>(); // 所有子表数据 + SparseArray parents = new SparseArray<>(); // 所有父表数据 + + try { + while (cursor.moveToNext()) { + int pRowId = cursor.getInt(cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat("rowid"))); + if (childs.get(pRowId) == null) { + childs.put(pRowId, new ArrayList()); + parents.put(pRowId, createParent(pRowId, parentClazz, pColumn, cursor)); + } + if (paged) { + List list = createChildren(db, childClazz, pColumn, entityColumn, parentColumn, + parents.get(pRowId)); + if (list != null) { + childs.get(pRowId).addAll(list); + } + } else { + childs.get(pRowId).add(createChild(childClazz, cColumn, cursor)); + } + } - if (!TextUtils.isEmpty(cColumnAlia)) { - cColumnAlia = cColumnAlia.substring(0, cColumnAlia.length() - 1); + List wFields = SqlUtil.getAllNotIgnoreField(wrapperClazz); + if (wFields == null || wFields.isEmpty()) { + return null; + } + for (int i = 0; i < parents.size(); i++) { + int pRowId = parents.keyAt(i); + T wrapper = wrapperClazz.newInstance(); + boolean isPSet = false, isCSet = false; // 保证One 或 Many 只设置一次 + for (Field f : wFields) { + if (!isPSet && f.getAnnotation(One.class) != null) { + f.set(wrapper, parents.get(pRowId)); + isPSet = true; + } + if (!isCSet && f.getAnnotation(Many.class) != null) { + f.set(wrapper, childs.get(pRowId)); + isCSet = true; + } + } + wrapper.handleConvert(); //处理下转换 + wrappers.add(wrapper); + } + } catch (Exception e) { + e.printStackTrace(); } - sb.append("SELECT "); + return wrappers; + } + + /** + * 创建子对象集合 + */ + private List createChildren(SQLiteDatabase db, Class childClazz, + List pColumn, + String entityColumn, String parentColumn, DbEntity parents) + throws IllegalAccessException { - if (!TextUtils.isEmpty(pColumnAlia)) { - sb.append(pColumnAlia).append(","); + for (Field field : pColumn) { + field.setAccessible(true); + if (field.getName().equals(parentColumn)) { + Object o = field.get(parents); + if (o instanceof String) { + o = URLEncoder.encode((String) o); + } + return findData(db, childClazz, entityColumn + "='" + o + "'"); + } } - if (!TextUtils.isEmpty(cColumnAlia)) { - sb.append(cColumnAlia); + return new ArrayList(); + } + + /** + * 创建子对象 + */ + private T createChild(Class childClazz, List cColumn, + Cursor cursor) + throws InstantiationException, IllegalAccessException { + T child = childClazz.newInstance(); + child.rowID = cursor.getInt(cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat("rowid"))); + for (Field field : cColumn) { + field.setAccessible(true); + int columnIndex = cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat(field.getName())); + setFieldValue(field.getType(), field, columnIndex, cursor, child); } - if (TextUtils.isEmpty(pColumnAlia) && TextUtils.isEmpty(cColumnAlia)) { - sb.append(" * "); + return child; + } + + /** + * 创建父对象 + */ + private T createParent(int rowId, Class parentClazz, List pColumn, + Cursor cursor) + throws InstantiationException, IllegalAccessException { + T parent = parentClazz.newInstance(); + parent.rowID = rowId; + for (Field field : pColumn) { + field.setAccessible(true); + int columnIndex = cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat(field.getName())); + setFieldValue(field.getType(), field, columnIndex, cursor, parent); } + return parent; + } - sb.append(" FROM ") - .append(pTableName) - .append(" INNER JOIN ") - .append(cTableName) - .append(" ON ") - .append(pTableName.concat(".").concat(m.parentColumn())) - .append(" = ") - .append(cTableName.concat(".").concat(m.entityColumn())); - String sql; - if (expression != null && expression.length > 0) { - if (!CommonUtil.checkSqlExpression(expression)) { + /** + * 条件查寻数据 + */ + List findData(SQLiteDatabase db, Class clazz, String... expression) { + db = checkDb(db); + if (!CommonUtil.checkSqlExpression(expression)) { return null; - } - sb.append(" WHERE ").append(expression[0]).append(" "); - sql = sb.toString(); - sql = sql.replace("?", "%s"); - Object[] params = new String[expression.length - 1]; - for (int i = 0, len = params.length; i < len; i++) { - params[i] = String.format("'%s'", SqlUtil.encodeStr(expression[i + 1])); - } - sql = String.format(sql, params); - } else { - sql = sb.toString(); } - boolean paged = false; - if (page != -1 && num != -1) { - paged = true; - sql = sql.concat(String.format(" Group by %s LIMIT %s,%s", - pTableName.concat(".").concat(m.parentColumn()), (page - 1) * num, num)); + String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), + expression[0]); + String[] params = new String[expression.length - 1]; + try { + // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 + System.arraycopy(expression, 1, params, 0, params.length); + } catch (Exception e) { + e.printStackTrace(); + return null; } - Cursor cursor = db.rawQuery(sql, null); - List data = - newInstanceEntity(wrapperClazz, parentClazz, childClazz, cursor, pColumn, cColumn, - paged, db, m.entityColumn(), m.parentColumn()); - - closeCursor(cursor); - return data; - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } else { - ALog.e(TAG, "查询数据失败,实体类没有使用@Wrapper 注解"); - return null; + + return exeNormalDataSql(db, clazz, sql, params); } - return null; - } - - /** - * 创建关联查询的数据 - * - * @param pColumn 父表的所有字段 - * @param cColumn 字表的所有字段 - */ - private synchronized List newInstanceEntity( - Class wrapperClazz, Class

parentClazz, - Class childClazz, - Cursor cursor, - List pColumn, List cColumn, boolean paged, SQLiteDatabase db, - String entityColumn, String parentColumn) { - List wrappers = new ArrayList<>(); - SparseArray> childs = new SparseArray<>(); // 所有子表数据 - SparseArray parents = new SparseArray<>(); // 所有父表数据 - - try { - while (cursor.moveToNext()) { - int pRowId = cursor.getInt(cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat("rowid"))); - if (childs.get(pRowId) == null) { - childs.put(pRowId, new ArrayList()); - parents.put(pRowId, createParent(pRowId, parentClazz, pColumn, cursor)); + + /** + * 获取分页数据 + */ + List findData(SQLiteDatabase db, Class clazz, int page, int num, + String... expression) { + if (page < 1 || num < 1) { + ALog.w(TAG, "page, bum 小于1"); + return null; } - if (paged) { - List list = createChildren(db, childClazz, pColumn, entityColumn, parentColumn, - parents.get(pRowId)); - if (list != null) { - childs.get(pRowId).addAll(list); - } - } else { - childs.get(pRowId).add(createChild(childClazz, cColumn, cursor)); + db = checkDb(db); + if (!CommonUtil.checkSqlExpression(expression)) { + return null; } - } - - List wFields = SqlUtil.getAllNotIgnoreField(wrapperClazz); - if (wFields == null || wFields.isEmpty()) { - return null; - } - for (int i = 0; i < parents.size(); i++) { - int pRowId = parents.keyAt(i); - T wrapper = wrapperClazz.newInstance(); - boolean isPSet = false, isCSet = false; // 保证One 或 Many 只设置一次 - for (Field f : wFields) { - if (!isPSet && f.getAnnotation(One.class) != null) { - f.set(wrapper, parents.get(pRowId)); - isPSet = true; - } - if (!isCSet && f.getAnnotation(Many.class) != null) { - f.set(wrapper, childs.get(pRowId)); - isCSet = true; - } + String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", + CommonUtil.getClassName(clazz), + expression[0], (page - 1) * num, num); + + String[] params = new String[expression.length - 1]; + try { + // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 + System.arraycopy(expression, 1, params, 0, params.length); + } catch (Exception e) { + e.printStackTrace(); + return null; } - wrapper.handleConvert(); //处理下转换 - wrappers.add(wrapper); - } - } catch (Exception e) { - e.printStackTrace(); + + return exeNormalDataSql(db, clazz, sql, params); } - return wrappers; - } - - /** - * 创建子对象集合 - */ - private List createChildren(SQLiteDatabase db, Class childClazz, - List pColumn, - String entityColumn, String parentColumn, DbEntity parents) - throws IllegalAccessException { - - for (Field field : pColumn) { - field.setAccessible(true); - if (field.getName().equals(parentColumn)) { - Object o = field.get(parents); - if (o instanceof String) { - o = URLEncoder.encode((String) o); + /** + * 模糊查寻数据 + */ + List findDataByFuzzy(SQLiteDatabase db, Class clazz, + String conditions) { + db = checkDb(db); + if (TextUtils.isEmpty(conditions)) { + throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); } - return findData(db, childClazz, entityColumn + "='" + o + "'"); - } - } - return new ArrayList(); - } - - /** - * 创建子对象 - */ - private T createChild(Class childClazz, List cColumn, - Cursor cursor) - throws InstantiationException, IllegalAccessException { - T child = childClazz.newInstance(); - child.rowID = cursor.getInt(cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat("rowid"))); - for (Field field : cColumn) { - field.setAccessible(true); - int columnIndex = cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat(field.getName())); - setFieldValue(field.getType(), field, columnIndex, cursor, child); - } - return child; - } - - /** - * 创建父对象 - */ - private T createParent(int rowId, Class parentClazz, List pColumn, - Cursor cursor) - throws InstantiationException, IllegalAccessException { - T parent = parentClazz.newInstance(); - parent.rowID = rowId; - for (Field field : pColumn) { - field.setAccessible(true); - int columnIndex = cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat(field.getName())); - setFieldValue(field.getType(), field, columnIndex, cursor, parent); - } - return parent; - } - - /** - * 条件查寻数据 - */ - List findData(SQLiteDatabase db, Class clazz, String... expression) { - db = checkDb(db); - if (!CommonUtil.checkSqlExpression(expression)) { - return null; - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), - expression[0]); - String[] params = new String[expression.length - 1]; - try { - // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 - System.arraycopy(expression, 1, params, 0, params.length); - } catch (Exception e) { - e.printStackTrace(); - return null; + if (!conditions.toUpperCase().contains("LIKE")) { + throw new IllegalArgumentException("sql语句表达式未包含LIEK"); + } + String sql = String.format("SELECT rowid, * FROM %s, WHERE %s", CommonUtil.getClassName(clazz), + conditions); + return exeNormalDataSql(db, clazz, sql, null); } - return exeNormalDataSql(db, clazz, sql, params); - } - - /** - * 获取分页数据 - */ - List findData(SQLiteDatabase db, Class clazz, int page, int num, - String... expression) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page, bum 小于1"); - return null; - } - db = checkDb(db); - if (!CommonUtil.checkSqlExpression(expression)) { - return null; - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", - CommonUtil.getClassName(clazz), - expression[0], (page - 1) * num, num); - - String[] params = new String[expression.length - 1]; - try { - // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 - System.arraycopy(expression, 1, params, 0, params.length); - } catch (Exception e) { - e.printStackTrace(); - return null; + /** + * 分页、模糊搜索数据 + */ + List findDataByFuzzy(SQLiteDatabase db, Class clazz, + int page, int num, String conditions) { + if (page < 1 || num < 1) { + ALog.w(TAG, "page, bum 小于1"); + return null; + } + db = checkDb(db); + if (TextUtils.isEmpty(conditions)) { + throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); + } + if (!conditions.toUpperCase().contains("LIKE")) { + throw new IllegalArgumentException("sql语句表达式未包含LIEK"); + } + String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", + CommonUtil.getClassName(clazz), conditions, (page - 1) * num, num); + return exeNormalDataSql(db, clazz, sql, null); } - return exeNormalDataSql(db, clazz, sql, params); - } - - /** - * 模糊查寻数据 - */ - List findDataByFuzzy(SQLiteDatabase db, Class clazz, - String conditions) { - db = checkDb(db); - if (TextUtils.isEmpty(conditions)) { - throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); - } - if (!conditions.toUpperCase().contains("LIKE")) { - throw new IllegalArgumentException("sql语句表达式未包含LIEK"); + /** + * 查找表的所有数据 + */ + List findAllData(SQLiteDatabase db, Class clazz) { + db = checkDb(db); + String sql = String.format("SELECT rowid, * FROM %s", CommonUtil.getClassName(clazz)); + return exeNormalDataSql(db, clazz, sql, null); } - String sql = String.format("SELECT rowid, * FROM %s, WHERE %s", CommonUtil.getClassName(clazz), - conditions); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 分页、模糊搜索数据 - */ - List findDataByFuzzy(SQLiteDatabase db, Class clazz, - int page, int num, String conditions) { - if (page < 1 || num < 1) { - ALog.w(TAG, "page, bum 小于1"); - return null; - } - db = checkDb(db); - if (TextUtils.isEmpty(conditions)) { - throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); - } - if (!conditions.toUpperCase().contains("LIKE")) { - throw new IllegalArgumentException("sql语句表达式未包含LIEK"); - } - String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", - CommonUtil.getClassName(clazz), conditions, (page - 1) * num, num); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 查找表的所有数据 - */ - List findAllData(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - String sql = String.format("SELECT rowid, * FROM %s", CommonUtil.getClassName(clazz)); - return exeNormalDataSql(db, clazz, sql, null); - } - - /** - * 执行查询普通数据的sql语句,并创建对象 - * - * @param sql sql 查询语句 - * @param selectionArgs 查询参数,如何sql语句中查询条件含有'?'则该参数不能为空 - */ - private List exeNormalDataSql(SQLiteDatabase db, Class clazz, - String sql, String[] selectionArgs) { - SqlUtil.checkOrCreateTable(db, clazz); - Cursor cursor; - if (selectionArgs != null) { - - String[] temp = new String[selectionArgs.length]; - int i = 0; - for (String arg : selectionArgs) { - temp[i] = SqlUtil.encodeStr(arg); - i++; - } - cursor = db.rawQuery(sql, temp); - } else { - cursor = db.rawQuery(sql, null); - } - List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; - closeCursor(cursor); - return data; - } - - /** - * 根据数据游标创建一个具体的对象 - */ - private synchronized List newInstanceEntity(Class clazz, - Cursor cursor) { - List fields = CommonUtil.getAllFields(clazz); - List entitys = new ArrayList<>(); - if (fields != null && fields.size() > 0) { - try { - while (cursor.moveToNext()) { - T entity = clazz.newInstance(); - String primaryName = ""; - for (Field field : fields) { - field.setAccessible(true); - if (SqlUtil.isIgnore(field)) { - continue; - } - Class type = field.getType(); - if (SqlUtil.isPrimary(field) && (type == int.class || type == Integer.class)) { - primaryName = field.getName(); + /** + * 执行查询普通数据的sql语句,并创建对象 + * + * @param sql sql 查询语句 + * @param selectionArgs 查询参数,如何sql语句中查询条件含有'?'则该参数不能为空 + */ + private List exeNormalDataSql(SQLiteDatabase db, Class clazz, + String sql, String[] selectionArgs) { + SqlUtil.checkOrCreateTable(db, clazz); + Cursor cursor; + try { + if (selectionArgs != null) { + String[] temp = new String[selectionArgs.length]; + int i = 0; + for (String arg : selectionArgs) { + temp[i] = SqlUtil.encodeStr(arg); + i++; + } + //sql执行失败 android.database.sqlite.SQLiteException: no such column: filePath 异常 + cursor = db.rawQuery(sql, temp); + } else { + cursor = db.rawQuery(sql, null); } + List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; + closeCursor(cursor); + return data; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } - int column = cursor.getColumnIndex(field.getName()); - if (column == -1) continue; - setFieldValue(type, field, column, cursor, entity); - } - //当设置了主键,而且主键的类型为integer时,查询RowID等于主键 - entity.rowID = cursor.getInt( - cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName)); - //mDataCache.put(getCacheKey(entity), entity); - entitys.add(entity); + /** + * 根据数据游标创建一个具体的对象 + */ + private synchronized List newInstanceEntity(Class clazz, + Cursor cursor) { + List fields = CommonUtil.getAllFields(clazz); + List entitys = new ArrayList<>(); + if (fields != null && fields.size() > 0) { + try { + while (cursor.moveToNext()) { + T entity = clazz.newInstance(); + String primaryName = ""; + for (Field field : fields) { + field.setAccessible(true); + if (SqlUtil.isIgnore(field)) { + continue; + } + + Class type = field.getType(); + if (SqlUtil.isPrimary(field) && (type == int.class || type == Integer.class)) { + primaryName = field.getName(); + } + + int column = cursor.getColumnIndex(field.getName()); + if (column == -1) continue; + setFieldValue(type, field, column, cursor, entity); + } + //当设置了主键,而且主键的类型为integer时,查询RowID等于主键 + entity.rowID = cursor.getInt( + cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName)); + //mDataCache.put(getCacheKey(entity), entity); + entitys.add(entity); + } + closeCursor(cursor); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } } - closeCursor(cursor); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } + return entitys; } - return entitys; - } - - /** - * 设置字段的值 - * - * @throws IllegalAccessException - */ - private void setFieldValue(Class type, Field field, int columnIndex, Cursor cursor, - DbEntity entity) - throws IllegalAccessException { - if (cursor == null || cursor.isClosed()) { - ALog.e(TAG, "cursor没有初始化"); - return; + + /** + * 设置字段的值 + * + * @throws IllegalAccessException + */ + private void setFieldValue(Class type, Field field, int columnIndex, Cursor cursor, + DbEntity entity) + throws IllegalAccessException { + if (cursor == null || cursor.isClosed()) { + ALog.e(TAG, "cursor没有初始化"); + return; + } + if (type == String.class) { + String temp = cursor.getString(columnIndex); + if (!TextUtils.isEmpty(temp)) { + field.set(entity, URLDecoder.decode(temp)); + } + } else if (type == int.class || type == Integer.class) { + field.setInt(entity, cursor.getInt(columnIndex)); + } else if (type == float.class || type == Float.class) { + field.setFloat(entity, cursor.getFloat(columnIndex)); + } else if (type == double.class || type == Double.class) { + field.setDouble(entity, cursor.getDouble(columnIndex)); + } else if (type == long.class || type == Long.class) { + field.setLong(entity, cursor.getLong(columnIndex)); + } else if (type == boolean.class || type == Boolean.class) { + String temp = cursor.getString(columnIndex); + if (TextUtils.isEmpty(temp)) { + field.setBoolean(entity, false); + } else { + field.setBoolean(entity, !temp.equalsIgnoreCase("false")); + } + } else if (type == java.util.Date.class || type == java.sql.Date.class) { + field.set(entity, new Date(URLDecoder.decode(cursor.getString(columnIndex)))); + } else if (type == byte[].class) { + field.set(entity, cursor.getBlob(columnIndex)); + } else if (type == Map.class) { + String temp = cursor.getString(columnIndex); + if (!TextUtils.isEmpty(temp)) { + field.set(entity, SqlUtil.str2Map(URLDecoder.decode(temp))); + } + } else if (type == List.class) { + String value = cursor.getString(columnIndex); + if (!TextUtils.isEmpty(value)) { + field.set(entity, SqlUtil.str2List(URLDecoder.decode(value), field)); + } + } } - if (type == String.class) { - String temp = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(temp)) { - field.set(entity, URLDecoder.decode(temp)); - } - } else if (type == int.class || type == Integer.class) { - field.setInt(entity, cursor.getInt(columnIndex)); - } else if (type == float.class || type == Float.class) { - field.setFloat(entity, cursor.getFloat(columnIndex)); - } else if (type == double.class || type == Double.class) { - field.setDouble(entity, cursor.getDouble(columnIndex)); - } else if (type == long.class || type == Long.class) { - field.setLong(entity, cursor.getLong(columnIndex)); - } else if (type == boolean.class || type == Boolean.class) { - String temp = cursor.getString(columnIndex); - if (TextUtils.isEmpty(temp)) { - field.setBoolean(entity, false); - } else { - field.setBoolean(entity, !temp.equalsIgnoreCase("false")); - } - } else if (type == java.util.Date.class || type == java.sql.Date.class) { - field.set(entity, new Date(URLDecoder.decode(cursor.getString(columnIndex)))); - } else if (type == byte[].class) { - field.set(entity, cursor.getBlob(columnIndex)); - } else if (type == Map.class) { - String temp = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(temp)) { - field.set(entity, SqlUtil.str2Map(URLDecoder.decode(temp))); - } - } else if (type == List.class) { - String value = cursor.getString(columnIndex); - if (!TextUtils.isEmpty(value)) { - field.set(entity, SqlUtil.str2List(URLDecoder.decode(value), field)); - } + + /** + * 获取所在行Id + */ + int[] getRowId(SQLiteDatabase db, Class clazz) { + db = checkDb(db); + Cursor cursor = db.rawQuery("SELECT rowid, * FROM " + CommonUtil.getClassName(clazz), null); + int[] ids = new int[cursor.getCount()]; + int i = 0; + while (cursor.moveToNext()) { + ids[i] = cursor.getInt(cursor.getColumnIndex("rowid")); + i++; + } + cursor.close(); + return ids; } - } - - /** - * 获取所在行Id - */ - int[] getRowId(SQLiteDatabase db, Class clazz) { - db = checkDb(db); - Cursor cursor = db.rawQuery("SELECT rowid, * FROM " + CommonUtil.getClassName(clazz), null); - int[] ids = new int[cursor.getCount()]; - int i = 0; - while (cursor.moveToNext()) { - ids[i] = cursor.getInt(cursor.getColumnIndex("rowid")); - i++; + + /** + * 获取行Id + */ + int getRowId(SQLiteDatabase db, Class clazz, Object[] wheres, Object[] values) { + db = checkDb(db); + if (wheres.length <= 0 || values.length <= 0) { + ALog.e(TAG, "请输入删除条件"); + return -1; + } else if (wheres.length != values.length) { + ALog.e(TAG, "groupHash 和 vaule 长度不相等"); + return -1; + } + StringBuilder sb = new StringBuilder(); + sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE "); + int i = 0; + for (Object where : wheres) { + sb.append(where).append("=").append("'").append(values[i]).append("'"); + sb.append(i >= wheres.length - 1 ? "" : ","); + i++; + } + Cursor c = db.rawQuery(sb.toString(), null); + int id = c.getColumnIndex("rowid"); + c.close(); + return id; } - cursor.close(); - return ids; - } - - /** - * 获取行Id - */ - int getRowId(SQLiteDatabase db, Class clazz, Object[] wheres, Object[] values) { - db = checkDb(db); - if (wheres.length <= 0 || values.length <= 0) { - ALog.e(TAG, "请输入删除条件"); - return -1; - } else if (wheres.length != values.length) { - ALog.e(TAG, "groupHash 和 vaule 长度不相等"); - return -1; + + /** + * 通过rowId判断数据是否存在 + */ + boolean itemExist(SQLiteDatabase db, Class clazz, long rowId) { + return itemExist(db, CommonUtil.getClassName(clazz), rowId); } - StringBuilder sb = new StringBuilder(); - sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE "); - int i = 0; - for (Object where : wheres) { - sb.append(where).append("=").append("'").append(values[i]).append("'"); - sb.append(i >= wheres.length - 1 ? "" : ","); - i++; + + /** + * 通过rowId判断数据是否存在 + */ + boolean itemExist(SQLiteDatabase db, String tableName, long rowId) { + db = checkDb(db); + String sql = "SELECT rowid FROM " + tableName + " WHERE rowid=" + rowId; + Cursor cursor = db.rawQuery(sql, null); + boolean isExist = cursor.getCount() > 0; + cursor.close(); + return isExist; } - Cursor c = db.rawQuery(sb.toString(), null); - int id = c.getColumnIndex("rowid"); - c.close(); - return id; - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean itemExist(SQLiteDatabase db, Class clazz, long rowId) { - return itemExist(db, CommonUtil.getClassName(clazz), rowId); - } - - /** - * 通过rowId判断数据是否存在 - */ - boolean itemExist(SQLiteDatabase db, String tableName, long rowId) { - db = checkDb(db); - String sql = "SELECT rowid FROM " + tableName + " WHERE rowid=" + rowId; - Cursor cursor = db.rawQuery(sql, null); - boolean isExist = cursor.getCount() > 0; - cursor.close(); - return isExist; - } } diff --git a/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java index 6d730625..97f0d1f9 100644 --- a/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java +++ b/SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java @@ -20,6 +20,7 @@ import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalThreadStateManager; +import com.arialyy.aria.core.loader.UploadThreadStateManager; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.sftp.SFtpTaskOption; @@ -41,7 +42,8 @@ public class SFtpULoaderUtil extends AbsNormalLoaderUtil { @Override public LoaderStructure BuildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); structure.addComponent(new SFtpURecordHandler((UTaskWrapper) getTaskWrapper())) - .addComponent(new NormalThreadStateManager(getListener())) +// .addComponent(new NormalThreadStateManager(getListener())) + .addComponent(new UploadThreadStateManager(getListener())) .addComponent(new SFtpUInfoTask((UTaskWrapper) getTaskWrapper())) .addComponent(new NormalTTBuilder(getTaskWrapper(), new SFtpUTTBuilderAdapter( (UTaskWrapper) getTaskWrapper())));