|
329 | 329 | if(self.keys.primary.column){ |
330 | 330 | var primary_key_columns = Array.isArray(self.keys.primary.column) ? self.keys.primary.column : [self.keys.primary.column]; |
331 | 331 | var pk_col, pk_vals = []; |
332 | | - var violation; |
333 | 332 | for(var pk=0; pk_col=primary_key_columns[pk]; pk++){ |
334 | 333 | var primary_index = self.colmap[pk_col]; |
335 | | - violation = false; |
336 | 334 | if(null === row[primary_index]){ |
337 | | - if(ignore === true){ |
338 | | - violation = true; |
339 | | - continue; |
340 | | - } |
| 335 | + if(ignore === true) return; |
341 | 336 | throw "Cannot insert a null value in a primary column"; |
342 | 337 | } |
343 | 338 | pk_vals.push(row[primary_index]); |
344 | 339 | } |
345 | | - if(!violation){ |
346 | | - pk_vals = JSON.stringify(pk_vals); |
347 | | - if(self.keys.primary.map.hasOwnProperty(pk_vals)) throw "Primary key violated"; |
348 | | - self.keys.primary.map[pk_vals] = self.data.length; |
| 340 | + pk_vals = JSON.stringify(pk_vals); |
| 341 | + if(self.keys.primary.map.hasOwnProperty(pk_vals)){ |
| 342 | + if(ignore === true) return; |
| 343 | + throw "Primary key violated"; |
349 | 344 | } |
| 345 | + self.keys.primary.map[pk_vals] = self.data.length; |
350 | 346 | } |
351 | 347 |
|
352 | 348 | // Check the unique keys, There may be multiple and they may be compound |
353 | 349 | var ukey; |
354 | 350 | for(var k=0; ukey=self.keys.unique[k]; k++){ |
355 | 351 | var key_columns = Array.isArray(ukey.column) ? ukey.column : [ukey.column]; |
356 | 352 | var col, vals = []; |
357 | | - var violation; |
358 | 353 | for(var uk=0; col=key_columns[uk]; uk++){ |
359 | 354 | var index = self.colmap[col]; |
360 | | - violation = false; |
361 | 355 | if(null === row[index]){ |
362 | | - if(ignore === true){ |
363 | | - violation = true; |
364 | | - continue; |
365 | | - } |
| 356 | + if(ignore === true) return; |
366 | 357 | throw "Cannot insert a null value in a unique column"; |
367 | 358 | } |
368 | 359 | vals.push(row[index]); |
369 | 360 | } |
370 | | - if(!violation){ |
371 | | - vals = JSON.stringify(vals); |
372 | | - if(ukey.map.hasOwnProperty(vals)) throw "Unique key violated"; |
373 | | - self.keys.unique[k].map[vals] = self.data.length; |
| 361 | + vals = JSON.stringify(vals); |
| 362 | + if(ukey.map.hasOwnProperty(vals)){ |
| 363 | + if(ignore === true) return; |
| 364 | + throw "Unique key violated"; |
374 | 365 | } |
| 366 | + self.keys.unique[k].map[vals] = self.data.length; |
375 | 367 | } |
376 | 368 |
|
377 | 369 | self.data.push(row); |
|
660 | 652 | this.data[i] = preparedVals.shift(); |
661 | 653 | } |
662 | 654 | } |
663 | | - jSQL.tables[this.table].insertRow(this.data, this.ignore); |
| 655 | + jSQL.tables[this.table].insertRow(this.data, this.ignoreFlag); |
664 | 656 | return this; |
665 | 657 | }; |
666 | 658 | this.ignore = function(){ this.ignoreFlag=true; return this; }; |
|
1221 | 1213 |
|
1222 | 1214 | case "INSERT": |
1223 | 1215 |
|
1224 | | - var table, cols=[], values = []; |
| 1216 | + var table, cols=[], values = [], ignore = false; |
1225 | 1217 |
|
| 1218 | + var into = words.shift().toUpperCase(); |
| 1219 | + if(into === "IGNORE"){ |
| 1220 | + ignore = true; |
| 1221 | + into = words.shift().toUpperCase(); |
| 1222 | + } |
| 1223 | + |
1226 | 1224 | // Next Word should be "INTO" |
1227 | | - if(words.shift().toUpperCase() !== "INTO") throw "Unintelligible query. Expected 'INTO'"; |
| 1225 | + if(into !== "INTO") throw "Unintelligible query. Expected 'INTO'"; |
1228 | 1226 |
|
1229 | 1227 | // Next word should be the table name |
1230 | 1228 | table = removeQuotes(words.shift()); |
|
1277 | 1275 | data[cols[i]] = values[i]; |
1278 | 1276 | } |
1279 | 1277 |
|
1280 | | - //jSQL.tables[table].insertRow(data); |
1281 | | - return jSQL.insertInto(table).values(data); |
| 1278 | + var q = jSQL.insertInto(table).values(data); |
| 1279 | + return ignore ? q.ignore() : q; |
1282 | 1280 |
|
1283 | 1281 | break; |
1284 | 1282 | case "CREATE": |
|
0 commit comments