-
Notifications
You must be signed in to change notification settings - Fork 13
/
SQLiteDb.uno
82 lines (70 loc) · 1.95 KB
/
SQLiteDb.uno
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
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Bolav.ForeignHelpers;
public class SQLiteDb : NativeModule
{
string filename;
extern (CIL) string db;
extern (iOS) IntPtr db;
extern (Android) Java.Object db;
extern (Uno) int db;
public SQLiteDb(string filename) {
this.filename = filename;
db = SQLiteImpl.OpenImpl(filename);
AddMember(new NativeFunction("close", (NativeCallback)Close));
AddMember(new NativeFunction("prepare", (NativeCallback)Prepare));
AddMember(new NativeFunction("execute", (NativeCallback)Execute));
AddMember(new NativeFunction("query", (NativeCallback)Query));
}
object Close(Context c, object[] args)
{
SQLiteImpl.CloseImpl(db);
return null;
}
object Prepare(Context c, object[] args) {
var statement = args[0] as string;
var mod = new SQLiteStatement(db, statement);
return mod.EvaluateExports(c, null);
}
object Execute(Context c, object[] args) {
var statement = args[0] as string;
var param_len = args.Length - 1;
string[] param = new string[param_len];
for (var i=0; i < param_len; i++) {
param[i] = args[i+1].ToString();
}
SQLiteImpl.ExecImpl(db, statement, param);
return null;
}
// TODO: QueryCursor
// TODO: QueryAsync
object Query(Context context, object[] args) {
var statement = args[0] as string;
var param_len = args.Length - 1;
string[] param = new string[param_len];
for (var j=0; j < param_len; j++) {
param[j] = args[j+1].ToString();
}
var jslist = new JSList(context);
if defined(!CIL) {
SQLiteImpl.QueryImpl(jslist, db, statement, param);
}
if defined(CIL) {
var result = SQLiteImpl.QueryImpl(db, statement, param);
int i = 0;
foreach (var row in result) {
var jsdict = jslist.NewDictRow();
foreach (var pair in row) {
string key = pair.Key as string;
string val = pair.Value as string;
jsdict.SetKeyVal(key,val);
}
i++;
}
}
return jslist.GetScriptingArray();
}
}