Skip to content

Commit ad40088

Browse files
committed
issue #15, added tests, removed dead code
1 parent 59da3a8 commit ad40088

File tree

17 files changed

+364
-236
lines changed

17 files changed

+364
-236
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ package-lock.json
55
node_modules
66
coverage
77
.coveralls.yml
8+
browser_tests

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module.exports = function(grunt) {
2929
'src/query_types/jSQLUpdateQuery.js',
3030
'src/query_types/jSQLCreateQuery.js',
3131
'src/lexer/jSQLLexer.js',
32+
'src/lexer/token_types.js',
33+
'src/lexer/token.js',
3234
'src/parser/jSQLParseQuery.js',
3335
'src/parser/jSQLParseCreateTokens.js',
3436
'src/parser/jSQLParseInsertTokens.js',

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![jSQL Logo](http://i.imgur.com/VQlJKOc.png)](http://pamblam.github.io/jSQL/)
22

3-
jSQL (Official) - Version 3.0.2 - *Now available without a prescription!*
3+
jSQL (Official) - Version 3.1.0 - *Now available without a prescription!*
44

55
[![npm version](https://badge.fury.io/js/jsql-official.svg)](https://badge.fury.io/js/jsql-official) [![Build Status](https://travis-ci.org/Pamblam/jSQL.svg?branch=master)](https://travis-ci.org/Pamblam/jSQL) [![Inline docs](http://inch-ci.org/github/Pamblam/jSQL.svg?branch=master)](https://github.com/Pamblam/jSQL/wiki) [![Code Climate](https://codeclimate.com/github/Pamblam/jSQL/badges/gpa.svg)](https://codeclimate.com/github/Pamblam/jSQL) [![Coverage Status](https://coveralls.io/repos/github/Pamblam/jSQL/badge.svg?branch=master)](https://coveralls.io/github/Pamblam/jSQL?branch=master)
66

jSQL.js

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jsql-official - v3.0.2
2+
* jsql-official - v3.1.0
33
* A persistent SQL database.
44
* @author Rob Parham
55
* @website http://pamblam.github.io/jSQL/
@@ -19,14 +19,11 @@ function jSQL_Error(error_no) {
1919
/* istanbul ignore next */
2020
switch(error_no){
2121
case "0001": this.message = "Corrupted function stored in data."; break;
22-
case "0002": this.message = "Attempted to apply a non-function as an error handler."; break;
2322
case "0003": this.message = "Invalid datatype definition."; break;
2423
case "0004": this.message = "DataType must have a `type` property."; break;
2524
case "0005": this.message = "DataType must have a `serialize` function."; break;
2625
case "0006": this.message = "DataType must have an `unserialize` function."; break;
2726
case "0007": this.message = "Unsupported data type."; break;
28-
case "0008": this.message = "Invalid table types array."; break;
29-
case "0009": this.message = "Unable to convert columns to array."; break;
3027
case "0010": this.message = "Invalid constraint."; break;
3128
case "0011": this.message = "This table already has a primary key."; break;
3229
case "0012": this.message = "renameColumn expects and old column name and a new one, both must be strings."; break;
@@ -89,6 +86,7 @@ function jSQL_Error(error_no) {
8986
case "0069": this.message = "NUMERIC or INT type invalid or out of range."; break;
9087
case "0070": this.message = "Unknown Lexer Error."; break;
9188
case "0071": this.message = "Unknown Parser Error."; break;
89+
case "0072": this.message = "Inserting null into a non-null column."; break;
9290
default: this.message = "Unknown error."; break;
9391
}
9492
this.toString = function () {
@@ -407,7 +405,6 @@ function jSQLTable(name, columns, data, types, keys, auto_increment){
407405

408406
// If the types array does not exist, create it
409407
if(undefined === types) types = [];
410-
if(!Array.isArray(types)) return _throw(new jSQL_Error("0008"));
411408

412409
// If first param is array, no third param
413410
if(Array.isArray(columns) && undefined === data)
@@ -427,15 +424,13 @@ function jSQLTable(name, columns, data, types, keys, auto_increment){
427424
}
428425

429426
// At this point, columns should be an array
430-
// - Double check and save it to the object
431-
if(!Array.isArray(columns)) return _throw(new jSQL_Error("0009"));
432427
self.columns = columns;
433428

434429
// Fill any missing holes in the types array
435430
// with "ambi" which means it can be any type
436431
for(var i=0; i<columns.length; i++)
437432
self.types[i] = undefined === types[i] || undefined === types[i].type ?
438-
{type:"ambi", args:[]} : types[i];
433+
{type:"ambi", args:[], default: undefined, null: true} : types[i];
439434

440435
// Validate & normalize each type
441436
for(var i=self.types.length; i--;){
@@ -515,7 +510,7 @@ function jSQLTable(name, columns, data, types, keys, auto_increment){
515510

516511
self.addColumn = function(name, defaultVal, type){
517512
if(undefined === type || undefined === type.type)
518-
type = {type:"AMBI",args:[]};
513+
type = {type:"AMBI",args:[], null:true, default: undefined};
519514
type.type = type.type.toUpperCase();
520515
if(undefined === defaultVal) defaultVal = null;
521516
if('string' != typeof name) name = (function r(n){
@@ -648,6 +643,9 @@ function jSQLTable(name, columns, data, types, keys, auto_increment){
648643

649644
self.normalizeColumnStoreValue = function(colName, value){
650645
var type = self.types[self.colmap[colName]];
646+
if([false, undefined].indexOf(type.null) >-1 && value === null) return _throw(new jSQL_Error("0072"));
647+
648+
if(null === value && type.default !== undefined) value = type.default;
651649
var storeVal = jSQL.types.getByType(type.type.toUpperCase()).serialize(value, type.args);
652650
if((!isNaN(parseFloat(storeVal)) && isFinite(storeVal)) || typeof storeVal === "string")
653651
return storeVal;
@@ -1107,6 +1105,8 @@ jSQLLexer.prototype.getTokens = function(){
11071105
return this.tokens;
11081106
};
11091107

1108+
1109+
11101110
jSQLLexer.token_types = [
11111111

11121112
// STRINGs
@@ -1245,6 +1245,16 @@ jSQLLexer.token_types = [
12451245
{pattern: /set/gi,
12461246
type: 'KEYWORD',
12471247
name: "SET"},
1248+
{pattern: /not null/gi,
1249+
type: 'KEYWORD',
1250+
name: "NOT NULL"},
1251+
{pattern: /null/gi,
1252+
type: 'KEYWORD',
1253+
name: "NULL"},
1254+
{pattern: /default/gi,
1255+
type: 'KEYWORD',
1256+
name: "DEFAULT"},
1257+
12481258

12491259
// DIRECTIVEs
12501260
{pattern: /create table/gi,
@@ -1275,6 +1285,8 @@ jSQLLexer.token_types = [
12751285
name: "UNQTD IDENTIFIER"}
12761286
];
12771287

1288+
1289+
12781290
function jSQLToken(pos, literal, tok_index){
12791291
this.type_id = tok_index;
12801292
this.input_pos = pos;
@@ -1293,11 +1305,13 @@ function jSQLToken(pos, literal, tok_index){
12931305
if(this.type === "STRING" && this.name === "SQ STRING")
12941306
this.value = literal.substr(1, literal.length - 2).replace(/\'/g, "'");
12951307
if(this.type === "NUMBER") this.value = parseFloat(this.literal);
1308+
if(this.type === "KEYWORD" && this.name === "NULL") this.value = null;
12961309
}
12971310

1311+
12981312
function jSQLParseQuery(query){
12991313

1300-
var tokens = new jSQLLexer(query).getTokens();
1314+
var tokens = jSQL.tokenize(query);
13011315

13021316
if(!tokens || !Array.isArray(tokens) || !tokens.length)
13031317
return _throw(new jSQL_Error("0041"));
@@ -1405,7 +1419,7 @@ function jSQLParseCreateTokens(tokens){
14051419
if(token.type === "IDENTIFIER") var col_name = token.value;
14061420
else return _throw(new jSQL_Parse_Error(token, "COLUMN NAME"));
14071421

1408-
var column = {name: col_name, type:"AMBI", args:[]};
1422+
var column = {name: col_name, type:"AMBI", args:[], null: true, default: undefined};
14091423

14101424
token = tokens.shift();
14111425

@@ -1427,16 +1441,26 @@ function jSQLParseCreateTokens(tokens){
14271441
}
14281442
token = tokens.shift();
14291443
}
1430-
1431-
if(token.type === "KEYWORD" && token.name === "AUTO_INCREMENT"){
1432-
column.auto_increment = true;
1433-
token = tokens.shift();
1434-
}
1435-
1436-
if(token.type === "KEYWORD" && (token.name === "UNIQUE KEY" || token.name === "PRIMARY KEY")){
1437-
keys.push({column: col_name, type: token.name.split(" ")[0].toLowerCase()});
1438-
token = tokens.shift();
1439-
}
1444+
}
1445+
1446+
if(token.type === "KEYWORD" && (token.name === "NULL" || token.name === "NOT NULL")){
1447+
column.null = token.name === "NULL";
1448+
token = tokens.shift();
1449+
}
1450+
1451+
if(token.type === "KEYWORD" && token.name === "DEFAULT"){
1452+
column.default = tokens.shift().value;
1453+
token = tokens.shift();
1454+
}
1455+
1456+
if(token.type === "KEYWORD" && token.name === "AUTO_INCREMENT"){
1457+
column.auto_increment = true;
1458+
token = tokens.shift();
1459+
}
1460+
1461+
if(token.type === "KEYWORD" && (token.name === "UNIQUE KEY" || token.name === "PRIMARY KEY")){
1462+
keys.push({column: col_name, type: token.name.split(" ")[0].toLowerCase()});
1463+
token = tokens.shift();
14401464
}
14411465

14421466
params[table_name].push(column);
@@ -2628,7 +2652,9 @@ function createTable(name, columnsOrData, types, keys, auto_increment){
26282652
columnsOrData.push(col);
26292653
types.push({
26302654
type: columnDefs[n].type,
2631-
args: (undefined===columnDefs[n].args ? []:columnDefs[n].args)
2655+
args: (undefined===columnDefs[n].args ? []:columnDefs[n].args),
2656+
default: (undefined===columnDefs[n].default?undefined:columnDefs[n].default),
2657+
null: (undefined===columnDefs[n].null?true:columnDefs[n].null),
26322658
});
26332659
// if a key is defined in the row column (only for single column keys)
26342660
if(columnDefs[n].hasOwnProperty("key") && Array.isArray(keys)){
@@ -2701,7 +2727,7 @@ function removeQuotes(str){
27012727
}
27022728

27032729
return {
2704-
version: "3.0.2",
2730+
version: "3.1.0",
27052731
tables: {},
27062732
query: jSQLParseQuery,
27072733
createTable: createTable,

jSQL.min.js

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsql-official",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "A persistent SQL database.",
55
"main": "jSQL.min.js",
66
"directories": {

src/error_handling/jSQL_Error.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ function jSQL_Error(error_no) {
77
/* istanbul ignore next */
88
switch(error_no){
99
case "0001": this.message = "Corrupted function stored in data."; break;
10-
case "0002": this.message = "Attempted to apply a non-function as an error handler."; break;
1110
case "0003": this.message = "Invalid datatype definition."; break;
1211
case "0004": this.message = "DataType must have a `type` property."; break;
1312
case "0005": this.message = "DataType must have a `serialize` function."; break;
1413
case "0006": this.message = "DataType must have an `unserialize` function."; break;
1514
case "0007": this.message = "Unsupported data type."; break;
16-
case "0008": this.message = "Invalid table types array."; break;
17-
case "0009": this.message = "Unable to convert columns to array."; break;
1815
case "0010": this.message = "Invalid constraint."; break;
1916
case "0011": this.message = "This table already has a primary key."; break;
2017
case "0012": this.message = "renameColumn expects and old column name and a new one, both must be strings."; break;
@@ -77,6 +74,7 @@ function jSQL_Error(error_no) {
7774
case "0069": this.message = "NUMERIC or INT type invalid or out of range."; break;
7875
case "0070": this.message = "Unknown Lexer Error."; break;
7976
case "0071": this.message = "Unknown Parser Error."; break;
77+
case "0072": this.message = "Inserting null into a non-null column."; break;
8078
default: this.message = "Unknown error."; break;
8179
}
8280
this.toString = function () {

0 commit comments

Comments
 (0)