Skip to content

Commit 31f5b2f

Browse files
committed
Modes sqlite.escapeId to Driver object, adds sqlite.sync()
1 parent 7e46f76 commit 31f5b2f

2 files changed

Lines changed: 90 additions & 10 deletions

File tree

lib/Drivers/DDL/sqlite.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
exports.sync = function (driver, opts, cb) {
2+
var queries = [];
3+
var definitions = [];
4+
var k, i, pending;
5+
6+
definitions.push(driver.escapeId(opts.id) + " INTEGER PRIMARY KEY AUTOINCREMENT");
7+
8+
for (k in opts.properties) {
9+
switch (opts.properties[k].type) {
10+
case "text":
11+
definitions.push(driver.escapeId(k) + " TEXT");
12+
break;
13+
case "number":
14+
definitions.push(driver.escapeId(k) + " REAL");
15+
break;
16+
case "boolean":
17+
definitions.push(driver.escapeId(k) + " INTEGER UNSIGNED NOT NULL");
18+
break;
19+
case "date":
20+
definitions.push(driver.escapeId(k) + " DATETIME");
21+
break;
22+
case "binary":
23+
definitions.push(driver.escapeId(k) + " BLOB");
24+
break;
25+
case "enum":
26+
definitions.push(driver.escapeId(k) + " INTEGER");
27+
break;
28+
default:
29+
throw new Error("Unknown property type: '" + opts.properties[k].type + "'");
30+
}
31+
}
32+
33+
for (i = 0; i < opts.one_associations.length; i++) {
34+
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INTEGER UNSIGNED NOT NULL");
35+
}
36+
37+
queries.push(
38+
"CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.table) +
39+
" (" + definitions.join(", ") + ")"
40+
);
41+
42+
for (i = 0; i < opts.one_associations.length; i++) {
43+
queries.push(
44+
"CREATE INDEX IF NOT EXISTS " + driver.escapeId(opts.table + "_" + opts.one_associations[i].field) +
45+
" ON " + driver.escapeId(opts.table) +
46+
" (" + driver.escapeId(opts.one_associations[i].field) + ")"
47+
);
48+
}
49+
50+
for (i = 0; i < opts.many_associations.length; i++) {
51+
queries.push(
52+
"CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable) +
53+
" (" +
54+
driver.escapeId(opts.many_associations[i].mergeId) + " INTEGER UNSIGNED NOT NULL, " +
55+
driver.escapeId(opts.many_associations[i].mergeAssocId) + " INTEGER UNSIGNED NOT NULL" +
56+
")"
57+
);
58+
queries.push(
59+
"CREATE INDEX IF NOT EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable) +
60+
" ON " + driver.escapeId(opts.table) +
61+
" (" + driver.escapeId(opts.many_associations[i].mergeId) + ", " + driver.escapeId(opts.many_associations[i].mergeAssocId) + ")"
62+
);
63+
}
64+
65+
console.log(queries);
66+
67+
pending = queries.length;
68+
for (i = 0; i < queries.length; i++) {
69+
driver.db.all(queries[i], function (err) {
70+
console.log(err);
71+
if (--pending === 0) {
72+
return cb(err);
73+
}
74+
});
75+
}
76+
};

lib/Drivers/DML/sqlite.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Driver(config, connection, opts) {
1616

1717
var escapes = {
1818
escape : escape,
19-
escapeId : escapeId
19+
escapeId : this.escapeId.bind(this)
2020
};
2121

2222
this.QuerySelect = new (require("../../sql/Select"))(escapes);
@@ -25,6 +25,10 @@ function Driver(config, connection, opts) {
2525
this.QueryRemove = new (require("../../sql/Remove"))(escapes);
2626
}
2727

28+
Driver.prototype.sync = function (opts, cb) {
29+
return require("../DDL/sqlite").sync(this, opts, cb);
30+
};
31+
2832
Driver.prototype.on = function (ev, cb) {
2933
if (ev == "error") {
3034
this.db.on("error", cb);
@@ -132,14 +136,22 @@ Driver.prototype.remove = function (table, conditions, cb) {
132136
};
133137

134138
Driver.prototype.clear = function (table, cb) {
135-
var query = "DELETE FROM " + escapeId(table);
139+
var query = "DELETE FROM " + this.escapeId(table);
136140

137141
if (this.opts.debug) {
138142
require("../Debug").sql('sqlite', query);
139143
}
140144
this.db.all(query, cb);
141145
};
142146

147+
Driver.prototype.escapeId = function (id) {
148+
var m = id.match(/^(.+)\.\*$/);
149+
if (m) {
150+
return '`' + m[1].replace(/\`/g, '``') + '`.*';
151+
}
152+
return '`' + id.replace(/\`/g, '``').replace(/\./g, '`.`') + '`';
153+
};
154+
143155
function escape(value) {
144156
if (Array.isArray(value)) {
145157
return "(" + value.map(escape) + ")";
@@ -151,11 +163,3 @@ function escape(value) {
151163

152164
return "'" + value.replace(/\'/g, "''") + "'";
153165
}
154-
155-
function escapeId(id) {
156-
var m = id.match(/^(.+)\.\*$/);
157-
if (m) {
158-
return '`' + m[1].replace(/\`/g, '``') + '`.*';
159-
}
160-
return '`' + id.replace(/\`/g, '``').replace(/\./g, '`.`') + '`';
161-
}

0 commit comments

Comments
 (0)