-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexample_sql.c
248 lines (228 loc) · 8.71 KB
/
example_sql.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
This Example implements a simple SQL syntax parser.
INSERT INTO <table> SET column1=value1,column2=value2,...
SELECT <* | column1,columnm2,...> FROM <table> [WHERE] [ORDER BY] [LIMIT] [OFFSET]
UPDATE <table> SET column1=value1,column2=value2 [WHERE] [ORDER BY] [LIMIT] [OFFSET]
DELETE FROM <table> [WHERE] [ORDER BY] [LIMIT] [OFFSET]
CREATE [UNIQUE] INDEX <name> ON <table> (column1,column2,...)
DROP {TABLE | INDEX} <name>
SHOW {TABLES | DATABASES}
DESCRIBE <TABLE>
help {INSERT | SELECT | UPDATE | DELETE | CREATE | DROP | SHOW | DESCRIBE | help | exit | history}
Build
# Windows MSVC
cl -D_CRT_SECURE_NO_WARNINGS -W4 User32.Lib crossline.c example_sql.c /Feexample_sql.exe
# Windows Clang
clang -D_CRT_SECURE_NO_WARNINGS -Wall -lUser32 crossline.c example_sql.c -o example_sql.exe
# Linux Clang
clang -Wall crossline.c example_sql.c -o example_sql
# GCC(Linux, MinGW, Cygwin, MSYS2)
gcc -Wall crossline.c example_sql.c -o example_sql
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crossline.h"
#ifdef _WIN32
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
void sql_add_completion (crossline_completions_t *pCompletion, const char *prefix, const char **match, const char **help)
{
int i, len = (int)strlen(prefix);
crossline_color_e wcolor, hcolor;
for (i = 0; NULL != match[i]; ++i) {
if (0 == strncasecmp(prefix, match[i], len)) {
if (NULL != help) {
if (i < 8) {
wcolor = CROSSLINE_FGCOLOR_BRIGHT | CROSSLINE_FGCOLOR_YELLOW;
} else {
wcolor = CROSSLINE_FGCOLOR_BRIGHT | CROSSLINE_FGCOLOR_CYAN;
}
hcolor = i%2 ? CROSSLINE_FGCOLOR_WHITE : CROSSLINE_FGCOLOR_CYAN;
crossline_completion_add_color (pCompletion, match[i], wcolor, help[i], hcolor);
} else {
crossline_completion_add_color (pCompletion, match[i], CROSSLINE_FGCOLOR_BRIGHT | CROSSLINE_FGCOLOR_MAGENTA, NULL, 0);
}
}
}
}
int sql_find_key (const char **match, const char *prefix)
{
int i;
for (i = 0; NULL != match[i]; ++i) {
if (0 == strcasecmp(prefix, match[i])) {
return i;
}
}
return -1;
}
enum {
CMD_INSERT,
CMD_SELECT,
CMD_UPDATE,
CMD_DELETE,
CMD_CREATE,
CMD_DROP,
CMD_SHOW,
CMD_DESCRIBE,
CMD_HELP,
CMD_EXIT,
CMD_HISTORY,
};
void sql_completion_hook (char const *buf, crossline_completions_t *pCompletion)
{
int num, cmd, bUnique = 0;
char split[8][128], last_ch;
static const char* sql_cmd[] = {"INSERT", "SELECT", "UPDATE", "DELETE", "CREATE", "DROP", "SHOW", "DESCRIBE", "help", "exit", "history", NULL};
static const char* sql_cmd_help[] = {
"Insert a record to table",
"Select records from table",
"Update records in table",
"Delete records from table",
"Create index on table",
"Drop index or table",
"Show tables or databases",
"Show table schema",
"Show help for topic",
"Exit shell",
"Show history"};
static const char* sql_caluse[] = {"WHERE", "ORDER BY", "LIMIT", "OFFSET", NULL};
static const char* sql_index[] = {"UNIQUE", "INDEX", NULL};
static const char* sql_drop[] = {"TABLE", "INDEX", NULL};
static const char* sql_show[] = {"TABLES", "DATABASES", NULL};
crossline_color_e tbl_color = CROSSLINE_FGCOLOR_WHITE | CROSSLINE_BGCOLOR_GREEN;
crossline_color_e col_color = CROSSLINE_FGCOLOR_WHITE | CROSSLINE_BGCOLOR_CYAN;
crossline_color_e idx_color = CROSSLINE_FGCOLOR_WHITE | CROSSLINE_BGCOLOR_YELLOW;
memset (split, '\0', sizeof (split));
num = sscanf (buf, "%s %s %s %s %s %s %s %s", split[0], split[1], split[2], split[3], split[4], split[5], split[6], split[7]);
cmd = sql_find_key (sql_cmd, split[0]);
if ((cmd < 0) && (num <= 1)) {
sql_add_completion (pCompletion, split[0], sql_cmd, sql_cmd_help);
}
last_ch = buf[strlen(buf) - 1];
switch (cmd) {
case CMD_INSERT: // INSERT INTO <table> SET column1=value1,column2=value2,...
if ((1 == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "INTO", NULL);
} else if ((2 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if ((3 == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "SET", NULL);
} else if ((4 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "column1=value1,column2=value2,...", col_color);
}
break;
case CMD_SELECT: // SELECT < * | column1,columnm2,...> FROM <table> [WHERE] [ORDER BY] [LIMIT] [OFFSET]
if ((1 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "* | column1,columnm2,...", col_color);
} else if ((2 == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "FROM", NULL);
} else if ((3 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if ((4 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_caluse, NULL);
} else if ((num > 4) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[num-1], sql_caluse, NULL);
}
break;
case CMD_UPDATE: // UPDATE <table> SET column1=value1,column2=value2 [WHERE] [ORDER BY] [LIMIT] [OFFSET]
if ((1 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if ((2 == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "SET", NULL);
} else if ((3 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "column1=value1,column2=value2,...", col_color);
} else if ((4 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_caluse, NULL);
} else if ((num > 4) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[num-1], sql_caluse, NULL);
}
break;
case CMD_DELETE: // DELETE FROM <table> [WHERE] [ORDER BY] [LIMIT] [OFFSET]
if ((1 == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "FROM", NULL);
} else if ((2 == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if ((3 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_caluse, NULL);
} else if ((num > 3) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[num-1], sql_caluse, NULL);
}
break;
case CMD_CREATE: // CREATE [UNIQUE] INDEX <name> ON <table> (column1,column2,...)
if ((1 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_index, NULL);
} else if ((2 == num) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[1], sql_index, NULL);
} else {
if ((num >= 2) && !strcasecmp (split[1], "UNIQUE")) {
bUnique = 1;
}
if ((2 == num) && bUnique && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "INDEX", NULL);
}
else if ((2+bUnique == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "index name", idx_color);
} else if ((3+bUnique == num) && (' ' == last_ch)) {
crossline_completion_add (pCompletion, "ON", NULL);
} else if ((4+bUnique == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if ((5+bUnique == num) && (' ' == last_ch)) {
crossline_hints_set_color (pCompletion, "(column1,column2,...)", col_color);
}
}
break;
case CMD_DROP: // DROP TABLE <name>, DROP INDEX <name>
if ((1 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_drop, NULL);
} else if ((2 == num) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[1], sql_drop, NULL);
} else if ((num == 2) && (' ' == last_ch)) {
if (!strcasecmp (split[1], "TABLE")) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
} else if (!strcasecmp (split[1], "INDEX")) {
crossline_hints_set_color (pCompletion, "index name", idx_color);
}
}
break;
case CMD_SHOW: // SHOW TABLES, SHOW DATABASES
if ((1 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_show, NULL);
} else if ((2 == num) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[1], sql_show, NULL);
}
break;
case CMD_DESCRIBE: // describe <table>
if (' ' == last_ch) {
crossline_hints_set_color (pCompletion, "table name", tbl_color);
}
break;
case CMD_HELP:
if ((1 == num) && (' ' == last_ch)) {
sql_add_completion (pCompletion, "", sql_cmd, NULL);
} else if ((2 == num) && (' ' != last_ch)) {
sql_add_completion (pCompletion, split[1], sql_cmd, NULL);
}
break;
case CMD_EXIT:
break;
}
}
int main ()
{
char buf[256];
crossline_completion_register (sql_completion_hook);
crossline_history_load ("history.txt");
crossline_prompt_color_set (CROSSLINE_FGCOLOR_BRIGHT | CROSSLINE_FGCOLOR_GREEN);
while (NULL != crossline_readline ("SQL> ", buf, sizeof(buf))) {
printf ("Read line: \"%s\"\n", buf);
if (!strcmp (buf, "history")) {
crossline_history_show ();
} else if (!strcmp (buf, "exit")) {
break;
}
}
crossline_history_save ("history.txt");
return 0;
}