| 133 | |
| 134 | public async insert(options: DB.InsertDatabaseOptions) { |
| 135 | await this.beginTransaction(); |
| 136 | |
| 137 | const sql = `INSERT INTO ${options.table} (${options.columns.map(c => "'" + c + "'").join(",")}) VALUES (${options.values.join(",")})`; |
| 138 | |
| 139 | let subrc = 0; |
| 140 | let dbcnt = 0; |
| 141 | try { |
| 142 | if (this.trace === true) { |
| 143 | console.log(sql); |
| 144 | } |
| 145 | |
| 146 | this.sqlite!.exec(sql); |
| 147 | dbcnt = 1; |
| 148 | } catch (error) { |
| 149 | if (this.trace === true) { |
| 150 | console.dir(error); |
| 151 | } |
| 152 | // eg "UNIQUE constraint failed" errors |
| 153 | subrc = 4; |
| 154 | } |
| 155 | return {subrc, dbcnt}; |
| 156 | } |
| 157 | |
| 158 | // // https://www.sqlite.org/lang_select.html |
| 159 | public async select(options: DB.SelectDatabaseOptions) { |
| 160 | let res: undefined | QueryExecResult[] = undefined; |
| 161 | |
| 162 | options.select = options.select.replace(/ UP TO (\d+) ROWS(.*)/i, "$2 LIMIT $1"); |
| 163 | if (options.primaryKey) { |
| 164 | options.select = options.select.replace(/ ORDER BY PRIMARY KEY/i, " ORDER BY " + options.primaryKey.join(", ")); |
| 165 | } else { |
| 166 | options.select = options.select.replace(/ ORDER BY PRIMARY KEY/i, ""); |
| 167 | } |
| 168 | options.select = options.select.replace(/ ASCENDING/ig, " ASC"); |
| 169 | options.select = options.select.replace(/ DESCENDING/ig, " DESC"); |
| 170 | options.select = options.select.replace(/~/g, "."); |