This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
forked from youngsamwei/DongmenDB
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexp_01_02_insert.c
106 lines (100 loc) · 3.62 KB
/
exp_01_02_insert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by Sam on 2018/2/13.
//
#include <parser/statement.h>
#include <parser/parser.h>
#include <utils/utils.h>
/**
* 在现有实现的基础上,修改parser以支持insert values多条的特性。
* insert into table(f1,f2,...,fn) values (v1,v2,...,vn),(v1,v2,...,vn),(v1,v2,...,vn)(v1,v2,...,vn);
*/
sql_stmt_insert *parse_sql_stmt_insert(ParserT *parser) {
char *tableName = NULL;
arraylist *fields = arraylist_create();
arraylist *values = arraylist_create();
if (!matchToken(parser, TOKEN_RESERVED_WORD, "insert")) {
return NULL;
}
if (!matchToken(parser, TOKEN_RESERVED_WORD, "into")) {
strcpy(parser->parserMessage, "invalid sql: should be into.");
return NULL;
}
TokenT *token = parseNextToken(parser);
if (token->type == TOKEN_WORD) {
tableName = new_id_name();
strcpy(tableName, token->text);
} else {
strcpy(parser->parserMessage, "invalid sql: missing table name.");
return NULL;
}
token = parseEatAndNextToken(parser);
if (!matchToken(parser, TOKEN_OPEN_PAREN, "(")) {
strcpy(parser->parserMessage, "invalid sql: missing (.");
return NULL;
}
token = parseNextToken(parser);
if (token->type == TOKEN_WORD) {
while (token->type == TOKEN_WORD) {
char *fieldName = new_id_name();
strcpy(fieldName, token->text);
arraylist_add(fields, fieldName);
token = parseEatAndNextToken(parser);
if (token->type==TOKEN_COMMA){
token = parseEatAndNextToken(parser);
}else{
break;
}
}
} else {
strcpy(parser->parserMessage, "invalid sql: missing field name.");
return NULL;
}
if (!matchToken(parser, TOKEN_CLOSE_PAREN, ")")) {
strcpy(parser->parserMessage, "invalid sql: missing ).");
return NULL;
}
if (!matchToken(parser, TOKEN_RESERVED_WORD, "values")) {
strcpy(parser->parserMessage, "invalid sql: missing values.");
return NULL;
}
if (!matchToken(parser, TOKEN_OPEN_PAREN, "(")) {
strcpy(parser->parserMessage, "invalid sql: missing (.");
return NULL;
}
token = parseNextToken(parser);
if (token->type == TOKEN_STRING || token->type == TOKEN_DECIMAL) {
while (token->type == TOKEN_STRING || token->type == TOKEN_DECIMAL) {
if (token->type == TOKEN_STRING){
/*去掉引号, 已经在tokenizer.c中完成*/
/* int len = strlen(token->text) - 1;
char *v = token->text + 1;
char *value = (char *) calloc(len, 1);
strcpy(value, v);
value[len-1] = '\0';*/
arraylist_add(values, token->text);
} else{
integer *i = (integer *)calloc(sizeof(integer *), 1);
i->val = atoi(token->text);
arraylist_add(values, i);
}
token = parseEatAndNextToken(parser);
if (token->type==TOKEN_COMMA){
token = parseEatAndNextToken(parser);
}else{
break;
}
}
} else {
strcpy(parser->parserMessage, "invalid sql: missing a value.");
return NULL;
}
if (!matchToken(parser, TOKEN_CLOSE_PAREN, ")")) {
strcpy(parser->parserMessage, "invalid sql: missing ).");
return NULL;
}
sql_stmt_insert *sqlStmtInsert = (sql_stmt_insert *)calloc(sizeof(sql_stmt_insert),1);
sqlStmtInsert->tableName = tableName;
sqlStmtInsert->fields = fields;
sqlStmtInsert->values = values;
return sqlStmtInsert;
};