Skip to content

Commit

Permalink
Fixed key persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamblam committed Aug 29, 2017
1 parent 1a93922 commit 13fcde7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![enter image description here](http://i.imgur.com/VQlJKOc.png)
[![jSQL Logo](http://i.imgur.com/VQlJKOc.png)](http://pamblam.github.io/jSQL/)

jSQL - Version 2.0 - *Now gluten free!*
jSQL - Version 2.1 - *Now gluten free!*

<hr>

Expand Down
8 changes: 6 additions & 2 deletions examples/keys/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
// Create a non-compound key using high-level syntax
jSQL.query("create table if not exists myTable (ID int primary, Name varchar, FOOD varchar)").execute(data);

//jSQL.insertInto('myTable').values({ID:0, Name:'Nerd', FOOD: "Bagels"}).ignore().execute();
// Insert ignore low level syntax
// jSQL.insertInto('myTable').values({ID:0, Name:'Nerd', FOOD: "Bagels"}).ignore().execute();

jSQL.query("insert into myTable VALUES (0, 'nerd', 'bagel')").execute();
// This will the primary key, adding "ignore" prevents it from throwing an error
jSQL.query("insert ignore into myTable VALUES (0, 'nerd', 'bagel')").execute();

jSQL.commit();

console.log("done");
});
Expand Down
18 changes: 13 additions & 5 deletions jSQL.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* jSQL.js v2
* jSQL.js v2.1
* A Javascript Query Language Database Engine
* @author Robert Parham
* @website https://github.com/Pamblam/jSQL#jsql
* @website http://pamblam.github.io/jSQL/
* @license http://www.apache.org/licenses/LICENSE-2.0
*/

Expand Down Expand Up @@ -2085,14 +2085,21 @@
var rows = [];
for(var tbl in jSQL.tables){
if(!jSQL.tables.hasOwnProperty(tbl)) continue;

var keys = [];
for(var i=0; i<jSQL.tables[tbl].keys.unique.length; i++)
keys.push({column: jSQL.tables[tbl].keys.unique.column, type: "unique"});
if(jSQL.tables[tbl].keys.primary.column)
keys.push({column: jSQL.tables[tbl].keys.primary.column, type: "primary"});

var data = jSQL.select("*").from(tbl).execute().fetchAll();
for(var i=data.length; i--;){
var row = data[i];
for(var n in row){
if(!row.hasOwnProperty(n)) continue;
row[n] = jSQL.tables[tbl].normalizeColumnStoreValue(n, row[n]);
}
rows.push({table: tbl, data:JSON.stringify(row), colTypes: JSON.stringify(jSQL.tables[tbl].types)});
rows.push({table: tbl, data:JSON.stringify(row), colTypes: JSON.stringify(jSQL.tables[tbl].types), keys: JSON.stringify(keys)});
}
}
self.api.delete("jSQL_data_schema", function(){
Expand Down Expand Up @@ -2135,13 +2142,14 @@
var tablename = r[i].table;
var rowdata = JSON.parse(r[i].data);
var colTypes = JSON.parse(r[i].colTypes);
var keys = JSON.parse(r[i].keys);
// Create the table in memory if it doesn't exist yet
if(undefined === jSQL.tables[tablename]){
var cols = [];
for(var c in rowdata)
if(rowdata.hasOwnProperty(c))
cols.push(c);
jSQL.createTable(tablename, cols, colTypes).execute();
jSQL.createTable(tablename, cols, colTypes, keys).execute();
}

for(var c in rowdata){
Expand Down Expand Up @@ -2336,7 +2344,7 @@
////////////////////////////////////////////////////////////////////////////

return {
version: 2,
version: 2.1,
tables: {},
query: jSQLParseQuery,
createTable: createTable,
Expand Down
7 changes: 3 additions & 4 deletions jSQL.min.js

Large diffs are not rendered by default.

0 comments on commit 13fcde7

Please sign in to comment.