-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeletetables.c
140 lines (115 loc) · 3.4 KB
/
deletetables.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
//Trashes all tables and functions for a model.
//gcc deletetables.c -o DELETETABLES -O3 -lpq
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <string.h>
int CheckSQLError(PGresult* res);
int main(int argc,char* argv[])
{
int i,j,numtables;
char query[512];
char M[32];
PGresult *res;
PGconn *conn;
if(argc < 2)
{
printf("Need model name.\n");
return 1;
}
//Connect to database
//conn = PQconnectdb("dbname=model_ifc host=s-iihr51.iihr.uiowa.edu port=5432 user=automated_solver password=C5.pfest0");
conn = PQconnectdb("dbname=model_test host=s-iihr51.iihr.uiowa.edu port=5432 user=scott password=C5.pfest0");
//Load up model names
sprintf(M,argv[1]);
numtables = 10;
printf("Deleting tables for model %s.\n\n",M);
//Delete tables
for(i=0;i<numtables;i++)
{
sprintf(query,"DROP TABLE IF EXISTS archive_hydroforecast_%s_%i;",M,i);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS archive_maps_%s_%i;",M,i);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS archive_peakflows_%s_%i;",M,i);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
}
sprintf(query,"DROP TABLE IF EXISTS master_archive_hydroforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS master_archive_maps_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS master_archive_peakflows_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS hydroforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS peakforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS ratioforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS stageforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP TABLE IF EXISTS warningforecast_%s;",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS function_on_insert_to_master_archive_hydroforecast_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS function_on_insert_to_master_archive_maps_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS function_on_insert_to_master_archive_peakflows_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS copy_to_archive_hydroforecast_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS get_stages_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
sprintf(query,"DROP FUNCTION IF EXISTS update_warnings_%s();",M);
res = PQexec(conn,query);
CheckSQLError(res);
PQclear(res);
//Clean up
printf("All done!\n");
PQfinish(conn);
return 0;
}
int CheckSQLError(PGresult* res)
{
short int status = PQresultStatus(res);
if( !(status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK) )
{
printf("Error making query. %i\n",PQresultStatus(res));
printf("%s\n",PQresultErrorMessage(res));
return 1;
}
else
return 0;
}