summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/resources/databaseDeploy/DAOByTablejava.txt
blob: 48c6413e0a036f3940caa7e9da8c4afd5ce717f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * 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#		
	}
}