-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.c
290 lines (277 loc) · 12 KB
/
database.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Implements operation related to sqlite3 db*/
/* Copyright (C) 2009 Arun S.A.G.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "database.h"
int sqlite_get_handle()
{
int retval;
gchar *path;
gint size=0;
char query_baptism[] = "create table if not exists baptism_cert(uid integer primary key autoincrement,kept_at text,place text,date_baptism date,name text,date_birth date,sex text,mom text,dad text,resi text,caste text,sponsor1 text,sponsor2 text,priest text,remarks text)";
char query_marriage[] = "create table if not exists marriage_cert(uid integer primary key autoincrement,date_marriage date,bride_name text,groom_name text,bride_dob date,groom_dob date,bride_cond text,groom_cond text,bride_rank text,groom_rank text,bride_fname text,groom_fname text,bride_addr text,groom_addr text,bride_villi text,groom_villi text,bride_dist text,groom_dist text,bride_pin text,groom_pin text,bride_wit_name text,groom_wit_name text,bride_wit_addr text,groom_wit_addr text,bride_wit_villi text,groom_wit_villi text,bride_wit_dist text,groom_wit_dist text,bride_wit_pin text,groom_wit_pin text,min_name text)";
size=strlen(g_getenv("HOME"));
size=size+24;
path=g_malloc(size);
g_strlcpy(path,g_getenv("HOME"),size);
g_strlcat(path,"/.parish/parish.sqlite3",size);
retval = sqlite3_open(path,&handle);
g_free(path);
if(retval)
{
g_print("something went wrong with the database,return value:%d", retval);
return -1;
}
else
{
retval = sqlite3_exec(handle,query_baptism,0,0,0);
retval = sqlite3_exec(handle,query_marriage,0,0,0);
//g_print("Return value after the query: %d",retval);
return retval;
}
}
void prepare_baptism_cert_query(GString *query,struct baptism_cert cert)
{
gchar date_dob[10];
gchar date_dobap[10];
prepare_date(cert.dob_date,cert.dob_month,cert.dob_year,date_dob);
prepare_date(cert.dobap_date,cert.dobap_month,cert.dobap_year,date_dobap);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.kept_at);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.place);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",date_dobap);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.name);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",date_dob);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.sex);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.mom);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.dad);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.resi);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.caste);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.spon1);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.spon2);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.priest);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.remarks);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",")");
}
int sqlite_store_baptism_cert(struct baptism_cert cert)
{
int retval;
GString *query;
query = g_string_new("insert into baptism_cert values(NULL,");
prepare_baptism_cert_query(query,cert);
retval = sqlite3_exec(handle,query->str,0,0,0);
g_string_free(query,TRUE);
return retval;
}
void do_search(gchar *querystring,gchar *searchby,gchar *table)
{
GString *query;
int retval =0;
query = g_string_new("select * from");
g_string_append_printf(query," %s","");
g_string_append_printf(query,"%s",table);
g_string_append_printf(query," %s","");
g_string_append_printf(query,"%s","where");
g_string_append_printf(query," %s","");
g_string_append_printf(query,"%s",searchby);
g_string_append_printf(query," %s","");
g_string_append_printf(query,"%s","like");
g_string_append_printf(query," %s","");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",querystring);
g_string_append_printf(query,"%s","'");
g_print(query->str);
retval = sqlite3_prepare_v2(handle,query->str,-1,&stmt,0);
if(retval)
{
g_print("something went wrong");
}
g_string_free(query,TRUE);
}
int sqlite_store_marriage_cert(marriage_cert cert)
{
int retval;
GString *query;
query = g_string_new("insert into marriage_cert values(NULL,");
prepare_marriage_cert_query(query,cert);
retval = sqlite3_exec(handle,query->str,0,0,0);
g_string_free(query,TRUE);
return retval;
}
void prepare_marriage_cert_query(GString *query,marriage_cert cert)
{
gchar date_marriage[10];
gchar bride_dob[10];
gchar groom_dob[10];
prepare_date(cert.marriage_date,cert.marriage_month,cert.marriage_year,date_marriage);
prepare_date(cert.bride_dob_date,cert.bride_dob_month,cert.bride_dob_year,bride_dob);
prepare_date(cert.groom_dob_date,cert.groom_dob_month,cert.groom_dob_year,groom_dob);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",date_marriage);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_name);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_name);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",bride_dob);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",groom_dob);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_cond);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_cond);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_rank);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_rank);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_father);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_father);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_addr);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_addr);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_villi);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_villi);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_dist);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_dist);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_pin);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_pin);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_wit_name);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_wit_name);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_wit_addr);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_wit_addr);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_wit_villi);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_wit_villi);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_wit_dist);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_wit_dist);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.bride_wit_pin);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.groom_wit_pin);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",",");
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",cert.minister);
g_string_append_printf(query,"%s","'");
g_string_append_printf(query,"%s",")");
}