Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,150 of 9,150 pages
About the help in other languages
All help pages

ODBC driver

This page is for administrators who want to reach a database through its ODBC driver, and for developers who work on the driver. From Exponential 6.0.15 the kernel has a generic ODBC driver, expODBCDB in…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/features/6.0/odbc-database-driver.md

On this page

ODBC driver

This page is for administrators who want to reach a database through its ODBC driver, and for developers who work on the driver. From Exponential 6.0.15 the kernel has a generic ODBC driver, expODBCDB in lib/ezdb/classes/: one driver for every database that has an ODBC driver, with the SQL it writes itself (paging, escaping, the new id, the catalogue, the portable helpers) chosen by a dialect in the settings.

What it is for. A site's own database over ODBC (SQL Server, PostgreSQL, MariaDB/MySQL, Db2): the installers, the setup wizard and the kickstarter install it, exp:cluster:enable puts a DFS cluster on it, and the upgrade stages run over it (A site over ODBC). Further: named connections of the data table layer (Data tables) to databases the installation has no native driver for, or that a host only offers through ODBC, and the import of MS Access files.

ODBC is a transport. For SQL Server and Db2, whose own drivers have an ODBC layer, DatabaseImplementation=odbc opens that driver (expSQLServerDB, expDb2DB) over PDO_ODBC: a site on SQL Server over ODBC behaves exactly as one on the SQL Server driver. PostgreSQL and MariaDB/MySQL run on expODBCDB, with their own schema handlers.

Contents

  1. Settings
  2. Dialects
  3. A site over ODBC
  4. What works and what does not, per dialect
  5. PHP layers
  6. A data table connection over ODBC
  7. Tests

Settings

[DatabaseSettings]
DatabaseImplementation=odbc
Server=db.example.com
Port=5432
User=crm
Password=...
Database=crm

[ODBCSettings]
Dialect=postgresql
Driver=PostgreSQL Unicode

ImplementationAlias[odbc] names expODBCDB. [ODBCSettings] of site.ini:

SettingMeaning
Dialectsqlserver, postgresql, mysql (MySQL and MariaDB), sqlite, oracle, msaccess or generic
Driverthe ODBC driver's name (odbcinst.ini on Linux, the ODBC administrator on Windows); empty: the dialect's usual one
DataSourceNamea DSN of odbc.ini instead of Driver and the server settings
ConnectionStringmore keyword=value pairs, ; separated (Encrypt=yes;TrustServerCertificate=yes)
Layerauto, pdo_odbc or odbc (below)

User and password go to the connect call, never into the connection string; a failed connection's message has the password masked. In code, the same settings go in the parameters: new expODBCDB( array( 'server' => 'db', 'database' => 'crm', 'user' => 'u', 'password' => $p, 'odbc' => array( 'Dialect' => 'mysql' ) ) ).

Default drivers (the names the packages register): ODBC Driver 18 for SQL Server; PostgreSQL Unicode (Debian and Ubuntu odbc-postgresql; PostgreSQL Unicode(x64) on Windows); MariaDB Unicode (odbc-mariadb); SQLite3 (libsqliteodbc); Microsoft Access Driver (*.mdb, *.accdb) on Windows and MDBTools elsewhere.

Dialects

The dialect decides everything the driver writes itself; databaseName() is the kernel's name of the engine (SQLServer, postgresql, mysql, sqlite, oracle, MSAccess, and odbc for generic), so the schema handlers and the data table layer treat the connection as that engine.

DialectPagingNew idText and escapingSession
sqlserverOFFSET ... FETCH (the SQL Server driver's own code)SCOPE_IDENTITY() in the INSERT's own batchstring literals bound as parameters (Unicode), backticks to bracketsNOCOUNT, XACT_ABORT, ANSI options, DATEFORMAT ymd
postgresqlLIMIT ... OFFSETcurrval() of the column's sequence (SERIAL or the kernel's <table>_<column>_seq), else lastval()quotes doubled
mysqlLIMIT ... OFFSETLAST_INSERT_ID()backslashes and quotes escaped (PDO_ODBC's quote() knows no backslash)SET NAMES utf8mb4
sqliteLIMIT ... OFFSETlast_insert_rowid()quotes doubled
oracleOFFSET ... FETCHthe table's sequence (s_<name>, as ezoracle)quotes doubled
msaccessTOP offset+limit, the first rows dropped in PHP@@IDENTITYquotes doubledsee MS Access
genericLIMIT ... OFFSETthe layer's lastInsertId() (usually none)quotes doubled

The portable helpers (sqlLimit(), sqlUpsert(), sqlGroupConcat(), sqlUnixTimestamp(), sqlFromUnixTimestamp(), sqlForUpdate(), sqlQuoteIdentifier()) follow the dialect, as on the native drivers.

A site over ODBC

From 6.0.15 a site can be installed on SQL Server, PostgreSQL, MariaDB/MySQL or Db2 through the database's ODBC driver. The site gets DatabaseImplementation=odbc and [ODBCSettings] (in settings/override and every siteaccess); everything else reads the dialect as the engine.

DialectRuns onSchema handlerDFS cluster backendxne backup and rollback
sqlserverexpSQLServerDB with [SQLServerSettings] Layer=pdo_odbcexpSQLServerSchemaSQL Server's, over PDO_ODBC (Layer=pdo_odbc, written by the installer)refused, as for SQL Server
postgresqlexpODBCDBeZPgsqlSchemaPostgreSQL's, over PDO_ODBC (DBODBCDriver, written by exp:cluster:enable); no pdo_pgsql neededas PostgreSQL, through pdo_pgsql of the PHP that runs xne
mysql (MariaDB too)expODBCDBeZMysqlSchemaMySQLi's (needs PHP's mysqli)as MySQL, through pdo_mysql of the PHP that runs xne
db2expDb2DB with [Db2Settings] Layer=pdo_odbcexpDb2Schemanone (Db2 has none)refused, as for Db2
# install.php (exp:install) - SQL Server, PostgreSQL, MariaDB through their ODBC drivers
EXP_INSTALL_DB_PASSWORD=... php bin/php/install.php --db=odbc --db-dialect=sqlserver --db-host=sql.example \
    --db-name=exponential --db-user=exp --db-trust-server-certificate --apply
php bin/php/install.php --db=odbc --db-dialect=postgresql --db-host=pg.example --db-name=exponential --db-user=exp --apply
php bin/php/install.php --db=odbc --db-dialect=mysql --db-dsn=exp-mariadb --db-name=exponential --db-user=exp --apply
OptionMeaning
--db-dialectsqlserver, postgresql, mysql (MariaDB too) or db2; the port, name and user default to the dialect's
--db-odbc-driverthe ODBC driver's name; default the dialect's usual one (below)
--db-dsnpostgresql and mysql: a data source of odbc.ini instead of the driver, host and port
--db-connection-stringmore keyword=value pairs
--cluster=one-nodewith postgresql, sqlserver or mysql: a one-node DFS cluster after the installation

The kickstarter takes [database_choice] Type=odbc with Dialect=, and [database_init] ODBCDriver, DataSourceName, ConnectionString (kickstart.ini-dist); odbc_sqlserver, odbc_pgsql, odbc_mysql and odbc_db2 are accepted as types too. The setup wizard offers these four types when PHP has pdo_odbc or odbc.

Measured on 2026-11-27 (PHP 8.5.13 with only PDO_ODBC and the odbc extension, Ubuntu 24.04, the package sevenx_multisite):

WhatPostgreSQL 17, psqlODBC 16MariaDB 11.4, MariaDB Connector/ODBC 3.1
installation (install.php --db=odbc)works, 302 sworks, 220 s
front page, admin login, search200200
schema read back against share/db_schema.dbathe same differences as the PostgreSQL driver reading the same database (the tables and the index the 6.0.15 migrations add, bigint defaults read as '0')the tables and the index the migrations add, nothing else
exp:upgrade:database (two kernel tables dropped first)-works: a caption with mysqldump, both tables made again, schema read back as before
exp:cluster:enable and DFSworks without pdo_pgsql: 312 files moved in, a 200 KB file stored, read back, sized and deleted in 34 ms, a stored image served by the gateway-

SQL Server 2022 over ODBC Driver 18 and MariaDB are installed by the GitHub job odbc (job pdo_odbc), PostgreSQL by the job odbc on PHP's odbc extension.

Upgrades. The upgrade stages (exp:upgrade) run over ODBC with the dialect's schema handler, and the caption before them uses the dialect's dump tool (mysqldump, pg_dump; like the SQL Server driver, SQL Server over ODBC has no caption dump, so make a backup of the database first or run with --no-backup). A site installed over ODBC starts with 6.0.15, so there is no earlier release to upgrade from; the first upgrade over ODBC is 6.0.15 to the next release.

Not here. SQLite, Oracle and MS Access as a site database over ODBC (SQLite and Oracle have native drivers; for Access see MS Access); a DSN for SQL Server and Db2 (their drivers build the connection string from the host and port).

What works and what does not, per dialect

Measured on 2026-11-25 with the live tests (below) on Ubuntu 24.04 (unixODBC 2.3.12, PHP 8.5.13), against SQL Server 2022 (ODBC Driver 18.7), PostgreSQL 17 (psqlODBC 16), MariaDB 11.4 (MariaDB Connector/ODBC 3.1) and SQLite 3.45 (sqliteodbc 0.99991), on both PHP layers; the same tests run in the GitHub job odbc.

TestSQL Server 2022PostgreSQL 17MariaDB 11.4SQLite 3.45
connect, version, eZTableList()worksworksworksworks
new id (lastSerialID())works (SCOPE_IDENTITY() in the INSERT's batch)works (sequence)worksworks
paging (limit/offset equals the slice, limit 0, past the end)worksworksworksworks
transactions (rollback, commit, nested)worksworksworksworks
Unicode (umlauts, emoji, CJK, combining marks; in a WHERE)works (text bound)worksworksworks
long text (1.2 MB)worksworksworksdoes not: sqliteodbc describes TEXT as 255 characters; PDO_ODBC refuses the value (01004 data right truncated), the odbc extension cuts it to 255 bytes
escaping (quote, backslash, NUL dropped, '' is not NULL)worksworksworksworks
sqlUpsert()works only with IDENTITY_INSERT on when the key is an identity column (MERGE compiles its INSERT branch with the key; expSQLServerDB switches it itself)worksworksworks
sqlGroupConcat()worksworksworksworks
unix time stamps there and backworks, past 2038works, past 2038works before 2038 only (MariaDB before 11.5: FROM_UNIXTIME() ends in 2038; a property of MariaDB, not of ODBC)works, past 2038
a keyword as a name (sqlQuoteIdentifier())worksworksworksworks
duplicate key (false, or eZDBException)worksworksworksworks
data table connection (engine odbc): test, make a table, write, plan again: nothing to doworksworks (after reading psqlODBC's 1 as true in the PostgreSQL schema handler)worksworks
Access import (MDBTools' 3 files, 4,103 rows)works; 31 s (a round trip per row)works, 2.6 sworks, 2.5 sworks, 0.4 s

Both layers gave the same results; Oracle (oracle dialect) is covered by the DB-less tests only.

PHP layers

LayerExtensionNotes
pdo_odbcPHP's PDO_ODBCthe default (auto). Converts bound text to UTF-16 when the driver asks for wide characters
odbcPHP's odbc extensionthe only layer with the catalogue calls (SQLTables, SQLColumns, SQLStatistics); auto picks it first for MS Access. A bound value written 'like this' (in single quotes) is read by the extension as a file name, so such a statement keeps literals

Both layers send the statement text in the client's code page: Microsoft's SQL Server driver turned UTF-8 in an N'...' literal into mojibake on both, which is why the sqlserver dialect binds string literals (measured: ä😀 arrived as äð as a literal, intact as a parameter). PostgreSQL's, MariaDB's and SQLite's Unicode drivers take UTF-8 statement text as it is.

A data table connection over ODBC

The data table layer has the engine odbc (datatable.ini [ConnectionSettings] Engines[]); its options carry the dialect and the driver:

[Connection_crm]
Engine=odbc
Server=db.example.com
Port=5432
Database=crm
User=crm
Secret=env:CRM_DB_PASSWORD
Dialect=postgresql
ODBCDriver=PostgreSQL Unicode
ConnectionString=SSLmode=require

expDataConnections::test() opens it, reads the version and the tables; expDataTableSchema makes and upgrades tables on it through the dialect's schema handler (MySQL, PostgreSQL, SQLite) or its own statements (SQL Server, Oracle, MS Access).

Tests

tests/tests/lib/ezdb/odbc/ (DB-less, testsuite odbc) and .../odbc/live/ (testsuite odbc-live; skipped without EXP_TEST_ODBC_SQLSERVER, _POSTGRESQL, _MYSQL, _SQLITE with _USER, _PASSWORD, _DRIVER, and EXP_TEST_ODBC_LAYER). The GitHub job odbc runs them on both layers with every target configured.

FileWhat
expODBCDBTestlayers, default drivers, connection strings, engine names, escaping, paging, masked passwords, settings; the transport to SQL Server's and Db2's drivers (resolve())
expODBCSiteInstallTestthe setup wizard's ODBC types, the kickstarter's options, the parameters the steps carry, install.php --db=odbc
expMSAccessDialectTestthe Access dialect (see MS Access)
live/expODBCLiveTestper target and layer: connect, new ids, paging, transactions, Unicode, 1 MB text, escaping, helpers, duplicate keys, a data table connection

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x