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+
11101110jSQLLexer . token_types = [
11111111
11121112 // STRINGs
@@ -1245,6 +1245,16 @@ jSQLLexer.token_types = [
12451245 { pattern : / s e t / gi,
12461246 type : 'KEYWORD' ,
12471247 name : "SET" } ,
1248+ { pattern : / n o t n u l l / gi,
1249+ type : 'KEYWORD' ,
1250+ name : "NOT NULL" } ,
1251+ { pattern : / n u l l / gi,
1252+ type : 'KEYWORD' ,
1253+ name : "NULL" } ,
1254+ { pattern : / d e f a u l t / gi,
1255+ type : 'KEYWORD' ,
1256+ name : "DEFAULT" } ,
1257+
12481258
12491259 // DIRECTIVEs
12501260 { pattern : / c r e a t e t a b l e / gi,
@@ -1275,6 +1285,8 @@ jSQLLexer.token_types = [
12751285 name : "UNQTD IDENTIFIER" }
12761286] ;
12771287
1288+
1289+
12781290function 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+
12981312function 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
27032729return {
2704- version : "3.0.2 " ,
2730+ version : "3.1.0 " ,
27052731 tables : { } ,
27062732 query : jSQLParseQuery ,
27072733 createTable : createTable ,
0 commit comments