Skip to content

Commit 4ef01a2

Browse files
committed
added export and import functionality
1 parent ae3731e commit 4ef01a2

File tree

9 files changed

+195
-43
lines changed

9 files changed

+195
-43
lines changed

Gruntfile.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11

22
module.exports = function(grunt) {
33

4+
var pkg = grunt.file.readJSON('package.json');
5+
pkg.version = pkg.version.split(".");
6+
var subversion = pkg.version.pop();
7+
subversion++;
8+
pkg.version.push(subversion);
9+
pkg.version = pkg.version.join(".");
10+
grunt.file.write('package.json', JSON.stringify(pkg, null, 2));
11+
12+
console.log("---------------------------------------");
13+
console.log(" Building jSQL Version "+pkg.version);
14+
console.log("---------------------------------------");
15+
416
grunt.initConfig({
5-
pkg: grunt.file.readJSON('package.json'),
17+
pkg: pkg,
618
concat: {
719
options: {
820
banner: '/**\n * <%= pkg.name %> - v<%= pkg.version %>' +
@@ -52,6 +64,8 @@ module.exports = function(grunt) {
5264
'src/helpers/jSQLReset.js',
5365
'src/helpers/jSQLMinifier.js',
5466
'src/helpers/removeQuotes.js',
67+
'src/import_export/export.js',
68+
'src/import_export/import.js',
5569
'src/wrapper/foot.js.part'
5670
],
5771
dest: 'jSQL.js',

README.md

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

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

5-
[![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)
5+
[![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) [![Coverage Status](https://coveralls.io/repos/github/Pamblam/jSQL/badge.svg?branch=master)](https://coveralls.io/github/Pamblam/jSQL?branch=master)
66

77
<hr>
88

jSQL.js

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jsql-official - v3.1.2
2+
* jsql-official - v3.2.1
33
* A persistent SQL database.
44
* @author Rob Parham
55
* @website http://pamblam.github.io/jSQL/
@@ -156,13 +156,13 @@ function jSQLDataTypeList(){
156156
},{
157157
type: "ENUM",
158158
serialize: function(value, args){
159-
if(value === null) return "-null-";
159+
if(value === null) return "jsqlNull";
160160
for(var i=args.length; i--;)
161161
if(value === removeQuotes(args[i])) return value;
162162
return _throw(new jSQL_Error("0068"));
163163
},
164164
unserialize: function(value, args){
165-
if(value === "-null-") return null;
165+
if(value === "jsqlNull") return null;
166166
for(var i=args.length; i--;)
167167
if(value === removeQuotes(args[i])) return value;
168168
return _throw(new jSQL_Error("0068"));
@@ -240,18 +240,18 @@ function jSQLDataTypeList(){
240240
type: "JSON",
241241
aliases: ["ARRAY", "OBJECT"],
242242
serialize: function(value){
243-
if(value === null) return "null";
243+
if(value === null) return "jsqlNull";
244244
if(typeof value === "string") return value;
245245
return JSON.stringify(value);
246246
},
247247
unserialize: function(value){
248-
if(value === "null") return null;
248+
if(value === "jsqlNull") return null;
249249
return JSON.parse(value);
250250
}
251251
},{
252252
type: "FUNCTION",
253253
serialize: function(value){
254-
if(value === null) return "null";
254+
if(value === null) return "jsqlNull";
255255
if(typeof value !== "function"){
256256
var f = null;
257257
try{
@@ -263,7 +263,7 @@ function jSQLDataTypeList(){
263263
return "jSQLFunct-"+value.toString();
264264
},
265265
unserialize: function(value){
266-
if(value === "null") return null;
266+
if(value === "jsqlNull") return null;
267267
var p = value.split("-");
268268
if(p.shift() !== "jSQLFunct") return _throw(new jSQL_Error("0001"));
269269
p = value.split("-");
@@ -279,22 +279,22 @@ function jSQLDataTypeList(){
279279
type: "BOOLEAN",
280280
aliases: ['BOOL'],
281281
serialize: function(value){
282-
if(value === null) return "null";
282+
if(value === null) return "jsqlNull";
283283
return value === true || value.toUpperCase() == "TRUE" || value == 1 ?
284284
"1" : "0" ;
285285
},
286286
unserialize: function(value){
287-
if(value === "null") return null;
287+
if(value === "jsqlNull") return null;
288288
return value === true || value.toUpperCase() == "TRUE" || value == 1 ;
289289
}
290290
},{
291291
type: "CHAR",
292292
serialize: function(value, args){
293-
if(value === null) return "-null-";
293+
if(value === null) return "jsqlNull";
294294
return ""+value;
295295
},
296296
unserialize: function(value, args){
297-
if(value === "-null-") return null;
297+
if(value === "jsqlNull") return null;
298298
var targetLength = args[0]>>0, padString = ' ';
299299
if (value.length > targetLength) return value.substr(0, args[0]);
300300
else {
@@ -309,35 +309,35 @@ function jSQLDataTypeList(){
309309
type: "VARCHAR",
310310
aliases: ["LONGTEXT", "MEDIUMTEXT"],
311311
serialize: function(value, args){
312-
if(value === null) return "-null-";
312+
if(value === null) return "jsqlNull";
313313
return ""+value;
314314
},
315315
unserialize: function(value, args){
316-
if(value === "-null-") return null;
316+
if(value === "jsqlNull") return null;
317317
return ""+value;
318318
}
319319
},{
320320
type: "DATE",
321321
serialize: function(value){
322-
if(value === null) return "-null-";
322+
if(value === null) return "jsqlNull";
323323
if(!(value instanceof Date)) return new Date(value).getTime();
324324
return value.getTime();
325325
},
326326
unserialize: function(value){
327-
if(value === "-null-") return null;
327+
if(value === "jsqlNull") return null;
328328
return new Date(value);
329329
}
330330
},{
331331
type: "AMBI",
332332
serialize: function(value){
333-
if(value === null) return "-null-";
333+
if(value === null) return "jsqlNull";
334334
if(value instanceof Date) return value.getTime();
335335
if(typeof value === "function") return "jSQLFunct-"+value.toString();
336336
if(!isNaN(parseFloat(value)) && isFinite(value)) return value;
337337
return ""+value;
338338
},
339339
unserialize: function(value){
340-
if(value === "-null-") return null;
340+
if(value === "jsqlNull") return null;
341341
if(typeof value === "string"){
342342
if(value.split("-")[0] === "jSQLFunct"){
343343
var p = value.split("-");
@@ -2748,8 +2748,75 @@ function removeQuotes(str){
27482748
return str;
27492749
}
27502750

2751+
function jsql_export(create_tables, table_names){
2752+
create_tables = create_tables || true;
2753+
table_names = table_names || [];
2754+
var dump_buffer = [];
2755+
for(var table in jSQL.tables){
2756+
if(!jSQL.tables.hasOwnProperty(table)) continue;
2757+
if(!table_names.length || ~table_names.indexOf(table)){
2758+
if(create_tables){
2759+
var table_buffer = [];
2760+
for(var i=0; i<jSQL.tables[table].columns.length; i++){
2761+
var col_buffer = [];
2762+
col_buffer.push("`"+jSQL.tables[table].columns[i]+"` ");
2763+
var args = "";
2764+
if(jSQL.tables[table].types[i].args.length) args = JSON.stringify(jSQL.tables[table].types[i].args).trim().slice(1, -1);
2765+
col_buffer.push(jSQL.tables[table].types[i].type+"("+args+") ");
2766+
col_buffer.push(jSQL.tables[table].types[i].null ? 'NULL ' : 'NOT NULL ');
2767+
if(jSQL.tables[table].types[i].default) col_buffer.push('DEFAULT '+JSON.stringify(jSQL.tables[table].types[i].default)+' ');
2768+
if(jSQL.tables[table].columns[i] === jSQL.tables[table].auto_inc_col) col_buffer.push('AUTO_INCREMENT');
2769+
table_buffer.push("\n\t"+(col_buffer.join('').trim()))
2770+
}
2771+
if(jSQL.tables[table].keys.primary.column){
2772+
col_buffer = [];
2773+
if(Array.isArray(jSQL.tables[table].keys.primary.column)){
2774+
for(var i=0; i<jSQL.tables[table].keys.primary.column.length; i++){
2775+
col_buffer.push('`'+jSQL.tables[table].keys.primary.column[i]+'`');
2776+
}
2777+
}else{
2778+
col_buffer.push('`'+jSQL.tables[table].keys.primary.column+'`');
2779+
}
2780+
table_buffer.push("\n\tPRIMARY KEY ("+(col_buffer.join(','))+")");
2781+
}
2782+
if(jSQL.tables[table].keys.unique.length){
2783+
for(var n=0; n<jSQL.tables[table].keys.unique.length; n++){
2784+
col_buffer = [];
2785+
if(Array.isArray(jSQL.tables[table].keys.unique[n].column)){
2786+
for(var i=0; i<jSQL.tables[table].keys.unique[n].column.length; i++){
2787+
col_buffer.push('`'+jSQL.tables[table].keys.unique[n].column[i]+'`');
2788+
}
2789+
}else{
2790+
col_buffer.push('`'+jSQL.tables[table].keys.unique[n].column+'`');
2791+
}
2792+
table_buffer.push("\n\tUNIQUE KEY ("+(col_buffer.join(','))+")");
2793+
}
2794+
}
2795+
dump_buffer.push('CREATE TABLE `'+table+'` ('+(table_buffer.join(","))+"\n)");
2796+
for(var i=0; i<jSQL.tables[table].data.length; i++){
2797+
var values = JSON.stringify(jSQL.tables[table].data[i]).trim().slice(1, -1).replace(/"jsqlNull"/g, 'null');
2798+
dump_buffer.push("INSERT INTO `"+table+"` (`"+(jSQL.tables[table].columns.join('`,`'))+"`) VALUES ("+values+")");
2799+
}
2800+
}
2801+
}
2802+
}
2803+
var header = ["-- Exported by jSQL v"+jSQL.version+" "+(new Date().toUTCString())];
2804+
var hwrapper = "-".repeat(header[0].length);
2805+
header.unshift(hwrapper);
2806+
header.push(hwrapper);
2807+
return (header.join("\n"))+"\n"+(dump_buffer.join(";\n"));
2808+
}
2809+
2810+
2811+
function jsql_import(dump){
2812+
dump = dump.split(";\n");
2813+
for(var i=0; i<dump.length; i++){
2814+
jSQL.query(dump[i]).execute();
2815+
}
2816+
}
2817+
27512818
return {
2752-
version: "3.1.2",
2819+
version: "3.2.1",
27532820
tables: {},
27542821
query: jSQLParseQuery,
27552822
createTable: createTable,
@@ -2767,7 +2834,9 @@ return {
27672834
rollback: persistenceManager.rollback,
27682835
setApiPriority: persistenceManager.setApiPriority,
27692836
getApi: persistenceManager.getApi,
2770-
tokenize: tokenize
2837+
tokenize: tokenize,
2838+
export: jsql_export,
2839+
import: jsql_import
27712840
};
27722841

27732842
})();

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsql-official",
3-
"version": "3.1.2",
3+
"version": "3.2.1",
44
"description": "A persistent SQL database.",
55
"main": "jSQL.min.js",
66
"directories": {
@@ -45,4 +45,4 @@
4545
"istanbul": "^0.4.5",
4646
"mocha": "^4.0.1"
4747
}
48-
}
48+
}

src/data_types/jSQLDataTypeList.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ function jSQLDataTypeList(){
1818
},{
1919
type: "ENUM",
2020
serialize: function(value, args){
21-
if(value === null) return "-null-";
21+
if(value === null) return "jsqlNull";
2222
for(var i=args.length; i--;)
2323
if(value === removeQuotes(args[i])) return value;
2424
return _throw(new jSQL_Error("0068"));
2525
},
2626
unserialize: function(value, args){
27-
if(value === "-null-") return null;
27+
if(value === "jsqlNull") return null;
2828
for(var i=args.length; i--;)
2929
if(value === removeQuotes(args[i])) return value;
3030
return _throw(new jSQL_Error("0068"));
@@ -102,18 +102,18 @@ function jSQLDataTypeList(){
102102
type: "JSON",
103103
aliases: ["ARRAY", "OBJECT"],
104104
serialize: function(value){
105-
if(value === null) return "null";
105+
if(value === null) return "jsqlNull";
106106
if(typeof value === "string") return value;
107107
return JSON.stringify(value);
108108
},
109109
unserialize: function(value){
110-
if(value === "null") return null;
110+
if(value === "jsqlNull") return null;
111111
return JSON.parse(value);
112112
}
113113
},{
114114
type: "FUNCTION",
115115
serialize: function(value){
116-
if(value === null) return "null";
116+
if(value === null) return "jsqlNull";
117117
if(typeof value !== "function"){
118118
var f = null;
119119
try{
@@ -125,7 +125,7 @@ function jSQLDataTypeList(){
125125
return "jSQLFunct-"+value.toString();
126126
},
127127
unserialize: function(value){
128-
if(value === "null") return null;
128+
if(value === "jsqlNull") return null;
129129
var p = value.split("-");
130130
if(p.shift() !== "jSQLFunct") return _throw(new jSQL_Error("0001"));
131131
p = value.split("-");
@@ -141,22 +141,22 @@ function jSQLDataTypeList(){
141141
type: "BOOLEAN",
142142
aliases: ['BOOL'],
143143
serialize: function(value){
144-
if(value === null) return "null";
144+
if(value === null) return "jsqlNull";
145145
return value === true || value.toUpperCase() == "TRUE" || value == 1 ?
146146
"1" : "0" ;
147147
},
148148
unserialize: function(value){
149-
if(value === "null") return null;
149+
if(value === "jsqlNull") return null;
150150
return value === true || value.toUpperCase() == "TRUE" || value == 1 ;
151151
}
152152
},{
153153
type: "CHAR",
154154
serialize: function(value, args){
155-
if(value === null) return "-null-";
155+
if(value === null) return "jsqlNull";
156156
return ""+value;
157157
},
158158
unserialize: function(value, args){
159-
if(value === "-null-") return null;
159+
if(value === "jsqlNull") return null;
160160
var targetLength = args[0]>>0, padString = ' ';
161161
if (value.length > targetLength) return value.substr(0, args[0]);
162162
else {
@@ -171,35 +171,35 @@ function jSQLDataTypeList(){
171171
type: "VARCHAR",
172172
aliases: ["LONGTEXT", "MEDIUMTEXT"],
173173
serialize: function(value, args){
174-
if(value === null) return "-null-";
174+
if(value === null) return "jsqlNull";
175175
return ""+value;
176176
},
177177
unserialize: function(value, args){
178-
if(value === "-null-") return null;
178+
if(value === "jsqlNull") return null;
179179
return ""+value;
180180
}
181181
},{
182182
type: "DATE",
183183
serialize: function(value){
184-
if(value === null) return "-null-";
184+
if(value === null) return "jsqlNull";
185185
if(!(value instanceof Date)) return new Date(value).getTime();
186186
return value.getTime();
187187
},
188188
unserialize: function(value){
189-
if(value === "-null-") return null;
189+
if(value === "jsqlNull") return null;
190190
return new Date(value);
191191
}
192192
},{
193193
type: "AMBI",
194194
serialize: function(value){
195-
if(value === null) return "-null-";
195+
if(value === null) return "jsqlNull";
196196
if(value instanceof Date) return value.getTime();
197197
if(typeof value === "function") return "jSQLFunct-"+value.toString();
198198
if(!isNaN(parseFloat(value)) && isFinite(value)) return value;
199199
return ""+value;
200200
},
201201
unserialize: function(value){
202-
if(value === "-null-") return null;
202+
if(value === "jsqlNull") return null;
203203
if(typeof value === "string"){
204204
if(value.split("-")[0] === "jSQLFunct"){
205205
var p = value.split("-");

0 commit comments

Comments
 (0)