Skip to content

Commit d2a5b6b

Browse files
authored
Merge pull request #10 from Pamblam/type-parameters
Type parameters
2 parents 75cdcee + 1589554 commit d2a5b6b

4 files changed

Lines changed: 159 additions & 40 deletions

File tree

README.md

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

3-
jSQL (Official) - Version 2.9 - *Now gluten free!*
3+
jSQL (Official) - Version 2.9.1 - *Now gluten free!*
4+
45
[![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)](http://inch-ci.org/github/Pamblam/jSQL)
56

67
<hr>
@@ -80,16 +81,18 @@ jSQL is fully documented in the [jSQL Wiki](https://github.com/Pamblam/jSQL/wiki
8081

8182
## Browser Support
8283

83-
Works in all major browsers, even really old ones.
84+
Works in basically all browsers. jSQL degrades gracefully because it falls back on cookies for persistence if localStorage, IndexedDB and WebSQL are not available. To that end, the library itself does not take advantage of any ES6 features, if you want something that does, have a look at [alaSQL](https://github.com/agershun/alasql).
85+
86+
While jSQL will work in basically all browsers, these ones are preferred:
8487

8588
| **FireFox** | **Android** | **Safari** | **Chrome** | **Samsung** | **Blackberry** | **IE** | **Opera** | **Edge** |
8689
|-------------|-------------|------------|------------|-------------|----------------|--------|-----------|----------|
8790
| 2+ | 2.1+ | 3.1+ | 4+ | 4+ | 7+ | 8+ | 11.5+ | 12+ |
8891

8992
<hr>
9093

91-
## License & Legal
94+
## It's Official
9295

93-
License info is available [here](https://github.com/Pamblam/jSQL/wiki/License).
96+
In the same way Fedex is Federal.
9497

9598
<hr>

jSQL.js

Lines changed: 121 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jSQL.js v2.9
2+
* jSQL.js v2.9.1
33
* A Javascript Query Language Database Engine
44
* @author Robert Parham
55
* @website http://pamblam.github.io/jSQL/
@@ -88,6 +88,8 @@
8888
case "0065": this.message = "AUTO_INCREMENT column must be a key."; break;
8989
case "0066": this.message = "AUTO_INCREMENT column must be an INT type."; break;
9090
case "0067": this.message = "API is out of memory, cannot store more data."; break;
91+
case "0068": this.message = "Invalid ENUM value."; break;
92+
case "0069": this.message = "NUMERIC or INT type invalid or out of range."; break;
9193
default: this.message = "Unknown error."; break;
9294
}
9395
this.toString = function () {
@@ -117,28 +119,101 @@
117119
aliases: ["NUMBER", "DECIMAL", "FLOAT"],
118120
serialize: function(value, args){
119121
return !isNaN(parseFloat(value)) && isFinite(value) ?
120-
parseFloat(value) : 0;
122+
parseFloat(value) :
123+
_throw(new jSQL_Error("0069")) ;
121124
},
122125
unserialize: function(value, args){
123126
return !isNaN(parseFloat(value)) && isFinite(value) ?
124-
parseFloat(value) : 0;
127+
parseFloat(value) :
128+
_throw(new jSQL_Error("0069")) ;
129+
}
130+
},{
131+
type: "ENUM",
132+
serialize: function(value, args){
133+
for(var i=args.length; i--;)
134+
if(value.toUpperCase() == removeQuotes(args[i]).toUpperCase()) return removeQuotes(args[i]);
135+
return _throw(new jSQL_Error("0068"));
136+
},
137+
unserialize: function(value, args){
138+
for(var i=args.length; i--;)
139+
if(value.toUpperCase() == removeQuotes(args[i]).toUpperCase()) return removeQuotes(args[i]);
140+
return _throw(new jSQL_Error("0068"));
141+
}
142+
},{
143+
type: "TINYINT",
144+
serialize: function(value, args){
145+
return !isNaN(parseInt(value)) && isFinite(value) &&
146+
value >= -128 && value <= 127 ?
147+
parseInt(value) : 0;
148+
},
149+
unserialize: function(value, args){
150+
return !isNaN(parseInt(value)) && isFinite(value) ?
151+
parseInt(value) : 0;
152+
}
153+
},{
154+
type: "SMALLINT",
155+
serialize: function(value, args){
156+
return !isNaN(parseInt(value)) && isFinite(value) &&
157+
value >= -32768 && value <= 32767 ?
158+
parseInt(value) :
159+
_throw(new jSQL_Error("0069")) ;
160+
},
161+
unserialize: function(value, args){
162+
return !isNaN(parseInt(value)) && isFinite(value) ?
163+
parseInt(value) :
164+
_throw(new jSQL_Error("0069")) ;
165+
}
166+
},{
167+
type: "MEDIUMINT",
168+
serialize: function(value, args){
169+
return !isNaN(parseInt(value)) && isFinite(value) &&
170+
value >= -8388608 && value <= 8388607 ?
171+
parseInt(value) :
172+
_throw(new jSQL_Error("0069")) ;
173+
},
174+
unserialize: function(value, args){
175+
return !isNaN(parseInt(value)) && isFinite(value) ?
176+
parseInt(value) :
177+
_throw(new jSQL_Error("0069")) ;
178+
}
179+
},{
180+
type: "INT",
181+
serialize: function(value, args){
182+
return !isNaN(parseInt(value)) && isFinite(value) &&
183+
value >= -2147483648 && value <= 2147483647 ?
184+
parseInt(value) : _throw(new jSQL_Error("0069"));
185+
},
186+
unserialize: function(value, args){
187+
return !isNaN(parseInt(value)) && isFinite(value) ?
188+
parseInt(value) : _throw(new jSQL_Error("0069"));
189+
}
190+
},{
191+
type: "BIGINT",
192+
serialize: function(value, args){
193+
return !isNaN(parseInt(value)) && isFinite(value) &&
194+
value >= -9007199254740991 && value <= 9007199254740991 ?
195+
parseInt(value) : _throw(new jSQL_Error("0069"));
196+
},
197+
unserialize: function(value, args){
198+
return !isNaN(parseInt(value)) && isFinite(value) ?
199+
parseInt(value) : _throw(new jSQL_Error("0069"));
125200
}
126201
},{
127202
type: "JSON",
128-
aliases: ["ARRAY","OBJECT"],
129-
serialize: function(value, args){
203+
aliases: ["ARRAY", "OBJECT"],
204+
serialize: function(value){
130205
return JSON.stringify(value);
131206
},
132-
unserialize: function(value, args){
207+
unserialize: function(value){
133208
return JSON.parse(value);
134209
}
135210
},{
136211
type: "FUNCTION",
137-
serialize: function(value, args){
212+
serialize: function(value){
138213
if(typeof value !== "function") value=function(){};
139214
return "jSQLFunct-"+value.toString();
140215
},
141-
unserialize: function(value, args){
216+
unserialize: function(value){
142217
var p = value.split("-");
143218
if(p.shift() !== "jSQLFunct") return _throw(new jSQL_Error("0001"));
144219
p = value.split("-");
@@ -153,41 +228,55 @@
153228
},{
154229
type: "BOOLEAN",
155230
aliases: ['BOOL'],
156-
serialize: function(value, args){ return !!value; },
157-
unserialize: function(value, args){ return !!value; }
231+
serialize: function(value){
232+
return value ? "1" : "0";
233+
},
234+
unserialize: function(value){
235+
return value == "1";
236+
}
158237
},{
159-
type: "INT",
238+
type: "CHAR",
160239
serialize: function(value, args){
161-
return !isNaN(parseInt(value)) && isFinite(value) ?
162-
parseInt(value) : 0;
240+
return ""+value;
163241
},
164242
unserialize: function(value, args){
165-
return !isNaN(parseInt(value)) && isFinite(value) ?
166-
parseInt(value) : 0;
243+
var targetLength = args[0]>>0, padString = ' ';
244+
if (value.length > targetLength) return value.substr(0, args[0]);
245+
else {
246+
targetLength = targetLength-value.length;
247+
if (targetLength > padString.length)
248+
padString += padString.repeat(targetLength/padString.length);
249+
return String(value) + padString.slice(0,targetLength);
250+
}
251+
return ""+value;
167252
}
168253
},{
169-
type: "CHAR",
170-
aliases: ["VARCHAR", "LONGTEXT", "MEDIUMTEXT"],
171-
serialize: function(value, args){ return ""+value; },
172-
unserialize: function(value, args){ return ""+value; }
254+
type: "VARCHAR",
255+
aliases: ["LONGTEXT", "MEDIUMTEXT"],
256+
serialize: function(value, args){
257+
return ""+value;
258+
},
259+
unserialize: function(value, args){
260+
return ""+value;
261+
}
173262
},{
174263
type: "DATE",
175-
serialize: function(value, args){
264+
serialize: function(value){
176265
if(!value instanceof Date) return new Date(0).getTime();
177266
return value.getTime();
178267
},
179-
unserialize: function(value, args){
268+
unserialize: function(value){
180269
return new Date(value);
181270
}
182271
},{
183272
type: "AMBI",
184-
serialize: function(value, args){
273+
serialize: function(value){
185274
if(value instanceof Date) return value.getTime();
186275
if(typeof value === "function") return "jSQLFunct-"+value.toString();
187276
if(!isNaN(parseFloat(value)) && isFinite(value)) return value;
188277
return ""+value;
189278
},
190-
unserialize: function(value, args){
279+
unserialize: function(value){
191280
if(typeof value === "string"){
192281
if(value.split("-")[0] === "jSQLFunct"){
193282
var p = value.split("-");
@@ -925,15 +1014,6 @@
9251014

9261015
function jSQLParseQuery(query){
9271016

928-
// Remove surrounding quotes from a string
929-
var removeQuotes = function(str){
930-
var quotes = ['"', "'", "`"];
931-
for (var i = quotes.length; i--; )
932-
if (str.substr(0, 1) == quotes[i] && str.substr(str.length - 1, 1) == quotes[i])
933-
return str.substr(1, str.length - 2);
934-
return str;
935-
};
936-
9371017
// Predcit the correct casing for column and table names
9381018
var convertToCol = function(c, skipErrors){
9391019
for(var i=0; i<jSQL.tables[table].columns.length; i++)
@@ -2487,7 +2567,6 @@
24872567
self.api.init([{name: "jSQL_data_schema", rows:[]}], successCallback);
24882568
APIIsSet = true;
24892569
}catch(ex){
2490-
console.log(ex);
24912570
APIIsSet = false;
24922571
}
24932572
if(!APIIsSet) loop(1+i);
@@ -2656,12 +2735,20 @@
26562735
.trim();
26572736
}
26582737

2738+
function removeQuotes(str){
2739+
var quotes = ['"', "'", "`"];
2740+
for (var i = quotes.length; i--; )
2741+
if (str.substr(0, 1) == quotes[i] && str.substr(str.length - 1, 1) == quotes[i])
2742+
return str.substr(1, str.length - 2);
2743+
return str;
2744+
}
2745+
26592746
////////////////////////////////////////////////////////////////////////////
26602747
// Exposed Methods /////////////////////////////////////////////////////////
26612748
////////////////////////////////////////////////////////////////////////////
26622749

26632750
return {
2664-
version: 2.9,
2751+
version: 2.91,
26652752
tables: {},
26662753
query: jSQLParseQuery,
26672754
createTable: createTable,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "jsql-official",
3-
"version": "2.9.0",
3+
"version": "2.9.1",
44
"description": "jSQL is a robust and persistent SQL engine written in Javascript for both Node and browser. See: pamblam.github.io/jSQL for details and usage.",
55
"main": "jSQL.min.js",
66
"directories": {
77
"example": "tests"
88
},
99
"scripts": {
10-
"test": "node ./tests/test1.js && node ./tests/test2.js && node ./tests/test3.js && node ./tests/test4.js && node ./tests/test5.js"
10+
"test": "node ./tests/test1.js && node ./tests/test2.js && node ./tests/test3.js && node ./tests/test4.js && node ./tests/test5.js && node ./tests/test6.js"
1111
},
1212
"repository": {
1313
"type": "git",

tests/test6.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* This script creates a table with every datatype, inserts a row then fetches it
3+
* @type Module jSQL|Module jSQL
4+
*/
5+
6+
var jSQL = require("../jSQL.js");
7+
8+
jSQL.load(function(){
9+
10+
jSQL.query("create table typetest ("+
11+
"id numeric(3), "+
12+
"data json, "+
13+
"greet function, "+
14+
"active boolean, "+
15+
"number int(3), "+
16+
"code char(4), "+
17+
"name varchar(4), "+
18+
"created date, "+
19+
"anything ambi, "+
20+
"something enum('hello','goodbye')"+
21+
" )").execute();
22+
23+
var sql = "insert into typetest values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
24+
params = [2.1, {"3":3}, function(){}, true, 4, 'y12', 'bob', new Date(), 'something', 'hello'];
25+
jSQL.query(sql).execute(params);
26+
27+
var data = jSQL.query("select * from typetest").execute().fetch();
28+
console.log("done 6");
29+
});

0 commit comments

Comments
 (0)