-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
70 lines (67 loc) · 2.14 KB
/
main.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
#include "common.h"
#include "sqlparser.h"
#include "transaction.h"
int main(int argc, char *argv[])
{
buffer_t *input_buffer = new_buffer_input();
if (argc < 2)
{
printf("Must supply a database filename.\n");
exit(EXIT_FAILURE);
}
char *filename = argv[1];
table_t *table = db_open(filename);
// print_help();
while (true)
{
print_prompt();
read_input(input_buffer);
if (input_buffer->buffer[0] == '.')
{
switch (do_meta_command(input_buffer, table))
{
case META_COMMAND_SUCCESS:
continue;
case META_COMMAND_UNRECOGNIZED_COMMAND:
printf("Unrecognized meta command '%s'\n", input_buffer->buffer);
continue;
}
}
statement_t statement;
switch (parse_statement(input_buffer, &statement))
{
case PARSE_SUCCESS:
break;
case PARSE_SYNTAX_ERROR:
printf("Syntax error. Could not parse '%s' sql statement.\n", input_buffer->buffer);
continue;
case PARSE_STRING_TOO_LONG:
printf("String is too long.\n");
continue;
case PARSE_UNIMPLEMENTED:
printf("SQL statement '%s' is not implemented.\n", strtok(input_buffer->buffer, " "));
continue;
case PARSE_UNRECOGNIZED_STATEMENT:
printf("Unrecognized keyword at start of '%s'.\n", input_buffer->buffer);
continue;
}
switch (execute_statement(&statement, table))
{
case EXECUTE_SUCCESS:
printf("Executed.\n");
break;
case EXECUTE_DUPLICATE_KEY:
printf("Execute Error: Duplicate key.\n");
break;
case EXECUTE_NO_SUCH_KEY:
printf("Execute Error: No such key %u\n", statement.row_value.id);
break;
case EXECUTE_TABLE_FULL:
printf("Execute Error: Table full.\n");
break;
case EXECUTE_UNKNOWN_STATEMENT:
printf("Execute Error: Unknown sql statement '%s' \n", input_buffer->buffer);
break;
}
}
}