@saltcorn/sql 0.3.4 → 0.3.6

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 +31 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -4,6 +4,7 @@ const Workflow = require("@saltcorn/data/models/workflow");
4
4
  const Form = require("@saltcorn/data/models/form");
5
5
  const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
6
6
  const Field = require("@saltcorn/data/models/field");
7
+ const Table = require("@saltcorn/data/models/table");
7
8
  const { getState } = require("@saltcorn/data/db/state");
8
9
  const SqlString = require("sqlstring");
9
10
  const { Parser } = require("node-sql-parser");
@@ -63,6 +64,14 @@ const configuration_workflow = (req) =>
63
64
  const pkey_options = getState().type_names.filter(
64
65
  (tnm) => getState().types[tnm]?.primaryKey
65
66
  );
67
+ const tables = await Table.find({});
68
+
69
+ const fkey_opts = [
70
+ "File",
71
+ ...tables
72
+ .filter((t) => !t.provider_name && !t.external)
73
+ .map((t) => `Key to ${t.name}`),
74
+ ];
66
75
  const theForm = new Form({
67
76
  blurb: pre(code(qres.query)) + tbl,
68
77
  fields: [
@@ -90,7 +99,9 @@ const configuration_workflow = (req) =>
90
99
  label: "Type",
91
100
  type: "String",
92
101
  required: true,
93
- attributes: { options: getState().type_names },
102
+ attributes: {
103
+ options: getState().type_names.concat(fkey_opts || []),
104
+ },
94
105
  },
95
106
  {
96
107
  name: "primary_key",
@@ -169,25 +180,36 @@ const runQuery = async (cfg, where, opts) => {
169
180
  const { ast } = parser.parse(sql, opt);
170
181
 
171
182
  const colNames = new Set((cfg?.columns || []).map((c) => c.name));
172
-
173
183
  let phIndex = 1;
174
184
  const phValues = [];
175
185
  for (const k of Object.keys(where)) {
176
186
  if (!colNames.has(k)) continue;
177
187
  const sqlCol = (ast[0].columns || []).find((c) => k === c.as);
178
- let left = {
179
- type: "column_ref",
180
- table: sqlCol?.expr?.table,
181
- column: db.sqlsanitize(k),
182
- };
188
+ const sqlExprCol = (ast[0].columns || []).find((c) => c.expr?.as == k);
189
+ let left = sqlExprCol
190
+ ? { ...sqlExprCol.expr, as: null }
191
+ : {
192
+ type: "column_ref",
193
+ table: sqlCol?.expr?.table,
194
+ column: db.sqlsanitize(k),
195
+ };
196
+ if (!sqlCol) {
197
+ const starCol = (ast[0].columns || []).find((c) => c.type === "star_ref");
198
+ if (starCol)
199
+ left = {
200
+ type: "column_ref",
201
+ table: starCol?.expr?.table,
202
+ column: db.sqlsanitize(k),
203
+ };
204
+ }
183
205
  const newClause = {
184
206
  type: "binary_expr",
185
- operator: "=",
207
+ operator: where[k]?.ilike ? "ILIKE" : "=",
186
208
  left,
187
209
  right: { type: "number", value: "$" + phIndex },
188
210
  };
189
211
  phIndex += 1;
190
- phValues.push(where[k]);
212
+ phValues.push(where[k]?.ilike ? where[k]?.ilike : where[k]);
191
213
  if (!ast[0].where) ast[0].where = newClause;
192
214
  else {
193
215
  ast[0].where = {