-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop_table.c
executable file
·60 lines (53 loc) · 1.78 KB
/
drop_table.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "jabberwocky_io.h"
#include "parsing_tools.h"
#include "drop_table.h"
int drop_table(int fd, char *dbPath, char *query, struct table **tableList, int size){
char **ptr = 0;
int q, i;
if ((q = split(query, " ", &ptr)) > 1){
for (i = 0; i < q; i++)
free(ptr[i]);
printf("Error: wrong query");
return -1;
}
if (';' == ptr[0][strlen(ptr[0])-1])
ptr[0][strlen(ptr[0])-1] = '\0';
if (!isForeignKey(ptr[0], *tableList, size)){
printf("Error: another table has link to this table");
return -1;
}
char *path = (char *)calloc(strlen(dbPath) + 1, sizeof(char));
strcpy(path, dbPath);
strcat(path, ptr[0]);
remove(path);
removeTable(ptr[0], tableList, size);
free(path);
printf("DEBUG: table '%s' was successfully removed\n", ptr[0]);
free(ptr[0]);
return 0;
}
int removeTable(char *tableName, struct table **tableList, int size){
int i, k;
for (i = 0; i < size - 1; i++){
if (!strcmp((*tableList)[i].table_name, tableName))
k = i;
if (i >= k)
(*tableList)[i] = (*tableList)[i+1];
}
*tableList = (struct table *)realloc(*tableList, (size - 1) * sizeof(struct table));
return 0;
}
int isForeignKey(char *tableName, struct table *tableList, int size){
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < tableList[i].column_count; j++)
if (tableList[i].columns[j].constraints >= 8 && !strcmp(tableList[i].columns[j].foreign_table->table_name, tableName))
return 0;
return 1;
}