Skip to content

feat(orm): build parameterized single-table SQL - #98

Merged
Antruly merged 1 commit into
masterfrom
feat/orm-sql
Nov 27, 2026
Merged

Antruly merged 1 commit into
masterfrom
feat/orm-sql

Conversation

@Antruly

@Antruly Antruly commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Part of #88. Stacked on #96 (feat/orm-model); retarget to master after #96 merges. This PR does not close #90 yet — it is the SQL construction step; result mapping/CRUD is #91.

What changed

Adds the SQL-construction layer above the model metadata:

  • src/orm/uvcpp_orm_dialect.h — explicit SQLite / MySQL / PostgreSQL dialect; ? vs $1..$n; identifier quoting (" vs backtick) with quote doubling; invalid dialect is rejected rather than silently producing unquoted SQL.
  • src/orm/uvcpp_orm_query.h — SELECT / INSERT / UPDATE / DELETE / COUNT builders:
    • column selection, WHERE (eq/ne/lt/le/gt/ge/like/in/is_null/is_not_null), AND groups and OR groups with explicit parentheses;
    • ORDER BY (model columns, ASC/DESC enum), LIMIT/OFFSET as bound parameters;
    • UPDATE/DELETE require WHERE; COUNT cannot be combined with column/order/limit/offset;
    • raw(sql, params) escape hatch with a documented scanner limitation;
    • all values go through uvcpp_db_params; no value literals are emitted.
  • src/orm/uvcpp_orm_error.h / uvcpp_orm_status.cpp — construction/validation status codes.
  • tests/functional/orm_sql_func.cpp — 295 checks: byte-exact golden SQL for three dialects, parameter order/count, identifier escaping, injection corpus, error paths and raw().

No DB client, C ABI, connection ownership or existing db behavior is changed. No SQL is executed in this layer.

Verification

$ ctest --test-dir build-orm-sql -R '^test_db_sqlite_func$|^test_orm_model_func$|^test_orm_sql_func$' --output-on-failure
1/3 Test #15: test_db_sqlite_func .............. Passed 0.10 sec
2/3 Test #33: test_orm_model_func .............. Passed 0.01 sec
3/3 Test #34: test_orm_sql_func ................ Passed 0.01 sec
100% tests passed, 0 tests failed out of 3

$ ./build-orm-sql/tests/functional/test_orm_sql_func
[orm_sql] checks=292 failures=0
[PASS] orm_sql: all 295 checks passed

C++11 compile: g++ -std=c++11 -pedantic-errors exits 0. With UVCPP_BUILD_ORM=OFF, test_orm_sql_func is absent and test_db_sqlite_func still passes.

Golden examples covered include SQLite/MySQL ?, PostgreSQL $1..$n, MySQL backtick vs SQLite/PG double-quote escaping, UPDATE SET order + WHERE parameter order, DELETE WHERE, COUNT(*), empty IN, invalid order direction, negative LIMIT/OFFSET, missing WHERE, unknown columns and parameter-count mismatches.

Unverified / known limits

  • No real MySQL/PostgreSQL server is touched; this issue deliberately does not execute SQL.
  • raw() is a lexical scanner, not a SQL parser: PostgreSQL dollar-quoted strings and MySQL backslash escapes are caller responsibility, documented in the header.
  • Full ctest was not run; only the ORM/DB SQLite targets above.
  • No .clang-format in the repository; files follow the surrounding 2-space UTF-8 style.

Add the dialect layer, identifier quoting and SELECT/INSERT/UPDATE/DELETE/COUNT builders on top of the model metadata. Every value is bound through uvcpp_db_params, PostgreSQL uses $1..$n, SQLite/MySQL use ?, and UPDATE/DELETE require a WHERE clause.

Add golden-SQL, parameter-order and injection tests for all three dialects.
@Antruly
Antruly changed the base branch from feat/orm-model to master November 27, 2026 06:16
@Antruly

Antruly commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Local review before merge: ORM SQL tests 295/295, ORM model 192/192, DB SQLite passes, C++11 pedantic build. No SQL is executed in this layer. Shared-runner CI remains queued.

@Antruly
Antruly merged commit 74576c7 into master Nov 27, 2026
0 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ORM] SQL 构造与方言

1 participant