package com.example.jiacloud.jiacloud.Tools.common.DB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by jiacloud on 16/11/8.
*/
public abstract class DataBaseHelper {
private DBManager manager;
/**
* 数据库对象
*/
protected SQLiteDatabase mDb;
/**
* 更新表语句
*/
private String[] mDbUpdateSql;
protected abstract String[] getDbUpdateSql(Context context);
public DataBaseHelper(Context context) {
this.mDbUpdateSql = this.getDbUpdateSql(context);
this.manager = new DBManager(context);
}
public void open(){
new Thread(new Runnable() {
@Override
public void run() {
manager.openDatabase();
mDb = manager.database;
}
}).start();
}
protected SQLiteDatabase getDB(){
return this.mDb;
}
public void close(){
manager.closeDatabase();
}
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String[] arr = DataBaseHelper.this.mDbUpdateSql;
//执行更新语句
for (int i=0;i<arr.length;i++){
String sql = arr[i];
db.execSQL(sql);
}
}
}
/**
* 统一对ContentValues处理
* @param contentValues
* @param key
* @param value
*/
private void ContentValuesPut(ContentValues contentValues, String key, Object value){
if (value==null){
contentValues.put(key,"");
}else{
String className = value.getClass().getName();
if (className.equals("java.lang.String")){
contentValues.put(key,value.toString());
} else if (className.equals("java.lang.Integer")){
contentValues.put(key,Integer.valueOf(value.toString()));
} else if (className.equals("java.lang.Float")){
contentValues.put(key,Float.valueOf(value.toString()));
} else if (className.equals("java.lang.Double")){
contentValues.put(key,Double.valueOf(value.toString()));
} else if (className.equals("java.lang.Boolean")){
contentValues.put(key,Boolean.valueOf(value.toString()));
} else if (className.equals("java.lang.Long")){
contentValues.put(key,Long.valueOf(value.toString()));
} else if (className.equals("java.lang.Short")){
contentValues.put(key,Short.valueOf(value.toString()));
}
}
}
/**
* 根据数组的列和值进行insert
* @param tableName
* @param columns
* @param values
* @return
*/
public boolean insert(String tableName,String[] columns,Object[] values){
ContentValues contentValues = new ContentValues();
for (int rows = 0; rows < columns.length;++rows){
ContentValuesPut(contentValues,columns[rows],values[rows]);
}
long rowId = this.mDb.insert(tableName,null,contentValues);
return rowId!=-1;
}
/**
* 根据map来进行insert
* @param tableName
* @param columnValues
* @return
*/
public boolean insert(String tableName,Map<String,Object> columnValues){
ContentValues contentValues = new ContentValues();
Iterator iterator = columnValues.keySet().iterator();
while (iterator.hasNext()){
String key = (String) iterator.next();
this.ContentValuesPut(contentValues,key,columnValues.get(key));
}
long rowId = this.mDb.insert(tableName,null,contentValues);
return rowId!=-1;
}
/**
* 统一对数组where条件进行拼接
* @param whereColumns
* @return
*/
private String initWhereSqlFromArray(String[] whereColumns){
StringBuffer whereStr = new StringBuffer();
for (int i=0;i<whereColumns.length;++i){
whereStr.append(whereColumns[i]).append(" = ? ");
if (i<whereColumns.length-1){
whereStr.append(" and ");
}
}
return whereStr.toString();
}
/**
* 统一对map的where条件和值进行处理
* @param whereParams
* @return
*/
private Map<String,Object> initWhereSqlFromMap(Map<String,String> whereParams){
Set set = whereParams.keySet();
String[] temp = new String[whereParams.size()];
int i = 0;
Iterator iterator = set.iterator();
StringBuffer whereStr = new StringBuffer();
while (iterator.hasNext()){
String key = (String) iterator.next();
whereStr.append(key).append(" = ? ");
temp[i] = whereParams.get(key);
if (i<set.size()-1){
whereStr.append(" and ");
}
i++;
}
HashMap result = new HashMap();
result.put("whereSql",whereStr);
result.put("whereSqlParam",temp);
return result;
}
/**
* 根据数组条件来update
* @param tableName
* @param columns
* @param values
* @param whereColumns
* @param whereArgs
* @return
*/
public boolean update(String tableName,String[] columns,Object[] values,String[] whereColumns,String[] whereArgs){
ContentValues contentValues = new ContentValues();
for (int i=0;i<columns.length;++i){
this.ContentValuesPut(contentValues,columns[i],values[i]);
}
String whereClause = this.initWhereSqlFromArray(whereColumns);
int rowNumber = this.mDb.update(tableName,contentValues,whereClause,whereArgs);
return rowNumber > 0 ;
}
/**
* 根据map值来进行update
* @param tableName
* @param columnValues
* @param whereParam
* @return
*/
public boolean update(String tableName,Map<String,Object> columnValues,Map<String,String> whereParam){
ContentValues contentValues = new ContentValues();
Iterator iterator = columnValues.keySet().iterator();
String columns;
while (iterator.hasNext()){
columns = (String) iterator.next();
ContentValuesPut(contentValues,columns,columnValues.get(columns));
}
Map map = this.initWhereSqlFromMap(whereParam);
int rowNumber = this.mDb.update(tableName,contentValues,(String)map.get("whereSql"),(String[]) map.get("whereSqlParam"));
return rowNumber > 0;
}
/**
* 根据数组条件进行delete
* @param tableName
* @param whereColumns
* @param whereParam
* @return
*/
public boolean delete(String tableName,String[] whereColumns,String[] whereParam){
String whereStr = this.initWhereSqlFromArray(whereColumns);
int rowNumber = this.mDb.delete(tableName,whereStr,whereParam);
return rowNumber > 0;
}
/**
* 根据map来进行delete
* @param tableName
* @param whereParams
* @return
*/
public boolean delete(String tableName,Map<String,String> whereParams){
Map map = this.initWhereSqlFromMap(whereParams);
int rowNumber = this.mDb.delete(tableName,map.get("whereSql").toString(),(String[]) map.get("whereSqlParam"));
return rowNumber > 0;
}
/**
* 查询返回List
* @param sql
* @param p