60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
import { sqlIdent, sqlOperator, sqlRawEquality } from "./sql-safe";
|
|
export default function sqlGenGenJoinStr({ join, mtch, table_name }) {
|
|
let values = [];
|
|
if (mtch.__batch) {
|
|
let btch_mtch = ``;
|
|
btch_mtch += `(`;
|
|
for (let i = 0; i < mtch.__batch.matches.length; i++) {
|
|
const __mtch = mtch.__batch.matches[i];
|
|
const { str, values: batch_values } = sqlGenGenJoinStr({
|
|
join,
|
|
mtch: __mtch,
|
|
table_name,
|
|
});
|
|
btch_mtch += str;
|
|
values.push(...batch_values);
|
|
if (i < mtch.__batch.matches.length - 1) {
|
|
btch_mtch += ` ${sqlOperator(mtch.__batch.operator, "OR")} `;
|
|
}
|
|
}
|
|
btch_mtch += `)`;
|
|
return {
|
|
str: btch_mtch,
|
|
values,
|
|
};
|
|
}
|
|
const equality = mtch.raw_equality ? sqlRawEquality(mtch.raw_equality) : "=";
|
|
const lhs = `${sqlIdent(typeof mtch.source == "object" ? mtch.source.tableName : table_name, "table name")}.${sqlIdent(typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source, "field name")}`;
|
|
if (mtch.between) {
|
|
values.push(mtch.between.min, mtch.between.max);
|
|
return {
|
|
str: `${lhs} BETWEEN ? AND ?`,
|
|
values,
|
|
};
|
|
}
|
|
if (mtch.not_between) {
|
|
values.push(mtch.not_between.min, mtch.not_between.max);
|
|
return {
|
|
str: `${lhs} NOT BETWEEN ? AND ?`,
|
|
values,
|
|
};
|
|
}
|
|
const rhs = (() => {
|
|
if (mtch.targetLiteral !== undefined && mtch.targetLiteral !== null) {
|
|
values.push(mtch.targetLiteral);
|
|
return `?`;
|
|
}
|
|
const targetTable = typeof mtch.target == "object"
|
|
? mtch.target.tableName
|
|
: join.alias || join.tableName;
|
|
const targetField = typeof mtch.target == "object"
|
|
? mtch.target.fieldName
|
|
: mtch.target;
|
|
return `${sqlIdent(targetTable, "table name")}.${sqlIdent(targetField, "field name")}`;
|
|
})();
|
|
return {
|
|
str: `${lhs} ${equality} ${rhs}`,
|
|
values,
|
|
};
|
|
}
|