Skip to content

Commit d6252e6

Browse files
committed
final v2.0 commit
1 parent cb90e3c commit d6252e6

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
# jSQL
2-
v2
1+
![enter image description here](http://i.imgur.com/VQlJKOc.png)
2+
3+
jSQL - Version 2.0 - *Now gluten free!*
4+
5+
<hr>
36

47
jSQL is a state and data management tool as well as a robust SQL engine. For complete documentation, please see [the jSQL Wiki](https://github.com/Pamblam/jSQL/wiki).
58

69
Under the hood, jSQL has 3 layers:
710

8-
- **At the Lowest level**, jSQL automatically chooses the best method of storage to save state and interacts directly with it. This layer exposes a persistence method, [`jSQL.commit()`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqlpersist), which is called to serialize and store all data currently in the jSQL database on the user's hard drive. While the app is open and loaded in the browser, this data is serialized and stored within reach in the [`jSQL.tables`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqltables) object where the library is able to perform operations on it.
11+
- **At the Lowest level**, jSQL automatically chooses the best method of storage to save state and interacts directly with it. This layer exposes a persistence method, [`jSQL.commit()`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqlcommit), which is called to serialize and store all data currently in the jSQL database on the user's hard drive. While the app is open and loaded in the browser, this data is serialized and stored within reach in the [`jSQL.tables`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqltables) object where the library is able to perform operations on it.
912

1013
- **In the middle**, a set of methods are used to build [`jSQLQuery` objects](https://github.com/Pamblam/jSQL/wiki/jSQLquery-interface) which execute CRUD commands on the jSQL database and it's tables. *(See: [`jSQL.createTable()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqlcreatetableparams), [`jSQL.select()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqlselectcolumns), [`jSQL.insertInto()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqlinsertintotablename), [`jSQL.dropTable()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqldroptabletablename), [`jSQL.update()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqlupdatetablename), and [`jSQL.deleteFrom()`](https://github.com/Pamblam/jSQL/wiki/Querying-the-Database#jsqldeletefromtablename))*
1114

@@ -28,43 +31,29 @@ jSQL is implemented in a single JavaScript file. Save the [`jSQL.js`](https://gi
2831
When the database has loaded into memory, you'll want to make sure you have a table to work with. Any database operations that are to be made immediately when the app loads should be called from within the [`jSQL.load()`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqlloadonloadcallback) callback.
2932

3033
jSQL.load(function(){
31-
jSQL.query("create table if not exists users (name varchar(25), age int)").execute();
32-
33-
// alternatively, the low level syntax is...
34-
// jSQL.createTable({users: {name: {type:varchar, args: 25}, age: {type: int}}}).ifNotExists().execute();
34+
var sql = "create table if not exists users (name varchar(25), age int)";
35+
jSQL.query(sql).execute();
3536
});
3637

37-
38-
3938
#### Insert into table
4039

4140
At some point, you might want to put some data in that table.
4241

4342
jSQL.query("insert into users ('bob', 34)").execute();
4443

45-
For a performance boost, you can use the low level syntax instead:
46-
47-
jSQL.insertInto('users').values({name:'bob', age:34}).execute();
48-
4944
Prefer prepared statements? Just replace values with question marks and pass the values to the execute method in an array.
5045

5146
jSQL.query("insert into users (?, ?)").execute(['bob', 34]);
5247

53-
You can use prepared statements in low level syntax too:
54-
55-
jSQL.insertInto('users').values({name:'?', age:'?'}).execute(['bob',34]);
56-
5748
#### Select from table
5849

5950
Once you've got the data in there, you're probably going to want to get it back out.
6051

6152
var users = jSQL.query("select * from users where name like '%ob'").execute().fetchAll("ASSOC");
62-
63-
The low level select sytax is easy too:
6453

65-
var users = jSQL.select('*').from('users').where('name').like('%ob').execute().fetchAll("ASSOC");
54+
#### Persisting changes in the browser
6655

67-
When you've made changes or additions to the database, call [`jSQL.persist()`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqlpersist) to commit your changes.
56+
When you've made changes or additions to the database, call [`jSQL.commit()`](https://github.com/Pamblam/jSQL/wiki/Persistence-Management#jsqlcommit) to commit your changes.
6857

6958
For more information and to read about other update, delete and other operations, see the [jSQL Wiki](https://github.com/Pamblam/jSQL/wiki#jsql-docs).
7059

examples/keys/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
// Create a non-compound key using high-level syntax
3232
jSQL.query("create table if not exists myTable (ID int primary, Name varchar, FOOD varchar)").execute(data);
3333

34-
jSQL.insertInto('myTable').values({ID:0, Name:'Nerd', FOOD: "Bagels"}).ignore().execute();
34+
//jSQL.insertInto('myTable').values({ID:0, Name:'Nerd', FOOD: "Bagels"}).ignore().execute();
35+
36+
jSQL.query("insert into myTable VALUES (0, 'nerd', 'bagel')").execute();
3537

3638
console.log("done");
3739
});

jSQL.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -329,49 +329,41 @@
329329
if(self.keys.primary.column){
330330
var primary_key_columns = Array.isArray(self.keys.primary.column) ? self.keys.primary.column : [self.keys.primary.column];
331331
var pk_col, pk_vals = [];
332-
var violation;
333332
for(var pk=0; pk_col=primary_key_columns[pk]; pk++){
334333
var primary_index = self.colmap[pk_col];
335-
violation = false;
336334
if(null === row[primary_index]){
337-
if(ignore === true){
338-
violation = true;
339-
continue;
340-
}
335+
if(ignore === true) return;
341336
throw "Cannot insert a null value in a primary column";
342337
}
343338
pk_vals.push(row[primary_index]);
344339
}
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";
349344
}
345+
self.keys.primary.map[pk_vals] = self.data.length;
350346
}
351347

352348
// Check the unique keys, There may be multiple and they may be compound
353349
var ukey;
354350
for(var k=0; ukey=self.keys.unique[k]; k++){
355351
var key_columns = Array.isArray(ukey.column) ? ukey.column : [ukey.column];
356352
var col, vals = [];
357-
var violation;
358353
for(var uk=0; col=key_columns[uk]; uk++){
359354
var index = self.colmap[col];
360-
violation = false;
361355
if(null === row[index]){
362-
if(ignore === true){
363-
violation = true;
364-
continue;
365-
}
356+
if(ignore === true) return;
366357
throw "Cannot insert a null value in a unique column";
367358
}
368359
vals.push(row[index]);
369360
}
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";
374365
}
366+
self.keys.unique[k].map[vals] = self.data.length;
375367
}
376368

377369
self.data.push(row);
@@ -660,7 +652,7 @@
660652
this.data[i] = preparedVals.shift();
661653
}
662654
}
663-
jSQL.tables[this.table].insertRow(this.data, this.ignore);
655+
jSQL.tables[this.table].insertRow(this.data, this.ignoreFlag);
664656
return this;
665657
};
666658
this.ignore = function(){ this.ignoreFlag=true; return this; };
@@ -1221,10 +1213,16 @@
12211213

12221214
case "INSERT":
12231215

1224-
var table, cols=[], values = [];
1216+
var table, cols=[], values = [], ignore = false;
12251217

1218+
var into = words.shift().toUpperCase();
1219+
if(into === "IGNORE"){
1220+
ignore = true;
1221+
into = words.shift().toUpperCase();
1222+
}
1223+
12261224
// 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'";
12281226

12291227
// Next word should be the table name
12301228
table = removeQuotes(words.shift());
@@ -1277,8 +1275,8 @@
12771275
data[cols[i]] = values[i];
12781276
}
12791277

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;
12821280

12831281
break;
12841282
case "CREATE":

jSQL.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)