-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertQueryParser.c
More file actions
94 lines (82 loc) · 2.4 KB
/
insertQueryParser.c
File metadata and controls
94 lines (82 loc) · 2.4 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "consts.h"
#include "column_types.h"
#include "table.h"
#include "query_types.h"
#include "compiled_query.h"
#include "compiled_column.h"
#define DOUBLE_QUOTE 34
#define SINGLE_QUOTE 39
extern int readHeadTable(FILE * headerFile, Table * table);
extern FILE * getHeaderFile(char * tableName, char * mode);
extern void removeFirstLastChar(char * temp);
extern void log(char * msg);
void getRequestFromInsertQuery(char queryParts[10][50], Table * table, CompiledQuery * compiledQuery)
{
if (strcmp(queryParts[1], "into") != 0)
{
log("Target of query not found. Aborting... \n");
return;
}
FILE * headerFile = getHeaderFile(queryParts[2], "rb");
if (headerFile == NULL)
{
log("Unable to open header file.");
}
readHeadTable(headerFile, table);
compiledQuery->type = INSERT;
compiledQuery->target = queryParts[2];
char temp[50];
int i = 0;
char * token;
char * rest = queryParts[3];
int hasColumnsReferenced = 0;
if (strcmp(queryParts[3],"values") == 0)
{
for (i = 0; i < table->info.columnCount; i++)
{
compiledQuery->queryColumns[i]->name = table->columns[i]->name;
}
compiledQuery->columnCount = table->info.columnCount;
}
else if (strcmp(queryParts[4], "values") == 0)
{
hasColumnsReferenced = 1;
removeFirstLastChar(rest);
i = 0;
while((token = strtok_r(rest, ",", &rest)))
{
strcpy(temp, token);
if (temp[0] == SINGLE_QUOTE || temp[0] == DOUBLE_QUOTE)
{
removeFirstLastChar(temp);
}
strcpy(compiledQuery->queryColumns[i]->name,temp);
i += 1;
}
compiledQuery->columnCount = i;
}
else
{
log("VALUES keyword not found. Aborting...");
return;
}
int valuesIndex = 4 + hasColumnsReferenced;
// TODO: refactor query parts to better data structure
strcpy(temp, queryParts[valuesIndex]);
removeFirstLastChar(temp);
strcpy(rest,temp);
int j = 0;
while((token = strtok_r(rest, ",", &rest)))
{
strcpy(temp, token);
if (temp[0] == SINGLE_QUOTE || temp[0] == DOUBLE_QUOTE)
{
removeFirstLastChar(temp);
}
strcpy(compiledQuery->queryColumns[j]->value,temp);
j += 1;
}
}