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: x,
|
17 | params: []
|
18 | };
|
19 | };
|
20 |
|
21 | insert = {
|
22 | sql: function(mohair) {
|
23 | var escapedKeys, keys, row, table, that;
|
24 | that = this;
|
25 | table = mohair._escape(mohair._table);
|
26 | keys = Object.keys(that._data);
|
27 | escapedKeys = keys.map(function(key) {
|
28 | return mohair._escape(key);
|
29 | });
|
30 | row = keys.map(function(key) {
|
31 | if (isRaw(that._data[key])) {
|
32 | return that._data[key].sql();
|
33 | } else {
|
34 | return '?';
|
35 | }
|
36 | });
|
37 | return "INSERT INTO " + table + "(" + (escapedKeys.join(', ')) + ") VALUES (" + (row.join(', ')) + ")";
|
38 | },
|
39 | params: function() {
|
40 | var params, that;
|
41 | that = this;
|
42 | params = [];
|
43 | Object.keys(that._data).map(function(key) {
|
44 | if (isRaw(that._data[key])) {
|
45 | return params = params.concat(that._data[key].params());
|
46 | } else {
|
47 | return params.push(that._data[key]);
|
48 | }
|
49 | });
|
50 | return params;
|
51 | }
|
52 | };
|
53 |
|
54 | module.exports.insert = function(data) {
|
55 | var object;
|
56 | object = Object.create(insert);
|
57 | object._data = data;
|
58 | return object;
|
59 | };
|
60 |
|
61 | insertMany = {
|
62 | sql: function(mohair) {
|
63 | var escapedKeys, first, keys, rows, table, that;
|
64 | that = this;
|
65 | table = mohair._escape(mohair._table);
|
66 | first = that._array[0];
|
67 | keys = Object.keys(first);
|
68 | escapedKeys = keys.map(function(key) {
|
69 | return mohair._escape(key);
|
70 | });
|
71 | rows = that._array.map(function(data) {
|
72 | var row;
|
73 | row = keys.map(function(key) {
|
74 | if (isRaw(data[key])) {
|
75 | return data[key].sql();
|
76 | } else {
|
77 | return '?';
|
78 | }
|
79 | });
|
80 | return "(" + (row.join(', ')) + ")";
|
81 | });
|
82 | return "INSERT INTO " + table + "(" + (escapedKeys.join(', ')) + ") VALUES " + (rows.join(', '));
|
83 | },
|
84 | params: function(mohair) {
|
85 | var firstKeys, params, that;
|
86 | that = this;
|
87 | firstKeys = Object.keys(that._array[0]);
|
88 | params = [];
|
89 | that._array.forEach(function(data) {
|
90 | return firstKeys.forEach(function(key) {
|
91 | if (isRaw(data[key])) {
|
92 | return params = params.concat(data[key].params());
|
93 | } else {
|
94 | return params.push(data[key]);
|
95 | }
|
96 | });
|
97 | });
|
98 | return params;
|
99 | }
|
100 | };
|
101 |
|
102 | module.exports.insertMany = function(array) {
|
103 | var object;
|
104 | object = Object.create(insertMany);
|
105 | object._array = array;
|
106 | return object;
|
107 | };
|
108 |
|
109 | select = {
|
110 | sql: function(mohair) {
|
111 | var parts, sql, table, that;
|
112 | that = this;
|
113 | table = mohair._escape(mohair._table);
|
114 | sql = '';
|
115 | if (mohair._with != null) {
|
116 | sql += 'WITH ';
|
117 | parts = [];
|
118 | parts = Object.keys(mohair._with).map(function(key) {
|
119 | return key + ' AS (' + asRaw(mohair._with[key]).sql() + ')';
|
120 | });
|
121 | sql += parts.join(', ');
|
122 | sql += ' ';
|
123 | }
|
124 | sql += "SELECT " + that._sql + " FROM " + table;
|
125 | mohair._joins.forEach(function(join) {
|
126 | sql += " " + join.sql;
|
127 | if (join.criterion != null) {
|
128 | return sql += " AND (" + (join.criterion.sql()) + ")";
|
129 | }
|
130 | });
|
131 | if (mohair._where != null) {
|
132 | sql += " WHERE " + (mohair._where.sql());
|
133 | }
|
134 | if (mohair._group != null) {
|
135 | sql += " GROUP BY " + mohair._group;
|
136 | }
|
137 | if (mohair._order != null) {
|
138 | sql += " ORDER BY " + mohair._order;
|
139 | }
|
140 | if (mohair._limit != null) {
|
141 | sql += " LIMIT ?";
|
142 | }
|
143 | if (mohair._offset != null) {
|
144 | sql += " OFFSET ?";
|
145 | }
|
146 | return sql;
|
147 | },
|
148 | params: function(mohair) {
|
149 | var params, that;
|
150 | that = this;
|
151 | params = [];
|
152 | if (mohair._with != null) {
|
153 | Object.keys(mohair._with).forEach(function(key) {
|
154 | return params = params.concat(asRaw(mohair._with[key]).params());
|
155 | });
|
156 | }
|
157 | if (that._params != null) {
|
158 | params = params.concat(that._params);
|
159 | }
|
160 | mohair._joins.forEach(function(join) {
|
161 | if (join.criterion != null) {
|
162 | return params = params.concat(join.criterion.params());
|
163 | }
|
164 | });
|
165 | if (mohair._where != null) {
|
166 | params = params.concat(mohair._where.params());
|
167 | }
|
168 | if (mohair._limit != null) {
|
169 | params.push(mohair._limit);
|
170 | }
|
171 | if (mohair._offset != null) {
|
172 | params.push(mohair._offset);
|
173 | }
|
174 | return params;
|
175 | }
|
176 | };
|
177 |
|
178 | module.exports.select = function(sql, params) {
|
179 | var object;
|
180 | object = Object.create(select);
|
181 | object._sql = sql;
|
182 | object._params = params;
|
183 | return object;
|
184 | };
|
185 |
|
186 | update = {
|
187 | sql: function(mohair) {
|
188 | var keys, sql, table, that, updates;
|
189 | that = this;
|
190 | table = mohair._escape(mohair._table);
|
191 | keys = Object.keys(that._updates);
|
192 | updates = keys.map(function(key) {
|
193 | var escapedKey;
|
194 | escapedKey = mohair._escape(key);
|
195 | if (isRaw(that._updates[key])) {
|
196 | return "" + escapedKey + " = " + (that._updates[key].sql());
|
197 | } else {
|
198 | return "" + escapedKey + " = ?";
|
199 | }
|
200 | });
|
201 | sql = "UPDATE " + table + " SET " + (updates.join(', '));
|
202 | if (mohair._where != null) {
|
203 | sql += " WHERE " + (mohair._where.sql());
|
204 | }
|
205 | return sql;
|
206 | },
|
207 | params: function(mohair) {
|
208 | var params, that;
|
209 | that = this;
|
210 | params = [];
|
211 | Object.keys(that._updates).forEach(function(key) {
|
212 | if (isRaw(that._updates[key])) {
|
213 | return params = params.concat(that._updates[key].params());
|
214 | } else {
|
215 | return params.push(that._updates[key]);
|
216 | }
|
217 | });
|
218 | if (mohair._where != null) {
|
219 | params = params.concat(mohair._where.params());
|
220 | }
|
221 | return params;
|
222 | }
|
223 | };
|
224 |
|
225 | module.exports.update = function(updates) {
|
226 | var object;
|
227 | object = Object.create(update);
|
228 | object._updates = updates;
|
229 | return object;
|
230 | };
|
231 |
|
232 | deletePrototype = {
|
233 | sql: function(mohair) {
|
234 | var sql, table, that;
|
235 | that = this;
|
236 | table = mohair._escape(mohair._table);
|
237 | sql = "DELETE FROM " + table;
|
238 | if (mohair._where != null) {
|
239 | sql += " WHERE " + (mohair._where.sql());
|
240 | }
|
241 | return sql;
|
242 | },
|
243 | params: function(mohair) {
|
244 | if (mohair._where != null) {
|
245 | return mohair._where.params();
|
246 | } else {
|
247 | return [];
|
248 | }
|
249 | }
|
250 | };
|
251 |
|
252 | module.exports["delete"] = function() {
|
253 | return Object.create(deletePrototype);
|
254 | };
|