Quellcode durchsuchen

添加操作符类型

myuan vor 2 Jahren
Ursprung
Commit
53dca97270
2 geänderte Dateien mit 53 neuen und 41 gelöschten Zeilen
  1. 10 36
      src/parser.y
  2. 43 5
      tests_config.py

+ 10 - 36
src/parser.y

@@ -77,9 +77,8 @@ cJSON* jroot;
 %type <jv> create_definition create_col_list create_table_stmt data_value
 %type <jv> create_definition create_col_list create_table_stmt data_value
 %type <jv> insert_stmt insert_list 
 %type <jv> insert_stmt insert_list 
 %type <jv> update_stmt update_list single_assign_item
 %type <jv> update_stmt update_list single_assign_item
-%type <jv> where_condition_item identifier identifier_or_const_value 
+%type <jv> identifier identifier_or_const_value 
 %type <jv> delete_stmt select_stmt select_item select_items drop_stmt
 %type <jv> delete_stmt select_stmt select_item select_items drop_stmt
-%type <jv> data_value_list identifier_or_const_value_or_const_value_list
 %type <jv> compare_expr single_expr where_expr logical_expr negative_expr op_where_expr expr_list contains_expr
 %type <jv> compare_expr single_expr where_expr logical_expr negative_expr op_where_expr expr_list contains_expr
 %type <jv> op_join table_field column_name
 %type <jv> op_join table_field column_name
 %type <jv> table_name_list
 %type <jv> table_name_list
@@ -252,8 +251,10 @@ op_where_expr: {$$=cJSON_CreateObject();}
 ;
 ;
 
 
 compare_expr: compare_expr bin_cmp_op compare_expr {
 compare_expr: compare_expr bin_cmp_op compare_expr {
-	fprintf(stderr, "compare_expr %s\n", $2);
-	SIMPLE_OP_NODE($$, $2, $1, $3);}
+		fprintf(stderr, "compare_expr %s\n", $2);
+		SIMPLE_OP_NODE($$, $2, $1, $3);
+		cJSON_AddStringToObject($$, "op_type", "bin_cmp_op");
+	}
 	| single_expr {MEET_JSON(single_expr from compare_expr, $1); $$=$1;}
 	| single_expr {MEET_JSON(single_expr from compare_expr, $1); $$=$1;}
 	| '(' where_expr ')' {MEET(括号where_expr from compare_expr); $$=$2;}
 	| '(' where_expr ')' {MEET(括号where_expr from compare_expr); $$=$2;}
 ;
 ;
@@ -265,11 +266,13 @@ negative_expr: NOT negative_expr {SIMPLE_OP_NODE_ONLY_LEFT($$, "非", $2);}
 
 
 contains_expr: identifier bin_contains_op '(' select_stmt ')' {
 contains_expr: identifier bin_contains_op '(' select_stmt ')' {
 		MEET_JSON(logical_expr bin_contains_op select_stmt, $2);
 		MEET_JSON(logical_expr bin_contains_op select_stmt, $2);
-		SIMPLE_OP_NODE($$, $2, $1, $4)
+		SIMPLE_OP_NODE($$, $2, $1, $4);
+		cJSON_AddStringToObject($$, "op_type", "bin_contains_op");
 	}
 	}
 	| identifier bin_contains_op '(' expr_list ')' {
 	| identifier bin_contains_op '(' expr_list ')' {
 		MEET_VAR(logical_expr bin_contains_op expr_list, $2);
 		MEET_VAR(logical_expr bin_contains_op expr_list, $2);
-		SIMPLE_OP_NODE($$, $2, $1, $4)
+		SIMPLE_OP_NODE($$, $2, $1, $4);
+		cJSON_AddStringToObject($$, "op_type", "bin_contains_op");
 	}
 	}
 	| negative_expr {MEET_JSON(negative_expr from contains_expr, $1); $$=$1;}
 	| negative_expr {MEET_JSON(negative_expr from contains_expr, $1); $$=$1;}
 ;
 ;
@@ -277,6 +280,7 @@ contains_expr: identifier bin_contains_op '(' select_stmt ')' {
 logical_expr: logical_expr bin_logical_op contains_expr {
 logical_expr: logical_expr bin_logical_op contains_expr {
 		fprintf(stderr, "logical_expr %s\n", $2);
 		fprintf(stderr, "logical_expr %s\n", $2);
 		SIMPLE_OP_NODE($$, $2, $1, $3);
 		SIMPLE_OP_NODE($$, $2, $1, $3);
+		cJSON_AddStringToObject($$, "op_type", "bin_logical_op");
 	}
 	}
 	| contains_expr {MEET_JSON(contains_expr from logical_expr, $1); $$=$1;}
 	| contains_expr {MEET_JSON(contains_expr from logical_expr, $1); $$=$1;}
 	| single_expr {MEET_JSON(single_expr from logical_expr, $1) $$=$1;}
 	| single_expr {MEET_JSON(single_expr from logical_expr, $1) $$=$1;}
@@ -314,36 +318,6 @@ identifier: IDENTIFIER {
 identifier_or_const_value: identifier {$$=$1;} | data_value {$$=$1;}
 identifier_or_const_value: identifier {$$=$1;} | data_value {$$=$1;}
 ;
 ;
 
 
-data_value_list: data_value {
-		cJSON* node = cJSON_CreateArray();
-		cJSON_AddItemToArray(node, $1);
-		$$=node;
-	}
-	| data_value_list ',' data_value {
-		cJSON_AddItemToArray($1, $3);
-		$$=$1;
-	}
-;
-
-identifier_or_const_value_or_const_value_list: identifier_or_const_value{$$=$1;}
-	| data_value_list {$$=$1;}
-;
-
-where_condition_item: identifier_or_const_value bin_cmp_op identifier_or_const_value {
-		cJSON* node = cJSON_CreateObject();
-		cJSON_AddStringToObject(node, "type", $2);
-		cJSON_AddItemToObject(node, "left", $1);
-		cJSON_AddItemToObject(node, "right", $3);
-		$$=node;
-	}
-	| identifier_or_const_value bin_contains_op '(' data_value_list ')' {
-		cJSON* node = cJSON_CreateObject();
-		cJSON_AddStringToObject(node, "type", $2);
-		cJSON_AddItemToObject(node, "left", $1);
-		cJSON_AddItemToObject(node, "right", $4);
-		$$=node;
-	}
-;
 
 
 bin_cmp_op: '=' {$$ = "相等";}
 bin_cmp_op: '=' {$$ = "相等";}
 	| '>' {$$ = "大于";}
 	| '>' {$$ = "大于";}

+ 43 - 5
tests_config.py

@@ -144,12 +144,15 @@ sql_parser_tests = [
                         "type": "相等",
                         "type": "相等",
                         "left": {"type": "identifier", "value": "col1"},
                         "left": {"type": "identifier", "value": "col1"},
                         "right": {"type": "int", "value": 2},
                         "right": {"type": "int", "value": 2},
+                        "op_type": "bin_cmp_op",
                     },
                     },
                     "right": {
                     "right": {
                         "type": "相等",
                         "type": "相等",
                         "left": {"type": "identifier", "value": "col2"},
                         "left": {"type": "identifier", "value": "col2"},
                         "right": {"type": "int", "value": 4},
                         "right": {"type": "int", "value": 4},
+                        "op_type": "bin_cmp_op",
                     },
                     },
+                    "op_type": "bin_logical_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -186,6 +189,7 @@ sql_parser_tests = [
                                         "type": "相等",
                                         "type": "相等",
                                         "left": {"type": "identifier", "value": "col1"},
                                         "left": {"type": "identifier", "value": "col1"},
                                         "right": {"type": "int", "value": 2},
                                         "right": {"type": "int", "value": 2},
+                                        "op_type": "bin_cmp_op",
                                     },
                                     },
                                 },
                                 },
                             },
                             },
@@ -194,13 +198,17 @@ sql_parser_tests = [
                             "type": "相等",
                             "type": "相等",
                             "left": {"type": "identifier", "value": "col2"},
                             "left": {"type": "identifier", "value": "col2"},
                             "right": {"type": "int", "value": 4},
                             "right": {"type": "int", "value": 4},
+                            "op_type": "bin_cmp_op",
                         },
                         },
+                        "op_type": "bin_logical_op",
                     },
                     },
                     "right": {
                     "right": {
                         "type": "相等",
                         "type": "相等",
                         "left": {"type": "identifier", "value": "col3"},
                         "left": {"type": "identifier", "value": "col3"},
                         "right": {"type": "identifier", "value": "col2"},
                         "right": {"type": "identifier", "value": "col2"},
+                        "op_type": "bin_cmp_op",
                     },
                     },
+                    "op_type": "bin_logical_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -214,23 +222,28 @@ sql_parser_tests = [
                 "where": {
                 "where": {
                     "type": "或",
                     "type": "或",
                     "left": {
                     "left": {
+                        "type": "且",
                         "left": {
                         "left": {
                             "type": "相等",
                             "type": "相等",
                             "left": {"type": "identifier", "value": "c1"},
                             "left": {"type": "identifier", "value": "c1"},
                             "right": {"type": "int", "value": 1},
                             "right": {"type": "int", "value": 1},
+                            "op_type": "bin_cmp_op",
                         },
                         },
-                        "type": "且",
                         "right": {
                         "right": {
                             "type": "相等",
                             "type": "相等",
                             "left": {"type": "identifier", "value": "c2"},
                             "left": {"type": "identifier", "value": "c2"},
                             "right": {"type": "int", "value": 3},
                             "right": {"type": "int", "value": 3},
+                            "op_type": "bin_cmp_op",
                         },
                         },
+                        "op_type": "bin_logical_op",
                     },
                     },
                     "right": {
                     "right": {
                         "type": "相等",
                         "type": "相等",
                         "left": {"type": "identifier", "value": "c3"},
                         "left": {"type": "identifier", "value": "c3"},
                         "right": {"type": "int", "value": 3},
                         "right": {"type": "int", "value": 3},
+                        "op_type": "bin_cmp_op",
                     },
                     },
+                    "op_type": "bin_logical_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -249,6 +262,7 @@ sql_parser_tests = [
                             "type": "相等",
                             "type": "相等",
                             "left": {"type": "identifier", "value": "c1"},
                             "left": {"type": "identifier", "value": "c1"},
                             "right": {"type": "int", "value": 1},
                             "right": {"type": "int", "value": 1},
+                            "op_type": "bin_cmp_op",
                         },
                         },
                         "right": {
                         "right": {
                             "type": "或",
                             "type": "或",
@@ -256,19 +270,25 @@ sql_parser_tests = [
                                 "type": "相等",
                                 "type": "相等",
                                 "left": {"type": "identifier", "value": "c2"},
                                 "left": {"type": "identifier", "value": "c2"},
                                 "right": {"type": "int", "value": 3},
                                 "right": {"type": "int", "value": 3},
+                                "op_type": "bin_cmp_op",
                             },
                             },
                             "right": {
                             "right": {
                                 "type": "相等",
                                 "type": "相等",
                                 "left": {"type": "identifier", "value": "c3"},
                                 "left": {"type": "identifier", "value": "c3"},
                                 "right": {"type": "int", "value": 3},
                                 "right": {"type": "int", "value": 3},
+                                "op_type": "bin_cmp_op",
                             },
                             },
+                            "op_type": "bin_logical_op",
                         },
                         },
+                        "op_type": "bin_logical_op",
                     },
                     },
                     "right": {
                     "right": {
                         "type": "相等",
                         "type": "相等",
                         "left": {"type": "identifier", "value": "c4"},
                         "left": {"type": "identifier", "value": "c4"},
                         "right": {"type": "string", "value": "'asd'"},
                         "right": {"type": "string", "value": "'asd'"},
+                        "op_type": "bin_cmp_op",
                     },
                     },
+                    "op_type": "bin_logical_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -309,6 +329,7 @@ sql_parser_tests = [
                     "type": "大于",
                     "type": "大于",
                     "left": {"type": "identifier", "value": "col1"},
                     "left": {"type": "identifier", "value": "col1"},
                     "right": {"type": "int", "value": 2},
                     "right": {"type": "int", "value": 2},
+                    "op_type": "bin_cmp_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -334,6 +355,7 @@ sql_parser_tests = [
                             {"type": "int", "value": 1},
                             {"type": "int", "value": 1},
                             {"type": "int", "value": 2},
                             {"type": "int", "value": 2},
                         ],
                         ],
+                        "op_type": "bin_contains_op",
                     },
                     },
                     "right": {
                     "right": {
                         "type": "包含于",
                         "type": "包含于",
@@ -343,7 +365,9 @@ sql_parser_tests = [
                             {"type": "int", "value": 4},
                             {"type": "int", "value": 4},
                             {"type": "int", "value": 5},
                             {"type": "int", "value": 5},
                         ],
                         ],
+                        "op_type": "bin_contains_op",
                     },
                     },
+                    "op_type": "bin_logical_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -391,8 +415,10 @@ sql_parser_tests = [
                                 "field": "Cno",
                                 "field": "Cno",
                             },
                             },
                             "right": {"type": "string", "value": "'81003'"},
                             "right": {"type": "string", "value": "'81003'"},
+                            "op_type": "bin_cmp_op",
                         },
                         },
                     },
                     },
+                    "op_type": "bin_contains_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -429,12 +455,14 @@ sql_parser_tests = [
                             "field": "Sno",
                             "field": "Sno",
                         },
                         },
                         "right": {"type": "table_field", "table": "SC", "field": "Sno"},
                         "right": {"type": "table_field", "table": "SC", "field": "Sno"},
+                        "op_type": "bin_cmp_op",
                     },
                     },
                 },
                 },
                 "where": {
                 "where": {
                     "type": "相等",
                     "type": "相等",
                     "left": {"type": "table_field", "table": "SC", "field": "Cno"},
                     "left": {"type": "table_field", "table": "SC", "field": "Cno"},
                     "right": {"type": "string", "value": "'81003'"},
                     "right": {"type": "string", "value": "'81003'"},
+                    "op_type": "bin_cmp_op",
                 },
                 },
             }
             }
         ],
         ],
@@ -477,6 +505,7 @@ sql_parser_tests = [
                                 "table": "SC",
                                 "table": "SC",
                                 "field": "Sno",
                                 "field": "Sno",
                             },
                             },
+                            "op_type": "bin_cmp_op",
                         },
                         },
                         "right": {
                         "right": {
                             "type": "相等",
                             "type": "相等",
@@ -486,7 +515,9 @@ sql_parser_tests = [
                                 "field": "Cno",
                                 "field": "Cno",
                             },
                             },
                             "right": {"type": "string", "value": "'81003'"},
                             "right": {"type": "string", "value": "'81003'"},
+                            "op_type": "bin_cmp_op",
                         },
                         },
+                        "op_type": "bin_logical_op",
                     },
                     },
                 },
                 },
                 "where": {},
                 "where": {},
@@ -543,7 +574,7 @@ sql_checker_tests = [
                 WHERE class.id = classId
                 WHERE class.id = classId
             );
             );
         """,
         """,
-        'column `grade` not exists in `person`',
+        "column `grade` not exists in `person`",
     ),
     ),
     (
     (
         """select person.name 
         """select person.name 
@@ -553,13 +584,20 @@ sql_checker_tests = [
         """,
         """,
         "column `person.grade` not exists in `person, class`",
         "column `person.grade` not exists in `person, class`",
     ),
     ),
-        (
+    (
         """select person.name 
         """select person.name 
         from person join class 
         from person join class 
         on class.id=person.classId and class.grade=2
         on class.id=person.classId and class.grade=2
         where age=22;
         where age=22;
         """,
         """,
-        True
+        True,
+    ),
+    (
+        """select person.name 
+        from person join class 
+        on class.id=person.classId and class.grade = 'zxc'
+        where age=22;
+        """,
+        True,
     ),
     ),
-
 ]
 ]