-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphesql.c
More file actions
244 lines (221 loc) · 6.74 KB
/
phesql.c
File metadata and controls
244 lines (221 loc) · 6.74 KB
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
#include <stdio.h>
#include <string.h>
#include "phext.h"
phext_coordinate phext_sql_tablespec_coordinate()
{
phext_coordinate result;
result.Z.library = 1;
result.Z.shelf = 1;
result.Z.series = 1;
result.Y.collection = 1;
result.Y.volume = 1;
result.Y.book = 1;
result.X.chapter = 1;
result.X.section = 1;
result.X.scroll = 1;
return result;
}
// -----------------------------------------------------------------------------------------------------------
// @fn phext_sql_select
// -----------------------------------------------------------------------------------------------------------
void phext_sql_select(const char* filename, const char* table, const char* fields, const char* limit)
{
FILE* fp = fopen(filename, "r");
if (fp)
{
fseek(fp, 0, SEEK_END);
int bytes = ftell(fp);
rewind(fp);
char* phext = malloc(bytes);
fread(phext, 1, bytes, fp);
fclose(fp);
phext_coordinate tablespec = phext_sql_tablespec_coordinate();
char* phext_address = phext_get_address(tablespec);
char* spec = phext_fetch_text(phext, tablespec);
char* token = spec;
int table_index = 1;
int scrolls = 0;
token = strtok(spec, "\n");
if (strcmp("tables", token) != 0)
{
fprintf(stderr, "Unable to locate phext sql tables at %s (expected 'tables', but found '%s')\n", phext_address, token);
return;
}
int found = 0;
while (token != 0)
{
++table_index;
token = strtok(0, "\n");
if (token != 0 && strlen(token) > 0)
{
if (strncmp(table, token, strlen(table)) == 0)
{
found = 1;
token = strtok(token, ",");
token = strtok(0, "\n");
scrolls = atoi(token);
// fprintf(stderr, "Found table = '%s', checked scroll limit: %d\n", table, scrolls);
break;
}
}
}
if (found == 1)
{
phext_coordinate data_address = tablespec;
data_address.X.section = table_index;
char* temp = malloc(strlen(fields) + 1);
strcpy(temp, fields);
token = strtok(temp, ",");
char* field1 = 0;
char* field2 = 0;
char* field3 = 0;
int field1length = 0;
int field2length = 0;
int field3length = 0;
int row = 0;
int testlength;
if (token != 0)
{
field1length = strlen(token);
field1 = malloc(field1length + 1);
strcpy(field1, token);
}
token = strtok(0, ",");
if (token != 0)
{
field2length = strlen(token);
field2 = malloc(field2length + 1);
strcpy(field2, token);
token = strtok(0, ",");
if (token != 0)
{
field3length = strlen(token);
field3 = malloc(field3length + 1);
strcpy(field3, token);
}
}
free(temp);
for (int i = 1; i <= scrolls; ++i)
{
data_address.X.scroll = i;
char* data = phext_fetch_text(phext, data_address);
token = strtok(data, "\n");
if (token != 0)
{
testlength = strlen(token);
if (field1length == testlength && strncmp(field1, token, testlength) == 0)
{
while (token != 0)
{
token = strtok(0, "\n");
if (token != 0)
{
if (limit == 0 || strcmp(limit, token) == 0)
{
fprintf(stderr, "Row %d: %s\n", row, token);
++row;
}
}
}
}
if (field2length == testlength && strncmp(field2, token, testlength) == 0)
{
while (token != 0)
{
token = strtok(0, "\n");
if (token != 0)
{
if (limit == 0 || strcmp(limit, token) == 0)
{
fprintf(stderr, "Row %d: %s\n", row, token);
++row;
}
}
}
}
if (field3length == testlength && strncmp(field3, token, testlength) == 0)
{
while (token != 0)
{
token = strtok(0, "\n");
if (token != 0)
{
if (limit == 0 || strcmp(limit, token) == 0)
{
fprintf(stderr, "Row %d: %s\n", row, token);
++row;
}
}
}
}
}
free(data);
}
}
else
{
fprintf(stderr, "unable to locate table '%s'\n", table);
}
free(spec);
free(phext_address);
}
}
// -----------------------------------------------------------------------------------------------------------
// @fn phext_sql_insert
// -----------------------------------------------------------------------------------------------------------
void phext_sql_insert(const char* filename, int argc, char** argv)
{
}
// -----------------------------------------------------------------------------------------------------------
// @fn phext_sql_update
// -----------------------------------------------------------------------------------------------------------
void phext_sql_update(const char* filename, int argc, char** argv)
{
}
// -----------------------------------------------------------------------------------------------------------
// @fn phext_sql_delete
// -----------------------------------------------------------------------------------------------------------
void phext_sql_delete(const char* filename, int argc, char** argv)
{
}
int main(int argc, char** argv)
{
// printf("phesql v1.0\n");
if (argc < 3)
{
fprintf(stderr, "Usage: phesql <file> <command> <options>\n");
return 1;
}
const char* file = argv[1];
const char* command = argv[2];
if (strcmp("select", command) == 0)
{
if (argc < 5)
{
fprintf(stderr, "Usage: phesql select <file> <table> <field_list>\n");
return 1;
}
const char* table = argv[3];
const char* fields = argv[4];
const char* limit = argc >= 6 ? argv[5] : 0;
phext_sql_select(file, table, fields, limit);
return 0;
}
if (strcmp("insert", command) == 0)
{
phext_sql_insert(file, argc, argv);
return 0;
}
if (strcmp("update", command) == 0)
{
phext_sql_update(file, argc, argv);
return 0;
}
if (strcmp("delete", command) == 0)
{
phext_sql_delete(file, argc, argv);
return 0;
}
fprintf(stderr, "Unknown command: %s\nNote: phesql is case-sensitive.\n", command);
return 1;
}