1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
2 | * Virginia Tech, 2009 |
---|
3 | * |
---|
4 | * LICENSE INFORMATION GOES HERE |
---|
5 | */ |
---|
6 | |
---|
7 | /* TODO description of this file |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include <stdio.h> |
---|
14 | |
---|
15 | #include "vtcross/cbr.h" |
---|
16 | #include "vtcross/common.h" |
---|
17 | |
---|
18 | |
---|
19 | /* TODO this appears to get used once in this entire file, so I'm making it |
---|
20 | * static and private to this file. Is that okay? */ |
---|
21 | static const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
---|
22 | |
---|
23 | // open a database or create a database if it does not exist |
---|
24 | int OpenDatabase(cbr _cbr){ |
---|
25 | int rc; |
---|
26 | //sqlite3 **db; |
---|
27 | |
---|
28 | //printf("database name: %s\n", _cbr->filename); |
---|
29 | //rc = sqlite3_open(_cbr->filename, db); |
---|
30 | rc = sqlite3_open(_cbr->filename, &(_cbr->db)); |
---|
31 | if (rc) { |
---|
32 | //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db)); |
---|
33 | fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db)); |
---|
34 | sqlite3_close(_cbr->db); |
---|
35 | exit(1); |
---|
36 | } else{ |
---|
37 | //printf("database opened.\n"); |
---|
38 | } |
---|
39 | //_cbr->db = *db; |
---|
40 | return rc; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | // simple callback function, display result |
---|
45 | int callback(void *notUsed, int argc, char **argv, char **azColName){ |
---|
46 | int i; |
---|
47 | for(i=0; i<argc; i++){ |
---|
48 | printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
---|
49 | } |
---|
50 | printf("\n"); |
---|
51 | return 0; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | // execute command |
---|
56 | int ExecuteCommand(cbr _cbr){ |
---|
57 | int rc; |
---|
58 | char *zErrMsg = 0; |
---|
59 | |
---|
60 | //printf("command: %s\n", _cbr->command); |
---|
61 | rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); |
---|
62 | if( rc!=SQLITE_OK){ |
---|
63 | fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command); |
---|
64 | sqlite3_free(zErrMsg); |
---|
65 | } else{ |
---|
66 | //printf("command executed.\n"); |
---|
67 | } |
---|
68 | return rc; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | // execute search command |
---|
73 | int ExecuteSearchCommand(cbr _cbr, float *_retvals){ |
---|
74 | int rc; |
---|
75 | unsigned int i; |
---|
76 | |
---|
77 | sqlite3_stmt * pStatement; |
---|
78 | rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL); |
---|
79 | if (rc == SQLITE_OK){ |
---|
80 | if (sqlite3_step(pStatement) == SQLITE_ROW){ |
---|
81 | for (i=0; i<_cbr->num_columns; ++i) |
---|
82 | _retvals[i] = sqlite3_column_double(pStatement, i); |
---|
83 | } else { |
---|
84 | printf("CBR:: No matched results returning default.\n"); |
---|
85 | rc=31337; |
---|
86 | } |
---|
87 | } else { |
---|
88 | printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command); |
---|
89 | } |
---|
90 | |
---|
91 | sqlite3_finalize(pStatement); |
---|
92 | |
---|
93 | return rc; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | // create database |
---|
98 | cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
---|
99 | { |
---|
100 | // cbr is a pointer to struct cbr_s |
---|
101 | cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); |
---|
102 | |
---|
103 | // create database |
---|
104 | |
---|
105 | // copy filename |
---|
106 | unsigned int i=0; |
---|
107 | strcpy(_cbr->filename, _filename); |
---|
108 | |
---|
109 | // execute create database command |
---|
110 | // database handle |
---|
111 | //_cbr->db = NULL; |
---|
112 | OpenDatabase(_cbr); |
---|
113 | |
---|
114 | // create table |
---|
115 | |
---|
116 | // copy tablename |
---|
117 | strcpy(_cbr->tablename, _tablename); |
---|
118 | |
---|
119 | // number of columns in the table |
---|
120 | _cbr->num_columns = _len; |
---|
121 | |
---|
122 | // generate command |
---|
123 | strcpy(_cbr->command, "CREATE TABLE "); |
---|
124 | strcat(_cbr->command, _cbr->tablename); |
---|
125 | strcat(_cbr->command, "("); |
---|
126 | for (i=0; i<_cbr->num_columns; i++) { |
---|
127 | strcat(_cbr->command, _cols[i]); |
---|
128 | strcat(_cbr->command, " FLOAT"); |
---|
129 | if (i != _cbr->num_columns-1) // not last entry |
---|
130 | strcat(_cbr->command, ", "); |
---|
131 | } |
---|
132 | strcat(_cbr->command, ");"); |
---|
133 | |
---|
134 | // execute create table command |
---|
135 | ExecuteCommand(_cbr); |
---|
136 | |
---|
137 | return _cbr; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | // free space |
---|
142 | void cbr_free(cbr _cbr) |
---|
143 | { |
---|
144 | // generate command, remove a table with its content |
---|
145 | strcpy(_cbr->command, "drop table "); |
---|
146 | strcat(_cbr->command, _cbr->tablename); |
---|
147 | |
---|
148 | // execute delete command |
---|
149 | ExecuteCommand(_cbr); |
---|
150 | |
---|
151 | // clean the database |
---|
152 | strcpy(_cbr->command, "vacuum"); |
---|
153 | ExecuteCommand(_cbr); |
---|
154 | |
---|
155 | free(_cbr); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | // print |
---|
160 | void cbr_print(cbr _cbr) |
---|
161 | { |
---|
162 | // generate commandi |
---|
163 | strcpy(_cbr->command, "select "); |
---|
164 | strcat(_cbr->command, _cbr->tablename); |
---|
165 | strcat(_cbr->command, ".* from "); |
---|
166 | strcat(_cbr->command, _cbr->tablename); |
---|
167 | strcat(_cbr->command, ";"); |
---|
168 | |
---|
169 | // execute print (select all) command |
---|
170 | ExecuteCommand(_cbr); |
---|
171 | printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | /*//static |
---|
176 | int cbr_callback(void *notUsed, int argc, char **argv, char **azColName) |
---|
177 | { |
---|
178 | return 0; |
---|
179 | }*/ |
---|
180 | |
---|
181 | |
---|
182 | // cbr search |
---|
183 | int cbr_search( |
---|
184 | cbr _cbr, |
---|
185 | char *_names[], |
---|
186 | int * _ops, |
---|
187 | float *_vals, |
---|
188 | unsigned int _n, |
---|
189 | float *_retvals) |
---|
190 | { |
---|
191 | int rc; |
---|
192 | // generate command |
---|
193 | strcpy(_cbr->command, "select "); |
---|
194 | strcat(_cbr->command, _cbr->tablename); |
---|
195 | strcat(_cbr->command, ".* from "); |
---|
196 | strcat(_cbr->command, _cbr->tablename); |
---|
197 | strcat(_cbr->command, " where "); |
---|
198 | |
---|
199 | unsigned int i; |
---|
200 | char str_buffer[64]; |
---|
201 | //printf("number of ops %d:\n", _n); |
---|
202 | for (i=0; i<_n; i++) { |
---|
203 | // ensure valid ops value |
---|
204 | if (_ops[i] < 0 || _ops[i] > 5) { |
---|
205 | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
206 | exit(1); |
---|
207 | } |
---|
208 | |
---|
209 | strcat(_cbr->command, _names[i]); |
---|
210 | strcat(_cbr->command, ops_str[_ops[i]]); |
---|
211 | sprintf(str_buffer, "%E", _vals[i]); |
---|
212 | strcat(_cbr->command, str_buffer); |
---|
213 | |
---|
214 | |
---|
215 | if (i<_n-1) |
---|
216 | strcat(_cbr->command, " AND "); |
---|
217 | else |
---|
218 | strcat(_cbr->command, " order by utility desc;"); |
---|
219 | } |
---|
220 | |
---|
221 | //printf("search command: %s\n", _cbr->command); |
---|
222 | |
---|
223 | //ExecuteCommand(_cbr); |
---|
224 | rc = ExecuteSearchCommand(_cbr, _retvals); |
---|
225 | |
---|
226 | /*printf("search result: "); |
---|
227 | for (i=0; i<_cbr->num_columns; i++) |
---|
228 | printf("%f, ",_retvals[i]); |
---|
229 | printf("\n");*/ |
---|
230 | |
---|
231 | return rc; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | // update a row |
---|
236 | int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals, |
---|
237 | unsigned int _wherelen, unsigned int _setlen) |
---|
238 | { |
---|
239 | unsigned int i; |
---|
240 | |
---|
241 | // generate command |
---|
242 | //printf("%s\n", _cbr->command); |
---|
243 | strcpy(_cbr->command, "UPDATE "); |
---|
244 | strcat(_cbr->command, _cbr->tablename); |
---|
245 | |
---|
246 | strcat(_cbr->command, " SET "); |
---|
247 | for (i=0; i<_setlen; i++) { |
---|
248 | strcat(_cbr->command, _set[i]); |
---|
249 | strcat(_cbr->command, " = "); |
---|
250 | sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]); |
---|
251 | strcat(_cbr->command, " "); |
---|
252 | if (i != _setlen-1) // not last entry |
---|
253 | strcat(_cbr->command, ", "); |
---|
254 | } |
---|
255 | strcat(_cbr->command, " WHERE "); |
---|
256 | |
---|
257 | for (i=0; i<_wherelen; i++) { |
---|
258 | strcat(_cbr->command, _where[i]); |
---|
259 | strcat(_cbr->command, " = "); |
---|
260 | sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]); |
---|
261 | strcat(_cbr->command, " "); |
---|
262 | if (i != _wherelen-1) // not last entry |
---|
263 | strcat(_cbr->command, "AND "); |
---|
264 | } |
---|
265 | strcat(_cbr->command, ";"); |
---|
266 | |
---|
267 | //printf("search command: %s\n", _cbr->command); |
---|
268 | // execute add command |
---|
269 | ExecuteCommand(_cbr); |
---|
270 | |
---|
271 | return 0; |
---|
272 | } |
---|
273 | |
---|
274 | // cbr add a row |
---|
275 | int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len) |
---|
276 | { |
---|
277 | unsigned int i; |
---|
278 | |
---|
279 | // generate command |
---|
280 | //printf("%s\n", _cbr->command); |
---|
281 | strcpy(_cbr->command, "insert into "); |
---|
282 | strcat(_cbr->command, _cbr->tablename); |
---|
283 | |
---|
284 | strcat(_cbr->command, " ("); |
---|
285 | for (i=0; i<_len; i++) { |
---|
286 | strcat(_cbr->command, _cols[i]); |
---|
287 | if (i != _cbr->num_columns-1) // not last entry |
---|
288 | strcat(_cbr->command, ", "); |
---|
289 | } |
---|
290 | strcat(_cbr->command, ") "); |
---|
291 | |
---|
292 | strcat(_cbr->command, " values("); |
---|
293 | for (i=0; i<_len; i++) { |
---|
294 | // ???? how to fill the values if _cbr->num_columns != _len |
---|
295 | // assume = in the following |
---|
296 | sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]); |
---|
297 | if (i != _cbr->num_columns-1) // not last entry |
---|
298 | strcat(_cbr->command, ", "); |
---|
299 | } |
---|
300 | strcat(_cbr->command, ");"); |
---|
301 | |
---|
302 | //printf("search command: %s\n", _cbr->command); |
---|
303 | // execute add command |
---|
304 | ExecuteCommand(_cbr); |
---|
305 | |
---|
306 | return 0; |
---|
307 | } |
---|
308 | |
---|