1 |
|
2 | var asRaw, deletePrototype, insert, insertMany, isRaw, select, update;
|
3 |
|
4 | isRaw = function(x) {
|
5 | return (x != null) && ('object' === typeof x) && ('function' === typeof x.sql);
|
6 | };
|
7 |
|
8 | asRaw = function(x) {
|
9 | if (isRaw(x)) {
|
10 | return x;
|
11 | }
|
12 | if ('string' !== typeof x) {
|
13 | throw new Exception('raw or string expected');
|
14 | }
|
15 | return {
|
16 | sql: function() {
|
17 | return x;
|
18 | },
|
19 | params: function() {
|
20 | return [];
|
21 | }
|
22 | };
|
23 | };
|
24 |
|
25 | insert = {
|
26 | sql: function(mohair) {
|
27 | var escapedKeys, keys, row, table, that;
|
28 | that = this;
|
29 | if (mohair._table == null) {
|
30 | throw new Error('sql of insert requires call to table before it');
|
31 | }
|
32 | table = mohair._escape(mohair._table);
|
33 | keys = Object.keys(that._data);
|
34 | escapedKeys = keys.map(function(key) {
|
35 | return mohair._escape(key);
|
36 | });
|
37 | row = keys.map(function(key) {
|
38 | if (isRaw(that._data[key])) {
|
39 | return that._data[key].sql();
|
40 | } else {
|
41 | return '?';
|
42 | }
|
43 | });
|
44 | return "INSERT INTO " + table + "(" + (escapedKeys.join(', ')) + ") VALUES (" + (row.join(', ')) + ")";
|
45 | },
|
46 | params: function() {
|
47 | var params, that;
|
48 | that = this;
|
49 | params = [];
|
50 | Object.keys(that._data).map(function(key) {
|
51 | if (isRaw(that._data[key])) {
|
52 | return params = params.concat(that._data[key].params());
|
53 | } else {
|
54 | return params.push(that._data[key]);
|
55 | }
|
56 | });
|
57 | return params;
|
58 | }
|
59 | };
|
60 |
|
61 | module.exports.insert = function(data) {
|
62 | var object;
|
63 | object = Object.create(insert);
|
64 | object._data = data;
|
65 | return object;
|
66 | };
|
67 |
|
68 | insertMany = {
|
69 | sql: function(mohair) {
|
70 | var escapedKeys, first, keys, rows, table, that;
|
71 | that = this;
|
72 | if (mohair._table == null) {
|
73 | throw new Error('sql of insertMany requires call to table before it');
|
74 | }
|
75 | table = mohair._escape(mohair._table);
|
76 | first = that._array[0];
|
77 | keys = Object.keys(first);
|
78 | escapedKeys = keys.map(function(key) {
|
79 | return mohair._escape(key);
|
80 | });
|
81 | rows = that._array.map(function(data) {
|
82 | var row;
|
83 | row = keys.map(function(key) {
|
84 | if (isRaw(data[key])) {
|
85 | return data[key].sql();
|
86 | } else {
|
87 | return '?';
|
88 | }
|
89 | });
|
90 | return "(" + (row.join(', ')) + ")";
|
91 | });
|
92 | return "INSERT INTO " + table + "(" + (escapedKeys.join(', ')) + ") VALUES " + (rows.join(', '));
|
93 | },
|
94 | params: function(mohair) {
|
95 | var firstKeys, params, that;
|
96 | that = this;
|
97 | firstKeys = Object.keys(that._array[0]);
|
98 | params = [];
|
99 | that._array.forEach(function(data) {
|
100 | return firstKeys.forEach(function(key) {
|
101 | if (isRaw(data[key])) {
|
102 | return params = params.concat(data[key].params());
|
103 | } else {
|
104 | return params.push(data[key]);
|
105 | }
|
106 | });
|
107 | });
|
108 | return params;
|
109 | }
|
110 | };
|
111 |
|
112 | module.exports.insertMany = function(array) {
|
113 | var object;
|
114 | object = Object.create(insertMany);
|
115 | object._array = array;
|
116 | return object;
|
117 | };
|
118 |
|
119 | select = {
|
120 | sql: function(mohair) {
|
121 | var parts, sql, table, that;
|
122 | that = this;
|
123 | table = mohair._escape(mohair._table);
|
124 | sql = '';
|
125 | if (mohair._with != null) {
|
126 | sql += 'WITH ';
|
127 | parts = [];
|
128 | parts = Object.keys(mohair._with).map(function(key) {
|
129 | return key + ' AS (' + asRaw(mohair._with[key]).sql() + ')';
|
130 | });
|
131 | sql += parts.join(', ');
|
132 | sql += ' ';
|
133 | }
|
134 | sql += "SELECT ";
|
135 | parts = [];
|
136 | that._selects.forEach(function(s) {
|
137 | var keys;
|
138 | if (isRaw(s)) {
|
139 | return parts.push('(' + s.sql() + ')');
|
140 | } else if ('object' === typeof s) {
|
141 | keys = Object.keys(s);
|
142 | if (keys.length === 0) {
|
143 | throw new Error('select object must have at least one property');
|
144 | }
|
145 | return keys.forEach(function(key) {
|
146 | var value;
|
147 | value = s[key];
|
148 | if (isRaw(value)) {
|
149 | return parts.push('(' + value.sql() + ') AS ' + key);
|
150 | } else {
|
151 | return parts.push(value + ' AS ' + key);
|
152 | }
|
153 | });
|
154 | } else {
|
155 | return parts.push(s);
|
156 | }
|
157 | });
|
158 | sql += parts.join(', ');
|
159 | if (mohair._table != null) {
|
160 | sql += " FROM " + table;
|
161 | }
|
162 | mohair._joins.forEach(function(join) {
|
163 | sql += " " + join.sql;
|
164 | if (join.criterion != null) {
|
165 | return sql += " AND (" + (join.criterion.sql()) + ")";
|
166 | }
|
167 | });
|
168 | if (mohair._where != null) {
|
169 | sql += " WHERE " + (mohair._where.sql());
|
170 | }
|
171 | if (mohair._group != null) {
|
172 | sql += " GROUP BY " + mohair._group;
|
173 | }
|
174 | if (mohair._having != null) {
|
175 | sql += " HAVING " + (mohair._having.sql());
|
176 | }
|
177 | if (mohair._order != null) {
|
178 | sql += " ORDER BY " + mohair._order;
|
179 | }
|
180 | if (mohair._limit != null) {
|
181 | sql += " LIMIT ?";
|
182 | }
|
183 | if (mohair._offset != null) {
|
184 | sql += " OFFSET ?";
|
185 | }
|
186 | return sql;
|
187 | },
|
188 | params: function(mohair) {
|
189 | var params, that;
|
190 | that = this;
|
191 | params = [];
|
192 | if (mohair._with != null) {
|
193 | Object.keys(mohair._with).forEach(function(key) {
|
194 | return params = params.concat(asRaw(mohair._with[key]).params());
|
195 | });
|
196 | }
|
197 | that._selects.forEach(function(s) {
|
198 | var keys;
|
199 | if (isRaw(s)) {
|
200 | return params = params.concat(s.params());
|
201 | } else if ('object' === typeof s) {
|
202 | keys = Object.keys(s);
|
203 | if (keys.length === 0) {
|
204 | throw new Error('select object must have at least one property');
|
205 | }
|
206 | return keys.forEach(function(key) {
|
207 | return params = params.concat(asRaw(s[key]).params());
|
208 | });
|
209 | }
|
210 | });
|
211 | mohair._joins.forEach(function(join) {
|
212 | if (join.criterion != null) {
|
213 | return params = params.concat(join.criterion.params());
|
214 | }
|
215 | });
|
216 | if (mohair._where != null) {
|
217 | params = params.concat(mohair._where.params());
|
218 | }
|
219 | if (mohair._limit != null) {
|
220 | params.push(mohair._limit);
|
221 | }
|
222 | if (mohair._offset != null) {
|
223 | params.push(mohair._offset);
|
224 | }
|
225 | return params;
|
226 | }
|
227 | };
|
228 |
|
229 | module.exports.select = function() {
|
230 | var object, selects;
|
231 | selects = Array.prototype.slice.call(arguments);
|
232 | object = Object.create(select);
|
233 | if (selects.length === 0) {
|
234 | selects = ['*'];
|
235 | }
|
236 | object._selects = selects;
|
237 | return object;
|
238 | };
|
239 |
|
240 | update = {
|
241 | sql: function(mohair) {
|
242 | var keys, sql, table, that, updates;
|
243 | that = this;
|
244 | if (mohair._table == null) {
|
245 | throw new Error('sql of update requires call to table before it');
|
246 | }
|
247 | table = mohair._escape(mohair._table);
|
248 | keys = Object.keys(that._updates);
|
249 | updates = keys.map(function(key) {
|
250 | var escapedKey;
|
251 | escapedKey = mohair._escape(key);
|
252 | if (isRaw(that._updates[key])) {
|
253 | return "" + escapedKey + " = " + (that._updates[key].sql());
|
254 | } else {
|
255 | return "" + escapedKey + " = ?";
|
256 | }
|
257 | });
|
258 | sql = "UPDATE " + table + " SET " + (updates.join(', '));
|
259 | if (mohair._where != null) {
|
260 | sql += " WHERE " + (mohair._where.sql());
|
261 | }
|
262 | return sql;
|
263 | },
|
264 | params: function(mohair) {
|
265 | var params, that;
|
266 | that = this;
|
267 | params = [];
|
268 | Object.keys(that._updates).forEach(function(key) {
|
269 | if (isRaw(that._updates[key])) {
|
270 | return params = params.concat(that._updates[key].params());
|
271 | } else {
|
272 | return params.push(that._updates[key]);
|
273 | }
|
274 | });
|
275 | if (mohair._where != null) {
|
276 | params = params.concat(mohair._where.params());
|
277 | }
|
278 | return params;
|
279 | }
|
280 | };
|
281 |
|
282 | module.exports.update = function(updates) {
|
283 | var object;
|
284 | object = Object.create(update);
|
285 | object._updates = updates;
|
286 | return object;
|
287 | };
|
288 |
|
289 | deletePrototype = {
|
290 | sql: function(mohair) {
|
291 | var sql, table, that;
|
292 | that = this;
|
293 | if (mohair._table == null) {
|
294 | throw new Error('sql of delete requires call to table before it');
|
295 | }
|
296 | table = mohair._escape(mohair._table);
|
297 | sql = "DELETE FROM " + table;
|
298 | if (mohair._where != null) {
|
299 | sql += " WHERE " + (mohair._where.sql());
|
300 | }
|
301 | return sql;
|
302 | },
|
303 | params: function(mohair) {
|
304 | if (mohair._where != null) {
|
305 | return mohair._where.params();
|
306 | } else {
|
307 | return [];
|
308 | }
|
309 | }
|
310 | };
|
311 |
|
312 | module.exports["delete"] = function() {
|
313 | return Object.create(deletePrototype);
|
314 | };
|