Skip to content

[MsSql] Add WITH (NOLOCK) table hint support to select query builder#5884

Open
valerii-kuzivanov wants to merge 1 commit into
drizzle-team:betafrom
valerii-kuzivanov:feat/mssql-nolock-table-hints
Open

[MsSql] Add WITH (NOLOCK) table hint support to select query builder#5884
valerii-kuzivanov wants to merge 1 commit into
drizzle-team:betafrom
valerii-kuzivanov:feat/mssql-nolock-table-hints

Conversation

@valerii-kuzivanov

Copy link
Copy Markdown

Closes #5883

Adds WITH (NOLOCK) table hint support to the MSSQL select query builder, following the same pattern as the MySQL index hints (useIndex / forceIndex / ignoreIndex).

API

Per-table hint config on .from() and all join methods:

db.select()
	.from(users, { withNoLock: true })
	.leftJoin(cities, eq(cities.id, users.cityId), { withNoLock: true });

Chainable .withNoLock() that applies the hint to the main table and every joined table (order-independent — it works regardless of whether joins are added before or after the call):

db.select()
	.from(users)
	.leftJoin(cities, eq(cities.id, users.cityId))
	.withNoLock();

Both produce:

select [users].[name], [cities].[name] from [users] with (nolock) left join [cities] with (nolock) on [cities].[id] = [users].[city_id]

Implementation notes

  • MsSqlTableHintConfig ({ withNoLock?: boolean }) is accepted only for MsSqlTable sources; subqueries, views and raw SQL are rejected at the type level with the same error-string-literal technique the MySQL index hints use.
  • Config plumbing mirrors MySQL: stored on MsSqlSelectConfig (withNoLock, allWithNoLock) and MsSqlSelectJoinConfig (withNoLock), emitted in MsSqlDialect.buildSelectQuery right after the table identifier/alias.
  • withNoLock is added to MsSqlSetOperatorExcludedMethods.
  • The config type leaves room to add further table hints later (READPAST, UPDLOCK, …) without an API break.
  • Scope is the select builder only (same as the MySQL index hints); RQB paths are unaffected (new config fields are optional).

Tests

  • Type tests in type-tests/mssql/select.ts: valid usage on tables and joins, @ts-expect-error for views/subqueries/unknown hint keys, .withNoLock() chaining — pnpm test:types passes.
  • Integration tests in integration-tests/tests/mssql/mssql.test.ts: three toSQL() query checks (from-hint, join-hint, .withNoLock() all-tables) plus one execution test against a real SQL Server (azure-sql-edge) instance.
  • Full tests/mssql/mssql.test.ts (162 passed / 1 pre-existing skip) and tests/mssql/mssql.rels.test.ts (88 passed) are green.

Adds an optional table hint config to .from() and all join methods
({ withNoLock: true }), plus a chainable .withNoLock() method that
applies the hint to the main table and every joined table at once.
Follows the same pattern as MySQL index hints (useIndex/forceIndex):
hints are accepted only for MsSqlTable sources, rejected at the type
level for subqueries, views and raw SQL.
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