Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: generic error for search with multiple arguments #265

Merged
merged 3 commits into from
Oct 9, 2023
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
12 changes: 6 additions & 6 deletions db-service/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const StandardFunctions = {
* @returns {string}
*/
search: function (ref, arg) {
if (!('val' in arg)) throw `SQLite only supports single value arguments for $search`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you make the error here more generic? It shares the implementation for all databases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of supporting search with multiple args for postgres as shown in #132 so separating the functions seemed like a good first step

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... applied your suggestion :)

if (!('val' in arg)) throw `Only single value arguments are allowed for $search`
const refs = ref.list || [ref],
{ toString } = ref
return '(' + refs.map(ref2 => this.contains(this.tolower(toString(ref2)), this.tolower(arg))).join(' or ') + ')'
Expand All @@ -31,7 +31,7 @@ const StandardFunctions = {
* @param {...string} args
* @returns {string}
*/
concat: (...args) => args.map(a => a.xpr ? `(${a})` : a).join(' || '),
concat: (...args) => args.map(a => (a.xpr ? `(${a})` : a)).join(' || '),

/**
* Generates SQL statement that produces a boolean value indicating whether the first string contains the second string
Expand Down Expand Up @@ -141,9 +141,9 @@ const StandardFunctions = {

// Date and Time Functions

current_date: p => p ? `current_date(${p})`: 'current_date',
current_time: p => p ? `current_time(${p})`: 'current_time',
current_timestamp: p => p ? `current_timestamp(${p})`: 'current_timestamp',
current_date: p => (p ? `current_date(${p})` : 'current_date'),
current_time: p => (p ? `current_time(${p})` : 'current_time'),
current_timestamp: p => (p ? `current_timestamp(${p})` : 'current_timestamp'),

/**
* Generates SQL statement that produces the year of a given timestamp
Expand Down Expand Up @@ -341,7 +341,7 @@ const HANAFunctions = {
*/
years_between(x, y) {
return `floor(${this.months_between(x, y)} / 12)`
}
},
}

for (let each in HANAFunctions) HANAFunctions[each.toUpperCase()] = HANAFunctions[each]
Expand Down
2 changes: 1 addition & 1 deletion postgres/lib/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const session = require('./session.json')

const StandardFunctions = {
session_context: x => {
let sql = `current_setting('${ session[x.val] || x.val }')`
let sql = `current_setting('${session[x.val] || x.val}')`
if (x.val === '$now') sql += '::timestamp'
return sql
},
Expand Down
21 changes: 10 additions & 11 deletions sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class SQLiteService extends SQLService {
dbc.function('session_context', key => dbc[$session][key])
dbc.function('regexp', { deterministic: true }, (re, x) => (RegExp(re).test(x) ? 1 : 0))
dbc.function('ISO', { deterministic: true }, d => d && new Date(d).toISOString())
dbc.function('json_merge', { varargs: true, deterministic: true }, (...args) => args.join('').replace(/}{/g, ','))
dbc.function('json_merge', { varargs: true, deterministic: true }, (...args) =>
args.join('').replace(/}{/g, ','),
)
if (!dbc.memory) dbc.pragma('journal_mode = WAL')
return dbc
},
Expand Down Expand Up @@ -136,7 +138,6 @@ class SQLiteService extends SQLService {
}

static CQN2SQL = class CQN2SQLite extends SQLService.CQN2SQL {

column_alias4(x, q) {
let alias = super.column_alias4(x, q)
if (alias) return alias
Expand Down Expand Up @@ -201,13 +202,7 @@ class SQLiteService extends SQLService {
}

// Used for SQL function expressions
static Functions = { ...super.Functions,
// Ensure ISO strings are returned for date/time functions
current_timestamp: () => 'ISO(current_timestamp)',
// SQLite doesn't support arguments for current_date and current_time
current_date: () => 'current_date',
current_time: () => 'current_time',
}
static Functions = { ...super.Functions, ...require('./func') }

// Used for CREATE TABLE statements
static TypeMap = {
Expand All @@ -219,8 +214,12 @@ class SQLiteService extends SQLService {
Timestamp: () => 'TIMESTAMP_TEXT',
}

get is_distinct_from_() { return 'is not' }
get is_not_distinct_from_() { return 'is' }
get is_distinct_from_() {
return 'is not'
}
get is_not_distinct_from_() {
return 'is'
}

static ReservedWords = { ...super.ReservedWords, ...require('./ReservedWords.json') }
}
Expand Down
17 changes: 17 additions & 0 deletions sqlite/lib/func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const StandardFunctions = {
// Ensure ISO strings are returned for date/time functions
current_timestamp: () => 'ISO(current_timestamp)',
// SQLite doesn't support arguments for current_date and current_time
current_date: () => 'current_date',
current_time: () => 'current_time',
}

const HANAFunctions = {
/** defined in db-service */
}

for (let each in HANAFunctions) HANAFunctions[each.toUpperCase()] = HANAFunctions[each]

module.exports = { ...StandardFunctions, ...HANAFunctions }