final class DbCompat (View source)

DbCompat class provides compatibility methods for different database types.

Constants

private SUBSTRING_FUNCTIONS

Mapping of database types to their corresponding substring functions.

private REGEXP_FUNCTIONS

Mapping of database types to their corresponding regular expression functions.

private NOW_FUNCTIONS

Mapping of database types to their corresponding current timestamp functions.

private BOOL_TRUE

Mapping of database types to their boolean true values.

private BOOL_FALSE

Mapping of database types to their boolean false values.

private DATE_SUBTRACT_FUNCTIONS

Mapping of database types to their date subtraction functions.

private CONCAT_FUNCTIONS

Mapping of database types to their string concatenation functions.

private GROUP_CONCAT_FUNCTIONS

Mapping of database types to their GROUP_CONCAT equivalent functions.

Methods

static string
substr(string $db_type)

Returns the appropriate substring function for the given database type.

static string
regexp(string $db_type)

Returns the appropriate regular expression function for the given database type.

static string
now(string $db_type)

Returns the appropriate current timestamp function for the given database type.

static string
boolTrue(string $db_type)

Returns the appropriate boolean true value for the given database type.

static string
boolFalse(string $db_type)

Returns the appropriate boolean false value for the given database type.

static string
dateSubtract(string $db_type, int $seconds)

Returns the appropriate date subtraction expression for the given database type.

static string
concat(string $db_type, array $values)

Returns the appropriate string concatenation function for the given database type.

static string
handleSqlMode(object $db, string $db_type)

Handles SQL mode for MySQL database connection by disabling 'ONLY_FULL_GROUP_BY' if needed.

static int
boolValue(bool $value)

Converts a PHP boolean value to the appropriate integer for database storage.

static int
boolFromDb(mixed $value)

Converts a database boolean value to integer (handles PostgreSQL 't'/'f' strings).

static string
groupConcat(string $db_type, string $column, string $separator = ', ')

Returns the appropriate GROUP_CONCAT expression for the given database type.

static void
restoreSqlMode(object $db, string $db_type, string $originalSqlMode)

Restores the original SQL mode for the MySQL database connection if needed.

static string
castToString(string $db_type, string $column)

Returns the expression to cast a record ID column to string for comparison against record_comment_links.record_id. Only for ASCII-safe values (record IDs); non-ASCII characters would be mangled on MySQL/MariaDB.

static string
isNumericString(string $db_type, string $column)

Returns the expression to check if a column contains only digits (is numeric string).

static string
escapeLike(string $value)

Escape LIKE wildcards (%, _) in a user-supplied value so it matches literally. Uses ! as the escape character (a backslash escape clause is unsafe to embed as a string literal across MySQL/PostgreSQL/SQLite), so the query must pair this with LIKE ? ESCAPE '!'. Append/prepend % to the returned value for the intended wildcard positions.

static string
binaryCollation(string|null $db_type)

Returns a collation clause that forces a byte-exact string comparison, to be appended to a column reference in a WHERE clause (e.g.

static string
accentSensitiveEquals(string|null $db_type, string $column, string $placeholder = '?')

Builds an equality predicate for an identity value (username, email, an SSO-mapped group/template name) that must match case-insensitively yet accent-exact. MySQL/MariaDB fold accents in their default collation, so case is folded explicitly and the result compared byte-for-byte. The column is converted to utf8mb4 first so the binary collation is valid even on older installs whose columns are still latin1 or utf8mb3. PostgreSQL and SQLite already compare byte-exact, so their existing comparison is left unchanged - the fix is a no-op there because they were never affected.

static string
caseInsensitiveEquals(string|null $db_type, string $column, string $placeholder = '?')

Equality that ignores case but not accents on every backend, for values like email addresses where User@x and user@x are one identity.

Details

static string substr(string $db_type)

Returns the appropriate substring function for the given database type.

Parameters

string $db_type

The type of database (e.g., "sqlite", "mysql", etc.)

Return Value

string

The substring function corresponding to the given database type.

static string regexp(string $db_type)

Returns the appropriate regular expression function for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

Return Value

string

The regular expression function corresponding to the given database type.

static string now(string $db_type)

Returns the appropriate current timestamp function for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

Return Value

string

The current timestamp function corresponding to the given database type.

static string boolTrue(string $db_type)

Returns the appropriate boolean true value for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

Return Value

string

The boolean true value corresponding to the given database type.

static string boolFalse(string $db_type)

Returns the appropriate boolean false value for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

Return Value

string

The boolean false value corresponding to the given database type.

static string dateSubtract(string $db_type, int $seconds)

Returns the appropriate date subtraction expression for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

int $seconds

The number of seconds to subtract from current time

Return Value

string

The date subtraction expression corresponding to the given database type.

static string concat(string $db_type, array $values)

Returns the appropriate string concatenation function for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

array $values

The values to concatenate

Return Value

string

The concatenation expression corresponding to the given database type.

static string handleSqlMode(object $db, string $db_type)

Handles SQL mode for MySQL database connection by disabling 'ONLY_FULL_GROUP_BY' if needed.

Parameters

object $db

The database connection object

string $db_type

The database type

Return Value

string

The original SQL mode if modified, or an empty string if no change was needed or not using MySQL.

static int boolValue(bool $value)

Converts a PHP boolean value to the appropriate integer for database storage.

Parameters

bool $value

The boolean value to convert

Return Value

int

1 for true, 0 for false

static int boolFromDb(mixed $value)

Converts a database boolean value to integer (handles PostgreSQL 't'/'f' strings).

PostgreSQL stores boolean columns as true/false and PDO returns them as 't'/'f' strings. MySQL stores them as TINYINT(1) and PDO may return integers (0/1) or strings ('0'/'1'). SQLite stores them as INTEGER and PDO returns integers (0/1). This method normalizes all formats to 0 or 1.

Parameters

mixed $value

The database value (can be int, bool, or string 't'/'f'/'0'/'1')

Return Value

int

1 for true values ('t', true, 1, '1'), 0 for false values ('f', false, 0, '0', null)

static string groupConcat(string $db_type, string $column, string $separator = ', ')

Returns the appropriate GROUP_CONCAT expression for the given database type.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", "pgsql")

string $column

The column to aggregate

string $separator

The separator between values (default: ', ')

Return Value

string

The GROUP_CONCAT expression corresponding to the given database type.

static void restoreSqlMode(object $db, string $db_type, string $originalSqlMode)

Restores the original SQL mode for the MySQL database connection if needed.

Parameters

object $db

The database connection object

string $db_type

The database type

string $originalSqlMode

The original SQL mode to be restored.

Return Value

void

static string castToString(string $db_type, string $column)

Returns the expression to cast a record ID column to string for comparison against record_comment_links.record_id. Only for ASCII-safe values (record IDs); non-ASCII characters would be mangled on MySQL/MariaDB.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", "pgsql")

string $column

The column to cast

Return Value

string

The CAST expression corresponding to the given database type.

static string isNumericString(string $db_type, string $column)

Returns the expression to check if a column contains only digits (is numeric string).

Used to distinguish between record IDs (numeric) and legacy usernames (non-numeric) in the comments.account field.

Parameters

string $db_type

The type of database (e.g., "mysql", "sqlite", "pgsql")

string $column

The column to check

Return Value

string

The expression that returns true if column contains only digits

static string escapeLike(string $value)

Escape LIKE wildcards (%, _) in a user-supplied value so it matches literally. Uses ! as the escape character (a backslash escape clause is unsafe to embed as a string literal across MySQL/PostgreSQL/SQLite), so the query must pair this with LIKE ? ESCAPE '!'. Append/prepend % to the returned value for the intended wildcard positions.

Parameters

string $value

The raw value to embed in a LIKE pattern

Return Value

string

The value with !, % and _ escaped by !

static string binaryCollation(string|null $db_type)

Returns a collation clause that forces a byte-exact string comparison, to be appended to a column reference in a WHERE clause (e.g.

"oidc_subject" . self::binaryCollation($db_type) . " = ?").

MySQL/MariaDB default to case- and accent-insensitive collations, so external identity values (OIDC/SAML subjects) that must match one-for-one need an explicit binary collation. PostgreSQL and SQLite compare strings byte-exact by default and need no clause.

Parameters

string|null $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

Return Value

string

The collation clause, or an empty string when not needed.

static string accentSensitiveEquals(string|null $db_type, string $column, string $placeholder = '?')

Builds an equality predicate for an identity value (username, email, an SSO-mapped group/template name) that must match case-insensitively yet accent-exact. MySQL/MariaDB fold accents in their default collation, so case is folded explicitly and the result compared byte-for-byte. The column is converted to utf8mb4 first so the binary collation is valid even on older installs whose columns are still latin1 or utf8mb3. PostgreSQL and SQLite already compare byte-exact, so their existing comparison is left unchanged - the fix is a no-op there because they were never affected.

Parameters

string|null $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

string $column

The column reference to compare

string $placeholder

The bound-parameter placeholder (e.g. "?" or ":name")

Return Value

string

The WHERE-clause predicate

static string caseInsensitiveEquals(string|null $db_type, string $column, string $placeholder = '?')

Equality that ignores case but not accents on every backend, for values like email addresses where User@x and user@x are one identity.

Parameters

string|null $db_type

The type of database (e.g., "mysql", "sqlite", etc.)

string $column

The column reference to compare

string $placeholder

The bound-parameter placeholder (e.g. "?" or ":name")

Return Value

string

The WHERE-clause predicate