Skip to content

Commit acae854

Browse files
committed
Route DATE_FORMAT through the shared date implementation
Replace SQLite format substitution with the shared formatter through a deterministic UDF. Cover dynamic formats, zero dates, week boundaries, fractional inputs, numeric comparisons, and FROM_UNIXTIME formatting through SQL. #491
1 parent a394d53 commit acae854

3 files changed

Lines changed: 285 additions & 110 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -291,51 +291,6 @@ class WP_MySQL_On_SQLite extends PDO {
291291
'geometrycollection' => 'TEXT',
292292
);
293293

294-
/**
295-
* A map of MySQL to SQLite date format translation.
296-
*
297-
* It maps MySQL DATE_FORMAT() formats to SQLite STRFTIME() formats.
298-
*
299-
* For MySQL formats, see:
300-
* https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
301-
*
302-
* For SQLite formats, see:
303-
* https://www.sqlite.org/lang_datefunc.html
304-
* https://strftime.org/
305-
*/
306-
private const MYSQL_DATE_FORMAT_TO_SQLITE_STRFTIME_MAP = array(
307-
'%a' => '%D',
308-
'%b' => '%M',
309-
'%c' => '%n',
310-
'%D' => '%jS',
311-
'%d' => '%d',
312-
'%e' => '%j',
313-
'%H' => '%H',
314-
'%h' => '%h',
315-
'%I' => '%h',
316-
'%i' => '%M',
317-
'%j' => '%z',
318-
'%k' => '%G',
319-
'%l' => '%g',
320-
'%M' => '%F',
321-
'%m' => '%m',
322-
'%p' => '%A',
323-
'%r' => '%h:%i:%s %A',
324-
'%S' => '%s',
325-
'%s' => '%s',
326-
'%T' => '%H:%i:%s',
327-
'%U' => '%W',
328-
'%u' => '%W',
329-
'%V' => '%W',
330-
'%v' => '%W',
331-
'%W' => '%l',
332-
'%w' => '%w',
333-
'%X' => '%Y',
334-
'%x' => '%o',
335-
'%Y' => '%Y',
336-
'%y' => '%y',
337-
);
338-
339294
/**
340295
* A map of MySQL data types to implicit default values for non-strict mode.
341296
*
@@ -5186,6 +5141,10 @@ private function translate_function_call( WP_Parser_Node $node ): string {
51865141
}
51875142

51885143
switch ( $name ) {
5144+
case 'DATE_FORMAT':
5145+
// QUOTE preserves the date's value and type across PDO's 32-bit integer UDF boundary.
5146+
// Older SQLite versions return integers from QUOTE(), so explicitly cast its result to text.
5147+
return 'DATE_FORMAT(CAST(QUOTE(' . $args[0] . ') AS TEXT), CAST(' . $args[1] . ' AS TEXT))';
51895148
case 'RAND':
51905149
/*
51915150
* Unseeded RAND() compiles to a fast native SQLite expression.
@@ -5205,20 +5164,6 @@ private function translate_function_call( WP_Parser_Node $node ): string {
52055164
return '((RANDOM() & ((1 << 53) - 1)) / ((1 << 53) * 1.0))';
52065165
}
52075166
return $this->translate_sequence( $node->get_children() );
5208-
case 'DATE_FORMAT':
5209-
list ( $date, $mysql_format ) = $args;
5210-
5211-
$format = strtr( $mysql_format, self::MYSQL_DATE_FORMAT_TO_SQLITE_STRFTIME_MAP );
5212-
if ( ! $format ) {
5213-
throw $this->new_driver_exception(
5214-
sprintf(
5215-
'Could not translate a DATE_FORMAT() format to STRFTIME format (%s)',
5216-
$mysql_format
5217-
)
5218-
);
5219-
}
5220-
5221-
return sprintf( 'STRFTIME(%s, %s)', $format, $date );
52225167
case 'CHAR_LENGTH':
52235168
// @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
52245169
return 'LENGTH(' . $args[0] . ')';

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ class WP_SQLite_PDO_User_Defined_Functions {
2222
public static function register_for( $pdo ): self {
2323
$instance = new self();
2424
foreach ( $instance->functions as $f => $t ) {
25+
$deterministic = 'date_format' === $f;
2526
if ( $pdo instanceof Pdo\Sqlite ) {
26-
$pdo->createFunction( $f, array( $instance, $t ) );
27+
$pdo->createFunction( $f, array( $instance, $t ), -1, $deterministic ? Pdo\Sqlite::DETERMINISTIC : 0 );
2728
} else {
28-
$pdo->sqliteCreateFunction( $f, array( $instance, $t ) );
29+
$pdo->sqliteCreateFunction( $f, array( $instance, $t ), -1, $deterministic ? PDO::SQLITE_DETERMINISTIC : 0 );
2930
}
3031
}
3132
return $instance;
@@ -75,6 +76,7 @@ public static function register_for( $pdo ): self {
7576
'to_base64' => 'to_base64',
7677
'inet_ntoa' => 'inet_ntoa',
7778
'inet_aton' => 'inet_aton',
79+
'date_format' => 'dateformat',
7880
'datediff' => 'datediff',
7981
'locate' => 'locate',
8082
'utc_date' => 'utc_date',
@@ -159,7 +161,7 @@ public function from_unixtime( $field, $format = null ) {
159161
// Convert to ISO time.
160162
$date = gmdate( 'Y-m-d H:i:s', $field );
161163

162-
return is_null( $format ) ? $date : $this->dateformat( $date, $format );
164+
return is_null( $format ) ? $date : WP_MySQL_Date_Time::format( $date, $format );
163165
}
164166

165167
/**
@@ -263,51 +265,28 @@ public function rand( $seed ) {
263265
}
264266

265267
/**
266-
* Method to emulate MySQL DATEFORMAT() function.
268+
* Emulate MySQL DATE_FORMAT() with a date transported as a SQLite QUOTE() result.
267269
*
268-
* @param string $date Formatted as '0000-00-00' or datetime as '0000-00-00 00:00:00'.
269-
* @param string $format The string format.
270+
* PDO SQLite truncates integer UDF arguments to 32 bits. QUOTE() preserves
271+
* their full value and distinguishes numeric dates from date strings.
270272
*
271-
* @return string formatted according to $format
273+
* @param string $date The quoted date value.
274+
* @param string|null $format The MySQL format.
275+
* @return string|null The formatted date, or NULL for invalid input.
272276
*/
273277
public function dateformat( $date, $format ) {
274-
$mysql_php_date_formats = array(
275-
'%a' => 'D',
276-
'%b' => 'M',
277-
'%c' => 'n',
278-
'%D' => 'jS',
279-
'%d' => 'd',
280-
'%e' => 'j',
281-
'%H' => 'H',
282-
'%h' => 'h',
283-
'%I' => 'h',
284-
'%i' => 'i',
285-
'%j' => 'z',
286-
'%k' => 'G',
287-
'%l' => 'g',
288-
'%M' => 'F',
289-
'%m' => 'm',
290-
'%p' => 'A',
291-
'%r' => 'h:i:s A',
292-
'%S' => 's',
293-
'%s' => 's',
294-
'%T' => 'H:i:s',
295-
'%U' => 'W',
296-
'%u' => 'W',
297-
'%V' => 'W',
298-
'%v' => 'W',
299-
'%W' => 'l',
300-
'%w' => 'w',
301-
'%X' => 'Y',
302-
'%x' => 'o',
303-
'%Y' => 'Y',
304-
'%y' => 'y',
305-
);
306-
307-
$time = strtotime( $date );
308-
$format = strtr( $format, $mysql_php_date_formats );
309-
310-
return gmdate( $format, $time );
278+
if ( 'NULL' === $date ) {
279+
return null;
280+
}
281+
if ( "'" === $date[0] ) {
282+
$date = str_replace( "''", "'", substr( $date, 1, -1 ) );
283+
} elseif ( 'X' === $date[0] ) {
284+
$date = hex2bin( substr( $date, 2, -1 ) );
285+
} else {
286+
// Valid integer dates fit exactly in a double, including on 32-bit PHP.
287+
$date = is_numeric( $date ) ? (float) $date : null;
288+
}
289+
return WP_MySQL_Date_Time::format( $date, $format );
311290
}
312291

313292
/**

0 commit comments

Comments
 (0)