blob: 48c6413e0a036f3940caa7e9da8c4afd5ce717f7 [file] [log] [blame]
/*
* Copyright (C) 2012 The Android Open Source Project
*
* 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 #packageName#;
import java.util.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;
public class #className# {
private DatabaseHelper dbHelper;
private static final String TAG = "#tableName#DAO";
private static final String DATABASE_NAME = "#dbName#";
private static final int DATABASE_VERSION = 1;
private static HashMap<String, String> #tableNameUpperCase#_PROJECTION_MAP;
private static final String TABLE_NAME = "#tableNameLowerCase#";
/**
* Singleton constructor
*/
private #className# ()
{
}
private static final #className# instance = new #className# ();
/**
* Get singleton
*/
public static #className# getInstance() {
return instance;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
#createTableStatement#
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + "to " + newVersion + ", which will destroy all old data");
#dropTableStatement#
}
}
/**
* Need to be called before using query, insert, delete or update methods
*/
public boolean openDatabase(Context context) {
dbHelper = new DatabaseHelper(context);
return (dbHelper == null) ? false : true;
}
/**
* Need to be called to release SQLite db connection
*/
public void closeDatabase(Context context) {
dbHelper.close();
}
/**
* Selects all items from table using all the columns
*/
public Cursor selectAll(String sort) {
return query(getProjectionMapAllItems(), null, null, null, null, sort);
}
public Cursor query(String[] projection, String selection, String[] selectionArgs, String groupBy, String having, String sort) {
SQLiteDatabase mDB = dbHelper.getReadableDatabase();
Cursor c = mDB.query(TABLE_NAME, projection, selection, selectionArgs, groupBy, having, sort);
return c;
}
public long insert(ContentValues initialValues) {
SQLiteDatabase mDB = dbHelper.getWritableDatabase();
long rowID;
ContentValues values;
if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}
#insertStatementCases#
rowID = mDB.insert("#tableNameLowerCase#", "#tableNameLowerCase#", values);
return rowID;
}
public int delete(String where, String[] whereArgs) {
SQLiteDatabase mDB = dbHelper.getWritableDatabase();
int count;
count = mDB.delete(TABLE_NAME, where, whereArgs);
return count;
}
public int update(ContentValues values, String where, String[] whereArgs) {
SQLiteDatabase mDB = dbHelper.getWritableDatabase();
int count;
count = mDB.update(TABLE_NAME, values, where, whereArgs);
return count;
}
public static String[] getProjectionMapAllItems(){
return #tableNameUpperCase#_PROJECTION_MAP.keySet().toArray(new String[]{});
}
static {
#tableNameUpperCase#_PROJECTION_MAP = new HashMap<String, String>();
#ProjectionMapStatementCases#
}
}