1 |
|
|
2 |
|
package pl.magot.vetch.ancal.database; |
3 |
|
|
4 |
|
|
5 |
|
import android.database.Cursor; |
6 |
|
|
7 |
|
|
|
|
| 82,2% |
Uncovered Elements: 21 (118) |
Complexity: 33 |
Complexity Density: 0,45 |
|
8 |
|
public class DataTable |
9 |
|
{ |
10 |
|
|
11 |
|
private DataRow dataRow = null; |
12 |
|
|
13 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
14 |
140
|
public DataTable(DataRow dataRow)... |
15 |
|
{ |
16 |
140
|
this.dataRow = dataRow; |
17 |
|
} |
18 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
19 |
193
|
public Database GetUserDb()... |
20 |
|
{ |
21 |
193
|
return dataRow.GetUserDb(); |
22 |
|
} |
23 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
24 |
212
|
public String GetTableName()... |
25 |
|
{ |
26 |
212
|
return dataRow.GetTableName(); |
27 |
|
} |
28 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
29 |
18
|
public DataRow GetDataRow()... |
30 |
|
{ |
31 |
18
|
return dataRow; |
32 |
|
} |
33 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
34 |
112
|
public boolean CreateTable()... |
35 |
|
{ |
36 |
112
|
if (GetUserDb().TableExists(GetTableName())) |
37 |
|
{ |
38 |
108
|
return true; |
39 |
|
} else { |
40 |
4
|
return GetUserDb().ExecSQL(GetSqlTableDefinition(GetTableName(), dataRow.GetTableDef())); |
41 |
|
} |
42 |
|
} |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
44 |
4
|
public String GetSqlTableDefinition(String sTableName, DataField[] vecTableDef)... |
45 |
|
{ |
46 |
4
|
String def = "CREATE TABLE " + sTableName + " ("; |
47 |
27
|
for (int i = 0; i < vecTableDef.length; i++) |
48 |
|
{ |
49 |
23
|
def += vecTableDef[i].GetColumnDefinition(); |
50 |
23
|
if (i < (vecTableDef.length - 1)) |
51 |
19
|
def += ", "; |
52 |
|
} |
53 |
4
|
def += ")"; |
54 |
4
|
return def; |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
57 |
13
|
public long InsertValues()... |
58 |
|
{ |
59 |
13
|
long lRowId = GetUserDb().GetSQLiteDb().insert(GetTableName(), null, dataRow.GetContentValues()); |
60 |
13
|
return lRowId; |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
63 |
5
|
public long UpdateValues(long lRowId)... |
64 |
|
{ |
65 |
5
|
String sWhere = String.format("_ID = %d", lRowId); |
66 |
5
|
long lRowsUpdated = GetUserDb().GetSQLiteDb().update(GetTableName(), dataRow.GetContentValues(), sWhere, null); |
67 |
5
|
return lRowsUpdated; |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
70 |
3
|
public long DeleteDataRow(long lRowId)... |
71 |
|
{ |
72 |
3
|
String sWhere = String.format("_ID = %d", lRowId); |
73 |
3
|
long lRowsUpdated = GetUserDb().GetSQLiteDb().delete(GetTableName(), sWhere, null); |
74 |
3
|
return lRowsUpdated; |
75 |
|
} |
76 |
|
|
|
|
| 87,5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
77 |
8
|
public Cursor LocateDataRow(long lRowId)... |
78 |
|
{ |
79 |
8
|
final String s = "select * from %s where _ID = %d"; |
80 |
8
|
String sql = String.format(s, GetTableName(), lRowId); |
81 |
8
|
Cursor cr = GetUserDb().GetSQLiteDb().rawQuery(sql, null); |
82 |
|
|
83 |
8
|
if ((cr != null) && (cr.getCount() > 0)) |
84 |
8
|
cr.moveToFirst(); |
85 |
8
|
return cr; |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
88 |
24
|
public Cursor LocateAlarmDataRow(int iType, long lRefID)... |
89 |
|
{ |
90 |
24
|
final String s = "select * from %s where Type = %d and RefID = %d"; |
91 |
24
|
String sql = String.format(s, GetTableName(), iType, lRefID); |
92 |
24
|
Cursor cr = GetUserDb().GetSQLiteDb().rawQuery(sql, null); |
93 |
|
|
94 |
24
|
if ((cr != null) && (cr.getCount() > 0)) |
95 |
7
|
cr.moveToFirst(); |
96 |
24
|
return cr; |
97 |
|
} |
98 |
|
|
|
|
| 70,8% |
Uncovered Elements: 7 (24) |
Complexity: 6 |
Complexity Density: 0,38 |
|
99 |
18
|
public Database.Result UpdateData(boolean bInsertMode, long lEditRowId)... |
100 |
|
{ |
101 |
18
|
Database.Result result = Database.Result.errUnknown; |
102 |
18
|
if (GetUserDb().IsOpened()) |
103 |
|
{ |
104 |
18
|
try |
105 |
|
{ |
106 |
18
|
dataRow.SetValuesForDataRow(); |
107 |
|
} catch (Exception e) { |
108 |
0
|
return Database.Result.errCantSetValuesForDataRow; |
109 |
|
} |
110 |
|
|
111 |
18
|
if (bInsertMode) |
112 |
|
{ |
113 |
|
|
114 |
13
|
long lRowId = InsertValues(); |
115 |
13
|
if (lRowId > 0) |
116 |
|
{ |
117 |
13
|
result = Database.Result.Success; |
118 |
|
} else { |
119 |
0
|
result = Database.Result.errCantInsertNewData; |
120 |
|
} |
121 |
|
} else { |
122 |
|
|
123 |
5
|
long lRowsUpdated = UpdateValues(lEditRowId); |
124 |
5
|
if (lRowsUpdated == 1) |
125 |
|
{ |
126 |
5
|
result = Database.Result.Success; |
127 |
|
} else { |
128 |
0
|
result = Database.Result.errCantUpdateData; |
129 |
|
} |
130 |
|
} |
131 |
|
} else { |
132 |
0
|
result = Database.Result.errNoDbAccess; |
133 |
|
} |
134 |
18
|
return result; |
135 |
|
} |
136 |
|
|
|
|
| 62,5% |
Uncovered Elements: 6 (16) |
Complexity: 4 |
Complexity Density: 0,4 |
|
137 |
3
|
public Database.Result DeleteData(long iRowId)... |
138 |
|
{ |
139 |
3
|
Database.Result result = Database.Result.errUnknown; |
140 |
3
|
if (GetUserDb().IsOpened()) |
141 |
|
{ |
142 |
3
|
if (GetUserDb().TableExists(GetTableName())) |
143 |
|
{ |
144 |
3
|
long lRowsDeleted = DeleteDataRow(iRowId); |
145 |
3
|
if (lRowsDeleted == 1) |
146 |
|
{ |
147 |
3
|
result = Database.Result.Success; |
148 |
|
} else { |
149 |
0
|
result = Database.Result.errCantDeleteData; |
150 |
|
} |
151 |
|
} else { |
152 |
0
|
result = Database.Result.errTableNotExists; |
153 |
|
} |
154 |
|
} else { |
155 |
0
|
result = Database.Result.errNoDbAccess; |
156 |
|
} |
157 |
3
|
return result; |
158 |
|
} |
159 |
|
|
|
|
| 65% |
Uncovered Elements: 7 (20) |
Complexity: 5 |
Complexity Density: 0,36 |
|
160 |
8
|
public Database.Result GetRowDataForEdit(long lRowId)... |
161 |
|
{ |
162 |
8
|
Database.Result result = Database.Result.errUnknown; |
163 |
|
|
164 |
8
|
Cursor cr = LocateDataRow(lRowId); |
165 |
8
|
if (cr == null) |
166 |
|
{ |
167 |
0
|
result = Database.Result.errCantGetData; |
168 |
|
} else { |
169 |
8
|
if (cr.getCount() > 0) |
170 |
|
{ |
171 |
8
|
if (dataRow.GetValuesFromCursor(cr)) |
172 |
|
{ |
173 |
8
|
try |
174 |
|
{ |
175 |
8
|
dataRow.GetValuesFromDataRow(); |
176 |
|
} catch (Exception e) { |
177 |
0
|
return Database.Result.errCantGetValuesFromDataRow; |
178 |
|
} |
179 |
8
|
result = Database.Result.Success; |
180 |
|
} else { |
181 |
0
|
result = Database.Result.errCantGetDataFromTable; |
182 |
|
} |
183 |
8
|
cr.close(); |
184 |
|
} else { |
185 |
0
|
result = Database.Result.errCantFindData; |
186 |
|
} |
187 |
|
} |
188 |
8
|
return result; |
189 |
|
} |
190 |
|
|
191 |
|
} |
192 |
|
|