Browse Source

添加括号处理

myuan 2 years ago
parent
commit
4b140c9255
2 changed files with 52 additions and 0 deletions
  1. 38 0
      run_test.py
  2. 14 0
      src/calc.y

+ 38 - 0
run_test.py

@@ -234,6 +234,44 @@ async def assert_sqls():
             }
         ],
     )
+    await assert_sql(
+        "delete from tb1 where c1 = 1 and (c2= 3 or c3=3) or (c4='asd');",
+        [
+            {
+                "type": "delete",
+                "table_name": "tb1",
+                "where": {
+                    "type": "或",
+                    "left": {
+                        "type": "且",
+                        "left": {
+                            "type": "相等",
+                            "left": {"type": "identifier", "value": "c1"},
+                            "right": {"type": "int", "value": 1},
+                        },
+                        "right": {
+                            "type": "或",
+                            "left": {
+                                "type": "相等",
+                                "left": {"type": "identifier", "value": "c2"},
+                                "right": {"type": "int", "value": 3},
+                            },
+                            "right": {
+                                "type": "相等",
+                                "left": {"type": "identifier", "value": "c3"},
+                                "right": {"type": "int", "value": 3},
+                            },
+                        },
+                    },
+                    "right": {
+                        "type": "相等",
+                        "left": {"type": "identifier", "value": "c4"},
+                        "right": {"type": "string", "value": "'asd'"},
+                    },
+                },
+            }
+        ],
+    )
 
 
 async def on_modified(event):

+ 14 - 0
src/calc.y

@@ -197,9 +197,14 @@ single_assign_item: identifier '=' identifier_or_const_value {
 		$$=node;
 	}
 ;
+
 where_conditions: where_condition_item {
 		$$=$1;
 	}
+	| '(' where_condition_item ')' {
+		$$=$2;
+	}
+
 	|  where_conditions bin_logical_op where_condition_item {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", $2);
@@ -208,12 +213,21 @@ where_conditions: where_condition_item {
 		cJSON_AddItemToObject(node, "right", $3);
 		$$=node;
 	}
+	|  where_conditions bin_logical_op '(' where_conditions ')' {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", $2);
+
+		cJSON_AddItemToObject(node, "left", $1);
+		cJSON_AddItemToObject(node, "right", $4);
+		$$=node;
+	}
 	| unary_compare_op where_condition_item {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", $1);
 		cJSON_AddItemToObject(node, "right", $2);
 		$$=node;
 	}
+	
 ;
 
 identifier: IDENTIFIER {