Skip to content

Commit b48a8ad

Browse files
committed
Implement WPDB's set_sql_mode() method for SQLite
1 parent 172fd72 commit b48a8ad

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

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

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

7373
/**
74-
* Method to dummy out wpdb::set_sql_mode()
74+
* Changes the current SQL mode, and ensures its WordPress compatibility.
7575
*
76-
* @see wpdb::set_sql_mode()
76+
* If no modes are passed, it will ensure the current MySQL server modes are compatible.
7777
*
78-
* @param array $modes Optional. A list of SQL modes to set.
78+
* This overrides wpdb::set_sql_mode() while closely mirroring its implementation.
79+
*
80+
* @param array $modes Optional. A list of SQL modes to set. Default empty array.
7981
*/
8082
public function set_sql_mode( $modes = array() ) {
83+
if ( ! $this->dbh instanceof WP_SQLite_Driver ) {
84+
return;
85+
}
86+
87+
if ( empty( $modes ) ) {
88+
$result = $this->dbh->query( 'SELECT @@SESSION.sql_mode' );
89+
if ( ! isset( $result[0] ) ) {
90+
return;
91+
}
92+
93+
$modes_str = $result[0]->{'@@SESSION.sql_mode'};
94+
if ( empty( $modes_str ) ) {
95+
return;
96+
}
97+
$modes = explode( ',', $modes_str );
98+
}
99+
100+
$modes = array_change_key_case( $modes, CASE_UPPER );
101+
102+
/**
103+
* Filters the list of incompatible SQL modes to exclude.
104+
*
105+
* @since 3.9.0
106+
*
107+
* @param array $incompatible_modes An array of incompatible modes.
108+
*/
109+
$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
110+
111+
foreach ( $modes as $i => $mode ) {
112+
if ( in_array( $mode, $incompatible_modes, true ) ) {
113+
unset( $modes[ $i ] );
114+
}
115+
}
116+
$modes_str = implode( ',', $modes );
117+
118+
$this->dbh->query( "SET SESSION sql_mode='$modes_str'" );
81119
}
82120

83121
/**
@@ -288,6 +326,7 @@ public function db_connect( $allow_bail = true ) {
288326
}
289327
$GLOBALS['@pdo'] = $this->dbh->get_pdo();
290328
$this->ready = true;
329+
$this->set_sql_mode();
291330
}
292331

293332
/**

0 commit comments

Comments
 (0)