import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @param
* @Comments : 功能
* @Author : Lampo
* @CreateDate : 2019/12/26 11:18
* @ModifiedBy : Lampo
* @ModifiedDate : 2019/12/26 11:18
* @Modified :
*/
public class DBImpl {
private SQLiteDatabase db;
public ImplDataBase mBaseDao;
private static DBImpl dbimpl;
public DBImpl() {
DatabaseHelper mHelper = DatabaseHelper.getInstance(CitApplication.getInstance());
mBaseDao = new ImplDataBase(mHelper);
db = mHelper.getWritableDatabase();
}
public static DBImpl getInstance() {
if (dbimpl == null)
dbimpl = new DBImpl();
return dbimpl;
}
public long insertVo(T t) {
long index;
try {
db.beginTransaction();
String tableName = t.getClass().getAnnotation(DatabaseTable.class).tableName();
ContentValues values = new ContentValues();
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (!field.getAnnotation(DatabaseField.class).generatedId()) {
values.put(field.getName(), String.valueOf(field.get(t)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
index = db.insert(tableName, "", values);
if (index == -1) {
index = mBaseDao.create(t);
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
ExceptionLogUtil.getInstance().save(e);
index = -1;
}
FileUtils.copyDB();
return index;
}
public int delete(Class cla) {
int index;
try {
db.beginTransaction();
String tableName = cla.getClass().getAnnotation(DatabaseTable.class).tableName();
index = db.delete(tableName, "", new String[]{});
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
e.printStackTrace();
index = -1;
}
FileUtils.copyDB();
return index;
}
public void deleteListVo(List list) {
for (T t : list) {
deleteVo(t);
}
}
public int deleteVo(T t) {
int index;
try {
db.beginTransaction();
String tableName = t.getClass().getAnnotation(DatabaseTable.class).tableName();
String value = "";
StringBuffer keyBuffer = new StringBuffer();
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.getAnnotation(DatabaseField.class).generatedId()) {
keyBuffer.append(field.getName()).append(" = ? ");
value = String.valueOf(field.get(t));
continue;
}
} catch (Exception e) {
e.printStackTrace();
}
}
index = db.delete(tableName, keyBuffer.toString(), new String[]{value});
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
ExceptionLogUtil.getInstance().save(e);
index = -1;
}
FileUtils.copyDB();
return index;
}
public int updateVo(T t) {
int index;
try {
db.beginTransaction();
String tableName = t.getClass().getAnnotation(DatabaseTable.class).tableName();
ContentValues values = new ContentValues();
String value = "";
StringBuffer keyBuffer = new StringBuffer();
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.getAnnotation(DatabaseField.class).generatedId()) {
keyBuffer.append(field.getName()).append(" = ? ");
value = String.valueOf(field.get(t));
} else {
values.put(field.getName(), String.valueOf(field.get(t)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
index = db.update(tableName, values, keyBuffer.toString(), new String[]{value});
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
ExceptionLogUtil.getInstance().save(e);
index = -1;
}
FileUtils.copyDB();
return index;
}
public List queryVo() {
List list = new ArrayList<>();
return list;
}
public T queryFrist(Class tClass) {
List list = queryAll(tClass);
if (list != null && !list.isEmpty()) {
return list.get(0);
}
return null;
}
public T queryLast(Class tClass) {
List list = queryAll(tClass);
if (list != null && !list.isEmpty()) {
return list.get(list.size() - 1);
}
return null;
}
public List queryForDetail(Map mDbWhere, Class tClass) {
List list = new ArrayList<>();
try {
PDALogger.e("queryForDetail ==start=");
db.beginTransaction();
T t = tClass.newInstance();
String tableName = t.getClass().getAnnotation(DatabaseTable.class).tableName();
StringBuffer selection = new StringBuffer();
int size = mDbWhere.size();
int count = 0;
String[] selectionArgs = new String[]{};
for (String key : Collections.list(Collections.enumeration(mDbWhere.keySet()))) {
selection.append(key).append("=?");
if (count != size) {
selection.append(" AND ");
}
selectionArgs[count] = String.valueOf(mDbWhere.get(key));
count++;
}
Cursor cursor = db.query(tableName, null, selection.toString(), selectionArgs, null, null, null);
if (cursor.getColumnCount() <= 0) {
return list;
} else {
while (cursor.moveToNext()) {
T t1 = readCursor(tClass, cursor);
list.add(t1);
}
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
e.printStackTrace();
}
PDALogger.e("queryForDetail ==end=");
return list;
}
public List queryAll(Class tClass) {
List list = new ArrayList<>();
PDALogger.e("queryForDetail =queryAll=start=");
try {
db.beginTransaction();
T t = tClass.newInstance();
String tableName = t.getClass().getAnnotation(DatabaseTable.class).tableName();
String sql = "SELECT * FROM " + tableName;
Cursor cursor = db.rawQuery(sql, null);
if (cursor.getColumnCount() <= 0) {
return list;
} else {
while (cursor.moveToNext()) {
T t1 = readCursor(tClass, cursor);
list.add(t1);
}
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
e.printStackTrace();
}
PDALogger.e("queryForDetail =queryAll=end=");
return list;
}
public T readCursor(Class tClass, Cursor cursor) throws Exception {
T t = tClass.newInstance();
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object a = getColumeValue(field, cursor);
field.set(t, a);
} catch (Exception e) {
e.printStackTrace();
}
}
return t;
}
private Object getColumeValue(Field field, Cursor cursor) throws IllegalAccessException {
Object object = new Object();
String type = field.getType().getSimpleName();
switch (type) {
case "int":
case "Integer":
object = cursor.getInt(cursor.getColumnIndex(field.getName()));
break;
case "String":
case "java.lang.String":
object = cursor.getString(cursor.getColumnIndex(field.getName()));
break;
case "boolean":
case "java.lang.Boolean":
object = cursor.getInt(cursor.getColumnIndex(field.getName())) == 1;
break;
case "double":
case "java.lang.Double":
object = cursor.getDouble(cursor.getColumnIndex(field.getName()));
break;
case "float":
case "java.lang.Float":
object = cursor.getFloat(cursor.getColumnIndex(field.getName()));
break;
case "long":
case "java.lang.Long":
object = cursor.getLong(cursor.getColumnIndex(field.getName()));
break;
case "short":
case "java.lang.Short":
object = cursor.getShort(cursor.getColumnIndex(field.getName()));
break;
}
return object;
}
}