-
Notifications
You must be signed in to change notification settings - Fork 22
/
PinDatabase.java
210 lines (177 loc) · 6.83 KB
/
PinDatabase.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package de.dotwee.micropinner.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import java.util.Map;
import de.dotwee.micropinner.BuildConfig;
import static android.database.DatabaseUtils.queryNumEntries;
import static de.dotwee.micropinner.tools.SQLiteStatementsLogger.logDelete;
import static de.dotwee.micropinner.tools.SQLiteStatementsLogger.logInsertWithOnConflict;
import static de.dotwee.micropinner.tools.SQLiteStatementsLogger.logUpdate;
/**
* Created by lukas on 10.08.2016.
*/
public class PinDatabase extends SQLiteOpenHelper {
/* integer columns */
static final String COLUMN_ID = "_id";
/* string columns */
static final String COLUMN_TITLE = "title";
static final String COLUMN_CONTENT = "content";
/* integer columns */
static final String COLUMN_VISIBILITY = "visibility";
static final String COLUMN_PRIORITY = "priority";
/* boolean columns */
static final String COLUMN_PERSISTENT = "persistent";
static final String COLUMN_SHOW_ACTIONS = "show_actions";
private static final String TABLE_PINS = "pins";
private static final String TAG = PinDatabase.class.getSimpleName();
private static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_PINS + "( "
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TITLE + " text not null, "
+ COLUMN_CONTENT + " text not null, "
+ COLUMN_VISIBILITY + " integer not null, "
+ COLUMN_PRIORITY + " integer not null, "
+ COLUMN_PERSISTENT + " boolean not null, "
+ COLUMN_SHOW_ACTIONS + " boolean not null);";
private static final String[] columns = {
PinDatabase.COLUMN_ID,
PinDatabase.COLUMN_TITLE,
PinDatabase.COLUMN_CONTENT,
PinDatabase.COLUMN_VISIBILITY,
PinDatabase.COLUMN_PRIORITY,
PinDatabase.COLUMN_PERSISTENT,
PinDatabase.COLUMN_SHOW_ACTIONS
};
private static PinDatabase instance = null;
private final SQLiteDatabase database;
private PinDatabase(@NonNull Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
database = getWritableDatabase();
}
public static synchronized PinDatabase getInstance(@NonNull Context context) {
if (PinDatabase.instance == null) {
PinDatabase.instance = new PinDatabase(context.getApplicationContext());
}
return PinDatabase.instance;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PINS);
onCreate(sqLiteDatabase);
}
/**
* This method decides whether a new pin should be created or updated in the database
*
* @param pin the pin to write
*/
public void writePin(@NonNull PinSpec pin) {
Log.i(TAG, "Write pin called for pin " + pin.toString());
if (pin.getId() == -1) {
createPin(pin);
} else {
updatePin(pin);
}
}
/**
* This method creates a pin within the database and gives it a unique id
*
* @param pin the pin to create
*/
private void createPin(@NonNull PinSpec pin) {
ContentValues contentValues = pin.toContentValues();
if (BuildConfig.DEBUG) {
logInsertWithOnConflict(PinDatabase.TABLE_PINS, null, contentValues, SQLiteDatabase.CONFLICT_NONE);
}
long id = database.insert(PinDatabase.TABLE_PINS, null, contentValues);
Log.i(TAG, "Created new pin with id " + id);
pin.setId(id);
onDatabaseAction();
}
/**
* This method updates a pin in the database without changing its id
*
* @param pin the pin to update
*/
private void updatePin(@NonNull PinSpec pin) {
ContentValues contentValues = pin.toContentValues();
long id = pin.getId();
String whereClause = PinDatabase.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{String.valueOf(id)};
if (BuildConfig.DEBUG) {
logUpdate(PinDatabase.TABLE_PINS, contentValues, whereClause, whereArgs);
}
database.update(PinDatabase.TABLE_PINS, contentValues, whereClause, whereArgs);
Log.i(TAG, "Updated new pin with id " + id);
pin.setId(id);
onDatabaseAction();
}
/**
* This method deletes a pin from the database
*
* @param pin to delete
*/
public void deletePin(PinSpec pin) {
long id = pin.getId();
String whereClause = PinDatabase.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{String.valueOf(id)};
if (BuildConfig.DEBUG) {
logDelete(PinDatabase.TABLE_PINS, whereClause, whereArgs);
}
boolean success = database.delete(PinDatabase.TABLE_PINS, whereClause, whereArgs) > 0;
Log.i(TAG, "Deleting pin with id " + id + "; success " + success);
pin.setId(-1);
onDatabaseAction();
}
public void deleteAll() {
Log.i(TAG, "Deleting all pins");
database.delete(PinDatabase.TABLE_PINS, null, null);
}
/**
* This method returns the amount of entries in the pin database
*
* @return the amount of entries
*/
public long count() {
return queryNumEntries(database, PinDatabase.TABLE_PINS);
}
/**
* This method gets called on insert() and delete()
*/
private void onDatabaseAction() {
long count = count();
Log.i(TAG, "onDatabaseAction() count " + count);
}
/**
* This method returns a map of all pins in the database with their id as key
*
* @return map of all pins
*/
@NonNull
public Map<Integer, PinSpec> getAllPinsMap() {
Map<Integer, PinSpec> pinMap = new ArrayMap<>();
Cursor cursor = database.query(PinDatabase.TABLE_PINS, columns, null, null, null, null, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
PinSpec pinSpec = new PinSpec(cursor);
pinMap.put(pinSpec.getIdAsInt(), pinSpec);
cursor.moveToNext();
}
}
cursor.close();
return pinMap;
}
}