Add tests, schema sync safeguards, and query/handler improvements
- Add bun test suite: SQL generator and value unit tests, plus MariaDB integration tests gated on BUN_MARIADB_TEST_DB - schema: add --dry-run and require confirmation (or --yes) before dropping tables/columns; block unapproved destructive statements - Always AND targetId with the query, regardless of searchOperator - Initialize lazily on first call; init throws instead of exiting - Generate IN (?, ?, ...) lists for IN / NOT IN - Share value binding between insert and update (booleans, embeddings, function values, "" -> NULL for non-text columns) - Keep Buffers and convert BigInt/Date without a JSON round-trip - DbSQL: report success for empty SELECT results - Run update() in a transaction with row locking - Key the client cache on the full connection config - Consolidate identifier quoting, drop dead code, merge column lookup - Read CLI version from package.json, pin @types/bun, chmod CLI on compile - Rebuild dist Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Vendored
+3
-2
@@ -165,10 +165,11 @@ function findFileContents(dir, predicate) {
|
||||
const current = stack.pop();
|
||||
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
|
||||
const full = path.join(current, entry.name);
|
||||
// Never follow symlinks from an extracted (untrusted) archive
|
||||
if (entry.isDirectory()) {
|
||||
stack.push(full);
|
||||
}
|
||||
else if (predicate(entry.name)) {
|
||||
else if (entry.isFile() && predicate(entry.name)) {
|
||||
return fs.readFileSync(full, "utf-8");
|
||||
}
|
||||
}
|
||||
@@ -184,7 +185,7 @@ function findSchemaFile(dir) {
|
||||
if (entry.isDirectory()) {
|
||||
stack.push(full);
|
||||
}
|
||||
else if (isSchemaFileName(entry.name)) {
|
||||
else if (entry.isFile() && isSchemaFileName(entry.name)) {
|
||||
return {
|
||||
content: fs.readFileSync(full, "utf-8"),
|
||||
fileName: entry.name === AppData.DbSchemaFileName
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
import type { BUN_MARIADB_FieldSchemaType } from "../types";
|
||||
/**
|
||||
* Grab a table's schema fields from the loaded (or last synced) schema.
|
||||
*/
|
||||
export default function grabTableFields(tableName: string): BUN_MARIADB_FieldSchemaType[];
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import { readLiveSchema } from "../functions/live-schema";
|
||||
/**
|
||||
* Grab a table's schema fields from the loaded (or last synced) schema.
|
||||
*/
|
||||
export default function grabTableFields(tableName) {
|
||||
const dbSchema = global.DB_SCHEMA || readLiveSchema();
|
||||
const tableSchema = dbSchema?.tables?.find((t) => t.tableName === tableName);
|
||||
return tableSchema?.fields || [];
|
||||
}
|
||||
Vendored
+1
-6
@@ -1,11 +1,6 @@
|
||||
import sanitizeHtml from "sanitize-html";
|
||||
import { readLiveSchema } from "../functions/live-schema";
|
||||
import getSanitizeHtmlOptions from "./sanitize-html-options";
|
||||
function grabTableFields(tableName) {
|
||||
const dbSchema = global.DB_SCHEMA || readLiveSchema();
|
||||
const tableSchema = dbSchema?.tables?.find((t) => t.tableName === tableName);
|
||||
return tableSchema?.fields || [];
|
||||
}
|
||||
import grabTableFields from "./grab-table-fields";
|
||||
/**
|
||||
* Only fields with an explicit `html: true` flag are sanitized.
|
||||
* Missing / falsy / non-true values are never sanitized.
|
||||
|
||||
Vendored
+5
-4
@@ -18,7 +18,6 @@ export const defaultSanitizeHtmlOptions = {
|
||||
"h6",
|
||||
"img",
|
||||
"div",
|
||||
"button",
|
||||
"pre",
|
||||
"code",
|
||||
"br",
|
||||
@@ -35,10 +34,12 @@ export const defaultSanitizeHtmlOptions = {
|
||||
"colgroup",
|
||||
"col",
|
||||
],
|
||||
// `style` / `id` are intentionally not allowed by default (CSS overlays,
|
||||
// DOM clobbering). Add them via `html_sanitize.allowed_attributes`.
|
||||
allowedAttributes: {
|
||||
a: ["href", "title", "class", "style", "target", "rel"],
|
||||
img: ["src", "alt", "width", "height", "class", "style"],
|
||||
"*": ["style", "class", "title", "id"],
|
||||
a: ["href", "title", "class", "target", "rel"],
|
||||
img: ["src", "alt", "width", "height", "class"],
|
||||
"*": ["class", "title"],
|
||||
},
|
||||
};
|
||||
function uniqueStrings(values) {
|
||||
|
||||
Vendored
+2
@@ -1,11 +1,13 @@
|
||||
import type { ServerQueryEqualities, ServerQueryObject, SQLInsertGenValueType } from "../types";
|
||||
type Params = {
|
||||
/** Already-quoted column reference */
|
||||
fieldName: string;
|
||||
value?: SQLInsertGenValueType;
|
||||
equality?: (typeof ServerQueryEqualities)[number];
|
||||
queryObj: ServerQueryObject<{
|
||||
[key: string]: any;
|
||||
}, string>;
|
||||
/** `value` is an already-quoted column reference, not a parameter */
|
||||
isValueFieldValue?: boolean;
|
||||
};
|
||||
type Return = {
|
||||
|
||||
Vendored
+16
-7
@@ -10,9 +10,12 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
if (queryObj.notNullValue) {
|
||||
return { str: `${fieldName} IS NOT NULL` };
|
||||
}
|
||||
if (value) {
|
||||
// `0` is a real value; only null / undefined / "" count as "no value"
|
||||
const hasValue = value !== undefined && value !== null && value !== "";
|
||||
if (hasValue) {
|
||||
const finalValue = isValueFieldValue ? value : "?";
|
||||
const finalParams = isValueFieldValue ? undefined : value;
|
||||
const likeParams = isValueFieldValue ? undefined : `%${value}%`;
|
||||
if (equality == "MATCH") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN NATURAL LANGUAGE MODE)`,
|
||||
@@ -28,7 +31,7 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
else if (equality == "LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
param: likeParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE_LOWER_RAW") {
|
||||
@@ -40,7 +43,7 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
else if (equality == "LIKE") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: `%${finalParams}%`,
|
||||
param: likeParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "LIKE_RAW") {
|
||||
@@ -52,7 +55,7 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
else if (equality == "NOT_LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
param: likeParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT_LIKE_LOWER_RAW") {
|
||||
@@ -64,7 +67,7 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
else if (equality == "NOT LIKE") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
param: likeParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "NOT LIKE_RAW") {
|
||||
@@ -97,6 +100,12 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality == "IN" || equality == "NOT IN") {
|
||||
return {
|
||||
str: `${fieldName} ${equality} (${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${sqlEqualityParser(equality)} ${finalValue}`,
|
||||
@@ -120,13 +129,13 @@ export default function sqlGenOperatorGen({ fieldName, value, equality, queryObj
|
||||
else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${sqlEqualityParser(equality)} ?`,
|
||||
param: value,
|
||||
param: value ?? "",
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
str: `${fieldName} = ?`,
|
||||
param: value,
|
||||
param: value ?? "",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -2,6 +2,7 @@ import type { ServerQueryParamsJoin, ServerQueryParamsJoinMatchObject, SQLInsert
|
||||
type Param = {
|
||||
mtch: ServerQueryParamsJoinMatchObject;
|
||||
join: ServerQueryParamsJoin;
|
||||
/** Already-quoted root table name */
|
||||
table_name: string;
|
||||
};
|
||||
export default function sqlGenGenJoinStr({ join, mtch, table_name }: Param): {
|
||||
|
||||
+18
-23
@@ -1,3 +1,4 @@
|
||||
import { columnRef, sqlLogicalOperator, sqlRawComparison, } from "./sql-identifier";
|
||||
export default function sqlGenGenJoinStr({ join, mtch, table_name }) {
|
||||
let values = [];
|
||||
if (mtch.__batch) {
|
||||
@@ -13,7 +14,7 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }) {
|
||||
btch_mtch += str;
|
||||
values.push(...batch_values);
|
||||
if (i < mtch.__batch.matches.length - 1) {
|
||||
btch_mtch += ` ${mtch.__batch.operator || "OR"} `;
|
||||
btch_mtch += ` ${sqlLogicalOperator(mtch.__batch.operator || "OR")} `;
|
||||
}
|
||||
}
|
||||
btch_mtch += `)`;
|
||||
@@ -22,28 +23,12 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }) {
|
||||
values,
|
||||
};
|
||||
}
|
||||
const equality = mtch.raw_equality || "=";
|
||||
const lhs = `${typeof mtch.source == "object" ? mtch.source.tableName : table_name}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}`;
|
||||
const rhs = `${(() => {
|
||||
if (mtch.targetLiteral) {
|
||||
values.push(mtch.targetLiteral);
|
||||
// if (typeof mtch.targetLiteral == "number") {
|
||||
// return `${mtch.targetLiteral}`;
|
||||
// }
|
||||
// return `'${mtch.targetLiteral}'`;
|
||||
return `?`;
|
||||
}
|
||||
if (join.alias) {
|
||||
return `${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target}`;
|
||||
}
|
||||
return `${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName}.${typeof mtch.target == "object" ? mtch.target.fieldName : mtch.target}`;
|
||||
})()}`;
|
||||
const equality = mtch.raw_equality
|
||||
? sqlRawComparison(mtch.raw_equality)
|
||||
: "=";
|
||||
const lhs = typeof mtch.source == "object"
|
||||
? columnRef(mtch.source.fieldName, mtch.source.tableName)
|
||||
: columnRef(String(mtch.source), table_name);
|
||||
if (mtch.between) {
|
||||
values.push(mtch.between.min, mtch.between.max);
|
||||
return {
|
||||
@@ -58,6 +43,16 @@ export default function sqlGenGenJoinStr({ join, mtch, table_name }) {
|
||||
values,
|
||||
};
|
||||
}
|
||||
const rhs = (() => {
|
||||
if (mtch.targetLiteral !== undefined && mtch.targetLiteral !== null) {
|
||||
values.push(mtch.targetLiteral);
|
||||
return `?`;
|
||||
}
|
||||
if (typeof mtch.target == "object") {
|
||||
return columnRef(mtch.target.fieldName, mtch.target.tableName);
|
||||
}
|
||||
return columnRef(String(mtch.target), join.alias || join.tableName);
|
||||
})();
|
||||
return {
|
||||
str: `${lhs} ${equality} ${rhs}`,
|
||||
values,
|
||||
|
||||
+1
@@ -7,6 +7,7 @@ type Param<T extends {
|
||||
genObject?: ServerQueryParam<T>;
|
||||
selectFields?: (keyof T | TableSelectFieldsObject<T>)[];
|
||||
append_table_names?: boolean;
|
||||
/** Already-quoted table name */
|
||||
table_name: string;
|
||||
full_text_match_str?: string;
|
||||
full_text_search_str?: string;
|
||||
|
||||
+77
-76
@@ -2,6 +2,7 @@ import _, { isUndefined } from "lodash";
|
||||
import sqlGenGrabConcatStr from "./sql-generator-grab-concat-str";
|
||||
import sqlGenGenJoinStr from "./sql-generator-gen-join-str";
|
||||
import sqlGenGrabSelectFieldSQL from "./sql-generator-grab-select-field-sql";
|
||||
import { columnRef, quoteIdentifier, quoteQualified, sqlJoinType, sqlLogicalOperator, } from "./sql-identifier";
|
||||
export default function sqlGenGenQueryStr(params) {
|
||||
let str = "SELECT";
|
||||
const genObject = params.genObject;
|
||||
@@ -42,106 +43,108 @@ export default function sqlGenGenQueryStr(params) {
|
||||
if (!countSubQuery)
|
||||
continue;
|
||||
const tableAlias = countSubQuery.table_alias;
|
||||
const countTableRef = quoteQualified(tableAlias || countSubQuery.table);
|
||||
let subQStr = `(SELECT COUNT(*)`;
|
||||
subQStr += ` FROM ${countSubQuery.table}${tableAlias ? ` ${tableAlias}` : ""}`;
|
||||
subQStr += ` FROM ${quoteQualified(countSubQuery.table)}${tableAlias ? ` ${quoteIdentifier(tableAlias)}` : ""}`;
|
||||
subQStr += ` WHERE (`;
|
||||
for (let j = 0; j < countSubQuery.srcTrgMap.length; j++) {
|
||||
const csqSrc = countSubQuery.srcTrgMap[j];
|
||||
if (!csqSrc)
|
||||
continue;
|
||||
subQStr += ` ${tableAlias || countSubQuery.table}.${csqSrc.src}`;
|
||||
subQStr += ` ${countTableRef}.${quoteIdentifier(csqSrc.src)}`;
|
||||
if (typeof csqSrc.trg == "string") {
|
||||
subQStr += ` = ?`;
|
||||
sqlSearhValues.push(csqSrc.trg);
|
||||
}
|
||||
else if (typeof csqSrc.trg == "object") {
|
||||
subQStr += ` = ${csqSrc.trg.table}.${csqSrc.trg.field}`;
|
||||
subQStr += ` = ${columnRef(csqSrc.trg.field, csqSrc.trg.table)}`;
|
||||
}
|
||||
if (j < countSubQuery.srcTrgMap.length - 1) {
|
||||
subQStr += ` AND `;
|
||||
}
|
||||
}
|
||||
subQStr += ` )) AS ${countSubQuery.alias}`;
|
||||
subQStr += ` )) AS ${quoteIdentifier(countSubQuery.alias)}`;
|
||||
countSqls.push(subQStr);
|
||||
}
|
||||
str += `, ${countSqls.join(",")}`;
|
||||
}
|
||||
if (genObject?.join) {
|
||||
const existingJoinTableNames = [table_name];
|
||||
str +=
|
||||
"," +
|
||||
genObject.join
|
||||
.flat()
|
||||
.filter((j) => !isUndefined(j))
|
||||
.map((joinObj) => {
|
||||
const joinTableName = joinObj.alias
|
||||
? joinObj.alias
|
||||
: joinObj.tableName;
|
||||
if (existingJoinTableNames.includes(joinTableName))
|
||||
return null;
|
||||
existingJoinTableNames.push(joinTableName);
|
||||
if (joinObj.group_concat) {
|
||||
return sqlGenGrabConcatStr({
|
||||
field: `${joinTableName}.${joinObj.group_concat.field}`,
|
||||
alias: joinObj.group_concat.alias,
|
||||
separator: joinObj.group_concat.separator,
|
||||
});
|
||||
const joinSelects = genObject.join
|
||||
.flat()
|
||||
.filter((j) => !isUndefined(j))
|
||||
.map((joinObj) => {
|
||||
const joinTableName = joinObj.alias
|
||||
? joinObj.alias
|
||||
: joinObj.tableName;
|
||||
const joinTableRef = quoteQualified(joinTableName);
|
||||
if (existingJoinTableNames.includes(joinTableRef))
|
||||
return null;
|
||||
existingJoinTableNames.push(joinTableRef);
|
||||
if (joinObj.group_concat) {
|
||||
return sqlGenGrabConcatStr({
|
||||
field: columnRef(joinObj.group_concat.field, joinTableName),
|
||||
alias: joinObj.group_concat.alias,
|
||||
separator: joinObj.group_concat.separator,
|
||||
});
|
||||
}
|
||||
else if (joinObj.selectFields) {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return columnRef(selectField, joinTableName);
|
||||
}
|
||||
else if (joinObj.selectFields) {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${joinTableName}.${selectField}`;
|
||||
}
|
||||
else if (typeof selectField == "object") {
|
||||
let aliasSelectField = `${joinTableName}.${selectField.field}`;
|
||||
if (selectField.count) {
|
||||
aliasSelectField = `COUNT(${joinTableName}.${selectField.field})`;
|
||||
}
|
||||
else if (selectField.sum) {
|
||||
aliasSelectField = `SUM(${selectField.distinct ? "DISTINCT " : ""}${joinTableName}.${selectField.field})`;
|
||||
}
|
||||
else if (selectField.average) {
|
||||
aliasSelectField = `AVERAGE(${joinTableName}.${selectField.field})`;
|
||||
}
|
||||
else if (selectField.max) {
|
||||
aliasSelectField = `MAX(${joinTableName}.${selectField.field})`;
|
||||
}
|
||||
else if (selectField.min) {
|
||||
aliasSelectField = `MIN(${joinTableName}.${selectField.field})`;
|
||||
}
|
||||
else if (selectField.group_concat &&
|
||||
selectField.alias) {
|
||||
return sqlGenGrabConcatStr({
|
||||
field: `${joinTableName}.${selectField.field}`,
|
||||
alias: selectField.alias,
|
||||
separator: selectField.group_concat
|
||||
.separator,
|
||||
distinct: selectField.group_concat
|
||||
.distinct,
|
||||
});
|
||||
}
|
||||
else if (selectField.distinct) {
|
||||
aliasSelectField = `DISTINCT ${joinTableName}.${selectField.field}`;
|
||||
}
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
}
|
||||
})
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${joinTableName}.*`;
|
||||
else if (typeof selectField == "object") {
|
||||
const fieldRef = columnRef(String(selectField.field), joinTableName);
|
||||
let aliasSelectField = fieldRef;
|
||||
if (selectField.count) {
|
||||
aliasSelectField = `COUNT(${fieldRef})`;
|
||||
}
|
||||
else if (selectField.sum) {
|
||||
aliasSelectField = `SUM(${selectField.distinct ? "DISTINCT " : ""}${fieldRef})`;
|
||||
}
|
||||
else if (selectField.average) {
|
||||
aliasSelectField = `AVG(${fieldRef})`;
|
||||
}
|
||||
else if (selectField.max) {
|
||||
aliasSelectField = `MAX(${fieldRef})`;
|
||||
}
|
||||
else if (selectField.min) {
|
||||
aliasSelectField = `MIN(${fieldRef})`;
|
||||
}
|
||||
else if (selectField.group_concat &&
|
||||
selectField.alias) {
|
||||
return sqlGenGrabConcatStr({
|
||||
field: fieldRef,
|
||||
alias: selectField.alias,
|
||||
separator: selectField.group_concat.separator,
|
||||
distinct: selectField.group_concat.distinct,
|
||||
});
|
||||
}
|
||||
else if (selectField.distinct) {
|
||||
aliasSelectField = `DISTINCT ${fieldRef}`;
|
||||
}
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${quoteIdentifier(selectField.alias)}`;
|
||||
return aliasSelectField;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${joinTableRef}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_));
|
||||
if (joinSelects.length) {
|
||||
str += "," + joinSelects.join(",");
|
||||
}
|
||||
}
|
||||
if (genObject?.fullTextSearch &&
|
||||
genObject.fullTextSearch.scoreAlias &&
|
||||
full_text_match_str &&
|
||||
full_text_search_str) {
|
||||
str += `, ${full_text_match_str} AS ${genObject.fullTextSearch.scoreAlias}`;
|
||||
str += `, ${full_text_match_str} AS ${quoteIdentifier(genObject.fullTextSearch.scoreAlias)}`;
|
||||
sqlSearhValues.push(full_text_search_str);
|
||||
}
|
||||
str += ` FROM ${table_name}`;
|
||||
@@ -152,11 +155,11 @@ export default function sqlGenGenQueryStr(params) {
|
||||
.flat()
|
||||
.filter((j) => !isUndefined(j))
|
||||
.map((join) => {
|
||||
return (join.joinType +
|
||||
return (sqlJoinType(join.joinType) +
|
||||
" " +
|
||||
(join.alias
|
||||
? `${join.tableName}` + " " + join.alias
|
||||
: `${join.tableName}`) +
|
||||
? `${quoteQualified(join.tableName)} ${quoteIdentifier(join.alias)}`
|
||||
: `${quoteQualified(join.tableName)}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@@ -172,9 +175,7 @@ export default function sqlGenGenQueryStr(params) {
|
||||
sqlSearhValues.push(...values);
|
||||
return str;
|
||||
})
|
||||
.join(join.operator
|
||||
? ` ${join.operator} `
|
||||
: " AND ") +
|
||||
.join(` ${sqlLogicalOperator(join.operator)} `) +
|
||||
")");
|
||||
}
|
||||
else if (typeof join.match == "object") {
|
||||
|
||||
@@ -3,6 +3,7 @@ type Param = {
|
||||
queryObj: ServerQueryQueryObject[string];
|
||||
join?: (ServerQueryParamsJoin | ServerQueryParamsJoin[] | undefined)[];
|
||||
field?: string;
|
||||
/** Already-quoted root table name */
|
||||
table_name: string;
|
||||
};
|
||||
export default function sqlGenGenSearchStr({ queryObj, join, field, table_name, }: Param): {
|
||||
|
||||
+63
-43
@@ -1,92 +1,112 @@
|
||||
import sqlGenOperatorGen from "./sql-gen-operator-gen";
|
||||
import { columnRef, sqlLogicalOperator, sqlRawComparison, } from "./sql-identifier";
|
||||
export default function sqlGenGenSearchStr({ queryObj, join, field, table_name, }) {
|
||||
let sqlSearhValues = [];
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
return columnRef(String(field), queryObj.tableName);
|
||||
}
|
||||
if (join) {
|
||||
return `${table_name}.${field}`;
|
||||
return columnRef(String(field), table_name);
|
||||
}
|
||||
return field;
|
||||
return columnRef(String(field));
|
||||
})();
|
||||
let str = `${finalFieldName}=?`;
|
||||
function grabValue(val) {
|
||||
const valueParsed = val;
|
||||
if (!valueParsed)
|
||||
if (val === undefined || val === null)
|
||||
return;
|
||||
const valueString = typeof valueParsed == "string" || typeof valueParsed == "number"
|
||||
? valueParsed
|
||||
: valueParsed
|
||||
? valueParsed.fieldName && valueParsed.tableName
|
||||
? `${valueParsed.tableName}.${valueParsed.fieldName}`
|
||||
: valueParsed.value
|
||||
: undefined;
|
||||
const valueEquality = typeof valueParsed == "object"
|
||||
? valueParsed.equality || queryObj.equality
|
||||
const isFieldRef = Boolean(typeof val == "object" && val.fieldName && val.tableName);
|
||||
const valueString = typeof val == "string" || typeof val == "number"
|
||||
? val
|
||||
: isFieldRef
|
||||
? columnRef(val.fieldName, val.tableName)
|
||||
: val.value;
|
||||
const valueEquality = typeof val == "object"
|
||||
? val.equality || queryObj.equality
|
||||
: queryObj.equality;
|
||||
const operatorStrParam = sqlGenOperatorGen({
|
||||
return sqlGenOperatorGen({
|
||||
queryObj,
|
||||
equality: valueEquality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: valueString || "",
|
||||
isValueFieldValue: Boolean(typeof valueParsed == "object" &&
|
||||
valueParsed.fieldName &&
|
||||
valueParsed.tableName),
|
||||
fieldName: finalFieldName,
|
||||
value: valueString ?? "",
|
||||
isValueFieldValue: isFieldRef,
|
||||
});
|
||||
return operatorStrParam;
|
||||
}
|
||||
if (Array.isArray(queryObj.value)) {
|
||||
const listEquality = queryObj.equality == "IN" || queryObj.equality == "NOT IN"
|
||||
? queryObj.equality
|
||||
: undefined;
|
||||
if (listEquality &&
|
||||
!queryObj.nullValue &&
|
||||
!queryObj.notNullValue &&
|
||||
queryObj.value !== undefined &&
|
||||
queryObj.value !== null) {
|
||||
const rawValues = Array.isArray(queryObj.value)
|
||||
? queryObj.value
|
||||
: [queryObj.value];
|
||||
const listValues = rawValues
|
||||
.map((val) => val !== null && typeof val == "object" ? val.value : val)
|
||||
.filter((val) => val !== undefined && val !== null);
|
||||
if (listValues.length === 0) {
|
||||
// Empty IN () is invalid SQL: IN matches nothing, NOT IN matches everything
|
||||
str = listEquality == "IN" ? "1 = 0" : "1 = 1";
|
||||
}
|
||||
else {
|
||||
str = `${finalFieldName} ${listEquality} (${listValues.map(() => "?").join(",")})`;
|
||||
sqlSearhValues.push(...listValues);
|
||||
}
|
||||
}
|
||||
else if (Array.isArray(queryObj.value)) {
|
||||
const strArray = [];
|
||||
queryObj.value.forEach((val) => {
|
||||
const operatorStrParam = grabValue(val);
|
||||
if (!operatorStrParam)
|
||||
if (!operatorStrParam?.str)
|
||||
return;
|
||||
if (operatorStrParam.str && operatorStrParam.param) {
|
||||
strArray.push(operatorStrParam.str);
|
||||
strArray.push(operatorStrParam.str);
|
||||
if (operatorStrParam.param !== undefined) {
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
}
|
||||
else if (operatorStrParam.str) {
|
||||
strArray.push(operatorStrParam.str);
|
||||
}
|
||||
});
|
||||
str = "(" + strArray.join(` ${queryObj.operator || "AND"} `) + ")";
|
||||
str =
|
||||
"(" +
|
||||
strArray.join(` ${sqlLogicalOperator(queryObj.operator)} `) +
|
||||
")";
|
||||
}
|
||||
else if (typeof queryObj.value == "object") {
|
||||
else if (queryObj.value !== null && typeof queryObj.value == "object") {
|
||||
const operatorStrParam = grabValue(queryObj.value);
|
||||
if (operatorStrParam?.str) {
|
||||
str = operatorStrParam.str;
|
||||
if (operatorStrParam.param) {
|
||||
if (operatorStrParam.param !== undefined) {
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (queryObj.raw_equality && queryObj.value) {
|
||||
str = `${finalFieldName} ${queryObj.raw_equality} ?`;
|
||||
else if (queryObj.raw_equality &&
|
||||
queryObj.value !== undefined &&
|
||||
queryObj.value !== null) {
|
||||
str = `${finalFieldName} ${sqlRawComparison(queryObj.raw_equality)} ?`;
|
||||
sqlSearhValues.push(queryObj.value);
|
||||
}
|
||||
else if (queryObj.between) {
|
||||
str = `${finalFieldName} BETWEEN ? AND ?`;
|
||||
sqlSearhValues.push(queryObj.between.min, queryObj.between.max);
|
||||
}
|
||||
else if (queryObj.not_between) {
|
||||
str = `${finalFieldName} NOT BETWEEN ? AND ?`;
|
||||
sqlSearhValues.push(queryObj.not_between.min, queryObj.not_between.max);
|
||||
}
|
||||
else {
|
||||
const valueParsed = queryObj.value ? queryObj.value : undefined;
|
||||
const operatorStrParam = sqlGenOperatorGen({
|
||||
equality: queryObj.equality,
|
||||
fieldName: finalFieldName || "",
|
||||
value: valueParsed,
|
||||
fieldName: finalFieldName,
|
||||
value: queryObj.value ?? undefined,
|
||||
queryObj,
|
||||
});
|
||||
if (operatorStrParam.str && operatorStrParam.param) {
|
||||
if (operatorStrParam.str) {
|
||||
str = operatorStrParam.str;
|
||||
}
|
||||
if (operatorStrParam.param !== undefined) {
|
||||
sqlSearhValues.push(operatorStrParam.param);
|
||||
}
|
||||
else if (operatorStrParam.str && !operatorStrParam.str.match(/\?/)) {
|
||||
str = operatorStrParam.str;
|
||||
}
|
||||
else {
|
||||
sqlSearhValues.push(valueParsed || "");
|
||||
}
|
||||
}
|
||||
return { str, values: sqlSearhValues };
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
type Param = {
|
||||
/** Already-quoted column reference */
|
||||
field: string;
|
||||
alias: string;
|
||||
separator?: string;
|
||||
|
||||
+3
-2
@@ -1,3 +1,4 @@
|
||||
import { escapeStringLiteral, quoteIdentifier } from "./sql-identifier";
|
||||
export default function sqlGenGrabConcatStr({ alias, field, separator = ",", distinct, }) {
|
||||
let gc = `GROUP_CONCAT(`;
|
||||
if (distinct) {
|
||||
@@ -5,9 +6,9 @@ export default function sqlGenGrabConcatStr({ alias, field, separator = ",", dis
|
||||
}
|
||||
gc += `${field}`;
|
||||
if (!distinct) {
|
||||
gc += `, '${separator}'`;
|
||||
gc += `, '${escapeStringLiteral(separator)}'`;
|
||||
}
|
||||
gc += `)`;
|
||||
gc += ` AS ${alias}`;
|
||||
gc += ` AS ${quoteIdentifier(alias)}`;
|
||||
return gc;
|
||||
}
|
||||
|
||||
+38
-45
@@ -1,55 +1,48 @@
|
||||
import sqlGenGrabConcatStr from "./sql-generator-grab-concat-str";
|
||||
import { columnRef, quoteIdentifier } from "./sql-identifier";
|
||||
export default function sqlGenGrabSelectFieldSQL({ selectFields, append_table_names, table_name }) {
|
||||
let str = "";
|
||||
str += ` ${selectFields
|
||||
?.map((fld) => {
|
||||
const table = append_table_names ? table_name : undefined;
|
||||
return ` ${selectFields
|
||||
.map((fld) => {
|
||||
if (typeof fld != "object") {
|
||||
return columnRef(String(fld), table);
|
||||
}
|
||||
const fld_name = columnRef(String(fld.fieldName), table);
|
||||
let fld_str = ``;
|
||||
const final_fld_name = typeof fld == "object"
|
||||
? append_table_names
|
||||
? `${table_name}.${String(fld)}`
|
||||
: `${String(fld.fieldName)}`
|
||||
: `${String(fld)}`;
|
||||
if (typeof fld == "object") {
|
||||
const fld_name = `${String(fld.fieldName)}`;
|
||||
if (fld.count) {
|
||||
fld_str += `COUNT(${fld_name})`;
|
||||
}
|
||||
else if (fld.sum) {
|
||||
fld_str += `SUM(${fld_name})`;
|
||||
}
|
||||
else if (fld.average) {
|
||||
fld_str += `AVERAGE(${fld_name})`;
|
||||
}
|
||||
else if (fld.max) {
|
||||
fld_str += `MAX(${fld_name})`;
|
||||
}
|
||||
else if (fld.min) {
|
||||
fld_str += `MIN(${fld_name})`;
|
||||
}
|
||||
else if (fld.distinct) {
|
||||
fld_str += `DISTINCT ${fld_name}`;
|
||||
}
|
||||
else if (fld.group_concat) {
|
||||
fld_str += sqlGenGrabConcatStr({
|
||||
field: fld_name,
|
||||
alias: fld.group_concat.alias,
|
||||
separator: fld.group_concat.separator,
|
||||
distinct: fld.group_concat.distinct,
|
||||
});
|
||||
}
|
||||
else {
|
||||
fld_str +=
|
||||
final_fld_name + (fld.alias ? ` as ${fld.alias}` : ``);
|
||||
}
|
||||
if (fld.alias) {
|
||||
fld_str += ` AS ${fld.alias}`;
|
||||
}
|
||||
if (fld.count) {
|
||||
fld_str = `COUNT(${fld_name})`;
|
||||
}
|
||||
else if (fld.sum) {
|
||||
fld_str = `SUM(${fld_name})`;
|
||||
}
|
||||
else if (fld.average) {
|
||||
fld_str = `AVG(${fld_name})`;
|
||||
}
|
||||
else if (fld.max) {
|
||||
fld_str = `MAX(${fld_name})`;
|
||||
}
|
||||
else if (fld.min) {
|
||||
fld_str = `MIN(${fld_name})`;
|
||||
}
|
||||
else if (fld.distinct) {
|
||||
fld_str = `DISTINCT ${fld_name}`;
|
||||
}
|
||||
else if (fld.group_concat) {
|
||||
// Carries its own alias
|
||||
return sqlGenGrabConcatStr({
|
||||
field: fld_name,
|
||||
alias: fld.group_concat.alias,
|
||||
separator: fld.group_concat.separator,
|
||||
distinct: fld.group_concat.distinct,
|
||||
});
|
||||
}
|
||||
else {
|
||||
fld_str += final_fld_name;
|
||||
fld_str = fld_name;
|
||||
}
|
||||
if (fld.alias) {
|
||||
fld_str += ` AS ${quoteIdentifier(fld.alias)}`;
|
||||
}
|
||||
return fld_str;
|
||||
})
|
||||
.join(",")}`;
|
||||
return str;
|
||||
}
|
||||
|
||||
Vendored
+11
-1
@@ -5,13 +5,23 @@ type Param<T extends {
|
||||
[key: string]: any;
|
||||
}> = {
|
||||
genObject?: ServerQueryParam<T>;
|
||||
/** Raw (unquoted) table name */
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
count?: boolean;
|
||||
/**
|
||||
* Restrict to a single row id. Always combined with AND, regardless of
|
||||
* `searchOperator`.
|
||||
*/
|
||||
targetId?: number | string;
|
||||
};
|
||||
type Return = {
|
||||
string: string;
|
||||
values: SQLInsertGenValueType[];
|
||||
/** `WHERE ...` clause alone (empty string if none) */
|
||||
where: string;
|
||||
/** Values for the `where` clause placeholders only */
|
||||
whereValues: SQLInsertGenValueType[];
|
||||
};
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
@@ -21,5 +31,5 @@ export default function sqlGenerator<T extends {
|
||||
[key: string]: any;
|
||||
} = {
|
||||
[key: string]: any;
|
||||
}>({ tableName, genObject, dbFullName, count }: Param<T>): Return;
|
||||
}>({ tableName: rawTableName, genObject, dbFullName, count, targetId, }: Param<T>): Return;
|
||||
export {};
|
||||
|
||||
Vendored
+76
-226
@@ -1,16 +1,20 @@
|
||||
import sqlGenGenSearchStr from "./sql-generator-gen-search-str";
|
||||
import sqlGenGenQueryStr from "./sql-generator-gen-query-str";
|
||||
import { columnRef, quoteIdentifier, quoteQualified, sqlLogicalOperator, sqlNonNegativeInt, sqlOrderStrategy, } from "./sql-identifier";
|
||||
/** MariaDB requires LIMIT before OFFSET; this is the documented "no limit" value */
|
||||
const MAX_LIMIT = "18446744073709551615";
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
* @description Builds parameterized SELECT SQL for MariaDB
|
||||
*/
|
||||
export default function sqlGenerator({ tableName, genObject, dbFullName, count }) {
|
||||
export default function sqlGenerator({ tableName: rawTableName, genObject, dbFullName, count, targetId, }) {
|
||||
const tableName = quoteQualified(rawTableName);
|
||||
const finalQuery = genObject?.query ? genObject.query : undefined;
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
const sqlSearhValues = [];
|
||||
const whereValues = [];
|
||||
let fullTextMatchStr = genObject?.fullTextSearch
|
||||
? ` MATCH(${genObject.fullTextSearch.fields
|
||||
.map((f) => genObject.join ? `${tableName}.${String(f)}` : `${String(f)}`)
|
||||
.map((f) => columnRef(String(f), genObject.join ? tableName : undefined))
|
||||
.join(",")}) AGAINST (? IN BOOLEAN MODE)`
|
||||
: undefined;
|
||||
const fullTextSearchStr = genObject?.fullTextSearch
|
||||
@@ -19,14 +23,13 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
||||
.map((t) => `${t}`)
|
||||
.join(" ")
|
||||
: undefined;
|
||||
let { str: queryString, values } = sqlGenGenQueryStr({
|
||||
let { str: queryString, values: selectValues } = sqlGenGenQueryStr({
|
||||
table_name: tableName,
|
||||
append_table_names: true,
|
||||
full_text_match_str: fullTextMatchStr,
|
||||
full_text_search_str: fullTextSearchStr,
|
||||
genObject,
|
||||
});
|
||||
sqlSearhValues.push(...values);
|
||||
const sqlSearhString = queryKeys?.map((field) => {
|
||||
const queryObj = finalQuery?.[field];
|
||||
if (!queryObj)
|
||||
@@ -34,7 +37,8 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
||||
if (queryObj.__query) {
|
||||
const subQueryGroup = queryObj.__query;
|
||||
const subSearchKeys = Object.keys(subQueryGroup);
|
||||
const subSearchString = subSearchKeys.map((_field) => {
|
||||
const subSearchString = subSearchKeys
|
||||
.map((_field) => {
|
||||
const newSubQueryObj = subQueryGroup?.[_field];
|
||||
if (newSubQueryObj) {
|
||||
const { str, values } = sqlGenGenSearchStr({
|
||||
@@ -43,12 +47,13 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
||||
join: genObject?.join,
|
||||
table_name: tableName,
|
||||
});
|
||||
sqlSearhValues.push(...values);
|
||||
whereValues.push(...values);
|
||||
return str;
|
||||
}
|
||||
});
|
||||
})
|
||||
.filter((str) => typeof str == "string");
|
||||
return ("(" +
|
||||
subSearchString.join(` ${queryObj.operator || "AND"} `) +
|
||||
subSearchString.join(` ${sqlLogicalOperator(queryObj.operator)} `) +
|
||||
")");
|
||||
}
|
||||
const { str, values } = sqlGenGenSearchStr({
|
||||
@@ -57,247 +62,92 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
|
||||
join: genObject?.join,
|
||||
table_name: tableName,
|
||||
});
|
||||
sqlSearhValues.push(...values);
|
||||
whereValues.push(...values);
|
||||
return str;
|
||||
});
|
||||
const cleanedUpSearchStr = sqlSearhString?.filter((str) => typeof str == "string");
|
||||
const isSearchStr = cleanedUpSearchStr?.[0] && cleanedUpSearchStr.find((str) => str);
|
||||
const whereConditions = [];
|
||||
if (isSearchStr) {
|
||||
const stringOperator = genObject?.searchOperator || "AND";
|
||||
queryString += ` WHERE ${cleanedUpSearchStr.join(` ${stringOperator} `)}`;
|
||||
const stringOperator = sqlLogicalOperator(genObject?.searchOperator);
|
||||
whereConditions.push(`(${cleanedUpSearchStr.join(` ${stringOperator} `)})`);
|
||||
}
|
||||
if (genObject?.fullTextSearch && fullTextSearchStr && fullTextMatchStr) {
|
||||
queryString += `${isSearchStr ? " AND" : " WHERE"} ${fullTextMatchStr}`;
|
||||
sqlSearhValues.push(fullTextSearchStr);
|
||||
whereConditions.push(fullTextMatchStr.trim());
|
||||
whereValues.push(fullTextSearchStr);
|
||||
}
|
||||
if (targetId !== undefined && targetId !== null && targetId !== "") {
|
||||
const id = Number(targetId);
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error(`Invalid targetId: ${String(targetId)}`);
|
||||
}
|
||||
whereConditions.push(`${columnRef("id", genObject?.join ? tableName : undefined)} = ?`);
|
||||
whereValues.push(id);
|
||||
}
|
||||
const where = whereConditions.length
|
||||
? `WHERE ${whereConditions.join(" AND ")}`
|
||||
: ``;
|
||||
if (where) {
|
||||
queryString += ` ${where}`;
|
||||
}
|
||||
if (genObject?.group) {
|
||||
const groupRef = (group) => typeof group == "object"
|
||||
? columnRef(String(group.field), group.table)
|
||||
: columnRef(String(group));
|
||||
let group_by_txt = ``;
|
||||
if (typeof genObject.group == "string") {
|
||||
group_by_txt = genObject.group;
|
||||
if (Array.isArray(genObject.group)) {
|
||||
group_by_txt = genObject.group
|
||||
.map((group) => groupRef(group))
|
||||
.join(",");
|
||||
}
|
||||
else if (Array.isArray(genObject.group)) {
|
||||
for (let i = 0; i < genObject.group.length; i++) {
|
||||
const group = genObject.group[i];
|
||||
if (typeof group == "string") {
|
||||
group_by_txt += `\`${group.toString()}\``;
|
||||
}
|
||||
else if (typeof group == "object" && group.table) {
|
||||
group_by_txt += `${group.table}.${String(group.field)}`;
|
||||
}
|
||||
else if (typeof group == "object") {
|
||||
group_by_txt += `${String(group.field)}`;
|
||||
}
|
||||
if (i < genObject.group.length - 1) {
|
||||
group_by_txt += ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (typeof genObject.group == "object") {
|
||||
if (genObject.group.table) {
|
||||
group_by_txt = `${genObject.group.table}.${String(genObject.group.field)}`;
|
||||
}
|
||||
else {
|
||||
group_by_txt = `${String(genObject.group.field)}`;
|
||||
}
|
||||
else {
|
||||
group_by_txt = groupRef(genObject.group);
|
||||
}
|
||||
queryString += ` GROUP BY ${group_by_txt}`;
|
||||
}
|
||||
function grabOrderString(order) {
|
||||
let orderFields = [];
|
||||
let orderSrt = ``;
|
||||
if (genObject?.fullTextSearch && genObject.fullTextSearch.scoreAlias) {
|
||||
orderFields.push(genObject.fullTextSearch.scoreAlias);
|
||||
}
|
||||
else if (genObject?.join) {
|
||||
orderFields.push(`${tableName}.${String(order.field)}`);
|
||||
const scoreAlias = genObject?.fullTextSearch?.scoreAlias;
|
||||
let orderField = ``;
|
||||
if (scoreAlias && String(order.field) === scoreAlias) {
|
||||
orderField = quoteIdentifier(scoreAlias);
|
||||
}
|
||||
else {
|
||||
orderFields.push(order.field);
|
||||
orderField = columnRef(String(order.field), genObject?.join ? tableName : undefined);
|
||||
}
|
||||
orderSrt += ` ${orderFields.join(", ")} ${order.strategy}`;
|
||||
return orderSrt;
|
||||
return ` ${orderField} ${sqlOrderStrategy(order.strategy)}`;
|
||||
}
|
||||
if (genObject?.order) {
|
||||
let orderSrt = ` ORDER BY`;
|
||||
if (Array.isArray(genObject.order)) {
|
||||
for (let i = 0; i < genObject.order.length; i++) {
|
||||
const order = genObject.order[i];
|
||||
if (order) {
|
||||
orderSrt +=
|
||||
grabOrderString(order) +
|
||||
(i < genObject.order.length - 1 ? `,` : "");
|
||||
}
|
||||
}
|
||||
const orders = (Array.isArray(genObject.order) ? genObject.order : [genObject.order]).filter(Boolean);
|
||||
if (orders.length) {
|
||||
queryString += ` ORDER BY${orders.map(grabOrderString).join(",")}`;
|
||||
}
|
||||
else {
|
||||
orderSrt += grabOrderString(genObject.order);
|
||||
}
|
||||
if (!count) {
|
||||
const limit = genObject?.limit !== undefined && genObject.limit !== null
|
||||
? sqlNonNegativeInt(genObject.limit, "limit")
|
||||
: undefined;
|
||||
let offset;
|
||||
if (genObject?.offset !== undefined && genObject.offset !== null) {
|
||||
offset = sqlNonNegativeInt(genObject.offset, "offset");
|
||||
}
|
||||
else if (genObject?.page && limit) {
|
||||
const page = sqlNonNegativeInt(genObject.page, "page");
|
||||
offset = Math.max(page - 1, 0) * limit;
|
||||
}
|
||||
if (limit) {
|
||||
queryString += ` LIMIT ${limit}`;
|
||||
}
|
||||
else if (offset) {
|
||||
queryString += ` LIMIT ${MAX_LIMIT}`;
|
||||
}
|
||||
if (offset) {
|
||||
queryString += ` OFFSET ${offset}`;
|
||||
}
|
||||
queryString += ` ${orderSrt}`;
|
||||
}
|
||||
if (genObject?.limit && !count)
|
||||
queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset) {
|
||||
queryString += ` OFFSET ${genObject.offset}`;
|
||||
}
|
||||
else if (genObject?.page && genObject.limit && !count) {
|
||||
queryString += ` OFFSET ${(genObject.page - 1) * genObject.limit}`;
|
||||
}
|
||||
return {
|
||||
string: queryString,
|
||||
values: sqlSearhValues,
|
||||
values: [...selectValues, ...whereValues],
|
||||
where,
|
||||
whereValues,
|
||||
};
|
||||
}
|
||||
// let queryString = (() => {
|
||||
// let str = "SELECT";
|
||||
// if (genObject?.select_sql) {
|
||||
// str += ` ${genObject.select_sql}`;
|
||||
// } else if (genObject?.selectFields?.[0]) {
|
||||
// if (genObject.join) {
|
||||
// str += sqlGenGrabSelectFieldSQL<T>({
|
||||
// selectFields: genObject.selectFields,
|
||||
// append_table_names: true,
|
||||
// table_name: tableName,
|
||||
// });
|
||||
// } else {
|
||||
// str += sqlGenGrabSelectFieldSQL({
|
||||
// selectFields: genObject.selectFields,
|
||||
// table_name: tableName,
|
||||
// });
|
||||
// }
|
||||
// } else {
|
||||
// if (genObject?.join) {
|
||||
// str += ` ${tableName}.*`;
|
||||
// } else {
|
||||
// str += " *";
|
||||
// }
|
||||
// }
|
||||
// if (genObject?.countSubQueries) {
|
||||
// let countSqls: string[] = [];
|
||||
// for (let i = 0; i < genObject.countSubQueries.length; i++) {
|
||||
// const countSubQuery = genObject.countSubQueries[i];
|
||||
// if (!countSubQuery) continue;
|
||||
// const tableAlias = countSubQuery.table_alias;
|
||||
// let subQStr = `(SELECT COUNT(*)`;
|
||||
// subQStr += ` FROM ${countSubQuery.table}${
|
||||
// tableAlias ? ` ${tableAlias}` : ""
|
||||
// }`;
|
||||
// subQStr += ` WHERE (`;
|
||||
// for (let j = 0; j < countSubQuery.srcTrgMap.length; j++) {
|
||||
// const csqSrc = countSubQuery.srcTrgMap[j];
|
||||
// if (!csqSrc) continue;
|
||||
// subQStr += ` ${tableAlias || countSubQuery.table}.${
|
||||
// csqSrc.src
|
||||
// }`;
|
||||
// if (typeof csqSrc.trg == "string") {
|
||||
// subQStr += ` = ?`;
|
||||
// sqlSearhValues.push(csqSrc.trg);
|
||||
// } else if (typeof csqSrc.trg == "object") {
|
||||
// subQStr += ` = ${csqSrc.trg.table}.${csqSrc.trg.field}`;
|
||||
// }
|
||||
// if (j < countSubQuery.srcTrgMap.length - 1) {
|
||||
// subQStr += ` AND `;
|
||||
// }
|
||||
// }
|
||||
// subQStr += ` )) AS ${countSubQuery.alias}`;
|
||||
// countSqls.push(subQStr);
|
||||
// }
|
||||
// str += `, ${countSqls.join(",")}`;
|
||||
// }
|
||||
// if (genObject?.join) {
|
||||
// const existingJoinTableNames: string[] = [tableName];
|
||||
// str +=
|
||||
// "," +
|
||||
// genObject.join
|
||||
// .flat()
|
||||
// .filter((j) => !isUndefined(j))
|
||||
// .map((joinObj) => {
|
||||
// const joinTableName = joinObj.alias
|
||||
// ? joinObj.alias
|
||||
// : joinObj.tableName;
|
||||
// if (existingJoinTableNames.includes(joinTableName))
|
||||
// return null;
|
||||
// existingJoinTableNames.push(joinTableName);
|
||||
// if (joinObj.group_concat) {
|
||||
// return sqlGenGrabConcatStr({
|
||||
// field: `${joinTableName}.${joinObj.group_concat.field}`,
|
||||
// alias: joinObj.group_concat.alias,
|
||||
// separator: joinObj.group_concat.separator,
|
||||
// });
|
||||
// } else if (joinObj.selectFields) {
|
||||
// return joinObj.selectFields
|
||||
// .map((selectField) => {
|
||||
// if (typeof selectField == "string") {
|
||||
// return `${joinTableName}.${selectField}`;
|
||||
// } else if (typeof selectField == "object") {
|
||||
// let aliasSelectField = selectField.count
|
||||
// ? `COUNT(${joinTableName}.${selectField.field})`
|
||||
// : `${joinTableName}.${selectField.field}`;
|
||||
// if (selectField.alias)
|
||||
// aliasSelectField += ` AS ${selectField.alias}`;
|
||||
// return aliasSelectField;
|
||||
// }
|
||||
// })
|
||||
// .join(",");
|
||||
// } else {
|
||||
// return `${joinTableName}.*`;
|
||||
// }
|
||||
// })
|
||||
// .filter((_) => Boolean(_))
|
||||
// .join(",");
|
||||
// }
|
||||
// if (
|
||||
// genObject?.fullTextSearch &&
|
||||
// fullTextMatchStr &&
|
||||
// fullTextSearchStr
|
||||
// ) {
|
||||
// str += `, ${fullTextMatchStr} AS ${genObject.fullTextSearch.scoreAlias}`;
|
||||
// sqlSearhValues.push(fullTextSearchStr);
|
||||
// }
|
||||
// str += ` FROM ${tableName}`;
|
||||
// if (genObject?.join) {
|
||||
// str +=
|
||||
// " " +
|
||||
// genObject.join
|
||||
// .flat()
|
||||
// .filter((j) => !isUndefined(j))
|
||||
// .map((join) => {
|
||||
// return (
|
||||
// join.joinType +
|
||||
// " " +
|
||||
// (join.alias
|
||||
// ? `${join.tableName}` + " " + join.alias
|
||||
// : `${join.tableName}`) +
|
||||
// " ON " +
|
||||
// (() => {
|
||||
// if (Array.isArray(join.match)) {
|
||||
// return (
|
||||
// "(" +
|
||||
// join.match
|
||||
// .map((mtch) =>
|
||||
// sqlGenGenJoinStr({
|
||||
// mtch,
|
||||
// join,
|
||||
// table_name: tableName,
|
||||
// }),
|
||||
// )
|
||||
// .join(
|
||||
// join.operator
|
||||
// ? ` ${join.operator} `
|
||||
// : " AND ",
|
||||
// ) +
|
||||
// ")"
|
||||
// );
|
||||
// } else if (typeof join.match == "object") {
|
||||
// return sqlGenGenJoinStr({
|
||||
// mtch: join.match,
|
||||
// join,
|
||||
// table_name: tableName,
|
||||
// });
|
||||
// }
|
||||
// })()
|
||||
// );
|
||||
// })
|
||||
// .join(" ");
|
||||
// }
|
||||
// return str;
|
||||
// })();
|
||||
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Quote a single identifier (table, column, alias) with backticks.
|
||||
* Already-quoted identifiers (e.g. "`users`") are returned unchanged.
|
||||
*/
|
||||
export declare function quoteIdentifier(identifier: string): string;
|
||||
/**
|
||||
* Quote a possibly qualified identifier: "table.field" -> `table`.`field`.
|
||||
* A bare "*" (or "table.*") is kept as a wildcard.
|
||||
*/
|
||||
export declare function quoteQualified(identifier: string): string;
|
||||
/**
|
||||
* Build a column reference, prefixing the table when one is given and the
|
||||
* field is not already qualified.
|
||||
*/
|
||||
export declare function columnRef(field: string, table?: string): string;
|
||||
/**
|
||||
* Escape a value for use inside a single-quoted SQL string literal.
|
||||
*/
|
||||
export declare function escapeStringLiteral(value: string): string;
|
||||
export declare function sqlLogicalOperator(operator?: string): "AND" | "OR";
|
||||
export declare function sqlOrderStrategy(strategy?: string): "ASC" | "DESC";
|
||||
export declare function sqlRawComparison(comparison: string): string;
|
||||
export declare function sqlJoinType(joinType: string): string;
|
||||
/**
|
||||
* Validate a LIMIT / OFFSET / page value as a non-negative integer.
|
||||
*/
|
||||
export declare function sqlNonNegativeInt(value: unknown, name: string): number;
|
||||
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
import { ServerQueryOperators, SQlComparisons } from "../types";
|
||||
/**
|
||||
* Quote a single identifier (table, column, alias) with backticks.
|
||||
* Already-quoted identifiers (e.g. "`users`") are returned unchanged.
|
||||
*/
|
||||
export function quoteIdentifier(identifier) {
|
||||
const str = String(identifier);
|
||||
if (/^`(?:[^`]|``)+`$/.test(str))
|
||||
return str;
|
||||
return `\`${str.replace(/`/g, "``")}\``;
|
||||
}
|
||||
/**
|
||||
* Quote a possibly qualified identifier: "table.field" -> `table`.`field`.
|
||||
* A bare "*" (or "table.*") is kept as a wildcard.
|
||||
*/
|
||||
export function quoteQualified(identifier) {
|
||||
const str = String(identifier);
|
||||
if (/^`(?:[^`]|``)+`$/.test(str))
|
||||
return str;
|
||||
return str
|
||||
.split(".")
|
||||
.map((part) => (part === "*" ? "*" : quoteIdentifier(part)))
|
||||
.join(".");
|
||||
}
|
||||
/**
|
||||
* Build a column reference, prefixing the table when one is given and the
|
||||
* field is not already qualified.
|
||||
*/
|
||||
export function columnRef(field, table) {
|
||||
const fieldStr = String(field);
|
||||
if (!table || fieldStr.includes("."))
|
||||
return quoteQualified(fieldStr);
|
||||
return `${quoteQualified(table)}.${fieldStr === "*" ? "*" : quoteIdentifier(fieldStr)}`;
|
||||
}
|
||||
/**
|
||||
* Escape a value for use inside a single-quoted SQL string literal.
|
||||
*/
|
||||
export function escapeStringLiteral(value) {
|
||||
return String(value).replace(/\\/g, "\\\\").replace(/'/g, "''");
|
||||
}
|
||||
export function sqlLogicalOperator(operator) {
|
||||
if (!operator)
|
||||
return "AND";
|
||||
const upper = String(operator).toUpperCase();
|
||||
if (ServerQueryOperators.includes(upper)) {
|
||||
return upper;
|
||||
}
|
||||
throw new Error(`Invalid logical operator: ${operator}`);
|
||||
}
|
||||
export function sqlOrderStrategy(strategy) {
|
||||
if (!strategy)
|
||||
return "ASC";
|
||||
const upper = String(strategy).toUpperCase();
|
||||
if (upper === "ASC" || upper === "DESC")
|
||||
return upper;
|
||||
throw new Error(`Invalid order strategy: ${strategy}`);
|
||||
}
|
||||
export function sqlRawComparison(comparison) {
|
||||
const upper = String(comparison).toUpperCase();
|
||||
if (SQlComparisons.includes(upper))
|
||||
return upper;
|
||||
throw new Error(`Invalid comparison operator: ${comparison}`);
|
||||
}
|
||||
const JoinTypes = ["INNER JOIN", "JOIN", "LEFT JOIN", "RIGHT JOIN"];
|
||||
export function sqlJoinType(joinType) {
|
||||
const upper = String(joinType).toUpperCase();
|
||||
if (JoinTypes.includes(upper))
|
||||
return upper;
|
||||
throw new Error(`Invalid join type: ${joinType}`);
|
||||
}
|
||||
/**
|
||||
* Validate a LIMIT / OFFSET / page value as a non-negative integer.
|
||||
*/
|
||||
export function sqlNonNegativeInt(value, name) {
|
||||
const num = typeof value == "string" && value.trim() !== "" ? Number(value) : value;
|
||||
if (typeof num != "number" || !Number.isInteger(num) || num < 0) {
|
||||
throw new Error(`Invalid ${name}: ${String(value)}`);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
Vendored
+9
-27
@@ -1,6 +1,6 @@
|
||||
function quoteIdentifier(identifier) {
|
||||
return `\`${identifier.replace(/`/g, "``")}\``;
|
||||
}
|
||||
import { quoteIdentifier } from "./sql-identifier";
|
||||
import sqlValuePlaceholder from "./sql-value-placeholder";
|
||||
import grabTableFields from "./grab-table-fields";
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
@@ -17,37 +17,19 @@ export default function sqlInsertGenerator({ tableName, data, dbFullName, }) {
|
||||
}
|
||||
});
|
||||
});
|
||||
const fieldsMap = new Map(grabTableFields(tableName).map((f) => [f.fieldName, f]));
|
||||
let queryBatches = [];
|
||||
let queryValues = [];
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(`(${insertKeys
|
||||
.map((ky) => {
|
||||
const value = item[ky];
|
||||
const is_embedding = Array.isArray(value) &&
|
||||
typeof value[0] == "number";
|
||||
const finalValue = typeof value == "string" ||
|
||||
typeof value == "number"
|
||||
? value
|
||||
: typeof value == "function"
|
||||
? value().value
|
||||
: is_embedding
|
||||
? JSON.stringify(value)
|
||||
: value
|
||||
? value
|
||||
: null;
|
||||
if (!finalValue && typeof value !== "number") {
|
||||
queryValues.push(null);
|
||||
return "?";
|
||||
}
|
||||
queryValues.push(finalValue);
|
||||
const placeholder = typeof value == "function"
|
||||
? value().placeholder
|
||||
: is_embedding
|
||||
? "VEC_FromText(?)"
|
||||
: "?";
|
||||
const { placeholder, value } = sqlValuePlaceholder({
|
||||
value: item[ky],
|
||||
field: fieldsMap.get(ky),
|
||||
});
|
||||
queryValues.push(value);
|
||||
return placeholder;
|
||||
})
|
||||
.filter((k) => Boolean(k))
|
||||
.join(",")})`);
|
||||
});
|
||||
const insertColumns = insertKeys.map(quoteIdentifier).join(",");
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import type { BUN_MARIADB_FieldSchemaType, SQLInsertGenValueType } from "../types";
|
||||
type Return = {
|
||||
placeholder: string;
|
||||
value: SQLInsertGenValueType;
|
||||
};
|
||||
/**
|
||||
* Map a JS value to a bound parameter + placeholder for INSERT / UPDATE.
|
||||
*
|
||||
* - `false` / `true` -> `0` / `1`
|
||||
* - number arrays (embeddings) -> `VEC_FromText(?)` with a JSON string
|
||||
* - `{ value, placeholder }` factories are called once
|
||||
* - `""` stays `""` for text columns and becomes `NULL` for everything else
|
||||
* (e.g. the `number | ""` typedefs for numeric columns)
|
||||
*/
|
||||
export default function sqlValuePlaceholder({ value, field, }: {
|
||||
value: any;
|
||||
field?: BUN_MARIADB_FieldSchemaType;
|
||||
}): Return;
|
||||
export {};
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
function isTextField(field) {
|
||||
if (!field || field.isVector)
|
||||
return false;
|
||||
return /char|text|enum|set|json/i.test(String(field.dataType || ""));
|
||||
}
|
||||
/**
|
||||
* Map a JS value to a bound parameter + placeholder for INSERT / UPDATE.
|
||||
*
|
||||
* - `false` / `true` -> `0` / `1`
|
||||
* - number arrays (embeddings) -> `VEC_FromText(?)` with a JSON string
|
||||
* - `{ value, placeholder }` factories are called once
|
||||
* - `""` stays `""` for text columns and becomes `NULL` for everything else
|
||||
* (e.g. the `number | ""` typedefs for numeric columns)
|
||||
*/
|
||||
export default function sqlValuePlaceholder({ value, field, }) {
|
||||
if (typeof value == "function") {
|
||||
const fnValue = value();
|
||||
return {
|
||||
placeholder: fnValue?.placeholder || "?",
|
||||
value: fnValue?.value ?? null,
|
||||
};
|
||||
}
|
||||
if (typeof value == "boolean") {
|
||||
return { placeholder: "?", value: value ? 1 : 0 };
|
||||
}
|
||||
if (value === undefined || value === null) {
|
||||
return { placeholder: "?", value: null };
|
||||
}
|
||||
if (value === "") {
|
||||
return { placeholder: "?", value: isTextField(field) ? "" : null };
|
||||
}
|
||||
if (Array.isArray(value) && typeof value[0] == "number") {
|
||||
return { placeholder: "VEC_FromText(?)", value: JSON.stringify(value) };
|
||||
}
|
||||
return { placeholder: "?", value };
|
||||
}
|
||||
Vendored
+5
-1
@@ -3,9 +3,13 @@ import fs from "fs";
|
||||
import grabSortedBackups from "./grab-sorted-backups";
|
||||
import { AppData } from "../data/app-data";
|
||||
import path from "path";
|
||||
import _ from "lodash";
|
||||
export default function trimBackups({ config }) {
|
||||
const { backup_dir } = grabDBDir({ config });
|
||||
const backups = grabSortedBackups({ config });
|
||||
// Only consider files written by `backup` (`{db_name}-{timestamp}.sql`)
|
||||
const backupNamePattern = new RegExp(`^${_.escapeRegExp(config.db_name)}-\\d+\\.sql$`);
|
||||
const backups = grabSortedBackups({ config }).filter((name) => backupNamePattern.test(name) &&
|
||||
fs.statSync(path.join(backup_dir, name)).isFile());
|
||||
const max_backups = config.max_backups || AppData["MaxBackups"];
|
||||
for (let i = 0; i < backups.length; i++) {
|
||||
const backup_name = backups[i];
|
||||
|
||||
Vendored
+5
-1
@@ -1,11 +1,15 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import _ from "lodash";
|
||||
import { AppData } from "../data/app-data";
|
||||
import grabDBDir from "./grab-db-dir";
|
||||
import grabSortedExports from "./grab-sorted-exports";
|
||||
export default function trimExports({ config }) {
|
||||
const { export_dir } = grabDBDir({ config });
|
||||
const exports = grabSortedExports({ config });
|
||||
// Only consider archives written by `export` (`{db_name}-{timestamp}.tar.gz|zip`)
|
||||
const exportNamePattern = new RegExp(`^${_.escapeRegExp(config.db_name)}-\\d+\\.(tar\\.gz|zip)$`);
|
||||
const exports = grabSortedExports({ config }).filter((name) => exportNamePattern.test(name) &&
|
||||
fs.statSync(path.join(export_dir, name)).isFile());
|
||||
const max_exports = config.max_exports || AppData["MaxExports"];
|
||||
for (let i = 0; i < exports.length; i++) {
|
||||
const export_name = exports[i];
|
||||
|
||||
Reference in New Issue
Block a user