mysql-all-in-one
Version:
A package that allows you to have a complete interaction with a MYSQL database, allowing to connect to the database, retrieve data and create queries.
173 lines (172 loc) • 7.82 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const esc_val_1 = require("../../esc_val");
const types_1 = require("../../types");
const utils_1 = require("../../utils");
const types_2 = require("./types");
const mergePrepStatements = (prepStatements, isAnd) => {
if (prepStatements === null ||
prepStatements === undefined ||
!Array.isArray(prepStatements))
return Object.assign({}, types_1.emptyPrepStatement);
const filteredPrepStatements = prepStatements.filter((v) => (0, types_1.isPreparedStatement)(v));
return filteredPrepStatements.length === 0
? Object.assign({}, types_1.emptyPrepStatement) : filteredPrepStatements.reduce((acc, cur) => ({
statement: `(${acc.statement} ${isAnd === true ? 'AND' : 'OR'} ${cur.statement})`,
values: [...acc.values, ...cur.values],
__is_prep_statement: true,
}));
};
const create_conditions = (value, alias, secondaryAlias) => {
let prepStatementQuery = '';
const prepStatementValues = [];
let isAnd = true;
if (Array.isArray(value)) {
value = [...value];
if (value.length > 0 && value[0] === '__or') {
value.shift();
isAnd = false;
}
if (value.length === 0)
return Object.assign({}, types_1.emptyPrepStatement);
const conditions = value.map((v) => create_conditions(v, alias, secondaryAlias));
return mergePrepStatements(conditions, isAnd);
}
if (typeof value === 'string')
throw `strings are not valid as condition, use the sqlExpression function to create custom expressions: sqlExpression\`${value}\``;
if (typeof value !== 'object')
throw `Value must be String or Object type, received type ${typeof value}\n${value}`;
if ((0, types_1.isSqlExpressionPreparedStatement)(value)) {
value.statement = value.statement
.split('__SQL__EXPRESSION__ALIAS__.')
.join(typeof alias === 'string' && alias.length !== 0
? `${alias}.`
: '');
return { values: value.values, statement: value.statement, __is_prep_statement: true, };
}
const operation = (val, column) => {
const colOrVal = (val) => {
if (val instanceof types_1.SqlColumn)
return (0, utils_1.safeApplyAlias)((0, utils_1.escapeNames)(val.column), alias);
prepStatementValues.push(val);
return '?';
};
if (val === undefined)
return;
if (Array.isArray(val)) {
prepStatementValues.push(...val);
return `${column} IN (${val.map((_) => '?').join(',')})`;
}
if ((0, types_2.isOperatorOptionsObject)(val)) {
const { like, notlike, rlike, notrlike, between, notbetween, in: inOperator, is, isnot, notin, regexp, notregexp, '<=>': safeNullEqual, '>': greaterThan, '<': smallerThan, '<>': different, '!=': notEqual, '>=': greatherOrEqual, '<=': smallerOrEqual, '=': equal, } = val;
if (like !== undefined) {
return `${column} LIKE ${colOrVal(like)}`;
}
if (notlike !== undefined) {
return `${column} NOT LIKE ${colOrVal(notlike)}`;
}
if (rlike !== undefined) {
return `${column} RLIKE ${colOrVal(rlike)}`;
}
if (notrlike !== undefined) {
return `${column} NOT RLIKE ${colOrVal(notrlike)}`;
}
if (regexp !== undefined) {
return `${column} REGEXP ${colOrVal(regexp)}`;
}
if (notregexp !== undefined) {
return `${column} NOT REGEXP ${colOrVal(notregexp)}`;
}
if (between !== undefined &&
Array.isArray(between) &&
between.length === 2) {
return `(${column} BETWEEN ${colOrVal(between[0])} AND ${colOrVal(between[1])})`;
}
if (notbetween !== undefined &&
Array.isArray(notbetween) &&
notbetween.length === 2) {
return `(${column} NOT BETWEEN ${colOrVal(notbetween[0])} AND ${colOrVal(notbetween[1])})`;
}
if (inOperator !== undefined &&
Array.isArray(inOperator) &&
inOperator.length !== 0) {
prepStatementValues.push(...inOperator);
return `${column} IN (${inOperator.map((_) => '?').join(',')})`;
}
if (notin !== undefined &&
Array.isArray(notin) &&
notin.length !== 0) {
prepStatementValues.push(...notin);
return `${column} NOT IN (${notin.map((_) => '?').join(',')})`;
}
if (is !== undefined) {
return `${column} IS ${(0, esc_val_1.escVal)(is)}`;
}
if (isnot !== undefined) {
return `${column} IS NOT ${(0, esc_val_1.escVal)(isnot)}`;
}
if (safeNullEqual !== undefined) {
prepStatementValues.push(safeNullEqual);
return `${column} <=> ?`;
}
if (greaterThan !== undefined) {
return `${column} > ${colOrVal(greaterThan)}`;
}
if (smallerThan !== undefined) {
return `${column} < ${colOrVal(smallerThan)}`;
}
if (different !== undefined) {
return `${column} <> ${colOrVal(different)}`;
}
if (notEqual !== undefined) {
return `${column} != ${colOrVal(notEqual)}`;
}
if (greatherOrEqual !== undefined) {
return `${column} >= ${colOrVal(greatherOrEqual)}`;
}
if (smallerOrEqual !== undefined) {
return `${column} <= ${colOrVal(smallerOrEqual)}`;
}
if (equal !== undefined) {
return `${column} = ${colOrVal(equal)}`;
}
return;
}
if (val === null || val === true || val === false) {
return `${column} IS ${(0, esc_val_1.escVal)(val)}`;
}
if ((0, types_1.isSqlExpressionPreparedStatement)(val)) {
val.statement = val.statement
.split('__SQL__EXPRESSION__ALIAS__.')
.join(typeof alias === 'string' && alias.length !== 0
? `${alias}.`
: '');
prepStatementValues.push(...val.values);
return `${column} = ${(0, utils_1.putBrackets)(val.statement)}`;
}
return `${column} = ${colOrVal(val)}`;
};
if ('__or' in value) {
value = Object.assign({}, value);
delete value['__or'];
isAnd = false;
}
const conditions = Object.entries(value)
.map(([key, val]) => {
if (key === '__col_relation' && (0, types_2.isColumnRelationObject)(val)) {
return Object.entries(val)
.map(([col1, col2]) => `${(0, utils_1.safeApplyAlias)((0, utils_1.escapeNames)(col1), alias)} = ${(0, utils_1.safeApplyAlias)((0, utils_1.escapeNames)(col2), secondaryAlias || alias)}`)
.join(isAnd ? ' AND ' : ' OR ');
}
const column = (0, utils_1.safeApplyAlias)((0, utils_1.escapeNames)(key), alias);
const operationResult = operation(val, column);
return val === undefined && operationResult === undefined
? undefined
: operationResult;
})
.filter((v) => v !== undefined)
.join(isAnd ? ' AND ' : ' OR ');
prepStatementQuery = conditions ? `(${conditions})` : '';
return { statement: prepStatementQuery, values: prepStatementValues, __is_prep_statement: true, };
};
exports.default = create_conditions;
;