Browse Source

select table 修改为 表列表

myuan 2 years ago
parent
commit
4dd66e21f8
2 changed files with 31 additions and 10 deletions
  1. 22 3
      src/parser.y
  2. 9 7
      tests_config.py

+ 22 - 3
src/parser.y

@@ -72,7 +72,7 @@ cJSON* jroot;
 
 %type <iv> INT_V
 %type <fv> FLOAT_V
-%type <sv> STRING_V NOT
+%type <sv> STRING_V NOT table_name
 %type <sv> IDENTIFIER data_type PRIMARY_KEY col_options bin_cmp_op bin_logical_op unary_compare_op bin_contains_op
 %type <jv> create_definition create_col_list create_table_stmt data_value
 %type <jv> insert_stmt insert_list 
@@ -82,6 +82,7 @@ cJSON* jroot;
 %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> op_join table_field column_name
+%type <jv> table_name_list
 
 
 %left OR
@@ -381,11 +382,29 @@ op_join: {$$ = NULL;}
 	}
 ;
 
-select_stmt: SELECT select_items FROM IDENTIFIER op_join op_where_expr {
+table_name: IDENTIFIER {$$=$1;}
+;
+
+table_name_list: table_name {
+		MEET_VAR(table_name, $1);
+		cJSON* node = cJSON_CreateArray();
+		cJSON_AddItemToArray(node, cJSON_CreateString($1));
+		$$=node;
+	}
+	| table_name_list ',' table_name {
+		MEET_VAR(table_name, $3);
+		MEET_VAR(table_name_list, $1);
+
+		cJSON_AddItemToArray($1, $3);
+		$$=$1;
+	}
+;
+
+select_stmt: SELECT select_items FROM table_name_list op_join op_where_expr {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", "select_stmt");
 		cJSON_AddItemToObject(node, "select_cols", $2);
-		cJSON_AddStringToObject(node, "table_name", $4);
+		cJSON_AddItemToObject(node, "table_names", $4);
 		if ($5 != NULL) {
 			cJSON_AddItemToObject(node, "join_options", $5);
 		}

+ 9 - 7
tests_config.py

@@ -279,7 +279,7 @@ sql_parser_tests = [
             {
                 "type": "select_stmt",
                 "select_cols": [{"type": "select_all_column"}],
-                "table_name": "t2",
+                "table_names": ["t2"],
                 "where": {},
             }
         ],
@@ -296,7 +296,7 @@ sql_parser_tests = [
                         "alias": "t",
                     }
                 ],
-                "table_name": "t2",
+                "table_names": ["t2"],
                 "where": {
                     "type": "大于",
                     "left": {"type": "identifier", "value": "col1"},
@@ -316,7 +316,7 @@ sql_parser_tests = [
                         "target": {"type": "identifier", "value": "Sname"},
                     }
                 ],
-                "table_name": "Student",
+                "table_names": ["Student"],
                 "where": {
                     "type": "且",
                     "left": {
@@ -362,7 +362,7 @@ sql_parser_tests = [
                         },
                     }
                 ],
-                "table_name": "Student",
+                "table_names": ["Student"],
                 "where": {
                     "type": "包含于",
                     "left": {"type": "identifier", "value": "Sno"},
@@ -374,7 +374,7 @@ sql_parser_tests = [
                                 "target": {"type": "identifier", "value": "Sno"},
                             }
                         ],
-                        "table_name": "SC",
+                        "table_names": ["SC"],
                         "where": {
                             "type": "相等",
                             "left": {
@@ -409,7 +409,7 @@ sql_parser_tests = [
                         },
                     }
                 ],
-                "table_name": "Student",
+                "table_names": ["Student"],
                 "join_options": {
                     "type": "join_options",
                     "join_with": {"type": "identifier", "value": "SC"},
@@ -451,7 +451,7 @@ sql_parser_tests = [
                         },
                     }
                 ],
-                "table_name": "Student",
+                "table_names": ["Student"],
                 "join_options": {
                     "type": "join_options",
                     "join_with": {"type": "identifier", "value": "SC"},
@@ -499,4 +499,6 @@ sql_checker_tests = [
     ('drop table class;', True),
     ('create table class (id int, grade int, faculty string);', True),
     ('select * from class where grade = 2 and faculty = \'Computer Science\';', True),
+    ('select * from class where grade = 2 and count=33;', 'column `"count"` not exists in `class`'),
+
 ]