| 201 | |
| 202 | // create database |
| 203 | CBR::CBR(char * _filename, char * _tablename, |
| 204 | char * _cols[], char * _primcols[], unsigned int _len, |
| 205 | unsigned int _primlen) |
| 206 | { |
| 207 | // create database |
| 208 | |
| 209 | // copy filename |
| 210 | unsigned int i=0; |
| 211 | strcpy(filename, _filename); |
| 212 | |
| 213 | // execute create database command |
| 214 | // database handle |
| 215 | //db = NULL; |
| 216 | OpenDatabase(); |
| 217 | |
| 218 | // create table |
| 219 | |
| 220 | // copy tablename |
| 221 | strcpy(tablename, _tablename); |
| 222 | |
| 223 | // number of columns in the table |
| 224 | numColumns = _len; |
| 225 | |
| 226 | // generate command |
| 227 | strcpy(command, "CREATE TABLE "); |
| 228 | strcat(command, tablename); |
| 229 | strcat(command, "("); |
| 230 | for (i=0; i<numColumns; i++) { |
| 231 | strcat(command, _cols[i]); |
| 232 | strcat(command, " FLOAT"); |
| 233 | strcat(command, ", "); |
| 234 | } |
| 235 | strcat(command, "PRIMARY KEY ("); |
| 236 | for (i=0; i<_primlen; i++) { |
| 237 | strcat(command, _primcols[i]); |
| 238 | if (i != _primlen-1) // not last entry |
| 239 | strcat(command, ", "); |
| 240 | } |
| 241 | strcat(command, "));"); |
| 242 | |
| 243 | // execute create table command |
| 244 | ExecuteCommand(); |
| 245 | } |
| 246 | |
| 330 | // cbr search for a sum |
| 331 | int32_t |
| 332 | CBR::SearchSum( |
| 333 | char *_name, |
| 334 | float *_retvals) |
| 335 | { |
| 336 | int rc; |
| 337 | // generate command |
| 338 | strcpy(command, "select SUM( "); |
| 339 | strcat(command, tablename); |
| 340 | strcat(command, "."); |
| 341 | strcat(command, _name); |
| 342 | strcat(command, ") from "); |
| 343 | strcat(command, tablename); |
| 344 | strcat(command, ";"); |
| 345 | |
| 346 | //printf("search command: %s\n", command); |
| 347 | |
| 348 | rc = ExecuteSearchCommand(_retvals); |
| 349 | /*printf("search result: "); |
| 350 | for (int i=0; i<numColumns; i++) |
| 351 | printf("%f, ",_retvals[i]); |
| 352 | printf("\n"); |
| 353 | */ |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | // cbr search |
| 359 | int32_t |
| 360 | CBR::SearchRand( |
| 361 | char *_names[], |
| 362 | int * _ops, |
| 363 | float *_vals, |
| 364 | unsigned int _n, |
| 365 | float *_retvals) |
| 366 | { |
| 367 | const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="}; |
| 368 | int rc; |
| 369 | // generate command |
| 370 | strcpy(command, "select "); |
| 371 | strcat(command, tablename); |
| 372 | strcat(command, ".* from "); |
| 373 | strcat(command, tablename); |
| 374 | strcat(command, " where "); |
| 375 | |
| 376 | unsigned int i; |
| 377 | char str_buffer[64]; |
| 378 | for (i=0; i<_n; i++) { |
| 379 | // ensure valid ops value |
| 380 | if (_ops[i] < 0 || _ops[i] > 5) { |
| 381 | printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
| 382 | exit(1); |
| 383 | } |
| 384 | |
| 385 | strcat(command, _names[i]); |
| 386 | strcat(command, ops_str[_ops[i]]); |
| 387 | sprintf(str_buffer, "%E", _vals[i]); |
| 388 | strcat(command, str_buffer); |
| 389 | |
| 390 | if (i<_n-1) |
| 391 | strcat(command, " AND "); |
| 392 | else |
| 393 | strcat(command, " order by RAND();"); |
| 394 | } |
| 395 | |
| 396 | //printf("search command: %s\n", command); |
| 397 | |
| 398 | rc = ExecuteSearchCommand(_retvals); |
| 399 | |
| 400 | /*printf("search result: "); |
| 401 | for (i=0; i<numColumns; i++) |
| 402 | printf("%f, ",_retvals[i]); |
| 403 | printf("\n"); |
| 404 | */ |
| 405 | return rc; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | |