@saltcorn/sql 0.3.6 → 0.3.8

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 CHANGED
@@ -133,6 +133,27 @@ module.exports = {
133
133
  plugin_name: "sql",
134
134
  actions: require("./action.js"),
135
135
  table_providers: require("./table-provider.js"),
136
+ functions: {
137
+ sqlQuery: {
138
+ run: async (query, parameters) => {
139
+ const is_sqlite = db.isSQLite;
140
+
141
+ const client = is_sqlite ? db : await db.getClient();
142
+ await client.query(`BEGIN;`);
143
+ if (!is_sqlite) {
144
+ await client.query(
145
+ `SET LOCAL search_path TO "${db.getTenantSchema()}";`
146
+ );
147
+ }
148
+ const qres = await db.query(query, parameters || []);
149
+
150
+ await client.query(`COMMIT;`);
151
+ return qres;
152
+ },
153
+ isAsync: true,
154
+ description: "Run an SQL query",
155
+ },
156
+ },
136
157
  viewtemplates: [
137
158
  {
138
159
  name: "SQLView",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -182,17 +182,21 @@ 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,
194
- column: db.sqlsanitize(k),
197
+ column: sqlCol?.expr?.column || db.sqlsanitize(k),
195
198
  };
199
+ //console.log({ k, sqlCol, sqlExprCol });
196
200
  if (!sqlCol) {
197
201
  const starCol = (ast[0].columns || []).find((c) => c.type === "star_ref");
198
202
  if (starCol)
@@ -243,6 +247,7 @@ const runQuery = async (cfg, where, opts) => {
243
247
  }
244
248
 
245
249
  const sqlQ = parser.sqlify(ast, opt);
250
+ //console.log(sqlQ, phValues);
246
251
  const qres = await client.query(sqlQ, phValues);
247
252
  qres.query = sqlQ;
248
253
  await client.query(`ROLLBACK;`);