|
| 1 | +apiTemp = Runtime.stackAlloc(4); |
| 2 | + |
| 3 | +SQLite = { |
| 4 | + # Constants are defined below |
| 5 | + errorMessages : [] # Will contain all SQLite error descriptions sorted by error codes |
| 6 | +}; |
| 7 | + |
| 8 | +dataTemp = [] |
| 9 | +callbackTemp = Runtime.addFunction (notUsed, argc, argv, colNames) -> |
| 10 | + curresult = if (dataTemp.length==0) then null else dataTemp[dataTemp.length-1] |
| 11 | + isNewResult = (curresult is null or argc isnt curresult.columns.length); |
| 12 | + curvalues = [] |
| 13 | + curcolumns = [] |
| 14 | + |
| 15 | + for i in [0..argc] |
| 16 | + column = Pointer_stringify getValue colNames+i*Runtime.QUANTUM_SIZE, 'i32' |
| 17 | + value = Pointer_stringify getValue argv+i*Runtime.QUANTUM_SIZE, 'i32' |
| 18 | + curvalues.push value |
| 19 | + curcolumns.push column |
| 20 | + if not isNewResult and column isnt curresult.columns[i] then isNewResult = true; |
| 21 | + |
| 22 | + if isNewResult |
| 23 | + dataTemp.push { |
| 24 | + 'columns' : curcolumns |
| 25 | + 'values' : [curvalues] |
| 26 | + } |
| 27 | + else |
| 28 | + curresult.values.push(curvalues); |
| 29 | + |
| 30 | +class Statement |
| 31 | + constructor: (@stmt) -> |
| 32 | + @pos = 1 # Index of the leftmost parameter is 1 |
| 33 | + step: -> |
| 34 | + @pos = 1 |
| 35 | + ret = sqlite3_step @stmt |
| 36 | + if ret is SQLite.ROW then return true |
| 37 | + else if ret is SQLite.DONE then return false |
| 38 | + else throw 'SQLite error: ' + handleErrors ret |
| 39 | + getNumber: (pos = @pos++) -> sqlite3_column_double @stmt, pos |
| 40 | + getString: (pos = @pos++) -> sqlite3_column_text @stmt, pos |
| 41 | + get: -> # Get all fields |
| 42 | + for field in [0 ... sqlite3_data_count(@stmt)] |
| 43 | + type = sqlite3_column_type @stmt, field |
| 44 | + if type in [SQLite.INTEGER, SQLite.FLOAT] then @getNumber field |
| 45 | + else if type in [SQLite.TEXT, SQLite.BLOB] then @getString field |
| 46 | + else null |
| 47 | + bindString: (string, pos = @pos++) -> |
| 48 | + ret = sqlite3_bind_text @stmt, pos, string, -1, NULL |
| 49 | + err = handleErrors ret |
| 50 | + if err isnt null then throw 'SQLite error : ' + err |
| 51 | + bindNumber: (num, pos = @pos++) -> |
| 52 | + ret = sqlite3_bind_double @stmt, pos, num |
| 53 | + err = handleErrors ret |
| 54 | + if err isnt null then throw 'SQLite error : ' + err |
| 55 | + bindValue: (val, pos = @pos++) -> |
| 56 | + switch typeof val |
| 57 | + when "string" then @bindString val, pos |
| 58 | + when "number" then @bindNumber val, pos |
| 59 | + # Not binding a parameter is the same as binding it to NULL |
| 60 | + bind : (values) -> |
| 61 | + @bindValue v,i+1 for v,i in values # Index of the leftmost parameter is 1 |
| 62 | + null |
| 63 | + reset : -> sqlite3_reset @stmt |
| 64 | + free: -> sqlite3_finalize @stmt |
| 65 | + |
| 66 | +class Database |
| 67 | + # Open a new database: |
| 68 | + #create a new one or open an existing database stored in the byte array passed in first argument |
| 69 | + constructor: (data) -> |
| 70 | + @filename = 'dbfile_' + (0xffffffff*Math.random()>>>0) |
| 71 | + if data? then FS.createDataFile '/', @filename, data, true, true |
| 72 | + ret = sqlite3_open @filename, apiTemp |
| 73 | + if ret isnt SQLite.OK then throw 'SQLite error: ' + SQLite.errorMessages[ret] |
| 74 | + @db = getValue(apiTemp, 'i32') |
| 75 | + |
| 76 | + # Close the database |
| 77 | + close: -> |
| 78 | + ret = sqlite3_close @db |
| 79 | + if ret isnt 0 then throw 'SQLite error: ' + SQLite_codes[ret].msg |
| 80 | + @db = null |
| 81 | + |
| 82 | + # Execute an SQL query, and returns the result |
| 83 | + exec: (sql) -> |
| 84 | + if not @db then throw "Database closed" |
| 85 | + dataTemp = [] |
| 86 | + setValue apiTemp, 0, 'i32' |
| 87 | + ret = sqlite3_exec @db, sql, callbackTemp, 0, apiTemp |
| 88 | + err = handleErrors ret, apiTemp |
| 89 | + if err isnt null then throw 'SQLite error : ' + err |
| 90 | + return dataTemp |
| 91 | + |
| 92 | + # Prepare an SQL statement |
| 93 | + prepare: (sql) -> |
| 94 | + setValue apiTemp, 0, 'i32' |
| 95 | + ret = sqlite3_prepare_v2 @db, sql, -1, apiTemp, NULL |
| 96 | + err = handleErrors ret, NULL |
| 97 | + if err isnt null then throw 'SQLite error: ' + err |
| 98 | + pStmt = getValue apiTemp, 'i32' # pointer to a statement, or null |
| 99 | + if pStmt is NULL then throw 'Nothing to prepare' |
| 100 | + return new Statement(pStmt) |
| 101 | + |
| 102 | + # Exports the contents of the database to a binary array |
| 103 | + export: -> new Uint8Array FS.root.contents[@filename].contents |
| 104 | + |
| 105 | +handleErrors = (ret, errPtrPtr) -> |
| 106 | + if not errPtrPtr |
| 107 | + return if ret is SQLite.OK then null else SQLite.errorMessages[ret] |
| 108 | + else |
| 109 | + errPtr = getValue errPtrPtr, 'i32' |
| 110 | + if errPtr isnt NULL and errPtr isnt undefined |
| 111 | + msg = Pointer_stringify errPtr |
| 112 | + sqlite3_free errPtr |
| 113 | + return msg |
| 114 | + else return null |
| 115 | + |
| 116 | +sqlite3_open = Module.cwrap 'sqlite3_open', 'number', ['string', 'number'] |
| 117 | +sqlite3_close = Module.cwrap 'sqlite3_close', 'number', ['number']; |
| 118 | +sqlite3_exec = Module.cwrap 'sqlite3_exec', 'number', ['number', 'string', 'number', 'number', 'number'] |
| 119 | +sqlite3_free = Module.cwrap 'sqlite3_free', '', ['number'] |
| 120 | + |
| 121 | +# Prepared statements |
| 122 | +## prepare |
| 123 | +sqlite3_prepare_v2 = Module.cwrap 'sqlite3_prepare_v2', 'number', ['number', 'string', 'number', 'number', 'number'] |
| 124 | +## Bind parameters |
| 125 | +#int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); |
| 126 | +sqlite3_bind_text = Module.cwrap 'sqlite3_bind_text', 'number', ['number', 'number', 'string', 'number', 'number'] |
| 127 | +#int sqlite3_bind_double(sqlite3_stmt*, int, double); |
| 128 | +sqlite3_bind_double = Module.cwrap 'sqlite3_bind_double', 'number', ['number', 'number', 'number'] |
| 129 | + |
| 130 | +## Get values |
| 131 | +# int sqlite3_step(sqlite3_stmt*) |
| 132 | +sqlite3_step = Module.cwrap 'sqlite3_step', 'number', ['number'] |
| 133 | +# int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 134 | +sqlite3_data_count = Module.cwrap 'sqlite3_data_count', 'number', ['number'] |
| 135 | +sqlite3_column_double = Module.cwrap 'sqlite3_column_double', 'number', ['number', 'number'] |
| 136 | +sqlite3_column_text = Module.cwrap 'sqlite3_column_text', 'string', ['number', 'number'] |
| 137 | +sqlite3_column_type = Module.cwrap 'sqlite3_column_type', 'number', ['number', 'number'] |
| 138 | +# int sqlite3_reset(sqlite3_stmt *pStmt); |
| 139 | +sqlite3_reset = Module.cwrap 'sqlite3_reset', 'number', ['number'] |
| 140 | +# int sqlite3_finalize(sqlite3_stmt *pStmt); |
| 141 | +sqlite3_finalize = Module.cwrap 'sqlite3_finalize', 'number', ['number'] |
| 142 | + |
| 143 | +# Global constants |
| 144 | +NULL = 0 # Null pointer |
| 145 | + |
| 146 | +SQLite.OK=0 |
| 147 | +SQLite.errorMessages[0]="Successful result" |
| 148 | +SQLite.ERROR=1 |
| 149 | +SQLite.errorMessages[1]="SQL error or missing database" |
| 150 | +SQLite.INTERNAL=2 |
| 151 | +SQLite.errorMessages[2]="Internal logic error in SQLite" |
| 152 | +SQLite.PERM=3 |
| 153 | +SQLite.errorMessages[3]="Access permission denied" |
| 154 | +SQLite.ABORT=4 |
| 155 | +SQLite.errorMessages[4]="Callback routine requested an abort" |
| 156 | +SQLite.BUSY=5 |
| 157 | +SQLite.errorMessages[5]="The database file is locked" |
| 158 | +SQLite.LOCKED=6 |
| 159 | +SQLite.errorMessages[6]="A table in the database is locked" |
| 160 | +SQLite.NOMEM=7 |
| 161 | +SQLite.errorMessages[7]="A malloc() failed" |
| 162 | +SQLite.READONLY=8 |
| 163 | +SQLite.errorMessages[8]="Attempt to write a readonly database" |
| 164 | +SQLite.INTERRUPT=9 |
| 165 | +SQLite.errorMessages[9]="Operation terminated by sqlite3_interrupt()" |
| 166 | +SQLite.IOERR=10 |
| 167 | +SQLite.errorMessages[10]="Some kind of disk I/O error occurred" |
| 168 | +SQLite.CORRUPT=11 |
| 169 | +SQLite.errorMessages[11]="The database disk image is malformed" |
| 170 | +SQLite.NOTFOUND=12 |
| 171 | +SQLite.errorMessages[12]="Unknown opcode in sqlite3_file_control()" |
| 172 | +SQLite.FULL=13 |
| 173 | +SQLite.errorMessages[13]="Insertion failed because database is full" |
| 174 | +SQLite.CANTOPEN=14 |
| 175 | +SQLite.errorMessages[14]="Unable to open the database file" |
| 176 | +SQLite.PROTOCOL=15 |
| 177 | +SQLite.errorMessages[15]="Database lock protocol error" |
| 178 | +SQLite.EMPTY=16 |
| 179 | +SQLite.errorMessages[16]="Database is empty" |
| 180 | +SQLite.SCHEMA=17 |
| 181 | +SQLite.errorMessages[17]="The database schema changed" |
| 182 | +SQLite.TOOBIG=18 |
| 183 | +SQLite.errorMessages[18]="String or BLOB exceeds size limit" |
| 184 | +SQLite.CONSTRAINT=19 |
| 185 | +SQLite.errorMessages[19]="Abort due to constraint violation" |
| 186 | +SQLite.MISMATCH=20 |
| 187 | +SQLite.errorMessages[20]="Data type mismatch" |
| 188 | +SQLite.MISUSE=21 |
| 189 | +SQLite.errorMessages[21]="Library used incorrectly" |
| 190 | +SQLite.NOLFS=22 |
| 191 | +SQLite.errorMessages[22]="Uses OS features not supported on host" |
| 192 | +SQLite.AUTH=23 |
| 193 | +SQLite.errorMessages[23]="Authorization denied" |
| 194 | +SQLite.FORMAT=24 |
| 195 | +SQLite.errorMessages[24]="Auxiliary database format error" |
| 196 | +SQLite.RANGE=25 |
| 197 | +SQLite.errorMessages[25]="2nd parameter to sqlite3_bind out of range" |
| 198 | +SQLite.NOTADB=26 |
| 199 | +SQLite.errorMessages[26]="File opened that is not a database file" |
| 200 | +SQLite.NOTICE=27 |
| 201 | +SQLite.errorMessages[27]="Notifications from sqlite3_log()" |
| 202 | +SQLite.WARNING=28 |
| 203 | +SQLite.errorMessages[28]="Warnings from sqlite3_log()" |
| 204 | +SQLite.ROW=100 |
| 205 | +SQLite.errorMessages[100]="sqlite3_step() has another row ready" |
| 206 | +SQLite.DONE=101 |
| 207 | +SQLite.errorMessages[101]="sqlite3_step() has finished executing" |
| 208 | + |
| 209 | +# Data types |
| 210 | +SQLite.INTEGER=1 |
| 211 | +SQLite.FLOAT=2 |
| 212 | +SQLite.TEXT=3 |
| 213 | +SQLite.BLOB=4 |
| 214 | +SQLite.NULL=5 |
| 215 | + |
| 216 | + |
| 217 | +# Export the API |
| 218 | +this['SQL'] = {'Database':Database} |
| 219 | +Module[i] = this['SQL'][i] for i of this['SQL'] |
0 commit comments