This repository was archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathm2sql.monkey2
More file actions
246 lines (161 loc) · 5.05 KB
/
m2sql.monkey2
File metadata and controls
246 lines (161 loc) · 5.05 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
' Original code by scurty
' http://monkeycoder.co.nz/forums/topic/wip-database-lib/
' Updated by Hezkore
Namespace m2sql
#Import "<std>"
#Import "<sqlite>"
Using sqlite..
Using std..
Extern
Function SQLITE_STATIC:Void( Void Ptr )
Function SQLITE_TRANSIENT :Void( Void Ptr )
Public
Class Database
Field db:sqlite3 Ptr
Field Path:String
' If a database exists, load it, otherwise, create it
Function Load:Database( path:String )
Local nDB := New Database
nDB.Path = path
If Not sqlite3_open( nDB.Path, Varptr nDB.db ) = SQLITE_OK
Print "Failed to Open Database - " + sqlite3_errmsg( nDB.db )
nDB.Close()
Endif
Return nDB
End
' Closes the database
Method Close()
sqlite3_close( db )
End
' Prepare a statement
Method Prepare:Statement( data:String )
Local nS := New Statement( Self )
nS.data = data
If sqlite3_prepare_v2( db, data, -1, Varptr nS.stmt, Null ) = SQLITE_OK
Return nS
Else
Print "Failed to Prepare Query - " + sqlite3_errmsg( db )
sqlite3_finalize( nS.stmt )
Endif
Return Null
End
' Simple query
Method Query:JsonObject( data:String )
Local tmp := Prepare( data )
If tmp Then
Return tmp.Execute( True )
Endif
Return Null
End
Class Statement
Field Parent:Database
Field stmt:sqlite3_stmt Ptr
Field result:JsonObject
Field data:String
Operator To:String()
If Not result Then Return Null
Return result.ToString()
End
Property Data:String()
Return data
End
Property Result:JsonObject()
Return result
End
Method New( parent:Database )
Parent = parent
End
Method Reset()
If stmt Then sqlite3_reset( stmt )
End
Method Discard()
If stmt Then
sqlite3_finalize( stmt )
stmt = Null
Endif
End
' Bind Int
Method BindInt( index:Int, value:Int )
If Not sqlite3_bind_int( stmt, index, value ) = SQLITE_OK Then
Print "Failed to Bind Int - " + sqlite3_errmsg( Parent.db )
sqlite3_finalize( stmt )
stmt = Null
Endif
End
' Bind Double
Method BindDouble( index:Int, value:Double )
If Not sqlite3_bind_double( stmt, index, value ) = SQLITE_OK Then
Print "Failed to Bind Double - " + sqlite3_errmsg( Parent.db )
sqlite3_finalize( stmt )
stmt = Null
Endif
End
' Bind Null
Method BindNull( index:Int )
If Not sqlite3_bind_null( stmt, index ) = SQLITE_OK Then
Print "Failed to Bind Null - " + sqlite3_errmsg( Parent.db )
sqlite3_finalize( stmt )
stmt = Null
Endif
End
' Bind Text
Method BindText( index:Int, text:String )
If Not sqlite3_bind_text( stmt, index, text, -1, SQLITE_TRANSIENT ) = SQLITE_OK Then
Print "Failed to Bind Text - " + sqlite3_errmsg( Parent.db )
sqlite3_finalize( stmt )
stmt = Null
Endif
End
' Execute the statment and return JSON data
Method Execute:JsonObject( discard:Bool = False )
result = New JsonObject
Local colnum:Int = sqlite3_column_count( stmt ) ' Get Result Count
Local rownum:Int = 0
Repeat
If stmt And sqlite3_step( stmt ) = SQLITE_ROW ' If there's a Row
'Local print_string := "" ' Debug Output
' Create New Row in Json
Local row_id := String( rownum ) ' Get Row String ID
result[row_id] = New JsonObject ' Create New Row from String ID
Local newrow := result.GetObject( row_id ) ' Get New Row Ref
' Generate Rows
For Local i:Int = 0 Until colnum
Local typeint := sqlite3_column_type( stmt, i ) ' Get Type of Entry
Local colname := sqlite3_column_name( stmt, i ) ' Get Column Name
Select typeint ' Determine the Correct Data Type
Case 1 ' ADD INTEGER COLUMN
Local colval:Int = sqlite3_column_int( stmt, i )
newrow.SetNumber( colname, colval )
'print_string += "INTEGER:" + colname + " " + colval + " | "
Case 2 ' ADD FLOAT/REAL COLUMN
Local colval:Double = sqlite3_column_double( stmt, i )
newrow.SetNumber( colname, colval )
'print_string += "REAL:" + colname + " " + colval + " | "
Case 3 ' ADD STRING/TEXT COLUMN
Local colval := sqlite3_column_text( stmt, i )
newrow.SetString( colname, colval )
'print_string += "TEXT:" + colname + " " + colval + " | "
Case 4 ' ADD BLOB/ARRAY? COLUMN (Probably coming soon)
'Local colval := sqlite3_column_blob( res, i )
'print_string += "BLOB:" + colname + " " + colval + " | "
Default ' NOT AN ACCEPTABLE TYPE FOR NOW
Print "Unknown sqlite3 type - " + typeint
'print_string += "UNKNOWN TYPE"
End Select
Next
'Print print_string
rownum += 1
Else
Exit ' - Exit Read Row Loop
End
Forever
If discard Then
Discard() ' Discard
Else
Reset() ' Reset
Endif
If rownum <= 0 Then Return Null
Return result
End Method
End Class
End Class