Skip to content

Conditions and SQL generation clean-up and fixes (v1.35.0) - #207

Closed
gmpassos wants to merge 9 commits into
masterfrom
refactor/conditions-sql-cleanup
Closed

gmpassos wants to merge 9 commits into
masterfrom
refactor/conditions-sql-cleanup

Conversation

@gmpassos

@gmpassos gmpassos commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Clean-up of the conditions and SQL generation code (behavior-preserving; the generated SQL is byte-identical), plus three bug fixes, each in its own commit with a test. Bumps to v1.35.0 (v1.34.0 is tagged).

Refactors (no behavior change)

  • Conditions (bones_api_condition.dart)
    • The four ordering comparisons (>, >=, <, <=) were written three times. They now go through a private _Ordering enum (gt, gte, lt, lte, with mirrored and test) and a private _KeyConditionOrdering base class. The public methods are one-line delegates, and each KeyCondition{GreaterThan,...} keeps its constructor, cast and symbol.
    • encodeValue moved into KeyCondition (KeyConditionRegExp keeps its override). The unused keyType parameter is documented, not removed.
    • ConditionParameter.matchesIn resolves its parameters once, not n×m times.
    • _literalRegExp renamed _literalRegExpCache and documented.
  • Encoder: isReservedWord checks a const set; TypeInfo.from(valueType) is computed once (late); new EncodingContext.parameterValue.
  • Condition SQL: new helpers _encodeIDComparison, _requireTableScheme, _writeEmptySetMatch and qualifiedColumnSQL.
  • SQL builder
    • New SQLGenerator.uniqueConstraintName, indexName, foreignKeyName and relationshipTableName, also used by the scheme check and the memory adapter. The names they produce are unchanged.
    • New SQLDialect.qualifiedColumnSQL; locals constrain* renamed constraint*; table caches renamed *Cache.
  • DB SQL adapter
    • _checkDBTableScheme split into helpers named after its sections, with _sameManagerEntityTypes, columnsOf, and the "No scheme" message written once.
    • Relationship generators: new _requireTableScheme and _requireRelationship. _generateConstrainRelationshipSQL now takes the already-resolved relationship. New shared _selectRelationshipSQLText and _executeSelectRelationshipSQL (log labels kept).
    • JOINs use qualifiedColumnSQL and _joinKeyword.
    • _positionalCache moved above the method that fills it; the lazy positional fields documented (not renamed: they are named constructor parameters).
  • Memory adapter: one select matcher, _hasNoReferences and _deleteMatchedEntries. Select and delete still pass different parameters (documented). Version caches renamed *Cache.
  • APIDBModule.select: split into _parseSelectDirectives, _extractIntDirective, _buildSelectTableHTML and _buildPaginationHTML.

Fixes

  • Condition.parse("name == 'a\\'b'") failed ("\'" expected): characterEscapeValue2 used characterEscape, which doesn't accept '. It now uses a new characterEscape2 built on the previously unused jsonEscapeCharsString2. Test in bones_api_condition_test.dart.
  • SQLDialect.toString was missing a space after name and five fields. Tests in bones_api_cov_sql_builder_test.dart.
  • APIDBModule.select wrote entity values into its HTML without escaping. They are now escaped with HtmlEscape (element mode for text, attribute mode for link IDs). The test in bones_api_cov_db_module_test.dart fails without the fix.

Not changed: KeyCondition{GreaterThan,...}.matchesEntity not forwarding keyEntityHandler. The only handler matchesEntity has is the root entity's, which is the wrong one for a path key (owner.balance), and EntityHandler.*ThanValue ignores the handler anyway, so no test could observe a change. KeyConditionEQ.matchesEntity behaves the same way.

Tests (local)

  • dart format is clean.
  • dart analyze --fatal-infos lib test example: no issues. Issues remain only in benchmark/, a separate package that is not set up locally.
  • The condition, SQL builder, entity index/select-order, SQL check, relational, SQLite, memory and DB module tests pass: 893 tests, plus bones_api_version_test.dart. Docker tests were not run (PostgreSQL and MySQL run in CI).

🤖 Generated with Claude Code

gmpassos and others added 9 commits November 8, 2026 05:13
- condition: a private `_Ordering` enum (gt, gte, lt, lte; `mirrored`,
  `test`) behind the ordering methods of `ConditionParameter` and
  `Condition` (public methods kept as one-line delegates; the unused
  `keyType` documented), and a private `_KeyConditionOrdering` base of the
  four `KeyCondition{GreaterThan,...}` classes (constructors, `cast` and
  symbols kept). `encodeValue` moved into `KeyCondition`.
- `ConditionParameter.matchesIn`: the parameters among the values are
  resolved once, not once per element.
- `KeyConditionRegExp._literalRegExp` -> `_literalRegExpCache`, documented.
- encoder: `isReservedWord` by a const set; `TypeInfo.from(valueType)`
  computed once; `EncodingContext.parameterValue`.
- condition SQL: `_encodeIDComparison`, `_requireTableScheme`,
  `_writeEmptySetMatch`, `qualifiedColumnSQL`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `SQLGenerator.uniqueConstraintName`, `indexName`, `foreignKeyName` and
  `relationshipTableName` (byte-identical names), also used by the scheme
  check and the in-memory adapter's relationship table.
- Locals `constrain*Name` -> `constraint*Name`.
- `SQLDialect.qualifiedColumnSQL` (used by `orderBySQL`).
- `CreateTableSQL`/`AlterTableSQL` table caches named `*Cache`, documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `_checkDBTableScheme` split into helpers named after its sections
  (`_findMissingReferenceConstraints`, `_findMissingRelationshipIndexes`,
  `_findMissingEnumConstraints`, `_dedupeMissingEntityIndexes`,
  `_findEntityIndexesNameConflicts`, `_tableCheckErrorLine`), with
  `_sameManagerEntityTypes` for the identical to-one/to-many blocks, a
  local `columnsOf`, and the "No scheme" message written once.
- Relationship generators: `_requireTableScheme`, `_requireRelationship`;
  `_generateConstrainRelationshipSQL` takes the resolved relationship;
  `_selectRelationshipSQLText` and `_executeSelectRelationshipSQL` shared
  by the single/multiple relationship selects.
- JOINs: `SQLDialect.qualifiedColumnSQL` and `_joinKeyword` (same SQL).
- `SQL`: `_positionalCache` declared before the method that fills it;
  the lazy positional fields documented.
- Typo locals (`colum`, `constrainSQL`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `_selectEntries`: the two near-identical matching branches as one
  closure (references resolved only when needed, as before).
- `_hasNoReferences` (select and delete) and `_deleteMatchedEntries` (the
  three delete branches). The parameters each passes are kept, and the
  difference documented (select: `namedParameters ?? parametersByPlaceholder`;
  delete: `parametersByPlaceholder`).
- `_tablesVersions`/`_tablesIndexesVersions` -> `*Cache`, documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`_parseSelectDirectives` (with `_extractIntDirective` for the identical
LIMIT/OFFSET/PAGE blocks), `_buildSelectTableHTML` and
`_buildPaginationHTML`. Same HTML.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`characterEscapeValue2` used `characterEscape`, whose pattern excludes
`'`, so `Condition.parse("name == 'a\\'b'")` failed (`"\'" expected`);
the unused `jsonEscapeCharsString2` was meant for it. Now
`characterEscape2`. Test in bones_api_condition_test.dart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It lacked a space after `name: $name,` and omitted
`acceptsVarcharWithoutMaximumSize`, `intParameterTypeSuffix`,
`maxIDsAsParameters`, `regExpSyntax` and `subqueryAsDerivedTable`.
Tests in bones_api_cov_sql_builder_test.dart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The values (and IDs in links) were written into the HTML as is: a value
with `<` or `&` broke the page and could inject markup or scripts. Now
escaped with `HtmlEscape` (element text; attribute for the links). Test
in bones_api_cov_db_module_test.dart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gmpassos added a commit that referenced this pull request Nov 8, 2026
…0 branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	CHANGELOG.md
@codecov

codecov Bot commented Nov 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.81%. Comparing base (32e1dbf) to head (8d34153).

Files with missing lines Patch % Lines
lib/src/bones_api_entity_db_sql.dart 99.48% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #207      +/-   ##
==========================================
- Coverage   95.85%   95.81%   -0.04%     
==========================================
  Files          75       75              
  Lines       26519    26400     -119     
==========================================
- Hits        25419    25296     -123     
- Misses       1100     1104       +4     
Flag Coverage Δ
unittests 95.81% <99.75%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmpassos

gmpassos commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Merged into the single v1.35.0 PR: #209.

@gmpassos gmpassos closed this Nov 8, 2026
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.

1 participant