@saltcorn/sql 0.3.5 → 0.3.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/table-provider.js +17 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -182,25 +182,38 @@ const runQuery = async (cfg, where, opts) => {
182
182
  const colNames = new Set((cfg?.columns || []).map((c) => c.name));
183
183
  let phIndex = 1;
184
184
  const phValues = [];
185
+ //console.log(ast[0].columns);
185
186
  for (const k of Object.keys(where)) {
186
187
  if (!colNames.has(k)) continue;
187
- const sqlCol = (ast[0].columns || []).find((c) => k === c.as);
188
+ const sqlCol = (ast[0].columns || []).find(
189
+ (c) => k === c.as || (!c.as && k === c.expr?.column)
190
+ );
188
191
  const sqlExprCol = (ast[0].columns || []).find((c) => c.expr?.as == k);
189
192
  let left = sqlExprCol
190
193
  ? { ...sqlExprCol.expr, as: null }
191
194
  : {
192
195
  type: "column_ref",
193
196
  table: sqlCol?.expr?.table,
197
+ column: sqlCol?.expr?.column || db.sqlsanitize(k),
198
+ };
199
+ //console.log({ k, sqlCol, sqlExprCol });
200
+ if (!sqlCol) {
201
+ const starCol = (ast[0].columns || []).find((c) => c.type === "star_ref");
202
+ if (starCol)
203
+ left = {
204
+ type: "column_ref",
205
+ table: starCol?.expr?.table,
194
206
  column: db.sqlsanitize(k),
195
207
  };
208
+ }
196
209
  const newClause = {
197
210
  type: "binary_expr",
198
- operator: "=",
211
+ operator: where[k]?.ilike ? "ILIKE" : "=",
199
212
  left,
200
213
  right: { type: "number", value: "$" + phIndex },
201
214
  };
202
215
  phIndex += 1;
203
- phValues.push(where[k]);
216
+ phValues.push(where[k]?.ilike ? where[k]?.ilike : where[k]);
204
217
  if (!ast[0].where) ast[0].where = newClause;
205
218
  else {
206
219
  ast[0].where = {
@@ -234,7 +247,7 @@ const runQuery = async (cfg, where, opts) => {
234
247
  }
235
248
 
236
249
  const sqlQ = parser.sqlify(ast, opt);
237
- console.log(sqlQ, phValues);
250
+ //console.log(sqlQ, phValues);
238
251
  const qres = await client.query(sqlQ, phValues);
239
252
  qres.query = sqlQ;
240
253
  await client.query(`ROLLBACK;`);