|
@@ -42,9 +42,11 @@ cJSON* jroot;
|
|
|
%type <iv> INT_V
|
|
|
%type <fv> FLOAT_V
|
|
|
%type <sv> STRING_V
|
|
|
-%type <sv> IDENTIFIER data_type PRIMARY_KEY col_options
|
|
|
+%type <sv> IDENTIFIER data_type PRIMARY_KEY col_options bin_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
|
|
|
+%type <jv> where_condition_item where_conditions identifier identifier_or_const_value
|
|
|
|
|
|
%start statement
|
|
|
|
|
@@ -57,6 +59,7 @@ statement: NEWLINE
|
|
|
|
|
|
sql_statement: create_table_stmt
|
|
|
| insert_stmt
|
|
|
+ | update_stmt
|
|
|
;
|
|
|
|
|
|
create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
|
|
@@ -164,6 +167,70 @@ data_value: INT_V {
|
|
|
}
|
|
|
;
|
|
|
|
|
|
+update_stmt: UPDATE IDENTIFIER SET update_list WHERE where_conditions NEWLINE {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "update");
|
|
|
+ cJSON_AddStringToObject(node, "table_name", $2);
|
|
|
+ cJSON_AddItemToObject(node, "set", $4);
|
|
|
+ cJSON_AddItemToObject(node, "where", $6);
|
|
|
+ cJSON_AddItemToArray(jroot, node);
|
|
|
+ $$=node;
|
|
|
+};
|
|
|
+update_list: single_assign_item {
|
|
|
+ cJSON* node = cJSON_CreateArray();
|
|
|
+ cJSON_AddItemToArray(node, $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+ | update_list ',' single_assign_item {
|
|
|
+ cJSON_AddItemToArray($1, $3);
|
|
|
+ $$=$1;
|
|
|
+ }
|
|
|
+;
|
|
|
+
|
|
|
+single_assign_item: identifier '=' identifier_or_const_value {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "assign_const");
|
|
|
+ cJSON_AddItemToObject(node, "left", $1);
|
|
|
+ cJSON_AddItemToObject(node, "right", $3);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+;
|
|
|
+where_conditions: where_condition_item {
|
|
|
+ cJSON* node = cJSON_CreateArray();
|
|
|
+ cJSON_AddItemToArray(node, $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+ | where_conditions AND where_condition_item {
|
|
|
+ cJSON_AddItemToArray($1, $3);
|
|
|
+ $$=$1;
|
|
|
+ }
|
|
|
+;
|
|
|
+
|
|
|
+identifier: IDENTIFIER {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "identifier");
|
|
|
+ cJSON_AddStringToObject(node, "value", $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+identifier_or_const_value: identifier {$$=$1;}
|
|
|
+ | data_value {$$=$1;}
|
|
|
+
|
|
|
+where_condition_item: identifier_or_const_value bin_compare_op identifier_or_const_value {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "where_condition");
|
|
|
+ cJSON_AddItemToObject(node, "left", $1);
|
|
|
+ cJSON_AddItemToObject(node, "right", $3);
|
|
|
+ $$=node;
|
|
|
+ };
|
|
|
+
|
|
|
+bin_compare_op: '=' {$$ = "=";}
|
|
|
+ | '>' {$$ = ">";}
|
|
|
+ | '<' {$$ = "<";}
|
|
|
+ | ">=" {$$ = ">=";}
|
|
|
+ | "<=" {$$ = "<=";}
|
|
|
+ | "!=" {$$ = "!=";}
|
|
|
+;
|
|
|
+
|
|
|
%%
|
|
|
|
|
|
int main(int ac, char** av) {
|