UNPKG

ts-sql-query

Version:

Type-safe SQL query builder like QueryDSL or JOOQ in Java or Linq in .Net for TypeScript with MariaDB, MySql, Oracle, PostgreSql, Sqlite and SqlServer support.

718 lines (717 loc) 36.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SqliteSqlBuilder = void 0; const SqlBuilder_1 = require("./SqlBuilder"); const values_1 = require("../expressions/values"); const AbstractSqlBuilder_1 = require("./AbstractSqlBuilder"); const values_2 = require("../expressions/values"); const Column_1 = require("../utils/Column"); class SqliteSqlBuilder extends AbstractSqlBuilder_1.AbstractSqlBuilder { constructor() { super(); this.sqlite = true; this._trueValue = '1'; this._falseValue = '0'; this._trueValueForCondition = '1'; this._falseValueForCondition = '0'; this._operationsThatNeedParenthesis._getMonth = true; this._operationsThatNeedParenthesis._getMilliseconds = true; } _getDateTimeFormat(type) { return this._connectionConfiguration.getDateTimeFormat(type); } _getUuidStrategy() { return this._connectionConfiguration.uuidStrategy || 'uuid-extension'; } _getValueSourceDateTimeFormat(valueSource) { if ((0, values_1.isValueSource)(valueSource)) { const valueSourcePrivate = (0, values_2.__getValueSourcePrivate)(valueSource); if ((0, values_1.__isLocalDateValueSource)(valueSourcePrivate)) { return this._getDateTimeFormat('date'); } else if ((0, values_1.__isLocalTimeValueSource)(valueSourcePrivate)) { return this._getDateTimeFormat('time'); } else if ((0, values_1.__isLocalDateTimeValueSource)(valueSourcePrivate)) { return this._getDateTimeFormat('dateTime'); } else { throw new Error('Unknown date type: ' + valueSourcePrivate.__valueType + ' ' + valueSourcePrivate.__valueTypeName); } } throw new Error('Unable to determine the value source type'); } _isReservedKeyword(word) { return word.toUpperCase() in reservedWords; } _buildSelectWithColumnsInfoForCompound(query, params, columnsForInsert, isOutermostQuery) { const result = this._buildSelectWithColumnsInfo(query, params, columnsForInsert, isOutermostQuery); if (query.__limit !== undefined || query.__offset !== undefined || query.__orderBy || query.__customization?.beforeOrderByItems || query.__customization?.afterOrderByItems) { return 'select * from (' + result + ')'; } return result; } _buildSelectOrderBy(query, params) { if (!this._connectionConfiguration.compatibilityMode) { return super._buildSelectOrderBy(query, params); } const orderBy = query.__orderBy; if (!orderBy) { let orderByColumns = ''; const customization = query.__customization; if (customization && customization.beforeOrderByItems) { orderByColumns += this._appendRawFragment(customization.beforeOrderByItems, params); } if (customization && customization.afterOrderByItems) { if (orderByColumns) { orderByColumns += ', '; } orderByColumns += this._appendRawFragment(customization.afterOrderByItems, params); } if (!orderByColumns) { return ''; } return ' order by ' + orderByColumns; } let orderByColumns = ''; const customization = query.__customization; if (customization && customization.beforeOrderByItems) { orderByColumns += this._appendRawFragment(customization.beforeOrderByItems, params); } for (const entry of orderBy) { if (orderByColumns) { orderByColumns += ', '; } const order = entry.order; if (!order) { orderByColumns += this._appendOrderByColumnAlias(entry, query, params); } else switch (order) { case 'asc': case 'asc nulls first': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' asc'; break; case 'desc': case 'desc nulls last': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' desc'; break; case 'asc nulls last': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' is null, ' + this._appendOrderByColumnAlias(entry, query, params) + ' asc'; break; case 'desc nulls first': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' is not null, ' + this._appendOrderByColumnAlias(entry, query, params) + ' desc'; break; case 'insensitive': orderByColumns += this._appendOrderByColumnAliasInsensitive(entry, query, params); break; case 'asc insensitive': case 'asc nulls first insensitive': orderByColumns += this._appendOrderByColumnAliasInsensitive(entry, query, params) + ' asc'; break; case 'desc insensitive': case 'desc nulls last insensitive': orderByColumns += this._appendOrderByColumnAliasInsensitive(entry, query, params) + ' desc'; break; case 'asc nulls last insensitive': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' is null, ' + this._appendOrderByColumnAliasInsensitive(entry, query, params) + ' asc'; break; case 'desc nulls first insensitive': orderByColumns += this._appendOrderByColumnAlias(entry, query, params) + ' is not null, ' + this._appendOrderByColumnAliasInsensitive(entry, query, params) + ' desc'; break; default: throw new Error('Invalid order by: ' + order); } } if (customization && customization.afterOrderByItems) { if (orderByColumns) { orderByColumns += ', '; } orderByColumns += this._appendRawFragment(customization.afterOrderByItems, params); } if (!orderByColumns) { return ''; } return ' order by ' + orderByColumns; } _buildSelectLimitOffset(query, params) { let result = ''; const limit = query.__limit; if (limit !== null && limit !== undefined) { result += ' limit ' + this._appendValue(limit, params, 'int', 'int', undefined); } const offset = query.__offset; if (offset !== null && offset !== undefined) { if (!result) { // Sqlite doesn't support an offset without a limit, let put the the max value of an int result += ' limit 2147483647'; } result += ' offset ' + this._appendValue(offset, params, 'int', 'int', undefined); } return result; } _appendUpdateOldValueForUpdate(_query, _updatePrimaryKey, _requiredTables, _params) { return ''; } _buildInsertOutput(_query, _params) { return ''; } _buildInsertReturning(query, params) { if (!this._connectionConfiguration.compatibilityMode || query.__from || query.__multiple || query.__columns || query.__onConflictUpdateSets) { return super._buildInsertReturning(query, params); } this._setContainsInsertReturningClause(params, false); return ''; } _is(params, valueSource, value, columnType, columnTypeName, typeAdapter) { if ((0, Column_1.isColumn)(valueSource) && (0, Column_1.isColumn)(value) && this._hasSameBooleanTypeAdapter(valueSource, value)) { return this._appendRawColumnName(valueSource, params) + ' is ' + this._appendRawColumnName(value, params); } return this._appendSqlParenthesis(valueSource, params) + ' is ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter); } _isNot(params, valueSource, value, columnType, columnTypeName, typeAdapter) { if ((0, Column_1.isColumn)(valueSource) && (0, Column_1.isColumn)(value) && this._hasSameBooleanTypeAdapter(valueSource, value)) { return this._appendRawColumnName(valueSource, params) + ' is not ' + this._appendRawColumnName(value, params); } return this._appendSqlParenthesis(valueSource, params) + ' is not ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter); } _currentDate(_params) { const dateTimeFormat = this._getDateTimeFormat('date'); switch (dateTimeFormat) { case 'localdate as text': case 'localdate as text using T separator': return "date('now', 'localtime')"; case 'UTC as text': case 'UTC as text using T separator': case 'UTC as text using Z timezone': case 'UTC as text using T separator and Z timezone': return "date('now')"; case 'Julian day as real number': return "julianday(date('now'))"; case 'Unix time seconds as integer': return "cast(strftime('%s', date('now')) as integer)"; case 'Unix time milliseconds as integer': return "(cast(strftime('%s', date('now')) as integer) * 1000)"; default: throw new Error('Invalid sqlite date time format: ' + dateTimeFormat); } } _currentTime(_params) { const dateTimeFormat = this._getDateTimeFormat('time'); switch (dateTimeFormat) { case 'localdate as text': case 'localdate as text using T separator': return "time('now', 'localtime')"; case 'UTC as text': case 'UTC as text using T separator': return "time('now')"; case 'UTC as text using Z timezone': case 'UTC as text using T separator and Z timezone': return "(time('now') || 'Z')"; case 'Julian day as real number': return "(julianday(strftime('1970-01-01 %H:%M:%f', 'now')) - julianday('1970-01-01'))"; case 'Unix time seconds as integer': return "cast(strftime('%s', strftime('1970-01-01 %H:%M:%S', 'now')) as integer)"; case 'Unix time milliseconds as integer': return "cast((julianday(strftime('1970-01-01 %H:%M:%f', 'now')) - 2440587.5) * 86400000.0 as integer)"; default: throw new Error('Invalid sqlite date time format: ' + dateTimeFormat); } } _currentTimestamp(_params) { const dateTimeFormat = this._getDateTimeFormat('dateTime'); switch (dateTimeFormat) { case 'localdate as text': return "datetime('now', 'localtime')"; case 'localdate as text using T separator': return "strftime('%Y-%m-%dT%H:%M:%S', 'now', 'localtime')"; case 'UTC as text': return "datetime('now')"; case 'UTC as text using T separator': return "strftime('%Y-%m-%dT%H:%M:%S', 'now')"; case 'UTC as text using Z timezone': return "strftime('%Y-%m-%d %H:%M:%SZ', 'now')"; case 'UTC as text using T separator and Z timezone': return "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')"; case 'Julian day as real number': return "julianday('now')"; case 'Unix time seconds as integer': return "cast(strftime('%s', 'now') as integer)"; case 'Unix time milliseconds as integer': return "cast((julianday('now') - 2440587.5) * 86400000.0 as integer)"; default: throw new Error('Invalid sqlite date time format: ' + dateTimeFormat); } } _valueWhenNull(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return 'ifnull(' + this._appendSql(valueSource, params) + ', ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ')'; } _divide(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return 'cast(' + this._appendSql(valueSource, params) + ' as real) / cast(' + this._appendValue(value, params, this._getMathArgumentType(columnType, columnTypeName, value), this._getMathArgumentTypeName(columnType, columnTypeName, value), typeAdapter) + ' as real)'; } _asDouble(params, valueSource) { return 'cast(' + this._appendSql(valueSource, params) + 'as real)'; } _ln(params, valueSource) { return 'log(' + this._appendSql(valueSource, params) + ')'; } _log10(params, valueSource) { return 'log10(' + this._appendSql(valueSource, params) + ')'; } _cbrt(params, valueSource) { return 'power(' + this._appendSql(valueSource, params) + ', 3)'; } _minimumBetweenTwoValues(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return 'min(' + this._appendSql(valueSource, params) + ', ' + this._appendValue(value, params, this._getMathArgumentType(columnType, columnTypeName, value), this._getMathArgumentTypeName(columnType, columnTypeName, value), typeAdapter) + ')'; } _maximumBetweenTwoValues(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return 'max(' + this._appendSql(valueSource, params) + ', ' + this._appendValue(value, params, this._getMathArgumentType(columnType, columnTypeName, value), this._getMathArgumentTypeName(columnType, columnTypeName, value), typeAdapter) + ')'; } _getDate(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%d', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%d', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%d', " + this._appendSql(valueSource, params) + ") as integer)"; } _getTime(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return '(' + this._appendSql(valueSource, params) + ' * 1000)'; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return this._appendSql(valueSource, params); } return "round((julianday(" + this._appendSql(valueSource, params) + ") - 2440587.5) * 86400000.0)"; } _getFullYear(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%Y', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%Y', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%Y', " + this._appendSql(valueSource, params) + ") as integer)"; } _getMonth(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%m', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer) - 1"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%m', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer) - 1"; } return "cast(strftime('%m', " + this._appendSql(valueSource, params) + ") as integer) - 1"; } _getDay(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%w'," + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%w'," + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%w'," + this._appendSql(valueSource, params) + ") as integer)"; } _getHours(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%H', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%H', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%H', " + this._appendSql(valueSource, params) + ") as integer)"; } _getMinutes(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%M', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%M', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%M', " + this._appendSql(valueSource, params) + ") as integer)"; } _getSeconds(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return "cast(strftime('%S', " + this._appendSql(valueSource, params) + ", 'unixepoch') as integer)"; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return "cast(strftime('%S', " + this._appendSqlParenthesis(valueSource, params) + " / 1000, 'unixepoch') as integer)"; } return "cast(strftime('%S', " + this._appendSql(valueSource, params) + ") as integer)"; } _getMilliseconds(params, valueSource) { if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time seconds as integer') { return '0'; } else if (this._getValueSourceDateTimeFormat(valueSource) === 'Unix time milliseconds as integer') { return this._appendSqlParenthesis(valueSource, params) + ' % 1000'; } return "strftime('%f', " + this._appendSql(valueSource, params) + ") * 1000 % 1000"; } _like(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + ' like ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'"; } _notLike(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + ' not like ' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'"; } _likeInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ' collate ' + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + ' like ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ') like lower(' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } } _notLikeInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + ' collate ' + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + ' not like ' + this._appendValueParenthesis(value, params, columnType, columnTypeName, typeAdapter) + " escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ') not like lower(' + this._appendValue(value, params, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } } _startsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } _notStartsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } _endsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } _notEndsWith(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } _startsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + ' like ((' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + ' like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ') like lower(' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } } _notStartsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + ' not like ((' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + ' not like (' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ') not like lower(' + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } } _endsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + " like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ") like lower('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } } _notEndsWithInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + " not like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ") not like lower('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + ") escape '\\'"; } } _contains(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } _notContains(params, valueSource, value, columnType, columnTypeName, typeAdapter) { return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } _containsInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + " like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + " like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ") like lower('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } } _notContainsInsensitive(params, valueSource, value, columnType, columnTypeName, typeAdapter) { const collation = this._connectionConfiguration.insesitiveCollation; if (collation) { return this._appendSqlParenthesis(valueSource, params) + " not like (('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') collate " + collation + ") escape '\\'"; } else if (collation === '') { return this._appendSqlParenthesis(valueSource, params) + " not like ('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } else { return 'lower(' + this._appendSql(valueSource, params) + ") not like lower('%' || " + this._escapeLikeWildcard(params, value, columnType, columnTypeName, typeAdapter) + " || '%') escape '\\'"; } } _stringConcat(params, separator, value) { if (separator === undefined || separator === null) { return 'group_concat(' + this._appendSql(value, params) + ')'; } else if (separator === '') { return 'group_concat(' + this._appendSql(value, params) + ", '')"; } else { return 'group_concat(' + this._appendSql(value, params) + ', ' + this._appendValue(separator, params, 'string', 'string', undefined) + ')'; } } _stringConcatDistinct(params, separator, value) { if (separator === undefined || separator === null) { return 'group_concat(distinct ' + this._appendSql(value, params) + ')'; } else if (separator === '') { return 'group_concat(distinct ' + this._appendSql(value, params) + ", '')"; } else { return 'group_concat(distinct ' + this._appendSql(value, params) + ', ' + this._appendValue(separator, params, 'string', 'string', undefined) + ')'; } } _appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) { if ((0, values_1.__isUuidValueType)(columnType) && this._getUuidStrategy() === 'uuid-extension') { return 'uuid_blob(' + super._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast) + ')'; } return super._appendParam(value, params, columnType, columnTypeName, typeAdapter, forceTypeCast); } _appendColumnValue(value, params, isOutermostQuery) { if (isOutermostQuery && this._getUuidStrategy() === 'uuid-extension') { if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(value))) { return 'uuid_str(' + this._appendSql(value, params) + ')'; } } return this._appendSql(value, params); } _asString(params, valueSource) { // Transform an uuid to string if (this._getUuidStrategy() === 'string') { // No conversion required return this._appendSql(valueSource, params); } return 'uuid_str(' + this._appendSql(valueSource, params) + ')'; } _appendAggragateArrayColumns(aggregatedArrayColumns, aggregatedArrayDistinct, params, _query) { const distict = aggregatedArrayDistinct ? 'distinct ' : ''; if ((0, values_1.isValueSource)(aggregatedArrayColumns)) { if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(aggregatedArrayColumns)) && this._getUuidStrategy() === 'uuid-extension') { return 'json_group_array(' + distict + 'uuid_str(' + this._appendSql(aggregatedArrayColumns, params) + '))'; } return 'json_group_array(' + distict + this._appendSql(aggregatedArrayColumns, params) + ')'; } else { const columns = {}; (0, SqlBuilder_1.flattenQueryColumns)(aggregatedArrayColumns, columns, ''); let result = ''; for (let prop in columns) { if (result) { result += ', '; } result += "'" + prop + "', "; const column = columns[prop]; if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(column)) && this._getUuidStrategy() === 'uuid-extension') { result += 'uuid_str(' + this._appendSql(column, params) + ')'; } else { result += this._appendSql(column, params); } } return 'json_group_array(' + distict + 'json_object(' + result + '))'; } } _appendAggragateArrayWrappedColumns(aggregatedArrayColumns, _params, aggregateId) { if ((0, values_1.isValueSource)(aggregatedArrayColumns)) { if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(aggregatedArrayColumns)) && this._getUuidStrategy() === 'uuid-extension') { return 'json_group_array(uuid_str(a_' + aggregateId + '_.result))'; } return 'json_group_array(a_' + aggregateId + '_.result)'; } else { const columns = {}; (0, SqlBuilder_1.flattenQueryColumns)(aggregatedArrayColumns, columns, ''); let result = ''; for (let prop in columns) { if (result) { result += ', '; } result += "'" + prop + "', "; const column = columns[prop]; if ((0, values_1.__isUuidValueSource)((0, values_2.__getValueSourcePrivate)(column)) && this._getUuidStrategy() === 'uuid-extension') { result += 'uuid_str(a_' + aggregateId + '_.' + this._escape(prop, true) + ')'; } else { result += 'a_' + aggregateId + '_.' + this._escape(prop, true); } } return 'json_group_array(json_object(' + result + '))'; } } } exports.SqliteSqlBuilder = SqliteSqlBuilder; // Source: https://www.sqlite.org/lang_keywords.html (version: 3.30.1) const reservedWords = { ABORT: true, ACTION: true, ADD: true, AFTER: true, ALL: true, ALTER: true, ANALYZE: true, AND: true, AS: true, ASC: true, ATTACH: true, AUTOINCREMENT: true, BEFORE: true, BEGIN: true, BETWEEN: true, BY: true, CASCADE: true, CASE: true, CAST: true, CHECK: true, COLLATE: true, COLUMN: true, COMMIT: true, CONFLICT: true, CONSTRAINT: true, CREATE: true, CROSS: true, CURRENT: true, CURRENT_DATE: true, CURRENT_TIME: true, CURRENT_TIMESTAMP: true, DATABASE: true, DEFAULT: true, DEFERRABLE: true, DEFERRED: true, DELETE: true, DESC: true, DETACH: true, DISTINCT: true, DO: true, DROP: true, EACH: true, ELSE: true, END: true, ESCAPE: true, EXCEPT: true, EXCLUDE: true, EXCLUSIVE: true, EXISTS: true, EXPLAIN: true, FAIL: true, FILTER: true, FIRST: true, FOLLOWING: true, FOR: true, FOREIGN: true, FROM: true, FULL: true, GLOB: true, GROUP: true, GROUPS: true, HAVING: true, IF: true, IGNORE: true, IMMEDIATE: true, IN: true, INDEX: true, INDEXED: true, INITIALLY: true, INNER: true, INSERT: true, INSTEAD: true, INTERSECT: true, INTO: true, IS: true, ISNULL: true, JOIN: true, KEY: true, LAST: true, LEFT: true, LIKE: true, LIMIT: true, MATCH: true, NATURAL: true, NO: true, NOT: true, NOTHING: true, NOTNULL: true, NULL: true, NULLS: true, OF: true, OFFSET: true, ON: true, OR: true, ORDER: true, OTHERS: true, OUTER: true, OVER: true, PARTITION: true, PLAN: true, PRAGMA: true, PRECEDING: true, PRIMARY: true, QUERY: true, RAISE: true, RANGE: true, RECURSIVE: true, REFERENCES: true, REGEXP: true, REINDEX: true, RELEASE: true, RENAME: true, REPLACE: true, RESTRICT: true, RIGHT: true, ROLLBACK: true, ROW: true, ROWS: true, SAVEPOINT: true, SELECT: true, SET: true, TABLE: true, TEMP: true, TEMPORARY: true, THEN: true, TIES: true, TO: true, TRANSACTION: true, TRIGGER: true, UNBOUNDED: true, UNION: true, UNIQUE: true, UPDATE: true, USING: true, VACUUM: true, VALUES: true, VIEW: true, VIRTUAL: true, WHEN: true, WHERE: true, WINDOW: true, WITH: true, WITHOUT: true };