forked from ArGeoph/react-native-sqlite-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.js
85 lines (78 loc) · 2.75 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
/*
* sqlite.js
*
* Created by Andrzej Porebski on 10/29/15.
* Copyright (c) 2015-16 Andrzej Porebski.
*
* This library is available under the terms of the MIT License (2008).
* See http://opensource.org/licenses/alphabetical for full text.
*/
var plugin = require('./lib/sqlite.core.js');
var {SQLiteFactory} = plugin;
var config = [
// meaning: [returnValueExpected,prototype,fn,argsNeedPadding,reverseCallbacks,rejectOnError]
[false,"SQLitePlugin","transaction",false,true,true],
[false,"SQLitePlugin","readTransaction",false,true,true],
[false,"SQLitePlugin","close",false,false,true],
[false,"SQLitePlugin","executeSql",true,false,true],
[false,"SQLitePlugin","sqlBatch",false,false,true],
[false,"SQLitePlugin","attach",true,false,true],
[false,"SQLitePlugin","detach",false,false,true],
[false,"SQLitePluginTransaction","executeSql",true,false,false],
[false,"SQLiteFactory","deleteDatabase",false,false,true],
[true, "SQLiteFactory","openDatabase",false,false,true],
[false,"SQLiteFactory","echoTest",false,false,true]
];
var originalFns = {};
config.forEach(entry => {
let [returnValueExpected,prototype,fn]= entry;
let originalFn = plugin[prototype].prototype[fn];
originalFns[prototype + "." + fn] = originalFn;
});
function enablePromiseRuntime(enable){
if (enable){
createPromiseRuntime();
} else {
createCallbackRuntime();
}
}
function createCallbackRuntime() {
config.forEach(entry => {
let [returnValueExpected,prototype,fn,argsNeedPadding,reverseCallbacks,rejectOnError]= entry;
plugin[prototype].prototype[fn] = originalFns[prototype + "." + fn];
});
plugin.log("Callback based runtime ready");
}
function createPromiseRuntime() {
config.forEach(entry => {
let [returnValueExpected,prototype,fn,argsNeedPadding,reverseCallbacks,rejectOnError]= entry;
let originalFn = plugin[prototype].prototype[fn];
plugin[prototype].prototype[fn] = function(...args){
if (argsNeedPadding && args.length == 1){
args.push([]);
}
var promise = new Promise((resolve,reject) => {
let success = function(...args){
if (!returnValueExpected) {
return resolve(args);
}
};
let error = function(err){
plugin.log('error: ',fn,...args,arguments);
if (rejectOnError) {
reject(err);
}
return false;
};
var retValue = originalFn.call(this,...args,reverseCallbacks ? error : success, reverseCallbacks ? success : error);
if (returnValueExpected){
return resolve(retValue);
}
});
return promise;
}
});
plugin.log("Promise based runtime ready");
}
SQLiteFactory.prototype.enablePromise = enablePromiseRuntime;
module.exports = new SQLiteFactory();