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

Commit ebfca2c

Browse files
committed
Implement INSERT IGNORE and UPDATE IGNORE
1 parent 822464a commit ebfca2c

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
905905
$this->execute_select_statement( $ast );
906906
break;
907907
case 'insertStatement':
908+
$this->execute_insert_statement( $ast );
909+
break;
908910
case 'updateStatement':
909911
$this->execute_update_statement( $ast );
910912
break;
@@ -1061,6 +1063,20 @@ private function execute_select_statement( WP_Parser_Node $node ): void {
10611063
);
10621064
}
10631065

1066+
private function execute_insert_statement( WP_Parser_Node $node ): void {
1067+
$parts = array();
1068+
foreach ( $node->get_children() as $child ) {
1069+
if ( $child instanceof WP_MySQL_Token && WP_MySQL_Lexer::IGNORE_SYMBOL === $child->id ) {
1070+
$parts[] = 'OR IGNORE';
1071+
} else {
1072+
$parts[] = $this->translate( $child );
1073+
}
1074+
}
1075+
$query = implode( ' ', $parts );
1076+
$this->execute_sqlite_query( $query );
1077+
$this->set_result_from_affected_rows();
1078+
}
1079+
10641080
private function execute_update_statement( WP_Parser_Node $node ): void {
10651081
// @TODO: Add support for UPDATE with multiple tables and JOINs.
10661082
// SQLite supports them in the FROM clause.
@@ -1077,32 +1093,40 @@ private function execute_update_statement( WP_Parser_Node $node ): void {
10771093
* Will be rewritten to:
10781094
* UPDATE t SET c = 1 WHERE rowid IN ( SELECT rowid FROM t WHERE c = 2 LIMIT 1 );
10791095
*/
1096+
$where_subquery = null;
10801097
if ( $has_order || $has_limit ) {
1081-
$subquery = 'SELECT rowid FROM ' . $this->translate_sequence(
1098+
$where_subquery = 'SELECT rowid FROM ' . $this->translate_sequence(
10821099
array(
10831100
$node->get_descendant_node( 'tableReference' ),
10841101
$node->get_descendant_node( 'whereClause' ),
10851102
$node->get_descendant_node( 'orderClause' ),
10861103
$node->get_descendant_node( 'simpleLimitClause' ),
10871104
)
10881105
);
1106+
}
10891107

1090-
$update_nodes = array();
1091-
foreach ( $node->get_children() as $child ) {
1092-
$update_nodes[] = $child;
1093-
if (
1094-
$child instanceof WP_Parser_Node
1095-
&& 'updateList' === $child->rule_name
1096-
) {
1097-
// Skip WHERE, ORDER BY, and LIMIT.
1098-
break;
1099-
}
1108+
$parts = array();
1109+
foreach ( $node->get_children() as $child ) {
1110+
if ( $child instanceof WP_MySQL_Token && WP_MySQL_Lexer::IGNORE_SYMBOL === $child->id ) {
1111+
$parts[] = 'OR IGNORE';
1112+
} else {
1113+
$parts[] = $this->translate( $child );
1114+
}
1115+
1116+
if (
1117+
$child instanceof WP_Parser_Node
1118+
&& 'updateList' === $child->rule_name
1119+
) {
1120+
// Skip WHERE, ORDER BY, and LIMIT.
1121+
break;
11001122
}
1101-
$query = $this->translate_sequence( $update_nodes )
1102-
. ' WHERE rowid IN ( ' . $subquery . ' )';
1103-
} else {
1104-
$query = $this->translate( $node );
11051123
}
1124+
1125+
$query = implode( ' ', $parts );
1126+
if ( null !== $where_subquery ) {
1127+
$query .= ' WHERE rowid IN ( ' . $where_subquery . ' )';
1128+
}
1129+
11061130
$this->execute_sqlite_query( $query );
11071131
$this->set_result_from_affected_rows();
11081132
}

0 commit comments

Comments
 (0)