@saltcorn/sql 0.3.4 → 0.3.5

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 +21 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
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,17 +180,19 @@ 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
+ };
183
196
  const newClause = {
184
197
  type: "binary_expr",
185
198
  operator: "=",
@@ -221,6 +234,7 @@ const runQuery = async (cfg, where, opts) => {
221
234
  }
222
235
 
223
236
  const sqlQ = parser.sqlify(ast, opt);
237
+ console.log(sqlQ, phValues);
224
238
  const qres = await client.query(sqlQ, phValues);
225
239
  qres.query = sqlQ;
226
240
  await client.query(`ROLLBACK;`);