Skip to content

Commit fe08371

Browse files
committed
Uses sql-query for SQL query building, removes unnecessary files, fixes some tests
1 parent cf6c629 commit fe08371

19 files changed

Lines changed: 335 additions & 1074 deletions

lib/Associations/Many.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
104104

105105
options.__merge = {
106106
from: { table: association.mergeTable, field: association.mergeAssocId },
107-
to: { table: association.model.table, field: association.model.id }
107+
to: { table: association.model.table, field: association.model.id },
108+
where: [ association.mergeTable, {} ]
108109
};
109110
options.extra = association.props;
110111
options.extra_info = {
@@ -114,11 +115,14 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
114115
assoc_prop: association.mergeAssocId
115116
};
116117

117-
conditions[association.mergeTable + "." + association.mergeId] = Instance[Model.id];
118-
conditions[association.mergeTable + "." + association.mergeAssocId] = [];
118+
options.__merge.where[1][association.mergeId] = Instance[Model.id];
119119

120-
for (var i = 0; i < Instances.length; i++) {
121-
conditions[association.mergeTable + "." + association.mergeAssocId].push(Instances[i][association.model.id]);
120+
if (Instances.length) {
121+
options.__merge.where[1][association.mergeAssocId] = [];
122+
123+
for (var i = 0; i < Instances.length; i++) {
124+
options.__merge.where[1][association.mergeAssocId].push(Instances[i][association.model.id]);
125+
}
122126
}
123127

124128
association.model.find(conditions, options, function (err, instances) {
@@ -163,8 +167,9 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
163167
}
164168

165169
options.__merge = {
166-
from: { table: association.mergeTable, field: association.mergeAssocId },
167-
to: { table: association.model.table, field: association.model.id }
170+
from : { table: association.mergeTable, field: association.mergeAssocId },
171+
to : { table: association.model.table, field: association.model.id },
172+
where : [ association.mergeTable, {} ]
168173
};
169174
options.extra = association.props;
170175
options.extra_info = {
@@ -180,7 +185,8 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
180185
if (conditions === null) {
181186
conditions = {};
182187
}
183-
conditions[association.mergeTable + "." + association.mergeId] = Instance[Model.id];
188+
189+
options.__merge.where[1][association.mergeId] = Instance[Model.id];
184190

185191
if (cb === null) {
186192
return association.model.find(conditions, limit, options);

lib/Drivers/DDL/mysql.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
exports.drop = function (driver, opts, cb) {
22
var i, queries = [], pending;
33

4-
queries.push("DROP TABLE IF EXISTS " + driver.escapeId(opts.table));
4+
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));
55

66
for (i = 0; i < opts.many_associations.length; i++) {
7-
queries.push("DROP TABLE IF EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable));
7+
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
88
}
99

1010
pending = queries.length;
@@ -35,46 +35,46 @@ exports.sync = function (driver, opts, cb) {
3535
var definitions = [];
3636
var k, i, pending;
3737

38-
definitions.push(driver.escapeId(opts.id) + " INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY");
38+
definitions.push(driver.query.escapeId(opts.id) + " INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY");
3939

4040
for (k in opts.properties) {
4141
definitions.push(buildColumnDefinition(driver, k, opts.properties[k]));
4242
}
4343

4444
for (i = 0; i < opts.one_associations.length; i++) {
4545
if (opts.one_associations[i].reversed) continue;
46-
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INT(11) UNSIGNED NOT NULL");
46+
definitions.push(driver.query.escapeId(opts.one_associations[i].field) + " INT(11) UNSIGNED NOT NULL");
4747
}
4848

49-
definitions.push("INDEX (" + driver.escapeId(opts.id) + ")");
49+
definitions.push("INDEX (" + driver.query.escapeId(opts.id) + ")");
5050
for (k in opts.properties) {
5151
if (opts.properties[k].unique === true) {
52-
definitions.push("UNIQUE KEY " + driver.escapeId(k) + " (" + driver.escapeId(k) + ")");
52+
definitions.push("UNIQUE KEY " + driver.query.escapeId(k) + " (" + driver.query.escapeId(k) + ")");
5353
}
5454
}
5555
for (i = 0; i < opts.one_associations.length; i++) {
5656
if (opts.one_associations[i].reversed) continue;
57-
definitions.push("INDEX (" + driver.escapeId(opts.one_associations[i].field) + ")");
57+
definitions.push("INDEX (" + driver.query.escapeId(opts.one_associations[i].field) + ")");
5858
}
5959

6060
queries.push(
61-
"CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.table) +
61+
"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.table) +
6262
" (" + definitions.join(", ") + ")"
6363
);
6464

6565
for (i = 0; i < opts.many_associations.length; i++) {
6666
definitions = [];
6767

68-
definitions.push(driver.escapeId(opts.many_associations[i].mergeId) + " INT(11) UNSIGNED NOT NULL");
69-
definitions.push(driver.escapeId(opts.many_associations[i].mergeAssocId) + " INT(11) UNSIGNED NOT NULL");
68+
definitions.push(driver.query.escapeId(opts.many_associations[i].mergeId) + " INT(11) UNSIGNED NOT NULL");
69+
definitions.push(driver.query.escapeId(opts.many_associations[i].mergeAssocId) + " INT(11) UNSIGNED NOT NULL");
7070

7171
for (k in opts.many_associations[i].props) {
7272
definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].props[k]));
7373
}
7474

75-
definitions.push("INDEX (" + driver.escapeId(opts.many_associations[i].mergeId) + ", " + driver.escapeId(opts.many_associations[i].mergeAssocId) + ")");
75+
definitions.push("INDEX (" + driver.query.escapeId(opts.many_associations[i].mergeId) + ", " + driver.query.escapeId(opts.many_associations[i].mergeAssocId) + ")");
7676
queries.push(
77-
"CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable) +
77+
"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable) +
7878
" (" + definitions.join(", ") + ")"
7979
);
8080
}
@@ -107,37 +107,37 @@ function buildColumnDefinition(driver, name, prop) {
107107

108108
switch (prop.type) {
109109
case "text":
110-
def = driver.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
110+
def = driver.query.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
111111
break;
112112
case "number":
113113
if (prop.rational === false) {
114-
def = driver.escapeId(name) + " INTEGER";
114+
def = driver.query.escapeId(name) + " INTEGER";
115115
} else {
116-
def = driver.escapeId(name) + " FLOAT";
116+
def = driver.query.escapeId(name) + " FLOAT";
117117
}
118118
if (prop.unsigned === true) {
119119
def += " UNSIGNED";
120120
}
121121
break;
122122
case "boolean":
123-
def = driver.escapeId(name) + " BOOLEAN NOT NULL";
123+
def = driver.query.escapeId(name) + " BOOLEAN NOT NULL";
124124
break;
125125
case "date":
126126
if (prop.time === false) {
127-
def = driver.escapeId(name) + " DATE";
127+
def = driver.query.escapeId(name) + " DATE";
128128
} else {
129-
def = driver.escapeId(name) + " DATETIME";
129+
def = driver.query.escapeId(name) + " DATETIME";
130130
}
131131
break;
132132
case "binary":
133133
if (prop.big === true) {
134-
def = driver.escapeId(name) + " LONGBLOB";
134+
def = driver.query.escapeId(name) + " LONGBLOB";
135135
} else {
136-
def = driver.escapeId(name) + " BLOB";
136+
def = driver.query.escapeId(name) + " BLOB";
137137
}
138138
break;
139139
case "enum":
140-
def = driver.escapeId(name) + " ENUM (" +
140+
def = driver.query.escapeId(name) + " ENUM (" +
141141
prop.values.map(driver.db.escape.bind(driver.db)) +
142142
")";
143143
break;

lib/Drivers/DDL/postgres.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
exports.drop = function (driver, opts, cb) {
22
var i, queries = [], pending;
33

4-
queries.push("DROP TABLE IF EXISTS " + driver.escapeId(opts.table));
4+
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));
55

66
for (i = 0; i < opts.many_associations.length; i++) {
7-
queries.push("DROP TABLE IF EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable));
7+
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
88
}
99

1010
pending = queries.length;
@@ -23,7 +23,7 @@ exports.sync = function (driver, opts, cb) {
2323
var definitions = [];
2424
var k, i, pending, tmp;
2525

26-
definitions.push(driver.escapeId(opts.id) + " SERIAL PRIMARY KEY");
26+
definitions.push(driver.query.escapeId(opts.id) + " SERIAL PRIMARY KEY");
2727

2828
for (k in opts.properties) {
2929
definitions.push(buildColumnDefinition(driver, opts.table, k, opts.properties[k]));
@@ -38,38 +38,38 @@ exports.sync = function (driver, opts, cb) {
3838

3939
for (i = 0; i < opts.one_associations.length; i++) {
4040
if (opts.one_associations[i].reversed) continue;
41-
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INTEGER NOT NULL");
41+
definitions.push(driver.query.escapeId(opts.one_associations[i].field) + " INTEGER NOT NULL");
4242
}
4343
for (k in opts.properties) {
4444
if (opts.properties[k].unique === true) {
45-
definitions.push("UNIQUE (" + driver.escapeId(k) + ")");
45+
definitions.push("UNIQUE (" + driver.query.escapeId(k) + ")");
4646
}
4747
}
4848

4949
tables.push({
5050
name : opts.table,
51-
query : "CREATE TABLE " + driver.escapeId(opts.table) +
51+
query : "CREATE TABLE " + driver.query.escapeId(opts.table) +
5252
" (" + definitions.join(", ") + ")",
5353
subqueries : subqueries
5454
});
5555
tables[tables.length - 1].subqueries.push(
56-
"CREATE INDEX ON " + driver.escapeId(opts.table) +
57-
" (" + driver.escapeId(opts.id) + ")"
56+
"CREATE INDEX ON " + driver.query.escapeId(opts.table) +
57+
" (" + driver.query.escapeId(opts.id) + ")"
5858
);
5959

6060
for (i = 0; i < opts.one_associations.length; i++) {
6161
if (opts.one_associations[i].reversed) continue;
6262
tables[tables.length - 1].subqueries.push(
63-
"CREATE INDEX ON " + driver.escapeId(opts.table) +
64-
" (" + driver.escapeId(opts.one_associations[i].field) + ")"
63+
"CREATE INDEX ON " + driver.query.escapeId(opts.table) +
64+
" (" + driver.query.escapeId(opts.one_associations[i].field) + ")"
6565
);
6666
}
6767

6868
for (i = 0; i < opts.many_associations.length; i++) {
6969
definitions = [];
7070

71-
definitions.push(driver.escapeId(opts.many_associations[i].mergeId) + " INTEGER NOT NULL");
72-
definitions.push(driver.escapeId(opts.many_associations[i].mergeAssocId) + " INTEGER NOT NULL");
71+
definitions.push(driver.query.escapeId(opts.many_associations[i].mergeId) + " INTEGER NOT NULL");
72+
definitions.push(driver.query.escapeId(opts.many_associations[i].mergeAssocId) + " INTEGER NOT NULL");
7373

7474
for (k in opts.many_associations[i].props) {
7575
definitions.push(buildColumnDefinition(driver, opts.many_associations[i].mergeTable,
@@ -78,15 +78,15 @@ exports.sync = function (driver, opts, cb) {
7878

7979
tables.push({
8080
name : opts.many_associations[i].mergeTable,
81-
query : "CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable) +
81+
query : "CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable) +
8282
" (" + definitions.join(", ") + ")",
8383
subqueries : []
8484
});
8585
tables[tables.length - 1].subqueries.push(
86-
"CREATE INDEX ON " + driver.escapeId(opts.many_associations[i].mergeTable) +
86+
"CREATE INDEX ON " + driver.query.escapeId(opts.many_associations[i].mergeTable) +
8787
" (" +
88-
driver.escapeId(opts.many_associations[i].mergeId) + ", " +
89-
driver.escapeId(opts.many_associations[i].mergeAssocId) +
88+
driver.query.escapeId(opts.many_associations[i].mergeId) + ", " +
89+
driver.query.escapeId(opts.many_associations[i].mergeAssocId) +
9090
")"
9191
);
9292
}
@@ -126,30 +126,30 @@ function buildColumnDefinition(driver, table, name, prop) {
126126

127127
switch (prop.type) {
128128
case "text":
129-
def = driver.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
129+
def = driver.query.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
130130
break;
131131
case "number":
132132
if (prop.rational === false) {
133-
def = driver.escapeId(name) + " INTEGER";
133+
def = driver.query.escapeId(name) + " INTEGER";
134134
} else {
135-
def = driver.escapeId(name) + " REAL";
135+
def = driver.query.escapeId(name) + " REAL";
136136
}
137137
break;
138138
case "boolean":
139-
def = driver.escapeId(name) + " BOOLEAN NOT NULL";
139+
def = driver.query.escapeId(name) + " BOOLEAN NOT NULL";
140140
break;
141141
case "date":
142142
if (prop.time === false) {
143-
def = driver.escapeId(name) + " DATE";
143+
def = driver.query.escapeId(name) + " DATE";
144144
} else {
145-
def = driver.escapeId(name) + " TIMESTAMP WITHOUT TIME ZONE";
145+
def = driver.query.escapeId(name) + " TIMESTAMP WITHOUT TIME ZONE";
146146
}
147147
break;
148148
case "binary":
149-
def = driver.escapeId(name) + " BYTEA";
149+
def = driver.query.escapeId(name) + " BYTEA";
150150
break;
151151
case "enum":
152-
def = driver.escapeId(name) + " " + driver.escapeId("enum_" + table + "_" + name);
152+
def = driver.query.escapeId(name) + " " + driver.query.escapeId("enum_" + table + "_" + name);
153153
break;
154154
default:
155155
throw new Error("Unknown property type: '" + prop.type + "'");

0 commit comments

Comments
 (0)