@saltcorn/sql 0.3.9 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -1
- package/package.json +1 -1
- package/table-provider.js +78 -5
- package/terminal.js +1 -1
package/index.js
CHANGED
|
@@ -144,10 +144,13 @@ module.exports = {
|
|
|
144
144
|
await client.query(
|
|
145
145
|
`SET LOCAL search_path TO "${db.getTenantSchema()}";`
|
|
146
146
|
);
|
|
147
|
+
await client.query(
|
|
148
|
+
`SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
|
|
149
|
+
);
|
|
147
150
|
}
|
|
148
151
|
const qres = await db.query(query, parameters || []);
|
|
149
152
|
|
|
150
|
-
await client.query(`
|
|
153
|
+
await client.query(`ROLLBACK;`);
|
|
151
154
|
return qres;
|
|
152
155
|
},
|
|
153
156
|
isAsync: true,
|
package/package.json
CHANGED
package/table-provider.js
CHANGED
|
@@ -185,10 +185,24 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
185
185
|
//console.log(ast[0].columns);
|
|
186
186
|
for (const k of Object.keys(where)) {
|
|
187
187
|
if (!colNames.has(k)) continue;
|
|
188
|
-
const sqlCol =
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
const sqlCol =
|
|
189
|
+
ast[0].columns == "*"
|
|
190
|
+
? {
|
|
191
|
+
type: "expr",
|
|
192
|
+
expr: { type: "column_ref", table: null, column: k },
|
|
193
|
+
as: null,
|
|
194
|
+
}
|
|
195
|
+
: (ast[0].columns || []).find(
|
|
196
|
+
(c) => k === c.as || (!c.as && k === c.expr?.column)
|
|
197
|
+
);
|
|
198
|
+
const sqlExprCol =
|
|
199
|
+
ast[0].columns == "*"
|
|
200
|
+
? {
|
|
201
|
+
type: "expr",
|
|
202
|
+
expr: { type: "column_ref", table: null, column: k },
|
|
203
|
+
as: null,
|
|
204
|
+
}
|
|
205
|
+
: (ast[0].columns || []).find((c) => c.expr?.as == k);
|
|
192
206
|
let left = sqlExprCol
|
|
193
207
|
? { ...sqlExprCol.expr, as: null }
|
|
194
208
|
: {
|
|
@@ -232,13 +246,72 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
232
246
|
{ type: "number", value: where.offset },
|
|
233
247
|
],
|
|
234
248
|
};
|
|
249
|
+
} else if (opts?.limit && opts?.offset) {
|
|
250
|
+
ast[0].limit = {
|
|
251
|
+
seperator: "offset",
|
|
252
|
+
value: [
|
|
253
|
+
{ type: "number", value: opts.limit },
|
|
254
|
+
{ type: "number", value: opts.offset },
|
|
255
|
+
],
|
|
256
|
+
};
|
|
235
257
|
} else if (where?.limit) {
|
|
236
258
|
ast[0].limit = {
|
|
237
259
|
seperator: "",
|
|
238
260
|
value: [{ type: "number", value: where.limit }],
|
|
239
261
|
};
|
|
262
|
+
} else if (opts?.limit) {
|
|
263
|
+
ast[0].limit = {
|
|
264
|
+
seperator: "",
|
|
265
|
+
value: [{ type: "number", value: opts.limit }],
|
|
266
|
+
};
|
|
240
267
|
}
|
|
268
|
+
//console.log(ast[0]);
|
|
269
|
+
//console.log(ast[0].orderby[[0]]);
|
|
241
270
|
|
|
271
|
+
const orderBy = where?.orderBy || opts?.orderBy;
|
|
272
|
+
const orderDesc = where?.orderDesc || opts?.orderDesc;
|
|
273
|
+
|
|
274
|
+
if (orderBy) {
|
|
275
|
+
if (typeof orderBy === "string")
|
|
276
|
+
ast[0].orderby = [
|
|
277
|
+
{
|
|
278
|
+
expr: {
|
|
279
|
+
type: "column_ref",
|
|
280
|
+
table: null,
|
|
281
|
+
column: db.sqlsanitize(orderBy),
|
|
282
|
+
},
|
|
283
|
+
type: orderDesc ? "DESC" : "ASC",
|
|
284
|
+
},
|
|
285
|
+
];
|
|
286
|
+
else if (orderBy.operator) {
|
|
287
|
+
const { operator, field, target } = orderBy;
|
|
288
|
+
const fieldCol = (cfg.columns || []).find((c) => c.name === field);
|
|
289
|
+
const type = getState().types[fieldCol?.type];
|
|
290
|
+
const op = type?.distance_operators[operator];
|
|
291
|
+
if (op?.type === "SqlBinOp") {
|
|
292
|
+
ast[0].orderby = [
|
|
293
|
+
{
|
|
294
|
+
expr: {
|
|
295
|
+
type: "binary_expr",
|
|
296
|
+
operator: op.name,
|
|
297
|
+
left: {
|
|
298
|
+
type: "column_ref",
|
|
299
|
+
table: null,
|
|
300
|
+
column: db.sqlsanitize(field),
|
|
301
|
+
},
|
|
302
|
+
right: {
|
|
303
|
+
type: "number",
|
|
304
|
+
value: "$" + phIndex,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
type: orderDesc ? "DESC" : "ASC",
|
|
308
|
+
},
|
|
309
|
+
];
|
|
310
|
+
phIndex += 1;
|
|
311
|
+
phValues.push(target);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
242
315
|
const client = is_sqlite ? db : await db.getClient();
|
|
243
316
|
await client.query(`BEGIN;`);
|
|
244
317
|
if (!is_sqlite) {
|
|
@@ -247,7 +320,7 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
247
320
|
}
|
|
248
321
|
|
|
249
322
|
const sqlQ = parser.sqlify(ast, opt);
|
|
250
|
-
|
|
323
|
+
console.log({ sqlQ, phValues, opts });
|
|
251
324
|
const qres = await client.query(sqlQ, phValues);
|
|
252
325
|
qres.query = sqlQ;
|
|
253
326
|
await client.query(`ROLLBACK;`);
|