Ver código fonte

允许使用not

myuan 2 anos atrás
pai
commit
f267aef7b0
2 arquivos alterados com 19 adições e 6 exclusões
  1. 8 5
      run_test.py
  2. 11 1
      src/calc.y

+ 8 - 5
run_test.py

@@ -168,7 +168,7 @@ async def assert_sqls():
         ],
     )
     await assert_sql(
-        "update tb1 set col1=3, col4=4 where col1=2 and col2=col3;",
+        "update tb1 set col1=3, col4=4 where not col1=2 and col2=3;",
         [
             {
                 "type": "update",
@@ -188,14 +188,17 @@ async def assert_sqls():
                 "where": {
                     "type": "且",
                     "left": {
-                        "type": "相等",
-                        "left": {"type": "identifier", "value": "col1"},
-                        "right": {"type": "int", "value": 2},
+                        "type": "非",
+                        "right": {
+                            "type": "相等",
+                            "left": {"type": "identifier", "value": "col1"},
+                            "right": {"type": "int", "value": 2},
+                        },
                     },
                     "right": {
                         "type": "相等",
                         "left": {"type": "identifier", "value": "col2"},
-                        "right": {"type": "identifier", "value": "col3"},
+                        "right": {"type": "int", "value": 3},
                     },
                 },
             }

+ 11 - 1
src/calc.y

@@ -42,7 +42,7 @@ cJSON* jroot;
 %type <iv> INT_V
 %type <fv> FLOAT_V
 %type <sv> STRING_V
-%type <sv> IDENTIFIER data_type PRIMARY_KEY col_options bin_compare_op bin_logical_op
+%type <sv> IDENTIFIER data_type PRIMARY_KEY col_options bin_compare_op bin_logical_op unary_compare_op
 %type <jv> create_definition create_col_list create_table_stmt data_value
 %type <jv> insert_stmt insert_list 
 %type <jv> update_stmt update_list single_assign_item
@@ -208,6 +208,12 @@ where_conditions: where_condition_item {
 		cJSON_AddItemToObject(node, "right", $3);
 		$$=node;
 	}
+	| unary_compare_op where_condition_item {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", $1);
+		cJSON_AddItemToObject(node, "right", $2);
+		$$=node;
+	}
 ;
 
 identifier: IDENTIFIER {
@@ -227,6 +233,7 @@ where_condition_item: identifier_or_const_value bin_compare_op identifier_or_con
 		$$=node;
 	};
 
+
 bin_compare_op: '=' {$$ = "相等";}
 	| '>' {$$ = "大于";}
 	| '<' {$$ = "小于";}
@@ -234,6 +241,9 @@ bin_compare_op: '=' {$$ = "相等";}
 	| "<=" {$$ = "小等";}
 	| "!=" {$$ = "不等";}
 ;
+
+unary_compare_op: NOT {$$ = "非";};
+
 bin_logical_op: AND {$$ = "且";}
 	| OR {$$ = "或";}