DbCompat
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
Returns the appropriate substring function for the given database type.
Returns the appropriate regular expression function for the given database type.
Returns the appropriate current timestamp function for the given database type.
Returns the appropriate boolean true value for the given database type.
Returns the appropriate boolean false value for the given database type.
Returns the appropriate date subtraction expression for the given database type.
Returns the appropriate string concatenation function for the given database type.
Handles SQL mode for MySQL database connection by disabling 'ONLY_FULL_GROUP_BY' if needed.
Converts a PHP boolean value to the appropriate integer for database storage.
Converts a database boolean value to integer (handles PostgreSQL 't'/'f' strings).
Returns the appropriate GROUP_CONCAT expression for the given database type.
Restores the original SQL mode for the MySQL database connection if needed.
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.
Returns the expression to check if a column contains only digits (is numeric string).
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.
Returns a collation clause that forces a byte-exact string comparison, to be appended to a column reference in a WHERE clause (e.g.
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.
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.
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).
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.
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).
Used to distinguish between record IDs (numeric) and legacy usernames (non-numeric) in the comments.account field.
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.
"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.
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.