Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 6d7a8e0

Browse files
committed
Implement WPDB's set_sql_mode() method for SQLite
1 parent 5d977cc commit 6d7a8e0

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

wp-includes/sqlite/class-wp-sqlite-db.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,51 @@ public function get_col_charset( $table, $column ) {
5959
}
6060

6161
/**
62-
* Method to dummy out wpdb::set_sql_mode()
62+
* Changes the current SQL mode, and ensures its WordPress compatibility.
6363
*
64-
* @see wpdb::set_sql_mode()
64+
* If no modes are passed, it will ensure the current MySQL server modes are compatible.
6565
*
66-
* @param array $modes Optional. A list of SQL modes to set.
66+
* This overrides wpdb::set_sql_mode() while closely mirroring its implementation.
67+
*
68+
* @param array $modes Optional. A list of SQL modes to set. Default empty array.
6769
*/
6870
public function set_sql_mode( $modes = array() ) {
71+
if ( ! $this->dbh instanceof WP_SQLite_Driver ) {
72+
return;
73+
}
74+
75+
if ( empty( $modes ) ) {
76+
$result = $this->dbh->query( 'SELECT @@SESSION.sql_mode' );
77+
if ( ! isset( $result[0] ) ) {
78+
return;
79+
}
80+
81+
$modes_str = $result[0]->{'@@SESSION.sql_mode'};
82+
if ( empty( $modes_str ) ) {
83+
return;
84+
}
85+
$modes = explode( ',', $modes_str );
86+
}
87+
88+
$modes = array_change_key_case( $modes, CASE_UPPER );
89+
90+
/**
91+
* Filters the list of incompatible SQL modes to exclude.
92+
*
93+
* @since 3.9.0
94+
*
95+
* @param array $incompatible_modes An array of incompatible modes.
96+
*/
97+
$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
98+
99+
foreach ( $modes as $i => $mode ) {
100+
if ( in_array( $mode, $incompatible_modes, true ) ) {
101+
unset( $modes[ $i ] );
102+
}
103+
}
104+
$modes_str = implode( ',', $modes );
105+
106+
$this->dbh->query( "SET SESSION sql_mode='$modes_str'" );
69107
}
70108

71109
/**
@@ -276,6 +314,7 @@ public function db_connect( $allow_bail = true ) {
276314
}
277315
$GLOBALS['@pdo'] = $this->dbh->get_pdo();
278316
$this->ready = true;
317+
$this->set_sql_mode();
279318
}
280319

281320
/**

0 commit comments

Comments
 (0)