|
@@ -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) {
|