Skip to content

Commit e961bf5

Browse files
committed
Use custom exception class for driver errors
1 parent 0f7060f commit e961bf5

6 files changed

Lines changed: 48 additions & 31 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once __DIR__ . '/WP_SQLite_Translator_Tests.php';
44
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
5+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
56
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
67

78
use PHPUnit\Framework\TestCase;

tests/WP_SQLite_Driver_Translation_Tests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
4+
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
45
require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
56

67
use PHPUnit\Framework\TestCase;

tests/tools/dump-sqlite-query.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-token.php';
99
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-parser.php';
1010
require_once __DIR__ . '/../../wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
11-
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
1211
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
12+
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
13+
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
1314

1415
$driver = new WP_SQLite_Driver(
1516
array(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
class WP_SQLite_Driver_Exception extends Exception {}

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class WP_SQLite_Driver {
331331
public function __construct( array $options ) {
332332
// Database name.
333333
if ( ! isset( $options['database'] ) || ! is_string( $options['database'] ) ) {
334-
throw new InvalidArgumentException( 'Option "database" is required.' );
334+
throw new WP_SQLite_Driver_Exception( 'Option "database" is required.' );
335335
}
336336
$this->db_name = $options['database'];
337337

@@ -343,7 +343,9 @@ public function __construct( array $options ) {
343343
// Create a PDO connection if it is not provided.
344344
if ( ! $this->pdo ) {
345345
if ( ! isset( $options['path'] ) || ! is_string( $options['path'] ) ) {
346-
throw new InvalidArgumentException( 'Option "path" is required when "connection" is not provided.' );
346+
throw new WP_SQLite_Driver_Exception(
347+
'Option "path" is required when "connection" is not provided.'
348+
);
347349
}
348350
$path = $options['path'];
349351

@@ -486,7 +488,7 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
486488
$ast = $parser->parse();
487489

488490
if ( null === $ast ) {
489-
throw new Exception( 'Failed to parse the MySQL query.' );
491+
throw new WP_SQLite_Driver_Exception( 'Failed to parse the MySQL query.' );
490492
}
491493

492494
// Handle transaction commands.
@@ -580,17 +582,7 @@ public function execute_sqlite_query( $sql, $params = array() ) {
580582
);
581583

582584
$stmt = $this->pdo->prepare( $sql );
583-
if ( false === $stmt || null === $stmt ) {
584-
$info = $this->pdo->errorInfo();
585-
throw new PDOException( implode( ' ', array( 'Error:', $info[0], $info[2], 'SQLite:', $sql ) ), $info[1] );
586-
}
587-
588-
$is_success = $stmt->execute( $params );
589-
if ( false === $is_success ) {
590-
$info = $stmt->errorInfo();
591-
throw new PDOException( implode( ' ', array( 'Error:', $info[0], $info[2], 'SQLite:', $sql ) ), $info[1] );
592-
}
593-
585+
$stmt->execute( $params );
594586
return $stmt;
595587
}
596588

@@ -694,12 +686,16 @@ public function rollback(): void {
694686
*/
695687
private function execute_mysql_query( WP_Parser_Node $ast ) {
696688
if ( 'query' !== $ast->rule_name ) {
697-
throw new Exception( sprintf( 'Expected "query" node, got: "%s"', $ast->rule_name ) );
689+
throw new WP_SQLite_Driver_Exception(
690+
sprintf( 'Expected "query" node, got: "%s"', $ast->rule_name )
691+
);
698692
}
699693

700694
$children = $ast->get_child_nodes();
701695
if ( count( $children ) !== 1 ) {
702-
throw new Exception( sprintf( 'Expected 1 child, got: %d', count( $children ) ) );
696+
throw new WP_SQLite_Driver_Exception(
697+
sprintf( 'Expected 1 child, got: %d', count( $children ) )
698+
);
703699
}
704700

705701
$ast = $children[0]->get_first_child_node();
@@ -724,9 +720,9 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
724720
$this->execute_create_table_statement( $ast );
725721
break;
726722
default:
727-
throw new Exception(
723+
throw $this->not_supported_exception(
728724
sprintf(
729-
'Unsupported statement type: "%s" > "%s"',
725+
'statement type: "%s" > "%s"',
730726
$ast->rule_name,
731727
$subtree->rule_name
732728
)
@@ -740,9 +736,9 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
740736
$this->execute_alter_table_statement( $ast );
741737
break;
742738
default:
743-
throw new Exception(
739+
throw $this->not_supported_exception(
744740
sprintf(
745-
'Unsupported statement type: "%s" > "%s"',
741+
'statement type: "%s" > "%s"',
746742
$ast->rule_name,
747743
$subtree->rule_name
748744
)
@@ -778,17 +774,19 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
778774
$this->execute_describe_statement( $subtree );
779775
break;
780776
default:
781-
throw new Exception(
777+
throw $this->not_supported_exception(
782778
sprintf(
783-
'Unsupported statement type: "%s" > "%s"',
779+
'statement type: "%s" > "%s"',
784780
$ast->rule_name,
785781
$subtree->rule_name
786782
)
787783
);
788784
}
789785
break;
790786
default:
791-
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
787+
throw $this->not_supported_exception(
788+
sprintf( 'statement type: "%s"', $ast->rule_name )
789+
);
792790
}
793791
}
794792

@@ -1265,9 +1263,9 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
12651263
$this->results = true;
12661264
return;
12671265
default:
1268-
throw new Exception(
1266+
throw $this->not_supported_exception(
12691267
sprintf(
1270-
'Unsupported statement type: "%s" > "%s"',
1268+
'statement type: "%s" > "%s"',
12711269
$node->rule_name,
12721270
$keyword1->value
12731271
)
@@ -1449,7 +1447,12 @@ private function translate( $ast ) {
14491447
}
14501448

14511449
if ( ! $ast instanceof WP_Parser_Node ) {
1452-
throw new Exception( 'translate_query only accepts WP_MySQL_Token and WP_Parser_Node instances' );
1450+
throw new WP_SQLite_Driver_Exception(
1451+
sprintf(
1452+
'Expected a WP_Parser_Node or WP_MySQL_Token instance, got: %s',
1453+
gettype( $ast )
1454+
)
1455+
);
14531456
}
14541457

14551458
$rule_name = $ast->rule_name;
@@ -1843,7 +1846,12 @@ private function translate_function_call( WP_Parser_Node $node ): string {
18431846

18441847
$format = strtr( $mysql_format, self::DATE_FORMAT_TO_STRFTIME_MAP );
18451848
if ( ! $format ) {
1846-
throw new Exception( "Could not translate a DATE_FORMAT() format to STRFTIME format ($mysql_format)" );
1849+
throw new WP_SQLite_Driver_Exception(
1850+
sprintf(
1851+
'Could not translate a DATE_FORMAT() format to STRFTIME format (%s)',
1852+
$mysql_format
1853+
)
1854+
);
18471855
}
18481856

18491857
/*
@@ -1967,7 +1975,9 @@ private function get_sqlite_create_table_statement( string $table_name, ?string
19671975
)->fetch( PDO::FETCH_ASSOC );
19681976

19691977
if ( false === $table_info ) {
1970-
throw new Exception( 'Table not found in information_schema' );
1978+
throw new WP_SQLite_Driver_Exception(
1979+
sprintf( 'Table "%s" not found in information schema', $table_name )
1980+
);
19711981
}
19721982

19731983
// 2. Get column info.
@@ -2424,11 +2434,11 @@ private function set_error( $line, $function_name, $message ) {
24242434
}
24252435

24262436
private function invalid_input_exception() {
2427-
throw new Exception( 'MySQL query syntax error.' );
2437+
throw new WP_SQLite_Driver_Exception( 'MySQL query syntax error.' );
24282438
}
24292439

24302440
private function not_supported_exception( string $cause ): Exception {
2431-
return new Exception(
2441+
return new WP_SQLite_Driver_Exception(
24322442
sprintf( 'MySQL query not supported. Cause: %s', $cause )
24332443
);
24342444
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public function db_connect( $allow_bail = true ) {
238238
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-lexer.php';
239239
require_once __DIR__ . '/../../wp-includes/mysql/class-wp-mysql-parser.php';
240240
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
241+
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
241242
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
242243
$this->dbh = new WP_SQLite_Driver(
243244
array(

0 commit comments

Comments
 (0)