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:
2026-10-14 02:27:59 +01:00
co-authored by Claude Opus 5.5
parent b81b09324a
commit ddf431e866
82 changed files with 2140 additions and 1403 deletions
+77 -76
View File
@@ -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") {