Skip to content

fix(plugin-mssql): establish the required session options and complete the routine list - #2855

Merged
datlechin merged 2 commits into
mainfrom
fix/mssql-session-options
Sep 15, 2026
Merged

datlechin merged 2 commits into
mainfrom
fix/mssql-session-options

Conversation

@datlechin

Copy link
Copy Markdown
Member

A SQL Server user reported that TablePro shows no Stored Procedures or Functions. It is not a missing feature and not version skew: the lists are empty on current main, for every SQL Server connection, and the same cause silently blocks a whole class of writes.

Root cause

FreeTDS db-lib inherits Sybase's defaults and hands back a session with none of SQL Server's required SET profile in place. Measured against SQL Server 2022 (16.0.4275.2) with a C probe linked against the shipped Libs/libsybdb.a:

Session option db-lib on connect Required
ANSI_NULLS 0 1
ANSI_PADDING 0 1
ANSI_WARNINGS 0 1
ARITHABORT 0 1
CONCAT_NULL_YIELDS_NULL 0 1
QUOTED_IDENTIFIER 0 1
NUMERIC_ROUNDABORT 0 0

MSSQLObjectQueries.routineList aggregates a parameter list through FOR XML PATH and .value('.', 'nvarchar(max)'). That is an XML data type method, and the server refuses it while those options are off:

Msg 1934: SELECT failed because the following SET options have incorrect settings:
'ANSI_NULLS, QUOTED_IDENTIFIER, CONCAT_NULL_YIELDS_NULL, ANSI_WARNINGS, ANSI_PADDING'

The failure does not stop at browsing. With the options off, an INSERT into a table carrying a filtered index or an index on a computed column fails with the same Msg 1934, so such a row could never be saved. Measured failing with them off and succeeding with them on, on one connection.

The fix

MSSQLSessionOptions establishes the whole profile, and FreeTDSConnection.adopt() applies it beside the TEXTSIZE it already raised. adopt is the only caller of the plugin's only dbopen, so every path reaches it: first connect, reconnect, and the metadata pool's own driver. Measured: the options survive a later dbuse, so a database switch keeps them.

A server that refuses one still gets a working connection. db-lib's defaults are wrong rather than fatal, and failing the connect over them would take the database away from a user who could otherwise work in it.

FreeTDSConnection.swift is compiled into the iOS app as well, so iOS gets this too.

Why not rewrite the query instead

Replacing the XML aggregate with STRING_AGG was the obvious alternative and does not survive the evidence. It cannot fix the INSERT failure, which involves no query of ours at all. And STRING_AGG is SQL Server 2017+, so it needs a version gate, and the version gate was itself broken (below).

NUMERIC_ROUNDABORT is part of the profile and nothing else would have caught it

NUMERIC_ROUNDABORT already arrives off, so on a default server setting it changes nothing. It is set because a database or a login can carry it on, and a session that inherits it fails those same writes while every other option reads correctly. Measured: with the six ON options set and NUMERIC_ROUNDABORT on, the INSERT fails with Msg 1934 naming NUMERIC_ROUNDABORT alone. The first version of this fix set five options and its probe asserted those five, so it would have reported OK on exactly that server while the writes still failed.

ARITHABORT is implied by ANSI_WARNINGS at compatibility level 90 and above, which is every server TDS 7.4 can reach, so it is belt and braces rather than load-bearing. It is set because Microsoft documents it as part of the profile.

Two defects this fix depends on or would have regressed

@@VERSION was truncated to 50 characters. On a patched server the build number falls outside the prefix, so MSSQLCapabilities.parse found no major.minor.build and reported the version as unknown. Measured on 2022 CU26: 50 characters is Microsoft SQL Server 2022 (RTM-CU26-GDR) (KB512276, and 16.0.4275.2 is cut off. Every version gate was dead, so CREATE OR ALTER went unused on servers that have had it since 2016. MSSQLServerBanner now keeps the product line whole for display and matches the build number against the whole banner.

CONCAT_NULL_YIELDS_NULL ON would have regressed the table-size query. CAST(...SUM(a.total_pages)...) + ' MB' yields NULL rather than ' MB' once the option is on. Guarded with ISNULL(SUM(...), 0). Caught by the review pass, not by me.

Also fixed in the same list

CLR and extended routines were never listed. The filter was o.type IN ('P','FN','IF','TF'), which drops PC, FS, FT, AF and X. A database holding them listed fewer routines than it has, with nothing saying so. The kind mapping read everything but P as a function, so adding the codes required fixing that too.

A routine the caller cannot read was labelled "Encrypted: YES". sys.sql_modules.definition is NULL both for WITH ENCRYPTION and for a caller without VIEW DEFINITION. I assumed the module row would be absent in the second case and measured that wrong: a user with only SELECT and EXECUTE still sees the row with a NULL definition. OBJECTPROPERTY(object_id, 'IsEncrypted') does separate them, and answers correctly for that caller, so the two now read differently in the row and throw different errors when opened.

CLR and extended routines had no source and were reported as missing. Widening the type filter surfaced them, and PC, FS, FT and AF live in sys.assembly_modules while X is a DLL entry point, so none has a sys.sql_modules row. The definition query's inner join returned zero rows for them, which the caller read as "no longer exists" for a routine sitting in the list in front of the reader; the listing also called their source permission-withheld and their language T-SQL. Measured with an object that has no module row: inner join 0 rows, left join 1 row. The query now drives from sys.objects with the module table joined on the outside and returns the object type, so a CLR routine reports its language as CLR, claims no SQL source, and says the source cannot be shown rather than inventing a cause.

Every routine body was fetched to draw the list and then discarded. routineList selected m.definition and the consumer never read it, so listing a schema pulled its whole source over the wire for nothing. routineDefinition reads the one the reader opens.

Verification

  • verify.sh build: PASS
  • verify.sh test MSSQLObjectQueryTests PostgreSQLObjectQueryTests: PASS
  • swift test in Packages/TableProCore (56 tests): PASS
  • docs/scripts/check-writing-style.sh and check-docs-against-source.py: PASS
  • scripts/check-mssql-session-options.sh against SQL Server 2022: db-lib defaults: 0 0 0 0 0 0 0 then after establish: 1 1 1 1 1 1 0

No UI automation: every failure here needs a live SQL Server, which TableProUITests has no deterministic access to. The probe covers the wired half and the unit tests cover the pure half.

A second model reviewed this branch (Codex, review --scope working-tree). It found four issues, all real and all fixed here: the missing NUMERIC_ROUNDABORT, the CLR routines with no module row, the password in generated C, and an unlocalized attribute label.

The probe is committed as scripts/check-mssql-session-options.sh so a FreeTDS bump re-checks it. It reads the option names and their required values out of MSSQLSessionOptions.swift rather than repeating them, so it cannot drift from what ships, and it takes its credentials from the environment rather than pasting them into generated C, which a password containing a quote or a backslash would otherwise break or corrupt. No unit test can catch this class of bug: the statement text is correct and the defect is in the session it runs against.

Fixtures used for the measurements were seeded into a throwaway tp_objects database: alias and table types, procedures with and without parameters, an encrypted procedure, a CLR-free multi-statement TVF, a low-privilege user, and a table with a filtered index and an indexed computed column.


https://claude.ai/code/session_016PSQC4EGqkpq32cWdPVMBJ

@mintlify

mintlify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
TablePro 🟢 Ready View Preview Sep 14, 2026, 11:57 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

@datlechin
datlechin merged commit 30286aa into main Sep 15, 2026
10 of 13 checks passed
@datlechin
datlechin deleted the fix/mssql-session-options branch September 15, 2026 00:43

This branch was successfully deployed

1 active deployment
staging - docs — 6c038d14 Deployed Sep 14, 2026 by mintlify[bot]
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