Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/pg",
"version": "2.6.1",
"version": "2.6.2",
"description": "MOST Web Framework PostgreSQL Adapter",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
26 changes: 26 additions & 0 deletions src/PostgreSQLFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,32 @@ class PostgreSQLFormatter extends SqlFormatter {
$date(p0) {
return sprintf('CAST(%s AS DATE)', this.escape(p0));
}

$toString(p0) {
return sprintf('CAST(%s AS VARCHAR)', this.escape(p0));
}

isComparison(obj) {
const key = Object.key(obj);
return (/^\$(eq|ne|lt|lte|gt|gte|in|nin|text|regex)$/g.test(key));
}
isLogical(obj) {
const key = Object.key(obj);
return (/^\$(and|or|not|nor)$/g.test(key));
}

$cond(ifExpr, thenExpr, elseExpr) {
// validate ifExpr which should an instance of QueryExpression or a comparison expression
let ifExpression;
if (instanceOf(ifExpr, QueryExpression)) {
ifExpression = this.formatWhere(ifExpr.$where);
} 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');
}
return sprintf('(CASE %s WHEN TRUE THEN %s ELSE %s END)', ifExpression, this.escape(thenExpr), this.escape(elseExpr));
}
}

export {
Expand Down