-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.c
245 lines (214 loc) · 5.35 KB
/
sqlite.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
/* Work with sqlite db */
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <errno.h>
#include "main.h"
#include "sqlite.h"
#include "utils.h"
#define PKG_TABLE "packages"
#define DB_PATH "./db.sqlite"
#define COLUMNS "name, version, descr, license, btime, itime" // Not used
/*
* Function to create a sql table
*/
int create_table(char *db_path) {
/* Checking argument*/
if(!db_path)
return 1;
sqlite3 *db;
char *error_msg = 0, *query;
int rc;
/* Create query */
query = "CREATE TABLE " PKG_TABLE " ("COLUMNS")";
/* Opening database */
rc = sqlite3_open(db_path, &db);
if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
/* Execution query */
rc = sqlite3_exec(db, query, NULL, NULL, &error_msg);
if(rc) {
fprintf(stderr, "SQL error: %s\n", error_msg);
sqlite3_free(error_msg);
sqlite3_close(db);
return 1;
}
return 0;
}
/*
* Function to insert an item in the table
*/
int add_pkg_to_db(char *db_path, pkg_t *pkg) {
/* Checking argument*/
if(!db_path)
return 1;
sqlite3 *db;
char *error_msg = 0;
//char *query = (char*)xcalloc(500, sizeof(char));
char query[500];
int rc;
/* Create query */
sprintf(query, "INSERT INTO " PKG_TABLE " ("COLUMNS") VALUES "
"('%s', '%s', '%s', '%s', '%s', '%s')", pkg->name, pkg->version,
pkg->descr, pkg->license, pkg->btime, pkg->itime);
/* Opening database */
rc = sqlite3_open(db_path, &db);
if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
/* Execution query */
rc = sqlite3_exec(db, query, NULL, NULL, &error_msg);
if(rc) {
fprintf(stderr, "SQL error: %s\n", error_msg);
sqlite3_free(error_msg);
sqlite3_close(db);
return 1;
}
return 0;
}
/*
* Function to delete the item from the table
*/
int del_pkg_from_db(char *db_path, pkg_t *pkg) {
/* Checking argument*/
if(!db_path)
return 1;
sqlite3 *db;
char *error_msg = 0;
char query[500];
int rc;
/* Opening database */
rc = sqlite3_open(db_path, &db);
if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
/* Create query */
strcpy(query, "DELETE FROM " PKG_TABLE " ");
strcat(query, create_query(pkg));
/* Execution query */
rc = sqlite3_exec(db, query, NULL, NULL, &error_msg);
if(rc) {
fprintf(stderr, "SQL error: %s\n", error_msg);
sqlite3_free(error_msg);
sqlite3_close(db);
return 1;
}
return 0;
}
/*
* Function to get an item from the table
*/
pkg_t* get_pkg_from_db(char *db_path, pkg_t *pkg) {
/* Checking argument*/
if(!db_path)
return NULL;
sqlite3 *db;
char *error_msg = 0;
char query[500];
int rc;
/* Opening database */
rc = sqlite3_open(db_path, &db);
if(rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return NULL;
}
/* Create query */
strcpy(query, "SELECT * FROM " PKG_TABLE " ");
strcat(query, create_query(pkg));
/* Getting data from the table
* and record them in pkgs[] */
char **s;
int nrow, ncolumn;
int i, t;
pkg_t* pkgs = NULL;
/* SQLite returns the data found on the criteria in s[] */
rc = sqlite3_get_table(db, query, &s, &nrow, &ncolumn, &error_msg);
if(rc) {
fprintf(stderr, "SQL error: %s\n", error_msg);
sqlite3_free(error_msg);
sqlite3_close(db);
return NULL;
}
/* Defining array */
pkgs = (pkg_t*)xcalloc(nrow + 1, sizeof(pkg_t));
for(t=0, i=ncolumn; t<nrow; t++) {
pkgs[t].name = s[i++];
pkgs[t].version = s[i++];
pkgs[t].descr = s[i++];
pkgs[t].license = s[i++];
pkgs[t].btime = s[i++];
pkgs[t].itime = s[i++];
}
/* Add NULL to the end of the array */
pkgs[t].name = NULL;
sqlite3_free_table(s);
return pkgs;
}
/*
* Function to create a parameter query
*/
char* create_query(pkg_t *pkg) {
char *query = (char*)xcalloc(500, sizeof(char)); // Тут утечка
int is_and = 0;
if(pkg->name || pkg->version || pkg->descr || pkg->license || pkg->btime || pkg->itime) {
strcat(query, "WHERE ");
if(pkg->name) {
strcat(query, "name='");
strcat(query, pkg->name);
strcat(query, "' ");
is_and = 1;
}
if(pkg->version) {
if(is_and)
strcat(query, "AND ");
strcat(query, "version='");
strcat(query, pkg->version);
strcat(query, "' ");
is_and = 1;
}
if(pkg->descr) {
if(is_and)
strcat(query, "AND ");
strcat(query, "descr='");
strcat(query, pkg->descr);
strcat(query, "' ");
is_and = 1;
}
if(pkg->license) {
if(is_and)
strcat(query, "AND ");
strcat(query, "license='");
strcat(query, pkg->license);
strcat(query, "' ");
is_and = 1;
}
if(pkg->btime) {
if(is_and)
strcat(query, "AND ");
strcat(query, "btime='");
strcat(query, pkg->btime);
strcat(query, "' ");
is_and = 1;
}
if(pkg->itime) {
if(is_and)
strcat(query, "AND ");
strcat(query, "itime='");
strcat(query, pkg->itime);
strcat(query, "' ");
is_and = 1;
}
}
//printf("query = %s\n", query); //debug
return query;
}