mobi.cangol.mobile.db.DaoImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2013 Cangol
*
* 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 mobi.cangol.mobile.db;
import android.annotation.TargetApi;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.StrictMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import mobi.cangol.mobile.logging.Log;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
class DaoImpl implements Dao {
private CoreSQLiteOpenHelper mDatabaseHelper;
private String mTableName;
private Class mClazz;
public DaoImpl(CoreSQLiteOpenHelper databaseHelper, Class clazz) {
this.mDatabaseHelper = databaseHelper;
this.mClazz = clazz;
DatabaseTable dbTable = clazz.getAnnotation(DatabaseTable.class);
if (dbTable != null) {
this.mTableName = "".equals(dbTable.value()) ? clazz.getSimpleName() : dbTable.value();
} else {
Log.e("has no Annotation DatabaseTable clazz=" + clazz.getSimpleName());
}
}
private Cursor query(SQLiteDatabase db, QueryBuilder queryBuilder) {
return db.query(queryBuilder.isDistinctValue(),
queryBuilder.getTable(),
null,
queryBuilder.getSelection(),
queryBuilder.getSelectionArgs(),
queryBuilder.getGroupByValue(),
queryBuilder.getHavingValue(),
queryBuilder.getOrderByValue(),
queryBuilder.getLimitValue());
}
@Override
public List query(QueryBuilder queryBuilder) {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
ArrayList list = new ArrayList();
try {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor cursor = query(db, queryBuilder);
T obj = null;
while (cursor.moveToNext()) {
obj = DatabaseUtils.cursorToObject(mClazz, cursor);
list.add(obj);
}
cursor.close();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return list;
}
@Override
public T queryForId(ID paramID) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
T obj = null;
try {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
QueryBuilder queryBuilder = new QueryBuilder(mClazz);
queryBuilder.addQuery(DatabaseUtils.getIdColumnName(mClazz), paramID, "=");
Cursor cursor = query(db, queryBuilder);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
obj = DatabaseUtils.cursorToObject(mClazz, cursor);
}
cursor.close();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return obj;
}
@Override
public List queryForAll() throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
ArrayList list = new ArrayList();
try {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
QueryBuilder queryBuilder = new QueryBuilder(mClazz);
Cursor cursor = query(db, queryBuilder);
T obj = null;
while (cursor.moveToNext()) {
obj = DatabaseUtils.cursorToObject(mClazz, cursor);
list.add(obj);
}
cursor.close();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return list;
}
@Override
public T refresh(T paramT) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
T result = null;
try {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
QueryBuilder queryBuilder = new QueryBuilder(mClazz);
queryBuilder.addQuery(DatabaseUtils.getIdColumnName(mClazz), DatabaseUtils.getIdValue(paramT), "=");
Cursor cursor = query(db, queryBuilder);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
result = DatabaseUtils.cursorToObject(paramT, cursor);
}
cursor.close();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int create(T paramT) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
long result = -1;
try {
db.beginTransaction();
result = db.insert(mTableName, null, DatabaseUtils.getContentValues(paramT));
db.setTransactionSuccessful();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
} finally {
db.endTransaction();
}
StrictMode.setThreadPolicy(oldPolicy);
return (int) result;
}
@Override
public int create(Collection paramTs) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
long result = -1;
try {
db.beginTransaction();
for (T paramT : paramTs) {
result = result + db.insert(mTableName, null, DatabaseUtils.getContentValues(paramT));
}
db.setTransactionSuccessful();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
} finally {
db.endTransaction();
}
StrictMode.setThreadPolicy(oldPolicy);
return (int) result;
}
@Override
public int update(T paramT) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = -1;
try {
result = db.update(mTableName, DatabaseUtils.getContentValues(paramT), DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + DatabaseUtils.getIdValue(paramT)});
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int update(Collection paramTs) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = -1;
try {
db.beginTransaction();
for (T paramT : paramTs) {
result = result + db.update(mTableName, DatabaseUtils.getContentValues(paramT), DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + DatabaseUtils.getIdValue(paramT)});
}
db.setTransactionSuccessful();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
} finally {
db.endTransaction();
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int updateById(T paramT, ID paramID) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = -1;
try {
result = db.update(mTableName, DatabaseUtils.getContentValues(paramT), DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + paramID});
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int delete(T paramT) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = -1;
try {
result = db.delete(mTableName, DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + DatabaseUtils.getIdValue(paramT)});
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int delete(Collection paramCollection) throws SQLException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = 0;
try {
db.beginTransaction();
for (T t : paramCollection) {
result = result + db.delete(mTableName, DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + DatabaseUtils.getIdValue(t)});
}
db.setTransactionSuccessful();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
} finally {
db.endTransaction();
}
StrictMode.setThreadPolicy(oldPolicy);
return result;
}
@Override
public int delete(DeleteBuilder deleteBuilder) throws SQLException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = db.delete(mTableName, deleteBuilder.getWhere(), deleteBuilder.getWhereArgs());
return result;
}
@Override
public int deleteById(ID paramID) throws SQLException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = db.delete(mTableName, DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + paramID});
return result;
}
@Override
public int deleteByIds(Collection paramCollection) throws SQLException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = 0;
try {
for (ID id : paramCollection) {
result = result + db.delete(mTableName, DatabaseUtils.getIdColumnName(mClazz) + "=?", new String[]{"" + id});
}
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
}
return result;
}
@Override
public int deleteAll() throws SQLException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int result = 0;
try {
db.beginTransaction();
result = db.delete(mTableName, null, null);
db.setTransactionSuccessful();
} catch (Exception e) {
throw new SQLException(mTableName + " error=" + e.getMessage());
} finally {
db.endTransaction();
}
return result;
}
@Override
public Class getEntityClass() {
return mClazz;
}
}