-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
SQLite.ts
117 lines (99 loc) · 3.08 KB
/
SQLite.ts
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
import './polyfillNextTick';
import customOpenDatabase from '@expo/websql/custom';
import { NativeModulesProxy } from '@unimodules/core';
import zipObject from 'lodash/zipObject';
import { Platform } from 'react-native';
import {
Query,
SQLiteCallback,
ResultSet,
ResultSetError,
WebSQLDatabase,
SQLiteFileInfo,
} from './SQLite.types';
const { ExponentSQLite } = NativeModulesProxy;
class SQLiteDatabase {
_name: string;
_key?: string;
_closed: boolean = false;
constructor({ name, key }: SQLiteFileInfo) {
this._name = name;
this._key = key;
}
exec(queries: Query[], readOnly: boolean, callback: SQLiteCallback): void {
if (this._closed) {
throw new Error(`The SQLite database is closed`);
}
ExponentSQLite.exec(this._name, this._key, queries.map(_serializeQuery), readOnly).then(
nativeResultSets => {
callback(null, nativeResultSets.map(_deserializeResultSet));
},
error => {
// TODO: make the native API consistently reject with an error, not a string or other type
callback(error instanceof Error ? error : new Error(error));
}
);
}
close() {
this._closed = true;
ExponentSQLite.close(this._name);
}
}
function _serializeQuery(query: Query): [string, unknown[]] {
return [query.sql, Platform.OS === 'android' ? query.args.map(_escapeBlob) : query.args];
}
function _deserializeResultSet(nativeResult): ResultSet | ResultSetError {
const [errorMessage, insertId, rowsAffected, columns, rows] = nativeResult;
// TODO: send more structured error information from the native module so we can better construct
// a SQLException object
if (errorMessage !== null) {
return { error: new Error(errorMessage) } as ResultSetError;
}
return {
insertId,
rowsAffected,
rows: rows.map(row => zipObject(columns, row)),
};
}
function _escapeBlob<T>(data: T): T {
if (typeof data === 'string') {
/* eslint-disable no-control-regex */
return data
.replace(/\u0002/g, '\u0002\u0002')
.replace(/\u0001/g, '\u0001\u0002')
.replace(/\u0000/g, '\u0001\u0001') as any;
/* eslint-enable no-control-regex */
} else {
return data;
}
}
const _openExpoSQLiteDatabase = customOpenDatabase(SQLiteDatabase);
function addExecMethod(db: any): WebSQLDatabase {
db.exec = (queries: Query[], readOnly: boolean, callback: SQLiteCallback): void => {
db._db.exec(queries, readOnly, callback);
};
return db;
}
export function openDatabase(
fileInfo: SQLiteFileInfo | string,
version: string = '1.0',
description: string,
size: number = 1,
callback?: (db: WebSQLDatabase) => void
): WebSQLDatabase {
let name: string;
let key: string | undefined;
if (typeof fileInfo === 'string') {
name = fileInfo;
key = undefined;
} else {
({ name, key } = fileInfo);
}
if (name === undefined) {
throw new TypeError(`The database name must not be undefined`);
}
description = description || name;
const db = _openExpoSQLiteDatabase({ name, key }, version, description, size, callback);
const dbWithExec = addExecMethod(db);
return dbWithExec;
}