72 | | CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len); |
73 | | CBR(char *_filename, char *_tablename, char *_cols[], \ |
74 | | char *_primcols[], uint32_t _len, uint32_t _primlen); |
| 72 | CBR(string _filename, string _tablename, string _cols[], uint32_t _len); |
| 73 | CBR(string _filename, string _tablename, string _cols[], \ |
| 74 | string _primcols[], uint32_t _len, uint32_t _primlen); |
130 | | // create table |
131 | | |
132 | | // copy tablename |
133 | | strcpy(tablename, _tablename); |
134 | | |
135 | | // number of columns in the table |
| 130 | /* Generate the command that will create the initial table within the |
| 131 | * VTCROSS database. */ |
| 132 | command = "CREATE TABLE " + tablename + "("; |
| 133 | for(size_t i = 0; i < numColumns; i++) { |
| 134 | command += _cols[i]; |
| 135 | command += " FLOAT"; |
| 136 | |
| 137 | /* If this column is not the last entry, add a comma to the command in |
| 138 | * preperation for the next entry. */ |
| 139 | if(i != numColumns - 1) |
| 140 | command += ", "; |
| 141 | } |
| 142 | command += ");"; |
| 143 | |
| 144 | /* Execute the generated command. At this point, the database is ready for |
| 145 | * use. */ |
| 146 | ExecuteCommand(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | CBR::CBR(string _filename, string _tablename, string _cols[], \ |
| 151 | string _primcols[], uint32_t _len, uint32_t _primlen) |
| 152 | { |
| 153 | /* Store database properties. */ |
| 154 | filename = _filename; |
| 155 | tablename = _tablename; |
138 | | // generate command |
139 | | strcpy(command, "CREATE TABLE "); |
140 | | strcat(command, tablename); |
141 | | strcat(command, "("); |
142 | | for (i=0; i<numColumns; i++) { |
143 | | strcat(command, _cols[i]); |
144 | | strcat(command, " FLOAT"); |
145 | | if (i != numColumns-1) // not last entry |
146 | | strcat(command, ", "); |
147 | | } |
148 | | strcat(command, ");"); |
149 | | |
150 | | // execute create table command |
| 158 | /* Create the database (or open it if it already exists). */ |
| 159 | OpenDatabase(); |
| 160 | |
| 161 | /* Generate the command that will create the initial table within the |
| 162 | * VTCROSS database with primary keys. */ |
| 163 | command = "CREATE TABLE " + tablename + "("; |
| 164 | for(size_t i = 0; i < numColumns; i++) { |
| 165 | command += _cols[i]; |
| 166 | command += " FLOAT, "; |
| 167 | } |
| 168 | |
| 169 | command += "PRIMARY KEY ("; |
| 170 | for(size_t j = 0; j < _primlen; j++) { |
| 171 | command += _primcols[j]; |
| 172 | |
| 173 | /* If this column is not the last entry, add a comma to the command in |
| 174 | * preperation for the next entry. */ |
| 175 | if(j != _primlen - 1) |
| 176 | command += ", "; |
| 177 | } |
| 178 | command += "));"; |
| 179 | |
| 180 | /* Execute the generated command. At this point, the database is ready for |
| 181 | * use. */ |
153 | | |
154 | | CBR::CBR(char * _filename, char * _tablename, |
155 | | char * _cols[], char * _primcols[], unsigned int _len, |
156 | | unsigned int _primlen) |
157 | | { |
158 | | // create database |
159 | | |
160 | | // copy filename |
161 | | unsigned int i=0; |
162 | | strcpy(filename, _filename); |
163 | | |
164 | | // execute create database command |
165 | | // database handle |
166 | | //db = NULL; |
167 | | OpenDatabase(); |
168 | | |
169 | | // create table |
170 | | |
171 | | // copy tablename |
172 | | strcpy(tablename, _tablename); |
173 | | |
174 | | // number of columns in the table |
175 | | numColumns = _len; |
176 | | |
177 | | // generate command |
178 | | strcpy(command, "CREATE TABLE "); |
179 | | strcat(command, tablename); |
180 | | strcat(command, "("); |
181 | | for (i=0; i<numColumns; i++) { |
182 | | strcat(command, _cols[i]); |
183 | | strcat(command, " FLOAT"); |
184 | | strcat(command, ", "); |
185 | | } |
186 | | strcat(command, "PRIMARY KEY ("); |
187 | | for (i=0; i<_primlen; i++) { |
188 | | strcat(command, _primcols[i]); |
189 | | if (i != _primlen-1) // not last entry |
190 | | strcat(command, ", "); |
191 | | } |
192 | | strcat(command, "));"); |
193 | | |
194 | | // execute create table command |
195 | | ExecuteCommand(); |
196 | | } |
197 | | |
252 | | int rc; |
253 | | unsigned int i; |
254 | | |
255 | | sqlite3_stmt * pStatement; |
256 | | rc = sqlite3_prepare_v2(db, command, -1, &pStatement, NULL); |
257 | | if (rc == SQLITE_OK){ |
258 | | if (sqlite3_step(pStatement) == SQLITE_ROW){ |
259 | | for (i=0; i<numColumns; ++i) |
| 231 | sqlite3_stmt *pStatement; |
| 232 | |
| 233 | int32_t rc = sqlite3_prepare_v2(db, command.c_str(), -1, &pStatement, NULL); |
| 234 | if(rc == SQLITE_OK) { |
| 235 | if(sqlite3_step(pStatement) == SQLITE_ROW) { |
| 236 | for(size_t i = 0; i < numColumns; ++i) { |
276 | | // generate commandi |
277 | | strcpy(command, "select "); |
278 | | strcat(command, tablename); |
279 | | strcat(command, ".* from "); |
280 | | strcat(command, tablename); |
281 | | strcat(command, ";"); |
282 | | |
283 | | // execute print (select all) command |
| 256 | /* Generate the sqlite command to print the database, which is effectively a |
| 257 | * 'select all elements' command, and then execute it. */ |
| 258 | command = "select " + tablename + ".* from " + tablename + ";"; |
| 259 | |
296 | | int rc; |
297 | | const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
298 | | // generate command |
299 | | strcpy(command, "select "); |
300 | | strcat(command, tablename); |
301 | | strcat(command, ".* from "); |
302 | | strcat(command, tablename); |
303 | | strcat(command, " where "); |
304 | | |
305 | | unsigned int i; |
| 269 | LOG("CBR::Search - number of ops %d:\n", _n); |
| 270 | |
307 | | printf("number of ops %d:\n", _n); |
308 | | for (i=0; i<_n; i++) { |
309 | | // ensure valid ops value |
310 | | if (_ops[i] < 0 || _ops[i] > 5) { |
311 | | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
312 | | exit(1); |
| 272 | const string ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
| 273 | |
| 274 | command = "select " + tablename + ".* from " + tablename + " where "; |
| 275 | |
| 276 | for(size_t i = 0; i < _n; i++) { |
| 277 | /* Make sure that the passed ops value is valid. */ |
| 278 | if((_ops[i] < 0) || (_ops[i] > 5)) { |
| 279 | ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
325 | | strcat(command, " order by utility desc;"); |
326 | | } |
327 | | |
328 | | |
329 | | rc = ExecuteSearchCommand(_retvals); |
| 292 | command += " order by utility desc;"; |
| 293 | } |
| 294 | |
| 295 | return ExecuteSearchCommand(_retvals); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | int32_t |
| 300 | CBR::SearchSum(string _name, float *_retvals) |
| 301 | { |
| 302 | command = "select SUM(" + tablename + "." + _name + ") from " + tablename + ";"; |
| 303 | |
| 304 | return ExecuteSearchCommand(_retvals); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | int32_t |
| 309 | CBR::SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
| 310 | float *_retvals) |
| 311 | { |
| 312 | char str_buffer[64]; |
| 313 | const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
| 314 | |
| 315 | command = "select " + tablename + ".* from " + tablename + " where "; |
| 316 | |
| 317 | for(size_t i = 0; i < _n; i++) { |
| 318 | /* Make sure that the passed ops value is valid. */ |
| 319 | if((_ops[i] < 0) || (_ops[i] > 5)) { |
| 320 | ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
| 321 | } |
| 322 | |
| 323 | command += _names[i] + ops_str[_ops[i]];; |
| 324 | |
| 325 | sprintf(str_buffer, "%E", _vals[i]); |
| 326 | command += str_buffer; |
| 327 | |
| 328 | if(i < _n - 1) |
| 329 | command += " AND "; |
| 330 | else |
| 331 | command += " order by RAND();"; |
| 332 | } |
| 333 | |
| 334 | return ExecuteSearchCommand(_retvals); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | int32_t |
| 339 | CBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, |
| 340 | uint32_t _wherelen, uint32_t _setlen) |
| 341 | { |
| 342 | char str_buffer[64]; |
| 343 | |
| 344 | /* Generate the command to update the table. */ |
| 345 | command = "UPDATE " + tablename + " SET "; |
| 346 | |
| 347 | for(size_t i = 0; i < _setlen; i++) { |
| 348 | command += _set[i] + " = "; |
| 349 | sprintf(str_buffer, "%f", _setvals[i]); |
| 350 | command += string(str_buffer) + " "; |
| 351 | |
| 352 | if(i != _setlen - 1) |
| 353 | command += ", "; |
| 354 | } |
| 355 | command += " WHERE "; |
| 356 | |
| 357 | for(size_t j = 0; j < _wherelen; j++) { |
| 358 | command += _where[j] + " = "; |
| 359 | sprintf(str_buffer, "%f", _wherevals[j]); |
| 360 | command += string(str_buffer) + " "; |
| 361 | |
| 362 | if(j != _wherelen - 1) |
| 363 | command += "AND "; |
| 364 | } |
| 365 | command += ";"; |
331 | | return rc; |
332 | | } |
333 | | |
334 | | int32_t |
335 | | CBR::SearchSum( |
336 | | char *_name, |
337 | | float *_retvals) |
338 | | { |
339 | | int rc; |
340 | | // generate command |
341 | | strcpy(command, "select SUM( "); |
342 | | strcat(command, tablename); |
343 | | strcat(command, "."); |
344 | | strcat(command, _name); |
345 | | strcat(command, ") from "); |
346 | | strcat(command, tablename); |
347 | | strcat(command, ";"); |
348 | | |
349 | | //printf("search command: %s\n", command); |
350 | | |
351 | | rc = ExecuteSearchCommand(_retvals); |
352 | | /*printf("search result: "); |
353 | | for (int i=0; i<numColumns; i++) |
354 | | printf("%f, ",_retvals[i]); |
355 | | printf("\n"); |
356 | | */ |
357 | | return rc; |
358 | | } |
359 | | |
360 | | |
361 | | int32_t |
362 | | CBR::SearchRand( |
363 | | char *_names[], |
364 | | int * _ops, |
365 | | float *_vals, |
366 | | unsigned int _n, |
367 | | float *_retvals) |
368 | | { |
369 | | const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
370 | | int rc; |
371 | | // generate command |
372 | | strcpy(command, "select "); |
373 | | strcat(command, tablename); |
374 | | strcat(command, ".* from "); |
375 | | strcat(command, tablename); |
376 | | strcat(command, " where "); |
377 | | |
378 | | unsigned int i; |
| 367 | return ExecuteCommand(); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | int32_t |
| 372 | CBR::AddRow(string _cols[], float *_vals, uint32_t _len) |
| 373 | { |
380 | | for (i=0; i<_n; i++) { |
381 | | // ensure valid ops value |
382 | | if (_ops[i] < 0 || _ops[i] > 5) { |
383 | | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
384 | | exit(1); |
385 | | } |
386 | | |
387 | | strcat(command, _names[i]); |
388 | | strcat(command, ops_str[_ops[i]]); |
389 | | sprintf(str_buffer, "%E", _vals[i]); |
390 | | strcat(command, str_buffer); |
391 | | |
392 | | if (i<_n-1) |
393 | | strcat(command, " AND "); |
394 | | else |
395 | | strcat(command, " order by RAND();"); |
396 | | } |
397 | | |
398 | | //printf("search command: %s\n", command); |
399 | | |
400 | | rc = ExecuteSearchCommand(_retvals); |
401 | | |
402 | | /*printf("search result: "); |
403 | | for (i=0; i<numColumns; i++) |
404 | | printf("%f, ",_retvals[i]); |
405 | | printf("\n"); |
406 | | */ |
407 | | return rc; |
408 | | } |
409 | | |
410 | | |
411 | | |
412 | | |
413 | | int32_t |
414 | | CBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, |
415 | | unsigned int _wherelen, unsigned int _setlen) |
416 | | { |
417 | | unsigned int i; |
418 | | |
419 | | // generate command |
420 | | strcpy(command, "UPDATE "); |
421 | | strcat(command, tablename); |
422 | | |
423 | | strcat(command, " SET "); |
424 | | for (i=0; i<_setlen; i++) { |
425 | | strcat(command, _set[i]); |
426 | | strcat(command, " = "); |
427 | | sprintf(command, "%s%f", command, _setvals[i]); |
428 | | strcat(command, " "); |
429 | | if (i != _setlen-1) // not last entry |
430 | | strcat(command, ", "); |
431 | | } |
432 | | strcat(command, " WHERE "); |
433 | | |
434 | | for (i=0; i<_wherelen; i++) { |
435 | | strcat(command, _where[i]); |
436 | | strcat(command, " = "); |
437 | | sprintf(command, "%s%f", command, _wherevals[i]); |
438 | | strcat(command, " "); |
439 | | if (i != _wherelen-1) // not last entry |
440 | | strcat(command, "AND "); |
441 | | } |
442 | | strcat(command, ";"); |
443 | | |
444 | | // execute add command |
445 | | ExecuteCommand(); |
446 | | |
447 | | return 0; |
448 | | } |
449 | | |
450 | | int32_t |
451 | | CBR::AddRow(char *_cols[], float *_vals, unsigned int _len) |
452 | | { |
453 | | unsigned int i; |
454 | | |
455 | | // generate command |
456 | | strcpy(command, "insert into "); |
457 | | strcat(command, tablename); |
458 | | |
459 | | strcat(command, " ("); |
460 | | for (i=0; i<_len; i++) { |
461 | | strcat(command, _cols[i]); |
462 | | if (i != numColumns-1) // not last entry |
463 | | strcat(command, ", "); |
464 | | } |
465 | | strcat(command, ") "); |
466 | | |
467 | | strcat(command, " values("); |
468 | | for (i=0; i<_len; i++) { |
| 375 | |
| 376 | command = "insert into " + tablename + " ("; |
| 377 | |
| 378 | for(size_t i = 0; i < _len; i++) { |
| 379 | command += _cols[i]; |
| 380 | |
| 381 | if(i != numColumns - 1) |
| 382 | command += ", "; |
| 383 | } |
| 384 | command += ") values("; |
| 385 | |
| 386 | for(size_t j = 0; j < _len; j++) { |
| 387 | // TODO I have no idea what the below question is about. |