/
SQLite.js
288 lines (266 loc) · 9.14 KB
/
SQLite.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict';
import { Platform } from '@unimodules/core';
import { Asset } from 'expo-asset';
import * as FS from 'expo-file-system';
import * as SQLite from 'expo-sqlite';
export const name = 'SQLite';
// TODO: Only tests successful cases, needs to test error cases like bad database name etc.
export function test(t) {
t.describe('SQLite', () => {
t.it('should be able to drop + create a table, insert, query', async () => {
const db = SQLite.openDatabase('test.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const nop = () => {};
const onError = (tx, error) => reject(error);
tx.executeSql('DROP TABLE IF EXISTS Users;', [], nop, onError);
tx.executeSql(
'CREATE TABLE IF NOT EXISTS Users (user_id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(64), k INT, j REAL);',
[],
nop,
onError
);
tx.executeSql(
'INSERT INTO Users (name, k, j) VALUES (?, ?, ?)',
['Tim Duncan', 1, 23.4],
nop,
onError
);
tx.executeSql(
'INSERT INTO Users (name, k, j) VALUES ("Manu Ginobili", 5, 72.8)',
[],
nop,
onError
);
tx.executeSql(
'INSERT INTO Users (name, k, j) VALUES ("Nikhilesh Sigatapu", 7, 42.14)',
[],
nop,
onError
);
tx.executeSql(
'SELECT * FROM Users',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(3);
t.expect(results.rows.item(0).j).toBeCloseTo(23.4);
},
onError
);
},
reject,
resolve
);
});
if (Platform.OS !== 'web') {
const { exists } = await FS.getInfoAsync(`${FS.documentDirectory}SQLite/test.db`);
t.expect(exists).toBeTruthy();
}
});
if (Platform.OS !== 'web') {
t.it(
'should work with a downloaded .db file',
async () => {
await FS.downloadAsync(
Asset.fromModule(require('../assets/asset-db.db')).uri,
`${FS.documentDirectory}SQLite/downloaded.db`
);
const db = SQLite.openDatabase('downloaded.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const onError = (tx, error) => reject(error);
tx.executeSql(
'SELECT * FROM Users',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(3);
t.expect(results.rows._array[0].j).toBeCloseTo(23.4);
},
onError
);
},
reject,
resolve
);
});
},
30000
);
}
t.it('should be able to recreate db from scratch by deleting file', async () => {
{
const db = SQLite.openDatabase('test.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const nop = () => {};
const onError = (tx, error) => reject(error);
tx.executeSql('DROP TABLE IF EXISTS Users;', [], nop, onError);
tx.executeSql(
'CREATE TABLE IF NOT EXISTS Users (user_id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(64), k INT, j REAL);',
[],
nop,
onError
);
tx.executeSql(
'INSERT INTO Users (name, k, j) VALUES (?, ?, ?)',
['Tim Duncan', 1, 23.4],
nop,
onError
);
tx.executeSql(
'SELECT * FROM Users',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(1);
},
onError
);
},
reject,
resolve
);
});
}
if (Platform.OS !== 'web') {
const { exists } = await FS.getInfoAsync(`${FS.documentDirectory}SQLite/test.db`);
t.expect(exists).toBeTruthy();
}
if (Platform.OS !== 'web') {
await FS.deleteAsync(`${FS.documentDirectory}SQLite/test.db`);
const { exists } = await FS.getInfoAsync(`${FS.documentDirectory}SQLite/test.db`);
t.expect(exists).toBeFalsy();
}
{
const db = SQLite.openDatabase('test.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const nop = () => {};
const onError = (tx, error) => reject(error);
tx.executeSql('DROP TABLE IF EXISTS Users;', [], nop, onError);
tx.executeSql(
'CREATE TABLE IF NOT EXISTS Users (user_id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(64), k INT, j REAL);',
[],
nop,
onError
);
tx.executeSql(
'SELECT * FROM Users',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(0);
},
onError
);
tx.executeSql(
'INSERT INTO Users (name, k, j) VALUES (?, ?, ?)',
['Tim Duncan', 1, 23.4],
nop,
onError
);
tx.executeSql(
'SELECT * FROM Users',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(1);
},
onError
);
},
reject,
resolve
);
});
}
});
t.it('should maintain correct type of potentialy null bind parameters', async () => {
const db = SQLite.openDatabase('test.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const nop = () => {};
const onError = (tx, error) => reject(error);
tx.executeSql('DROP TABLE IF EXISTS Nulling;', [], nop, onError);
tx.executeSql(
'CREATE TABLE IF NOT EXISTS Nulling (id INTEGER PRIMARY KEY NOT NULL, x NUMERIC, y NUMERIC)',
[],
nop,
onError
);
tx.executeSql('INSERT INTO Nulling (x, y) VALUES (?, ?)', [null, null], nop, onError);
tx.executeSql('INSERT INTO Nulling (x, y) VALUES (null, null)', [], nop, onError);
tx.executeSql(
'SELECT * FROM Nulling',
[],
(tx, results) => {
t.expect(results.rows.item(0).x).toBeNull();
t.expect(results.rows.item(0).y).toBeNull();
t.expect(results.rows.item(1).x).toBeNull();
t.expect(results.rows.item(1).y).toBeNull();
},
onError
);
},
reject,
resolve
);
});
if (Platform.OS !== 'web') {
const { exists } = await FS.getInfoAsync(`${FS.documentDirectory}SQLite/test.db`);
t.expect(exists).toBeTruthy();
}
});
// Do not try to test PRAGMA statements support in web
// as it is expected to not be working.
// See https://stackoverflow.com/a/10298712
if (Platform.OS !== 'web') {
t.it('should support PRAGMA statements', async () => {
const db = SQLite.openDatabase('test.db');
await new Promise((resolve, reject) => {
db.transaction(
tx => {
const nop = () => {};
const onError = (tx, error) => reject(error);
tx.executeSql('DROP TABLE IF EXISTS SomeTable;', [], nop, onError);
tx.executeSql(
'CREATE TABLE IF NOT EXISTS SomeTable (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(64));',
[],
nop,
onError
);
// a result-returning pragma
tx.executeSql(
'PRAGMA table_info(SomeTable);',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(2);
t.expect(results.rows.item(0).name).toEqual('id');
t.expect(results.rows.item(1).name).toEqual('name');
},
onError
);
// a no-result pragma
tx.executeSql('PRAGMA case_sensitive_like = true;', [], nop, onError);
// a setter/getter pragma
tx.executeSql('PRAGMA user_version = 123;', [], nop, onError);
tx.executeSql(
'PRAGMA user_version;',
[],
(tx, results) => {
t.expect(results.rows.length).toEqual(1);
t.expect(results.rows.item(0).user_version).toEqual(123);
},
onError
);
},
reject,
resolve
);
});
});
}
});
}