Home | History | Annotate | Download | only in databaseDeploy
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package #packageName#;
     17 
     18 import java.util.*;
     19 
     20 import android.content.*;
     21 import android.database.*;
     22 import android.database.sqlite.*;
     23 import android.util.*;
     24 
     25 
     26 public class #className# {
     27 
     28 	private DatabaseHelper dbHelper;
     29 	private static final String TAG = "#tableName#DAO";
     30 	private static final String DATABASE_NAME = "#dbName#";
     31 	private static final int DATABASE_VERSION = 1;
     32 	private static HashMap<String, String> #tableNameUpperCase#_PROJECTION_MAP;
     33 	private static final String TABLE_NAME = "#tableNameLowerCase#";
     34 
     35 	/**
     36 	 * Singleton constructor
     37 	 */
     38 	private #className# ()
     39 	{
     40 	}
     41 
     42 	private static final #className#  instance = new #className# ();
     43 
     44 	/**
     45 	 * Get singleton
     46 	 */
     47 	public static #className#  getInstance() {
     48 		return instance;
     49 	}
     50 
     51 	private static class DatabaseHelper extends SQLiteOpenHelper {
     52 		 /**
     53     	  * Constructor
     54     	  * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     55     	  * @param context
     56     	  */
     57 		public DatabaseHelper(Context context) {
     58 			super(context, DATABASE_NAME, null, DATABASE_VERSION);
     59 		}
     60 
     61 		public void onCreate(SQLiteDatabase db) {
     62 			#createTableStatement#
     63 		}
     64 
     65 		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     66 			Log.w(TAG, "Upgrading database from version " + oldVersion + "to " + newVersion + ", which will destroy all old data");
     67 			#dropTableStatement#
     68 		}
     69 	}
     70 
     71 	/**
     72 	 * Need to be called before using query, insert, delete or update methods
     73 	 */
     74 	public boolean openDatabase(Context context) {
     75 		dbHelper = new DatabaseHelper(context);
     76 		return (dbHelper == null) ? false : true;
     77 	}
     78 
     79 	/**
     80 	 * Need to be called to release SQLite db connection
     81 	 */
     82 	public void closeDatabase(Context context) {
     83 		dbHelper.close();
     84 	}
     85 
     86 	/**
     87 	 * Selects all items from table using all the columns
     88 	 */
     89 	public Cursor selectAll(String sort) {
     90 		return query(getProjectionMapAllItems(), null, null, null, null, sort);
     91 	}
     92 
     93 	public Cursor query(String[] projection, String selection, String[] selectionArgs, String groupBy, String having, String sort) {
     94 		SQLiteDatabase mDB = dbHelper.getReadableDatabase();
     95 		Cursor c = mDB.query(TABLE_NAME, projection, selection, selectionArgs, groupBy, having, sort);
     96 		return c;
     97 	}
     98 
     99 	public long insert(ContentValues initialValues) {
    100 		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
    101 		long rowID;
    102 		ContentValues values;
    103 		if (initialValues != null) {
    104 			values = new ContentValues(initialValues);
    105 		} else {
    106 			values = new ContentValues();
    107 		}
    108 
    109 		#insertStatementCases#
    110 
    111 		rowID = mDB.insert("#tableNameLowerCase#", "#tableNameLowerCase#", values);
    112 		return rowID;
    113 	}
    114 
    115 	public int delete(String where, String[] whereArgs) {
    116 	 	SQLiteDatabase mDB = dbHelper.getWritableDatabase();
    117 		int count;
    118 		count = mDB.delete(TABLE_NAME, where, whereArgs);
    119 		return count;
    120 	}
    121 
    122 	public int update(ContentValues values, String where, String[] whereArgs) {
    123 		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
    124 		int count;
    125 		count = mDB.update(TABLE_NAME, values, where, whereArgs);
    126 		return count;
    127 	}
    128 
    129 	public static String[] getProjectionMapAllItems(){
    130 		return #tableNameUpperCase#_PROJECTION_MAP.keySet().toArray(new String[]{});
    131 	}
    132 
    133 
    134 	static {
    135 		#tableNameUpperCase#_PROJECTION_MAP = new HashMap<String, String>();
    136 #ProjectionMapStatementCases#
    137 	}
    138 }
    139