-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBHelper.hx
214 lines (168 loc) · 5.24 KB
/
DBHelper.hx
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
package ;
#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end
#if android
import openfl.utils.JNI;
#end
// http://www.tutorialspoint.com/android/android_sqlite_database.htm
class DBHelper
{
#if android
static inline var pkg = 'org.haxe.extension';
static var jni_arrayDemo : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"arrayDemo",
"(I[Ljava/lang/String;[Ljava/lang/String;)[[Ljava/lang/String;"
);
static var jni_insert : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"insert",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)I"
);
static var jni_update : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"update",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)I"
);
static var jni_delete : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"delete",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)I"
);
static var jni_loadOne : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"loadOne",
"(Ljava/lang/String;I)[[Ljava/lang/String;"
);
static var jni_getList : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"getList",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)[[Ljava/lang/String;"
);
static var jni_count : Dynamic = openfl.utils.JNI.createStaticMethod(
pkg + ".DBHelper",
"count",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)I"
);
static var dbhelper_insert = Lib.load("aptiekaextension", "dbhelper_insert", 3);
/*
* Before calling a Java object's method from JNI, we need its signature. For example, the method
long myMethod (int n, String s, int[] arr);
is seen from JNI with the signature
(ILJAVA/LANG/STRING;[I)J
*
*/
public static function arrayDemo(id:Int, keys:Array<String>, values:Array<String>)
{
return jni_arrayDemo(id, keys, values);
}
public static function insert(table:String, fields:Dynamic):Int
{
//var env:Dynamic = JNI.getEnv();
//env.PushLocalFrame(env, 10);
var keys:Array<String> = [];
var values:Array<String> = [];
for (k in Reflect.fields(fields))
{
keys.push(k);
values.push(''+Reflect.field(fields, k));
}
var result:Int = dbhelper_insert(table, keys, values);
//env.PopLocalFrame(env);
return result;
}
public static function loadOne(table:String, id:Int):Dynamic
{
var result:Dynamic = {};
var jni_result:Array<Array<String>> = jni_loadOne(table, id);
for (i in 0...jni_result[0].length)
{
var val:Dynamic = jni_result[1][i];
switch (jni_result[2][i])
{
case '1': val = Std.parseInt(val);
case '2': val = Std.parseFloat(val);
}
Reflect.setField(result, jni_result[0][i], val);
}
return result;
}
public static function update(table:String, fields:Dynamic, cond:Dynamic):Int
{
var keys:Array<String> = [];
var values:Array<String> = [];
for (k in Reflect.fields(fields))
{
keys.push(k);
values.push(''+Reflect.field(fields, k));
}
var cond_keys:Array<String> = [];
var cond_values:Array<String> = [];
for (k in Reflect.fields(cond))
{
cond_keys.push(k);
cond_values.push(''+Reflect.field(cond, k));
}
return jni_update(table, keys, values, cond_keys, cond_values);
}
public static function delete(table:String, cond:Dynamic):Int
{
var cond_keys:Array<String> = [];
var cond_values:Array<String> = [];
for (k in Reflect.fields(cond))
{
cond_keys.push(k);
cond_values.push(''+Reflect.field(cond, k));
}
return jni_delete(table, cond_keys, cond_values);
}
public static function getList(table:String, cond:Dynamic):Array<Dynamic>
{
var cond_keys:Array<String> = [];
var cond_values:Array<String> = [];
for (k in Reflect.fields(cond))
{
cond_keys.push(k);
cond_values.push(''+Reflect.field(cond, k));
}
var result = new Array<Dynamic>();
var jni_result:Array<Array<String>> = jni_getList(table, cond_keys, cond_values);
var cols = jni_result[0].length;
var j = 0;
while (j < jni_result[1].length)
{
var row:Dynamic = {};
for (i in 0...cols)
{
var val:Dynamic = jni_result[1][j + i];
switch (jni_result[2][i])
{
case '1': val = Std.parseInt(val);
case '2': val = Std.parseFloat(val);
}
Reflect.setField(row, jni_result[0][i], val);
}
result.push(row);
j += cols;
}
return result;
}
public static function count(table:String, ?cond:Dynamic):Int
{
var cond_keys:Array<String> = [];
var cond_values:Array<String> = [];
if (cond != null)
{
for (k in Reflect.fields(cond))
{
cond_keys.push(k);
cond_values.push(''+Reflect.field(cond, k));
}
}
return jni_count(table, cond_keys, cond_values);
}
#end
}