@@ -905,6 +905,8 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
905
905
$ this ->execute_select_statement ( $ ast );
906
906
break ;
907
907
case 'insertStatement ' :
908
+ $ this ->execute_insert_statement ( $ ast );
909
+ break ;
908
910
case 'updateStatement ' :
909
911
$ this ->execute_update_statement ( $ ast );
910
912
break ;
@@ -1061,6 +1063,20 @@ private function execute_select_statement( WP_Parser_Node $node ): void {
1061
1063
);
1062
1064
}
1063
1065
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
+
1064
1080
private function execute_update_statement ( WP_Parser_Node $ node ): void {
1065
1081
// @TODO: Add support for UPDATE with multiple tables and JOINs.
1066
1082
// SQLite supports them in the FROM clause.
@@ -1077,32 +1093,40 @@ private function execute_update_statement( WP_Parser_Node $node ): void {
1077
1093
* Will be rewritten to:
1078
1094
* UPDATE t SET c = 1 WHERE rowid IN ( SELECT rowid FROM t WHERE c = 2 LIMIT 1 );
1079
1095
*/
1096
+ $ where_subquery = null ;
1080
1097
if ( $ has_order || $ has_limit ) {
1081
- $ subquery = 'SELECT rowid FROM ' . $ this ->translate_sequence (
1098
+ $ where_subquery = 'SELECT rowid FROM ' . $ this ->translate_sequence (
1082
1099
array (
1083
1100
$ node ->get_descendant_node ( 'tableReference ' ),
1084
1101
$ node ->get_descendant_node ( 'whereClause ' ),
1085
1102
$ node ->get_descendant_node ( 'orderClause ' ),
1086
1103
$ node ->get_descendant_node ( 'simpleLimitClause ' ),
1087
1104
)
1088
1105
);
1106
+ }
1089
1107
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 ;
1100
1122
}
1101
- $ query = $ this ->translate_sequence ( $ update_nodes )
1102
- . ' WHERE rowid IN ( ' . $ subquery . ' ) ' ;
1103
- } else {
1104
- $ query = $ this ->translate ( $ node );
1105
1123
}
1124
+
1125
+ $ query = implode ( ' ' , $ parts );
1126
+ if ( null !== $ where_subquery ) {
1127
+ $ query .= ' WHERE rowid IN ( ' . $ where_subquery . ' ) ' ;
1128
+ }
1129
+
1106
1130
$ this ->execute_sqlite_query ( $ query );
1107
1131
$ this ->set_result_from_affected_rows ();
1108
1132
}
0 commit comments