Kaynağa Gözat

添加insert

myuan 2 yıl önce
ebeveyn
işleme
1f9fd1ee07
3 değiştirilmiş dosya ile 103 ekleme ve 5 silme
  1. 28 1
      run_test.py
  2. 7 3
      src/calc.l
  3. 68 1
      src/calc.y

+ 28 - 1
run_test.py

@@ -167,6 +167,34 @@ async def assert_sqls():
             }
         ],
     )
+    await assert_sql(
+        "update tb1 set col1=3 where col1=2 and col2=col3;",
+        [
+            {
+                "type": "update",
+                "table_name": "tb1",
+                "set": [
+                    {
+                        "type": "assign_const",
+                        "left": {"type": "identifier", "value": "col1"},
+                        "right": {"type": "int", "value": 3},
+                    }
+                ],
+                "where": [
+                    {
+                        "type": "where_condition",
+                        "left": {"type": "identifier", "value": "col1"},
+                        "right": {"type": "int", "value": 2},
+                    },
+                    {
+                        "type": "where_condition",
+                        "left": {"type": "identifier", "value": "col2"},
+                        "right": {"type": "identifier", "value": "col3"},
+                    },
+                ],
+            }
+        ],
+    )
 
 
 async def on_modified(event):
@@ -187,7 +215,6 @@ async def on_modified(event):
         print(datetime.now(), colored("all tests right!", "green"))
 
 
-
 async def restart():
     async for _ in awatch(__file__):
         print("restart")

+ 7 - 3
src/calc.l

@@ -39,15 +39,19 @@ extern YYSTYPE yylval;
 "INT"       {cp_yylval_and_return(INT_T);}
 "FLOAT"     {cp_yylval_and_return(FLOAT_T);}
 "STRING"    {cp_yylval_and_return(STRING_T);}
+"AND"       return AND;
 
 
 ;		    {return NEWLINE;}
 
 [*]			return *yytext;
 [,]			return *yytext;
-[=]			return *yytext;
-[<]			return *yytext;
-[>]			return *yytext;
+[=]			{cp_yylval_and_return('=');}
+[<]			{cp_yylval_and_return('<');}
+[>]			{cp_yylval_and_return('>');}
+[>][=]      {cp_yylval_and_return(">=");}
+[<][=]      {cp_yylval_and_return("<=");}
+
 [(]         return *yytext;
 [)]         return *yytext;
 

+ 68 - 1
src/calc.y

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