|
package com.awsle.aibatis.client;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import android.content.Context;
|
import android.database.Cursor;
|
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
import com.awsle.aibatis.exception.IdNotFoundException;
|
import com.awsle.aibatis.log.AibatisLog;
|
import com.awsle.aibatis.reflect.ObjectUtils;
|
import com.awsle.aibatis.sql.DynamicSql;
|
import com.awsle.aibatis.xml.aibatis.node.DataSource;
|
import com.awsle.aibatis.xml.aibatis.node.SqlNode;
|
|
/**
|
*
|
* @author 席有芳
|
* @url http://code.awsle.com/index.php/p/aibatis/
|
* @mail 951868171@qq.com
|
* @version 1.0
|
* @since aibatis-Alpha1.0.zip
|
*/
|
public class SqlMapClientImpl implements SqlMapClient {
|
// SQL节点
|
private Map<String, SqlNode> sqlNodeMap;
|
// 数据源
|
private DataSource dataSource;
|
// SQLite数据库
|
private SQLiteDatabase db;
|
|
public void setSqlNodeMap(Map<String, SqlNode> sqlNodeMap) {
|
this.sqlNodeMap = sqlNodeMap;
|
}
|
|
public Map<String, SqlNode> getSqlNodeMap() {
|
return sqlNodeMap;
|
}
|
|
|
|
/**
|
* 初始化
|
*/
|
public SqlMapClient init(Context context) {
|
return init(context,null);
|
}
|
|
/**
|
* 初始化
|
*/
|
public SqlMapClient init(Context context,DbUpdateListener dbUpdateListener) {
|
DaoConfig config = new DaoConfig();
|
config.setContext(context);
|
config.setDbName(dataSource.getDbName());
|
config.setDbVersion(Integer.parseInt(dataSource.getDbVersion()));
|
config.setDbUpdateListener(dbUpdateListener);
|
if (config.getContext() == null)
|
throw new RuntimeException("android context is null");
|
this.setDb(new SqliteDbHelper(config.getContext(), config.getDbName(),
|
config.getDbVersion(), config.getDbUpdateListener())
|
.getWritableDatabase());
|
return this;
|
}
|
|
public DataSource getDataSource() {
|
return dataSource;
|
}
|
|
public void setDataSource(DataSource dataSource) {
|
this.dataSource = dataSource;
|
}
|
|
public SQLiteDatabase getDb() {
|
return db;
|
}
|
|
public void setDb(SQLiteDatabase db) {
|
this.db = db;
|
}
|
|
// 数据库配置
|
public static class DaoConfig {
|
|
private Context context = null;// android上下文
|
private String dbName = "abatis.db";// 数据库名字
|
private int dbVersion = 1;// 数据库版本
|
private boolean debug = true;
|
private DbUpdateListener dbUpdateListener;
|
|
public Context getContext() {
|
return context;
|
}
|
|
public void setContext(Context context) {
|
this.context = context;
|
}
|
|
public String getDbName() {
|
return dbName;
|
}
|
|
public void setDbName(String dbName) {
|
this.dbName = dbName;
|
}
|
|
public int getDbVersion() {
|
return dbVersion;
|
}
|
|
public void setDbVersion(int dbVersion) {
|
this.dbVersion = dbVersion;
|
}
|
|
public boolean isDebug() {
|
return debug;
|
}
|
|
public void setDebug(boolean debug) {
|
this.debug = debug;
|
}
|
|
public DbUpdateListener getDbUpdateListener() {
|
return dbUpdateListener;
|
}
|
|
public void setDbUpdateListener(DbUpdateListener dbUpdateListener) {
|
this.dbUpdateListener = dbUpdateListener;
|
}
|
|
}
|
|
class SqliteDbHelper extends SQLiteOpenHelper {
|
|
private DbUpdateListener mDbUpdateListener;
|
|
public SqliteDbHelper(Context context, String name, int version,
|
DbUpdateListener dbUpdateListener) {
|
super(context, name, null, version);
|
this.mDbUpdateListener = dbUpdateListener;
|
}
|
|
public void onCreate(SQLiteDatabase db) {
|
}
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
if (mDbUpdateListener != null) {
|
mDbUpdateListener.onUpgrade(db, oldVersion, newVersion);
|
} else { // 清空所有的数据信息
|
/*
|
Cursor cursor = db.rawQuery(
|
"SELECT name FROM sqlite_master WHERE type ='table'",
|
null);
|
if (cursor != null) {
|
while (cursor.moveToNext()) {
|
db.execSQL("DROP TABLE " + cursor.getString(0));
|
}
|
}
|
if (cursor != null) {
|
cursor.close();
|
cursor = null;
|
}
|
*/
|
}
|
}
|
|
}
|
|
public interface DbUpdateListener {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
|
}
|
|
|
|
/**
|
* 通过ID查询Object
|
*/
|
public Object queryForObject(String id, Object parameterObject)
|
throws IdNotFoundException {
|
SqlNode sqlNode = sqlNodeMap.get(id);
|
if (sqlNode == null) {
|
try {
|
throw new IdNotFoundException("Id:" + id + ",NotFound!");
|
} catch (IdNotFoundException e) {
|
e.printStackTrace();
|
}
|
}
|
DynamicSql dynamicSql = DynamicSql.parser(sqlNode.getSql());
|
dynamicSql.buildBindArgs(parameterObject);
|
AibatisLog.d(dynamicSql);
|
String resultClass = sqlNode.getResultClass();
|
Cursor cursor = db.rawQuery(dynamicSql.getSql(),dynamicSql.getBindArgs());
|
Map map = new HashMap();
|
try {
|
while (cursor.moveToNext()) {
|
int count = cursor.getColumnCount();
|
for (int i = 0; i < count; i++) {
|
map.put(cursor.getColumnName(i), cursor.getString(i));
|
}
|
break;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null)
|
cursor.close();
|
cursor = null;
|
}
|
if("java.util.HashMap".equals(resultClass)){
|
return map;
|
}
|
else{
|
return ObjectUtils.mapToObject(map, resultClass);
|
}
|
|
}
|
|
public List queryForList(String id, Object parameterObject)
|
throws IdNotFoundException {
|
SqlNode sqlNode = sqlNodeMap.get(id);
|
if (sqlNode == null) {
|
throw new IdNotFoundException("Id:" + id + ",NotFound!");
|
}
|
DynamicSql dynamicSql = DynamicSql.parser(sqlNode.getSql());
|
dynamicSql.buildBindArgs(parameterObject);
|
AibatisLog.d(dynamicSql);
|
String resultClass = sqlNode.getResultClass();
|
String[] bindArgs = dynamicSql.getBindArgs();
|
if(bindArgs == null||bindArgs.length == 0){
|
bindArgs = null;
|
}
|
Cursor cursor = db.rawQuery(dynamicSql.getSql(),bindArgs);
|
List<Object> maps = new ArrayList<Object>();
|
try {
|
while (cursor.moveToNext()) {
|
Map map = new HashMap();
|
int count = cursor.getColumnCount();
|
for (int i = 0; i < count; i++) {
|
map.put(cursor.getColumnName(i), cursor.getString(i));
|
}
|
if("java.util.HashMap".equals(resultClass)){
|
maps.add(map);
|
}else{
|
maps.add(ObjectUtils.mapToObject(map, resultClass));
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null)
|
cursor.close();
|
cursor = null;
|
}
|
return maps;
|
}
|
|
public Object queryForObject(String id) throws IdNotFoundException {
|
return queryForObject(id, null);
|
}
|
|
public List queryForList(String id) throws IdNotFoundException {
|
return queryForList(id, null);
|
}
|
|
/**
|
* 更新操作
|
*/
|
public int update(String id, Object parameterObject) throws IdNotFoundException {
|
SqlNode sqlNode = sqlNodeMap.get(id);
|
if (sqlNode == null) {
|
throw new IdNotFoundException("Id:" + id + ",NotFound!");
|
}
|
DynamicSql dynamicSql = DynamicSql.parser(sqlNode.getSql());
|
dynamicSql.buildBindArgs(parameterObject);
|
try {
|
AibatisLog.d(dynamicSql);
|
String[] bindArgs = dynamicSql.getBindArgs();
|
if(bindArgs == null||bindArgs.length == 0){
|
db.execSQL(dynamicSql.getSql());
|
}else{
|
db.execSQL(dynamicSql.getSql(),bindArgs);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
return -1;
|
}
|
|
return 1;
|
}
|
/**
|
*
|
*/
|
public int delete(String id, Object parameterObject)throws IdNotFoundException {
|
return update(id, parameterObject);
|
}
|
/**
|
*
|
*/
|
public int insert(String id, Object parameterObject) throws IdNotFoundException {
|
return update(id, parameterObject);
|
}
|
/**
|
* 更新
|
*/
|
public int update(String id) throws IdNotFoundException {
|
return update(id, null);
|
}
|
|
/**
|
* 删除
|
*/
|
public int delete(String id) throws IdNotFoundException {
|
return delete(id, null);
|
}
|
/**
|
*
|
*/
|
public int insert(String id) throws IdNotFoundException {
|
return insert(id,null);
|
}
|
/**
|
*
|
*/
|
public void beginTransaction(){
|
|
db.beginTransaction();
|
}
|
/**
|
*
|
*/
|
public void endTransaction(){
|
db.endTransaction();
|
}
|
|
}
|