From b102d8a4361c28c3363fc0684e48ebbf4f7af571 Mon Sep 17 00:00:00 2001 From: Kyriakos Barbounakis Date: Wed, 7 Jun 2023 18:22:51 +0300 Subject: [PATCH] format logical expression in case when expression (#33) * format logical expression in case when * 2.7.1 --- package-lock.json | 4 ++-- package.json | 2 +- src/MSSqlFormatter.js | 9 ++++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 77a8d7d..e2c3a77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@themost/mssql", - "version": "2.7.0", + "version": "2.7.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@themost/mssql", - "version": "2.7.0", + "version": "2.7.1", "license": "BSD-3-Clause", "dependencies": { "async": "^3.2.3", diff --git a/package.json b/package.json index 98759ad..129053e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@themost/mssql", - "version": "2.7.0", + "version": "2.7.1", "description": "MOST Web Framework MSSQL Data Adapter", "main": "dist/index.js", "module": "dist/index.esm.js", diff --git a/src/MSSqlFormatter.js b/src/MSSqlFormatter.js index 18793e3..34a3bf0 100644 --- a/src/MSSqlFormatter.js +++ b/src/MSSqlFormatter.js @@ -270,12 +270,19 @@ class MSSqlFormatter extends SqlFormatter { return sprintf('CAST(%s AS NVARCHAR)', this.escape(p0)); } + isLogical = function(obj) { + for (const key in obj) { + return (/^\$(and|or|not|nor)$/g.test(key)); + } + return false; + } + $cond(ifExpr, thenExpr, elseExpr) { // validate ifExpr which should an instance of QueryExpression or a comparison expression let ifExpression; if (ifExpr instanceof QueryExpression) { ifExpression = this.formatWhere(ifExpr.$where); - } else if (this.isComparison(ifExpr)) { + } else if (this.isComparison(ifExpr) || this.isLogical(ifExpr)) { ifExpression = this.formatWhere(ifExpr); } else { throw new Error('Condition parameter should be an instance of query or comparison expression');