-
Notifications
You must be signed in to change notification settings - Fork 13
/
SQLite.uno
60 lines (53 loc) · 1.44 KB
/
SQLite.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
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;
using Bolav.ForeignHelpers;
// Version: 0.03
// https://github.com/bolav/fuse-sqlite
public class SQLite : NativeModule {
public SQLite()
{
AddMember(new NativeFunction("open", (NativeCallback)Open));
AddMember(new NativeFunction("openFromBundle", (NativeCallback)OpenFromBundle));
}
object Open (Context c, object[] args)
{
var filename = args[0] as string;
var filepath = Path.Combine(Directory.GetUserDirectory(UserDirectory.Data), filename);
var db = new SQLiteDb(filepath);
return db.EvaluateExports(c, null);
}
object OpenFromBundle (Context c, object[] args)
{
var filename = args[0] as string;
var filepath = Path.Combine(Directory.GetUserDirectory(UserDirectory.Data), filename);
if (File.Exists(filepath)) {
return Open(c,args);
}
BundleFile found = null;
foreach (var f in Uno.IO.Bundle.AllFiles) {
if (f.SourcePath == filename) {
found = f;
break;
}
}
if (found != null) {
// http://stackoverflow.com/questions/230128/how-do-i-copy-the-contents-of-one-stream-to-another
var input = found.OpenRead();
var output = File.OpenWrite(filepath);
byte[] buffer = new byte[1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, read);
}
input.Close();
output.Close();
}
else {
debug_log filename + " not found in bundle";
}
return Open(c,args);
}
}