Skip to content

Commit fae97e1

Browse files
committed
finished all types except enum
1 parent 75cdcee commit fae97e1

3 files changed

Lines changed: 145 additions & 32 deletions

File tree

jSQL.js

Lines changed: 113 additions & 31 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/
@@ -123,22 +123,89 @@
123123
return !isNaN(parseFloat(value)) && isFinite(value) ?
124124
parseFloat(value) : 0;
125125
}
126+
},{
127+
type: "ENUM",
128+
serialize: function(value, args){ return "tits";
129+
for(var i=args.length; i--;)
130+
if(value == removeQuotes(args[i])) return removeQuotes(args[i]);
131+
return removeQuotes(args[0]);
132+
},
133+
unserialize: function(value, args){ return "tits";
134+
for(var i=args.length; i--;)
135+
if(value == removeQuotes(args[i])) return removeQuotes(args[i]);
136+
return removeQuotes(args[0]);
137+
}
138+
},{
139+
type: "TINYINT",
140+
serialize: function(value, args){
141+
return !isNaN(parseInt(value)) && isFinite(value) &&
142+
value >= -128 && value <= 127 ?
143+
parseInt(value) : 0;
144+
},
145+
unserialize: function(value, args){
146+
return !isNaN(parseInt(value)) && isFinite(value) ?
147+
parseInt(value) : 0;
148+
}
149+
},{
150+
type: "SMALLINT",
151+
serialize: function(value, args){
152+
return !isNaN(parseInt(value)) && isFinite(value) &&
153+
value >= -32768 && value <= 32767 ?
154+
parseInt(value) : 0;
155+
},
156+
unserialize: function(value, args){
157+
return !isNaN(parseInt(value)) && isFinite(value) ?
158+
parseInt(value) : 0;
159+
}
160+
},{
161+
type: "MEDIUMINT",
162+
serialize: function(value, args){
163+
return !isNaN(parseInt(value)) && isFinite(value) &&
164+
value >= -8388608 && value <= 8388607 ?
165+
parseInt(value) : 0;
166+
},
167+
unserialize: function(value, args){
168+
return !isNaN(parseInt(value)) && isFinite(value) ?
169+
parseInt(value) : 0;
170+
}
171+
},{
172+
type: "INT",
173+
serialize: function(value, args){
174+
return !isNaN(parseInt(value)) && isFinite(value) &&
175+
value >= -2147483648 && value <= 2147483647 ?
176+
parseInt(value) : 0;
177+
},
178+
unserialize: function(value, args){
179+
return !isNaN(parseInt(value)) && isFinite(value) ?
180+
parseInt(value) : 0;
181+
}
182+
},{
183+
type: "BIGINT",
184+
serialize: function(value, args){
185+
return !isNaN(parseInt(value)) && isFinite(value) &&
186+
value >= -9007199254740991 && value <= 9007199254740991 ?
187+
parseInt(value) : 0;
188+
},
189+
unserialize: function(value, args){
190+
return !isNaN(parseInt(value)) && isFinite(value) ?
191+
parseInt(value) : 0;
192+
}
126193
},{
127194
type: "JSON",
128-
aliases: ["ARRAY","OBJECT"],
129-
serialize: function(value, args){
195+
aliases: ["ARRAY", "OBJECT"],
196+
serialize: function(value){
130197
return JSON.stringify(value);
131198
},
132-
unserialize: function(value, args){
199+
unserialize: function(value){
133200
return JSON.parse(value);
134201
}
135202
},{
136203
type: "FUNCTION",
137-
serialize: function(value, args){
204+
serialize: function(value){
138205
if(typeof value !== "function") value=function(){};
139206
return "jSQLFunct-"+value.toString();
140207
},
141-
unserialize: function(value, args){
208+
unserialize: function(value){
142209
var p = value.split("-");
143210
if(p.shift() !== "jSQLFunct") return _throw(new jSQL_Error("0001"));
144211
p = value.split("-");
@@ -153,41 +220,55 @@
153220
},{
154221
type: "BOOLEAN",
155222
aliases: ['BOOL'],
156-
serialize: function(value, args){ return !!value; },
157-
unserialize: function(value, args){ return !!value; }
223+
serialize: function(value){
224+
return value ? "1" : "0";
225+
},
226+
unserialize: function(value){
227+
return value == "1";
228+
}
158229
},{
159-
type: "INT",
230+
type: "CHAR",
160231
serialize: function(value, args){
161-
return !isNaN(parseInt(value)) && isFinite(value) ?
162-
parseInt(value) : 0;
232+
return ""+value;
163233
},
164234
unserialize: function(value, args){
165-
return !isNaN(parseInt(value)) && isFinite(value) ?
166-
parseInt(value) : 0;
235+
var targetLength = args[0]>>0, padString = ' ';
236+
if (value.length > targetLength) return value.substr(0, args[0]);
237+
else {
238+
targetLength = targetLength-value.length;
239+
if (targetLength > padString.length)
240+
padString += padString.repeat(targetLength/padString.length);
241+
return String(value) + padString.slice(0,targetLength);
242+
}
243+
return ""+value;
167244
}
168245
},{
169-
type: "CHAR",
170-
aliases: ["VARCHAR", "LONGTEXT", "MEDIUMTEXT"],
171-
serialize: function(value, args){ return ""+value; },
172-
unserialize: function(value, args){ return ""+value; }
246+
type: "VARCHAR",
247+
aliases: ["LONGTEXT", "MEDIUMTEXT"],
248+
serialize: function(value, args){
249+
return ""+value;
250+
},
251+
unserialize: function(value, args){
252+
return ""+value;
253+
}
173254
},{
174255
type: "DATE",
175-
serialize: function(value, args){
256+
serialize: function(value){
176257
if(!value instanceof Date) return new Date(0).getTime();
177258
return value.getTime();
178259
},
179-
unserialize: function(value, args){
260+
unserialize: function(value){
180261
return new Date(value);
181262
}
182263
},{
183264
type: "AMBI",
184-
serialize: function(value, args){
265+
serialize: function(value){
185266
if(value instanceof Date) return value.getTime();
186267
if(typeof value === "function") return "jSQLFunct-"+value.toString();
187268
if(!isNaN(parseFloat(value)) && isFinite(value)) return value;
188269
return ""+value;
189270
},
190-
unserialize: function(value, args){
271+
unserialize: function(value){
191272
if(typeof value === "string"){
192273
if(value.split("-")[0] === "jSQLFunct"){
193274
var p = value.split("-");
@@ -500,6 +581,7 @@
500581
var storeVal = jSQL.types.getByType(type.type.toUpperCase()).serialize(value, type.args);
501582
if((!isNaN(parseFloat(storeVal)) && isFinite(storeVal)) || typeof storeVal === "string")
502583
return storeVal;
584+
console.log(storeVal);
503585
return _throw(new jSQL_Error("0020"));
504586
};
505587

@@ -671,6 +753,7 @@
671753
this.data[i] = preparedVals.shift();
672754
}
673755
}
756+
console.log(this.data);
674757
jSQL.tables[this.table].insertRow(this.data, this.ignoreFlag);
675758
return this;
676759
};
@@ -925,15 +1008,6 @@
9251008

9261009
function jSQLParseQuery(query){
9271010

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-
9371011
// Predcit the correct casing for column and table names
9381012
var convertToCol = function(c, skipErrors){
9391013
for(var i=0; i<jSQL.tables[table].columns.length; i++)
@@ -2656,12 +2730,20 @@
26562730
.trim();
26572731
}
26582732

2733+
function removeQuotes(str){
2734+
var quotes = ['"', "'", "`"];
2735+
for (var i = quotes.length; i--; )
2736+
if (str.substr(0, 1) == quotes[i] && str.substr(str.length - 1, 1) == quotes[i])
2737+
return str.substr(1, str.length - 2);
2738+
return str;
2739+
}
2740+
26592741
////////////////////////////////////////////////////////////////////////////
26602742
// Exposed Methods /////////////////////////////////////////////////////////
26612743
////////////////////////////////////////////////////////////////////////////
26622744

26632745
return {
2664-
version: 2.9,
2746+
version: 2.91,
26652747
tables: {},
26662748
query: jSQLParseQuery,
26672749
createTable: createTable,

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": "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": {

tests/test6.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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('poop','pee')"+
21+
" )").execute();
22+
23+
var sql = "insert into typetest values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
24+
params = [2.1, {"3":3}, function(){alert("hello")}, true, 4, 'y12', 'bob', new Date(), 'poooo', 'pee'];
25+
jSQL.query(sql).execute(params);
26+
27+
// var data = jSQL.query("select * from typetest").execute().fetch();
28+
// console.log(data);
29+
// data.greet()
30+
31+
});

0 commit comments

Comments
 (0)