Skip to content

Commit d44655a

Browse files
authored
Use more than 2 arguments in concat method (#72)
* concat function with more than 2 argumnets * 2.6.11
1 parent b73c741 commit d44655a

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@themost/sqlite",
3-
"version": "2.6.10",
3+
"version": "2.6.11",
44
"description": "MOST Web Framework SQLite Adapter",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

spec/StringFunctions.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { QueryField } from '@themost/query';
12
import { TestApplication } from './TestApplication';
23

34
describe('StringFunctions', () => {
@@ -41,6 +42,25 @@ describe('StringFunctions', () => {
4142
});
4243
});
4344

45+
it('should use concat()', async () => {
46+
await app.executeInTestTranscaction(async (context) => {
47+
let items = await context.model('Product')
48+
.select(new QueryField('id'), new QueryField({
49+
name: {
50+
$concat: [
51+
new QueryField('name'),
52+
'-',
53+
new QueryField('model')
54+
]
55+
}
56+
})).where('name').startsWith('Apple').equal(true).getItems();
57+
expect(items).toBeInstanceOf(Array);
58+
for (const item of items) {
59+
expect(item.name.startsWith('Apple')).toBeTruthy();
60+
}
61+
});
62+
});
63+
4464
it('should use lower()', async () => {
4565
await app.executeInTestTranscaction(async (context) => {
4666
let items = await context.model('Product')

src/SqliteFormatter.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,18 @@ class SqliteFormatter extends SqlFormatter {
136136
* @param {*} p1
137137
* @returns {string}
138138
*/
139+
// eslint-disable-next-line no-unused-vars
139140
$concat(p0, p1) {
140-
return sprintf('(IFNULL(%s,\'\') || IFNULL(%s,\'\'))', this.escape(p0), this.escape(p1));
141+
const args = Array.from(arguments);
142+
if (args.length < 2) {
143+
throw new Error('Concat method expects two or more arguments');
144+
}
145+
let result = '(';
146+
result += Array.from(args).map((arg) => {
147+
return `IFNULL(${this.escape(arg)},\'\')`
148+
}).join(' || ');
149+
result += ')';
150+
return result;
141151
}
142152
/**
143153
* Implements substring(str,pos) expression formatter.

0 commit comments

Comments
 (0)