瀏覽代碼

完成朴素 select

myuan 2 年之前
父節點
當前提交
bbbaf4ae67
共有 3 個文件被更改,包括 81 次插入5 次删除
  1. 32 0
      run_test.py
  2. 1 1
      src/calc.l
  3. 48 4
      src/calc.y

+ 32 - 0
run_test.py

@@ -272,6 +272,38 @@ async def assert_sqls():
             }
         ],
     )
+    await assert_sql(
+        "select * from t2;",
+        [
+            {
+                "type": "select",
+                "select_cols": [{"type": "select_all_column"}],
+                "table_name": "t2",
+                "where": {},
+            }
+        ],
+    )
+    await assert_sql(
+        "select c2 as t from t2 where col1>2;",
+        [
+            {
+                "type": "select",
+                "select_cols": [
+                    {
+                        "type": "select_column",
+                        "target": {"type": "identifier", "value": "c2"},
+                        "alias": "t",
+                    }
+                ],
+                "table_name": "t2",
+                "where": {
+                    "type": "大于",
+                    "left": {"type": "identifier", "value": "col1"},
+                    "right": {"type": "int", "value": 2},
+                },
+            }
+        ],
+    )
 
 
 async def on_modified(event):

+ 1 - 1
src/calc.l

@@ -42,7 +42,7 @@ extern YYSTYPE yylval;
 "AND"       return AND;
 "OR"        return OR;
 "NOT"       return NOT;
-
+"AS"        return AS;
 
 ;		    {return NEWLINE;}
 

+ 48 - 4
src/calc.y

@@ -30,6 +30,7 @@ cJSON* jroot;
 %token IDENTIFIER 
 
 %token SELECT FROM WHERE INSERT INTO VALUES DELETE UPDATE SET JOIN CREATE TABLE 
+%token AS
 %token AND OR NOT
 %token INT_V FLOAT_V STRING_V // 作为 value 出现的
 %token INT_T FLOAT_T STRING_T // 作为 字面量 type 出现的情况
@@ -46,8 +47,8 @@ cJSON* jroot;
 %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
-%type <jv> where_condition_item where_conditions identifier identifier_or_const_value
-%type <jv> delete_stmt
+%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 
 
 %start statement
 
@@ -62,6 +63,7 @@ sql_statement: create_table_stmt
 	| insert_stmt
 	| update_stmt
 	| delete_stmt
+	| select_stmt
 ;
 
 create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
@@ -198,6 +200,9 @@ single_assign_item: identifier '=' identifier_or_const_value {
 	}
 ;
 
+the_whole_where_smt: {$$=cJSON_CreateObject();}
+	| WHERE where_conditions {$$=$2;};
+
 where_conditions: where_condition_item {
 		$$=$1;
 	}
@@ -261,14 +266,53 @@ unary_compare_op: NOT {$$ = "非";};
 bin_logical_op: AND {$$ = "且";}
 	| OR {$$ = "或";}
 
-delete_stmt: DELETE FROM IDENTIFIER WHERE where_conditions NEWLINE {
+delete_stmt: DELETE FROM IDENTIFIER the_whole_where_smt NEWLINE {
 		cJSON* node = cJSON_CreateObject();
 		cJSON_AddStringToObject(node, "type", "delete");
 		cJSON_AddStringToObject(node, "table_name", $3);
-		cJSON_AddItemToObject(node, "where", $5);
+		cJSON_AddItemToObject(node, "where", $4);
 		cJSON_AddItemToArray(jroot, node);
 		$$=node;
 };
+
+select_stmt: SELECT select_items FROM IDENTIFIER the_whole_where_smt NEWLINE {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "select");
+		cJSON_AddItemToObject(node, "select_cols", $2);
+		cJSON_AddStringToObject(node, "table_name", $4);
+		cJSON_AddItemToObject(node, "where", $5);
+		$$=node;
+		cJSON_AddItemToArray(jroot, node);
+};
+select_items: select_item {
+		cJSON* node = cJSON_CreateArray();
+		cJSON_AddItemToArray(node, $1);
+		$$=node;
+	}
+	| select_items ',' select_item {
+		cJSON_AddItemToArray($1, $3);
+		$$=$1;
+	}
+select_item: identifier_or_const_value {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "select_column");
+		cJSON_AddItemToObject(node, "target", $1);
+		$$=node;
+	}
+	| identifier_or_const_value AS IDENTIFIER {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "select_column");
+		cJSON_AddItemToObject(node, "target", $1);
+		cJSON_AddStringToObject(node, "alias", $3);
+		$$=node;
+	} 
+	| '*' {
+		cJSON* node = cJSON_CreateObject();
+		cJSON_AddStringToObject(node, "type", "select_all_column");
+		$$=node;
+	}
+	| 
+;
 %%
 
 int main(int ac, char** av) {