Skip to content

SqlConnection: decide the SQLite narrow-text mode the way the driver does - #712

Merged
christianparpart merged 1 commit into
masterfrom
fix/sqlite-narrow-text-mode-follows-driver
Nov 22, 2026
Merged

christianparpart merged 1 commit into
masterfrom
fix/sqlite-narrow-text-mode-follows-driver

Conversation

@christianparpart

Copy link
Copy Markdown
Member

Follow-up to #708.

Problem

#708 decides whether a SQLite connection binds narrow text wide (converted with the process ANSI code page) from the connection string's OEMCP key only. The sqliteodbc driver decides OEMCP differently, so where the two disagree, narrow text is still bound as SQL_C_CHAR and converted with the thread's ANSI code page.

Measured on Windows (thread code page 1251, process 1252; 0xDF bound as SQL_C_CHAR, stored bytes):

Connect DSN profile OEMCP Stored Driver converts
SQLConnect not set D0AF yes
SQLConnect 1 D0AF yes
SQLConnect 0 DF no
SQLDriverConnect DSN= not set DF no
SQLDriverConnect DSN= 1 D0AF yes
SQLDriverConnect DSN= 0 DF no
SQLDriverConnect DSN=…;OEMCP=0 1 DF no
SQLDriverConnect DSN=…;OEMCP=1 not set D0AF yes
SQLDriverConnect Driver= – DF no
SQLDriverConnect Driver=…;OEMCP=1 – D0AF yes

So the driver uses the connection-string key, then the profile, then a default that is on for SQLConnect and off for a connection string.

Fix

  • SqlConnection decides the mode in that order. The profile is read with SQLGetPrivateProfileStringW (user, then system DSN). Connect(SqlConnectionDataSource) now records its data source so the mode is decided for data-source connects too. Doxygen of NarrowTextBindingMode updated; only a borrowed handle keeps the narrow default.
  • SqlDataBinder<char>: parameters, output columns, GetColumn and column-wise batches go through SQL_C_WCHAR under the process-code-page mode.
  • Backup batch layer: BatchManager takes the target connection's mode (restore passes it); narrow text columns are sent as UTF-16 under that mode.
  • Statement text: nothing to do. On Windows, statement text always goes through the wide ODBC entry points.
  • Windows builds link odbccp32 (plus legacy_stdio_definitions, which its import library needs).

Tests

All Windows-only, under a thread locale whose ANSI code page differs from the process one. All fail before this change: 11 of 59 assertions in 4 of the 5 cases.

  • The mode follows the profile (OEMCP=1/0/unset) and the data-source connect default, with the connection-string key overriding the profile.
  • A narrow round trip on a DSN with OEMCP only in its profile, and on a data-source connect.
  • A char bound, read and batched on an OEMCP=1 connection.
  • BatchManager restoring non-ASCII narrow text (VARCHAR and CHAR) into an OEMCP=1 connection.

The thread-locale helpers moved into src/tests/ForeignThreadLocale.hpp.

Locally with MSVC on SQLite: full suite green.

@christianparpart
christianparpart requested a review from a team as a code owner November 22, 2026 18:43
@github-actions github-actions Bot added tests Data Binder SQL Data Binder support Core API labels Nov 22, 2026
…does

The SQLite ODBC driver takes its OEMCP option from the connection string,
then from the data source's profile, and defaults it to on for a
data-source connect (SQLConnect) and to off for a connection string.
Lightweight only looked at the connection string, so a DSN with OEMCP in
its profile, or any data-source connect, still bound narrow text as
SQL_C_CHAR, which the driver then converted with the thread's ANSI code
page.

The mode now follows the same order: the connection-string key, else the
data source's profile (SQLGetPrivateProfileString, user then system DSN),
else the connect path's default. Connect(SqlConnectionDataSource) records
its data source so the mode is decided for it too.

The narrow paths that ignored the mode follow it now as well: the char
binder (parameters, output columns, column-wise batches) and the backup
batch layer's narrow text columns, which restore passes the target
connection's mode. Statement text needs nothing: on Windows it always goes
through the wide entry points.

Signed-off-by: Christian Parpart <christian@parpart.family>
@christianparpart
christianparpart force-pushed the fix/sqlite-narrow-text-mode-follows-driver branch from 984735d to fda900e Compare November 22, 2026 18:47
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 23.17073% with 63 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Lightweight/DataBinder/Primitives.hpp 13.15% 33 Missing ⚠️
src/Lightweight/SqlBackup/BatchManager.cpp 14.28% 30 Missing ⚠️

📢 Thoughts on this report? Let us know!

@christianparpart
christianparpart merged commit edc6a1e into master Nov 22, 2026
31 checks passed
@christianparpart
christianparpart deleted the fix/sqlite-narrow-text-mode-follows-driver branch November 22, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core API Data Binder SQL Data Binder support tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant