1 |
|
|
2 |
|
package pl.magot.vetch.ancal.database; |
3 |
|
|
4 |
|
|
5 |
|
import java.util.Vector; |
6 |
|
import pl.magot.vetch.ancal.R; |
7 |
|
import android.content.*; |
8 |
|
import android.database.sqlite.*; |
9 |
|
import android.database.*; |
10 |
|
|
11 |
|
|
|
|
| 48,4% |
Uncovered Elements: 66 (128) |
Complexity: 35 |
Complexity Density: 0,47 |
|
12 |
|
public class Database |
13 |
|
{ |
14 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
15 |
|
public enum Result |
16 |
|
{ |
17 |
|
Success, |
18 |
|
errUnknown, |
19 |
|
errCantInsertNewData, |
20 |
|
errCantUpdateData, |
21 |
|
errCantCreateTable, |
22 |
|
errNoDbAccess, |
23 |
|
errCantGetDataFromTable, |
24 |
|
errCantFindData, |
25 |
|
errCantGetData, |
26 |
|
errCantDeleteData, |
27 |
|
errTableNotExists, |
28 |
|
errCantSetValuesForDataRow, |
29 |
|
errCantGetValuesFromDataRow, |
30 |
|
}; |
31 |
|
|
32 |
|
|
33 |
|
public static final String sTableNameAppointments = "Appointments"; |
34 |
|
public static final String sTableNameTasks = "Tasks"; |
35 |
|
public static final String sTableNameNotes = "Notes"; |
36 |
|
public static final String sTableNameAlarms = "Alarms"; |
37 |
|
|
38 |
|
|
39 |
|
private final String dbName = "AnCalDatabase.db"; |
40 |
|
private Context ctx = null; |
41 |
|
private SQLiteDatabase db = null; |
42 |
|
private Result resultDbTablesCreated = Result.errUnknown; |
43 |
|
|
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
45 |
28
|
public Database(Context context)... |
46 |
|
{ |
47 |
28
|
ctx = context; |
48 |
28
|
Open(); |
49 |
28
|
CreateTables(); |
50 |
|
} |
51 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
52 |
0
|
public final String Name()... |
53 |
|
{ |
54 |
0
|
return dbName; |
55 |
|
} |
56 |
|
|
|
|
| 0% |
Uncovered Elements: 46 (46) |
Complexity: 12 |
Complexity Density: 0,5 |
|
57 |
0
|
public static int GetErrDesc(Result result)... |
58 |
|
{ |
59 |
0
|
int msgId = R.string.errUnknown; |
60 |
0
|
if (result == Result.errCantInsertNewData) |
61 |
0
|
msgId = R.string.errCantInsertNewData; |
62 |
0
|
if (result == Result.errCantUpdateData) |
63 |
0
|
msgId = R.string.errCantUpdateData; |
64 |
0
|
if (result == Result.errCantCreateTable) |
65 |
0
|
msgId = R.string.errCantCreateTable; |
66 |
0
|
if (result == Result.errNoDbAccess) |
67 |
0
|
msgId = R.string.errNoDbAccess; |
68 |
0
|
if (result == Result.errCantGetDataFromTable) |
69 |
0
|
msgId = R.string.errCantGetDataFromTable; |
70 |
0
|
if (result == Result.errCantFindData) |
71 |
0
|
msgId = R.string.errCantFindData; |
72 |
0
|
if (result == Result.errCantGetData) |
73 |
0
|
msgId = R.string.errCantGetData; |
74 |
0
|
if (result == Result.errCantDeleteData) |
75 |
0
|
msgId = R.string.errCantDeleteData; |
76 |
0
|
if (result == Result.errTableNotExists) |
77 |
0
|
msgId = R.string.errTableNotExists; |
78 |
0
|
if (result == Result.errCantSetValuesForDataRow) |
79 |
0
|
msgId = R.string.errCantSetValuesForDataRow; |
80 |
0
|
if (result == Result.errCantGetValuesFromDataRow) |
81 |
0
|
msgId = R.string.errCantGetValuesFromDataRow; |
82 |
0
|
return msgId; |
83 |
|
} |
84 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
85 |
28
|
public boolean Open()... |
86 |
|
{ |
87 |
28
|
boolean bSuccess = false; |
88 |
|
|
89 |
28
|
db = ctx.openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null); |
90 |
|
|
91 |
28
|
if (IsOpened()) |
92 |
|
{ |
93 |
28
|
bSuccess = true; |
94 |
|
} else { |
95 |
0
|
db = null; |
96 |
|
} |
97 |
28
|
return bSuccess; |
98 |
|
} |
99 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
100 |
26
|
public void Close()... |
101 |
|
{ |
102 |
26
|
if (IsOpened()) |
103 |
26
|
db.close(); |
104 |
|
} |
105 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
106 |
342
|
public boolean IsOpened()... |
107 |
|
{ |
108 |
342
|
if (db != null) |
109 |
342
|
return true; |
110 |
0
|
return false; |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
113 |
141
|
public SQLiteDatabase GetSQLiteDb()... |
114 |
|
{ |
115 |
141
|
return db; |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
118 |
0
|
public boolean Delete()... |
119 |
|
{ |
120 |
0
|
Close(); |
121 |
0
|
return ctx.deleteDatabase(dbName); |
122 |
|
} |
123 |
|
|
|
|
| 87,5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
124 |
4
|
public boolean ExecSQL(String sql)... |
125 |
|
{ |
126 |
4
|
boolean bSuccess = false; |
127 |
4
|
try |
128 |
|
{ |
129 |
4
|
if (IsOpened()) |
130 |
4
|
db.execSQL(sql); |
131 |
4
|
bSuccess = true; |
132 |
|
} catch (SQLException e) { |
133 |
|
} |
134 |
4
|
return bSuccess; |
135 |
|
} |
136 |
|
|
|
|
| 92,3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0,33 |
|
137 |
115
|
public boolean TableExists(String sTableName)... |
138 |
|
{ |
139 |
115
|
boolean bResult = false; |
140 |
115
|
if (IsOpened()) |
141 |
|
{ |
142 |
115
|
String sql = "select name from sqlite_master where type = 'table' and name = '%s'"; |
143 |
115
|
sql = String.format(sql, sTableName); |
144 |
115
|
Cursor cr = db.rawQuery(sql, null); |
145 |
115
|
if (cr.getCount() > 0) |
146 |
111
|
bResult = true; |
147 |
115
|
cr.close(); |
148 |
|
} |
149 |
115
|
return bResult; |
150 |
|
} |
151 |
|
|
|
|
| 75% |
Uncovered Elements: 5 (20) |
Complexity: 4 |
Complexity Density: 0,29 |
|
152 |
28
|
private void CreateTables()... |
153 |
|
{ |
154 |
28
|
resultDbTablesCreated = Database.Result.errUnknown; |
155 |
28
|
if (IsOpened()) |
156 |
|
{ |
157 |
28
|
Vector<DataRow> vecDataRows = new Vector<DataRow>(); |
158 |
28
|
vecDataRows.add(new DataRowAppointment(this)); |
159 |
28
|
vecDataRows.add(new DataRowTask(this)); |
160 |
28
|
vecDataRows.add(new DataRowNote(this)); |
161 |
28
|
vecDataRows.add(new DataRowAlarm(this)); |
162 |
|
|
163 |
140
|
for (int i = 0; i < vecDataRows.size(); i++) |
164 |
|
{ |
165 |
112
|
DataTable dataTable = new DataTable(vecDataRows.get(i)); |
166 |
112
|
if (dataTable.CreateTable()) |
167 |
|
{ |
168 |
112
|
resultDbTablesCreated = Database.Result.Success; |
169 |
|
} else { |
170 |
0
|
resultDbTablesCreated = Database.Result.errCantCreateTable; |
171 |
0
|
break; |
172 |
|
} |
173 |
|
} |
174 |
|
} else { |
175 |
0
|
resultDbTablesCreated = Database.Result.errNoDbAccess; |
176 |
|
} |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
179 |
120
|
public boolean TablesCreated()... |
180 |
|
{ |
181 |
120
|
return resultDbTablesCreated == Result.Success; |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
184 |
120
|
public synchronized boolean DatabaseReady()... |
185 |
|
{ |
186 |
120
|
return (IsOpened() && TablesCreated()); |
187 |
|
} |
188 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
189 |
0
|
public Result TablesCreationResult()... |
190 |
|
{ |
191 |
0
|
return resultDbTablesCreated; |
192 |
|
} |
193 |
|
|
194 |
|
} |