myuan 2 rokov pred
rodič
commit
0f6e197d4b
2 zmenil súbory, kde vykonal 34 pridanie a 15 odobranie
  1. 20 10
      run_test.py
  2. 14 5
      src/calc.y

+ 20 - 10
run_test.py

@@ -23,12 +23,12 @@ async def assert_sql(sql, target):
         stderr=subprocess.PIPE,
     )
     stdout, stderr = await p.communicate()
-    if stderr:
-        print(colored(stderr.decode("utf-8"), "yellow"))
     if b"error" in stdout:
         print(stdout.decode("utf-8"))
         print(datetime.now(), "-" * 40)
+        print(f'other: {colored(stderr.decode("utf-8"), "yellow")}')
         assert False, "sql-parser error"
+
     try:
         output = orjson.loads(stdout)
     except Exception as e:
@@ -40,6 +40,8 @@ async def assert_sql(sql, target):
 input: {colored(sql, "yellow")}
 expect: {colored(target, "green")}
 actual: {colored(output, "red")}
+other: {colored(stderr.decode("utf-8"), "yellow")}
+
 """
 
 
@@ -203,7 +205,7 @@ async def assert_sqls():
         ],
     )
     await assert_sql(
-        "update tb1 set col1=3, col4=4 where not not not col1=2 and col2=4;",
+        "update tb1 set col1=3, col4=4 where not not not col1=2 and col2=4 or col3=col2;",
         [
             {
                 "type": "update",
@@ -221,25 +223,33 @@ async def assert_sqls():
                     },
                 ],
                 "where": {
-                    "type": "",
+                    "type": "",
                     "left": {
-                        "type": "",
+                        "type": "",
                         "left": {
                             "type": "非",
                             "left": {
                                 "type": "非",
                                 "left": {
-                                    "type": "相等",
-                                    "left": {"type": "identifier", "value": "col1"},
-                                    "right": {"type": "int", "value": 2},
+                                    "type": "非",
+                                    "left": {
+                                        "type": "相等",
+                                        "left": {"type": "identifier", "value": "col1"},
+                                        "right": {"type": "int", "value": 2},
+                                    },
                                 },
                             },
                         },
+                        "right": {
+                            "type": "相等",
+                            "left": {"type": "identifier", "value": "col2"},
+                            "right": {"type": "int", "value": 4},
+                        },
                     },
                     "right": {
                         "type": "相等",
-                        "left": {"type": "identifier", "value": "col2"},
-                        "right": {"type": "int", "value": 4},
+                        "left": {"type": "identifier", "value": "col3"},
+                        "right": {"type": "identifier", "value": "col2"},
                     },
                 },
             }

+ 14 - 5
src/calc.y

@@ -74,7 +74,7 @@ cJSON* jroot;
 %type <jv> where_condition_item where_conditions identifier identifier_or_const_value the_whole_where_smt 
 %type <jv> delete_stmt select_stmt select_item select_items 
 %type <jv> data_value_list identifier_or_const_value_or_const_value_list
-%type <jv> search_expr compare_expr single_expr expr where_expr logical_expr negative_expr
+%type <jv> search_expr compare_expr single_expr expr where_expr logical_expr negative_expr op_where_expr
 
 
 // %left OR
@@ -239,15 +239,24 @@ single_expr: identifier {$$=$1;}
 ;
 where_expr: logical_expr {$$=$1;};
 ;
+op_where_expr: {$$=cJSON_CreateObject();} 
+	| WHERE where_expr {$$=$2;}
 
-compare_expr: compare_expr bin_cmp_op compare_expr {SIMPLE_OP_NODE($$, $2, $1, $3);}
+compare_expr: compare_expr bin_cmp_op compare_expr {
+	fprintf(stderr, "compare_expr %s\n", $2);
+	SIMPLE_OP_NODE($$, $2, $1, $3);}
 	| single_expr {$$=$1;}
 
 ;
-negative_expr: NOT negative_expr {SIMPLE_OP_NODE_ONLY_LEFT($$, "非", $2);}
+negative_expr: NOT negative_expr {
+		fprintf(stderr, "negative_expr\n");
+	SIMPLE_OP_NODE_ONLY_LEFT($$, "非", $2);}
 	| compare_expr {$$=$1;}
 ;
-logical_expr: negative_expr bin_logical_op negative_expr {SIMPLE_OP_NODE($$, $2, $1, $3);}
+logical_expr: logical_expr bin_logical_op negative_expr {
+	fprintf(stderr, "logical_expr %s\n", $2);
+	SIMPLE_OP_NODE($$, $2, $1, $3);}
+	| negative_expr {$$=$1;}
 	| single_expr {$$=$1;}
 ;
 
@@ -371,7 +380,7 @@ bin_logical_op: AND {$$ = "且";}
 bin_contains_op: IN {$$ = "包含于";}
 	| NOT IN {$$ = "不包含于";}
 
-delete_stmt: DELETE FROM IDENTIFIER the_whole_where_smt NEWLINE {
+delete_stmt: DELETE FROM IDENTIFIER op_where_expr NEWLINE {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", "delete");
 		cJSON_AddStringToObject(node, "table_name", $3);