|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: SQLite wrapper |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <string> |
|
19 #include <sqlite3.h> |
|
20 |
|
21 #include "com_nokia_mj_impl_rms_SQLite.h" |
|
22 #include "javajniutils.h" |
|
23 #include "logger.h" |
|
24 |
|
25 std::string jstringToString(JNIEnv* aEnv, const jstring& aString); |
|
26 |
|
27 /* |
|
28 * Class: com_nokia_mj_impl_rms_SQLite |
|
29 * Method: sqlite3_open |
|
30 * Signature: (Ljava/lang/String;)I |
|
31 */ |
|
32 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1open |
|
33 (JNIEnv* aEnv, jclass, jstring aFilename) |
|
34 { |
|
35 std::string filename = jstringToString(aEnv, aFilename); |
|
36 sqlite3* db = 0; |
|
37 int rc = sqlite3_open(filename.c_str(), &db); |
|
38 |
|
39 if (rc) |
|
40 { |
|
41 std::string error = sqlite3_errmsg(db); |
|
42 ELOG3(EMidpRms, "sqlite3_open failed (%d): %s (db:%s)", |
|
43 rc, error.c_str(), filename.c_str()); |
|
44 sqlite3_close(db); |
|
45 java::util::JniUtils::throwNewException(aEnv, |
|
46 "javax/microedition/rms/RecordStoreException", error); |
|
47 return 0; |
|
48 } |
|
49 return reinterpret_cast<jint>(db); |
|
50 } |
|
51 |
|
52 /* |
|
53 * Class: com_nokia_mj_impl_rms_SQLite |
|
54 * Method: sqlite3_close |
|
55 * Signature: (I)I |
|
56 */ |
|
57 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1close |
|
58 (JNIEnv*, jclass, jint aDbHandle) |
|
59 { |
|
60 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
61 int rc = sqlite3_close(db); |
|
62 return rc; |
|
63 } |
|
64 |
|
65 /* |
|
66 * Class: com_nokia_mj_impl_rms_SQLite |
|
67 * Method: sqlite3_exec |
|
68 * Signature: (ILjava/lang/String;)I |
|
69 */ |
|
70 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1exec |
|
71 (JNIEnv* aEnv, jclass, jint aDbHandle, jstring aStatement) |
|
72 { |
|
73 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
74 std::string statement = jstringToString(aEnv, aStatement); |
|
75 |
|
76 char* errmsg = 0; |
|
77 int rc = sqlite3_exec(db, statement.c_str(), 0, 0, &errmsg); |
|
78 if (rc) |
|
79 { |
|
80 ELOG3(EMidpRms, "sqlite3_exec failed (%d): %s (sql:%s)", |
|
81 rc , errmsg, statement.c_str()); |
|
82 } |
|
83 sqlite3_free(errmsg); |
|
84 |
|
85 return rc; |
|
86 } |
|
87 |
|
88 /* |
|
89 * Class: com_nokia_mj_impl_rms_SQLite |
|
90 * Method: sqlite3_prepare_v2 |
|
91 * Signature: (ILjava/lang/String;)I |
|
92 */ |
|
93 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1prepare_1v2 |
|
94 (JNIEnv* aEnv, jclass, jint aDbHandle, jstring aStatement) |
|
95 { |
|
96 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
97 std::string statement = jstringToString(aEnv, aStatement); |
|
98 |
|
99 sqlite3_stmt* ppStmt = 0; |
|
100 const char* tail = 0; |
|
101 int rc = sqlite3_prepare_v2(db, statement.c_str(), statement.size(), &ppStmt, &tail); |
|
102 |
|
103 if (rc) |
|
104 { |
|
105 ELOG3(EMidpRms, "sqlite3_prepare_v2 failed (%d): %s (sql:%s)", |
|
106 rc, sqlite3_errmsg(db), statement.c_str()); |
|
107 } |
|
108 |
|
109 return reinterpret_cast<int>(ppStmt); |
|
110 } |
|
111 |
|
112 /* |
|
113 * Class: com_nokia_mj_impl_rms_SQLite |
|
114 * Method: sqlite3_step |
|
115 * Signature: (I)I |
|
116 */ |
|
117 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1step |
|
118 (JNIEnv*, jclass, jint aStmtHandle) |
|
119 { |
|
120 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
121 int rc = sqlite3_step(ppStmt); |
|
122 return rc; |
|
123 } |
|
124 |
|
125 /* |
|
126 * Class: com_nokia_mj_impl_rms_SQLite |
|
127 * Method: sqlite3_reset |
|
128 * Signature: (I)I |
|
129 */ |
|
130 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1reset |
|
131 (JNIEnv*, jclass, jint aStmtHandle) |
|
132 { |
|
133 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
134 int rc = sqlite3_reset(ppStmt); |
|
135 return rc; |
|
136 } |
|
137 |
|
138 /* |
|
139 * Class: com_nokia_mj_impl_rms_SQLite |
|
140 * Method: sqlite3_finalize |
|
141 * Signature: (I)I |
|
142 */ |
|
143 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1finalize |
|
144 (JNIEnv*, jclass, jint aStmtHandle) |
|
145 { |
|
146 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
147 int rc = sqlite3_finalize(ppStmt); |
|
148 return rc; |
|
149 } |
|
150 |
|
151 /* |
|
152 * Class: com_nokia_mj_impl_rms_SQLite |
|
153 * Method: sqlite3_changes |
|
154 * Signature: (I)I |
|
155 */ |
|
156 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1changes |
|
157 (JNIEnv *, jclass, jint aDbHandle) |
|
158 { |
|
159 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
160 int rc = sqlite3_changes(db); |
|
161 return rc; |
|
162 } |
|
163 |
|
164 /* |
|
165 * Class: com_nokia_mj_impl_rms_SQLite |
|
166 * Method: sqlite3_errmsg |
|
167 * Signature: (I)Ljava/lang/String; |
|
168 */ |
|
169 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1errmsg |
|
170 (JNIEnv* aEnv, jclass, jint aDbHandle) |
|
171 { |
|
172 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
173 const char* errmsg = sqlite3_errmsg(db); |
|
174 jstring str = aEnv->NewStringUTF(errmsg); |
|
175 return str; |
|
176 } |
|
177 |
|
178 /* |
|
179 * Class: com_nokia_mj_impl_rms_SQLite |
|
180 * Method: sqlite3_bind_blob |
|
181 * Signature: (II[B)I |
|
182 */ |
|
183 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1bind_1blob |
|
184 (JNIEnv* aEnv, jclass, jint aStmtHandle, jint aIndex, jbyteArray aArray) |
|
185 { |
|
186 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
187 |
|
188 int len = 0; |
|
189 char* blob = 0; |
|
190 if (aArray) |
|
191 { |
|
192 len = aEnv->GetArrayLength(aArray); |
|
193 blob = new char[len]; |
|
194 if (!blob) |
|
195 { |
|
196 return SQLITE_NOMEM; |
|
197 } |
|
198 aEnv->GetByteArrayRegion(aArray, 0, len, reinterpret_cast<jbyte*>(blob)); |
|
199 } |
|
200 int rc = sqlite3_bind_blob(ppStmt, aIndex, blob, len, SQLITE_TRANSIENT); |
|
201 delete[] blob; |
|
202 |
|
203 return rc; |
|
204 } |
|
205 |
|
206 /* |
|
207 * Class: com_nokia_mj_impl_rms_SQLite |
|
208 * Method: sqlite3_bind_int |
|
209 * Signature: (III)I |
|
210 */ |
|
211 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1bind_1int |
|
212 (JNIEnv*, jclass, jint aStmtHandle, jint aIndex, jint aValue) |
|
213 { |
|
214 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
215 int rc = sqlite3_bind_int(ppStmt, aIndex, aValue); |
|
216 return rc; |
|
217 } |
|
218 |
|
219 /* |
|
220 * Class: com_nokia_mj_impl_rms_SQLite |
|
221 * Method: sqlite3_bind_int64 |
|
222 * Signature: (IIJ)I |
|
223 */ |
|
224 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1bind_1int64 |
|
225 (JNIEnv*, jclass, jint aStmtHandle, jint aIndex, jlong aValue) |
|
226 { |
|
227 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
228 int rc = sqlite3_bind_int64(ppStmt, aIndex, aValue); |
|
229 return rc; |
|
230 } |
|
231 |
|
232 /* |
|
233 * Class: com_nokia_mj_impl_rms_SQLite |
|
234 * Method: sqlite3_bind_text |
|
235 * Signature: (ILjava/lang/String;)I |
|
236 */ |
|
237 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1bind_1text |
|
238 (JNIEnv* aEnv, jclass, jint aStmtHandle, jint aIndex, jstring aText) |
|
239 { |
|
240 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
241 std::string text = jstringToString(aEnv, aText); |
|
242 int rc = sqlite3_bind_text(ppStmt, aIndex, text.c_str(), text.size(), SQLITE_TRANSIENT); |
|
243 return rc; |
|
244 } |
|
245 |
|
246 /* |
|
247 * Class: com_nokia_mj_impl_rms_SQLite |
|
248 * Method: sqlite3_column_blob |
|
249 * Signature: (II)[B |
|
250 */ |
|
251 JNIEXPORT jbyteArray JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1column_1blob |
|
252 (JNIEnv* aEnv, jclass, jint aStmtHandle, jint aIndex) |
|
253 { |
|
254 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
255 const void* blob = sqlite3_column_blob(ppStmt, aIndex); |
|
256 int len = sqlite3_column_bytes(ppStmt, aIndex); |
|
257 |
|
258 jbyteArray bytes = aEnv->NewByteArray(len); |
|
259 aEnv->SetByteArrayRegion(bytes, 0, len, (jbyte*)blob); |
|
260 return bytes; |
|
261 } |
|
262 |
|
263 /* |
|
264 * Class: com_nokia_mj_impl_rms_SQLite |
|
265 * Method: sqlite3_column_int |
|
266 * Signature: (II)I |
|
267 */ |
|
268 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1column_1int |
|
269 (JNIEnv*, jclass, jint aStmtHandle, jint aIndex) |
|
270 { |
|
271 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
272 int rc = sqlite3_column_int(ppStmt, aIndex); |
|
273 return rc; |
|
274 } |
|
275 |
|
276 /* |
|
277 * Class: com_nokia_mj_impl_rms_SQLite |
|
278 * Method: sqlite3_column_int64 |
|
279 * Signature: (II)J |
|
280 */ |
|
281 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1column_1int64 |
|
282 (JNIEnv*, jclass, jint aStmtHandle, jint aIndex) |
|
283 { |
|
284 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
285 jlong rc = sqlite3_column_int64(ppStmt, aIndex); |
|
286 return rc; |
|
287 } |
|
288 |
|
289 /* |
|
290 * Class: com_nokia_mj_impl_rms_SQLite |
|
291 * Method: sqlite3_column_text |
|
292 * Signature: (II)Ljava/lang/String; |
|
293 */ |
|
294 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1column_1text |
|
295 (JNIEnv* aEnv, jclass, jint aStmtHandle, jint aIndex) |
|
296 { |
|
297 sqlite3_stmt* ppStmt = reinterpret_cast<sqlite3_stmt*>(aStmtHandle); |
|
298 char* text = (char*)sqlite3_column_text(ppStmt, aIndex); |
|
299 |
|
300 jstring str = aEnv->NewStringUTF(text); |
|
301 return str; |
|
302 } |
|
303 |
|
304 /* |
|
305 * Class: com_nokia_mj_impl_rms_SQLite |
|
306 * Method: sqlite3_last_insert_rowid |
|
307 * Signature: (I)J |
|
308 */ |
|
309 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1last_1insert_1rowid |
|
310 (JNIEnv *, jclass, jint aDbHandle) |
|
311 { |
|
312 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
313 sqlite_int64 rc = sqlite3_last_insert_rowid(db); |
|
314 return rc; |
|
315 } |
|
316 |
|
317 /* |
|
318 * Class: com_nokia_mj_impl_rms_SQLite |
|
319 * Method: sqlite3_busy_timeout |
|
320 * Signature: (II)I |
|
321 */ |
|
322 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rms_SQLite_sqlite3_1busy_1timeout |
|
323 (JNIEnv *, jclass, jint aDbHandle, jint aMilliseconds) |
|
324 { |
|
325 sqlite3* db = reinterpret_cast<sqlite3*>(aDbHandle); |
|
326 int rc = sqlite3_busy_timeout(db, aMilliseconds); |
|
327 return rc; |
|
328 } |
|
329 |
|
330 std::string jstringToString(JNIEnv* aEnv, const jstring& aString) |
|
331 { |
|
332 const char* buf = aEnv->GetStringUTFChars(aString, 0); |
|
333 std::string result = ""; |
|
334 if (buf) |
|
335 { |
|
336 result = buf; |
|
337 } |
|
338 aEnv->ReleaseStringUTFChars(aString, buf); |
|
339 return result; |
|
340 } |
|
341 |