-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
202 lines (191 loc) · 6.96 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
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
/*Csv2sql by: Noel Cortes
*
*This program is a C-based cmd tool to construct basic DML SQL queries from .csv file data...
*WIP: .csv formatting rules to come...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "declarations.h"
int main()
{
// printf("inside main\n");
char dml[7];
char table[30];
char column[200];
char data[200];
char refColumn[200];
char refData[200];
char full[850];
int elaborate = 0;
int incorrectFormat = 0;
int lineNumber = 0;
puts("\n/******Enter in SQL data:******/\n");
while (fgets(full, sizeof(full), stdin) != NULL) {
// printf("inside while loop\n");
lineNumber++;
populate(full, dml, table, column, data, refColumn, refData, &elaborate, &incorrectFormat);
if (incorrectFormat != 0) {
fprintf(stderr, "Error: Data input is of an incorrect format : %d\n", lineNumber);
fprintf(stderr, "\n\nPROGRAM TERMINATED\n\n");
return 2;
}
// printf("%s %s %s %s %s %s\n", dml, table, column, data, refColumn, refData);
switch (dml[0]) {
case 'I':
// printf("case i\n");
ins(dml, table, column, data, &elaborate);
break;
case 'U':
// printf("case u\n");
upd(dml, table, column, data, refColumn, refData);
break;
case 'D':
// printf("case d\n");
del(dml, table, column, data);
break;
default:
printf("\n/******END OF DATA******/\n");
return 0;
}
}
printf("\n/******END OF DATA******/\n");
return 0;
}
void populate(char total[], char t1[], char t2[], char t3[], char t4[], char t5[], char t6[],
int *elaborate, int *incorrectFormat) {
char *token = strtok(total, ",");
int counter = 0, repeat = 0, dashBreaks = 0;
int columnCount = 0, valueCount = 0;
*elaborate = 0, *incorrectFormat = 0;
// printf("elaborate = %d\n", *elaborate);
while (token != NULL) {
if (strchr(token, '-')) {
repeat = 0;
counter++;
dashBreaks++;
if (dashBreaks > 1 ||
((*elaborate == 1 && counter == 4) && (columnCount != valueCount))) {
*incorrectFormat = 1;
return;
}
// printf("%i\n", counter);
token = strtok(NULL, ",");
continue;
} else {
dashBreaks = 0;
}
// printf("%s\n", token);
// printf("%i\n", counter);
switch (counter) {
case 0: /*DML Statement: INSERT, UPDATE, DELETE*/
if (strchr(token, 'I')) {
strcpy(t1, "INSERT");
} else if (strchr(token, 'U')) {
strcpy(t1, "UPDATE");
} else if (strchr(token, 'D')) {
strcpy(t1, "DELETE");
} else {
*incorrectFormat = 1;
return;
}
break;
case 1: /*Table Name*/
strcpy(t2, token);
break;
case 2: /*Column Name(s)*/
if (repeat == 0) {
strcpy(t3, token);
repeat++;
break;
} else {
if (t1[0] == 'U' || t1[0] == 'D') { /*Temp fix: If Update has repeats, flag incorrectFormat*/
*incorrectFormat = 1;
return;
} else {
strcat(t3, ",");
strcat(t3, token);
*elaborate = 1;
columnCount++;
// printf("Column count: %d\n", columnCount);
// printf("elaborate = %d\n", *elaborate);
break;
}
}
case 3: /*Value(s) for Column Name(s)*/
if (repeat == 0) {
strcpy(t4, token);
repeat++;
break;
} else {
if (t1[0] == 'U' || t1[0] == 'D') { /*Temp fix: If Update has repeats, flag incorrectFormat*/
*incorrectFormat = 1;
return;
} else {
strcat(t4, ",");
strcat(t4, token);
valueCount++;
// printf("Value count: %d\n", valueCount);
break;
}
}
case 4: /*Reference Column Name(s)*/
if (t1[0] == 'I' || t1[0] == 'D') {
*incorrectFormat = 1;
return;
} else if (repeat == 0) {
strcpy(t5, token);
repeat++;
break;
} else {
if (t1[0] == 'U') { /*Temp fix: If Update has repeats, flag incorrectFormat*/
*incorrectFormat = 1;
return;
} else {
strcat(t5, ",");
strcat(t5, token);
break;
}
}
case 5: /*Reference Value(s) for Reference Column Name(s)*/
if (t1[0] == 'I' || t1[0] == 'D') {
*incorrectFormat = 1;
return;
} else if (repeat == 0) {
strcpy(t6, token);
repeat++;
break;
} else {
if (t1[0] == 'U') { /*Temp fix: If Update has repeats, flag incorrectFormat*/
*incorrectFormat = 1;
return;
} else {
strcat(t6, ",");
strcat(t6, token);
break;
}
}
}
token = strtok(NULL, ",");
}
if (counter == 3 && t1[0] == 'I') {
strcpy(t4,t3);
strcpy(t3,"");
*elaborate = 0;
} else if (counter != 4 && counter != 6) {
*incorrectFormat = 1;
}
}
void ins(char dml[], char table[], char column[], char data[], int *elaborate) {
if (*elaborate == 1) {
printf("\n%s INTO %s(%s) VALUES=(%s);\n\n", dml, table, column, data);
} else {
printf("\n%s INTO %s VALUES=(%s);\n\n", dml, table, data);
}
}
void upd(char dml[], char table[], char column[], char data[], char refColumn[], char refData[]) {
printf("\n%s %s SET %s=%s WHERE %s=%s;\n\n", dml, table, column, data, refColumn, refData);
}
void del(char dml[], char table[], char column[], char data[]) {
printf("\n%s FROM %s WHERE %s=%s;\n\n", dml, table, column, data);
}