Changeset 472

Show
Ignore:
Timestamp:
09/09/09 14:01:12 (15 years ago)
Author:
bhilburn
Message:

Cleaned up the CBR code and converted it from c-strings to std::string.
Need to propogate this change through the various CE codes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/include/vtcross/cbr.h

    r471 r472  
    7070         * don't fail (i.e. we cannot rely on the compiler-provided constructor. */ 
    7171        CBR(){}; 
    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); 
    7575 
    7676        /* Destructor for the CBR class. Note that this destructor will be 
     
    9595        /* Search the VTCROSS database for specific fields and store the results 
    9696         * in the passed retvals argument. */ 
    97         virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \ 
     97        virtual int32_t Search(string _names[], int32_t *_ops, float *_vals, \ 
    9898                uint32_t _n, float *_retvals); 
    99         virtual int32_t SearchSum(char *_name, float *_retvals); 
    100         virtual int32_t SearchRand(char *_names[], int *_ops, float *_vals, uint32_t _n, \ 
     99        virtual int32_t SearchSum(string _name, float *_retvals); 
     100        virtual int32_t SearchRand(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ 
    101101            float *_retvals); 
    102102 
    103103        /* Update a row in the VTCROSS sqlite3 database. */ 
    104         virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \ 
     104        virtual int32_t Update(string _where[], string _set[], float *_wherevals, \ 
    105105                float *_setvals, uint32_t _wherelen, uint32_t _setlen); 
    106106 
    107107        /* Add a row to the VTCROSS sqlite3 database. */ 
    108         virtual int32_t AddRow(char *_cols[], float *_vals, uint32_t _len); 
     108        virtual int32_t AddRow(string _cols[], float *_vals, uint32_t _len); 
    109109 
    110110    protected: 
    111         char filename[64]; 
    112         char tablename[64]; 
    113         char command[2048]; 
     111        string filename; 
     112        string tablename; 
     113        string command; 
     114 
    114115        sqlite3 *db;  
    115116        uint32_t numColumns; 
     
    117118 
    118119 
    119 CBR::CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len) 
    120 { 
    121     // copy filename 
    122     unsigned int i=0; 
    123     strcpy(filename, _filename); 
    124  
    125     // execute create database command 
    126     // database handle 
    127     //db = NULL; 
     120CBR::CBR(string _filename, string _tablename, string _cols[], uint32_t _len) 
     121{ 
     122    /* Store database properties. */ 
     123    filename = _filename; 
     124    tablename = _tablename; 
     125    numColumns = _len; 
     126 
     127    /* Create the database (or open it if it already exists). */ 
    128128    OpenDatabase(); 
    129129 
    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 
     150CBR::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; 
    136156    numColumns = _len; 
    137157 
    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. */ 
    151182    ExecuteCommand(); 
    152183} 
    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  
    198184 
    199185 
    200186CBR::~CBR() 
    201187 
    202     // generate command, remove a table with its content 
    203     strcpy(command, "drop table "); 
    204     strcat(command, tablename); 
    205  
    206     // execute delete command 
     188    /* Generate the sqlite command to delete a table and all of its contents, 
     189     * and then execute it. */ 
     190    command = "drop table " + tablename; 
    207191    ExecuteCommand(); 
    208192 
    209     // clean the database 
    210     strcpy(command, "vacuum"); 
     193    /* Tell sqlite to clean up the database. */ 
     194    command = "vacuum"; 
    211195    ExecuteCommand(); 
    212196} 
    213197 
    214198 
    215  
    216  
    217199int32_t  
    218200CBR::OpenDatabase() 
    219201{ 
    220     int32_t rc; 
    221  
    222     rc = sqlite3_open(filename, &db); 
     202    int32_t rc = sqlite3_open(filename.c_str(), &db); 
    223203    if(rc) { 
    224204        WARNING("Can't open database: %s\n", sqlite3_errmsg(db)); 
     
    234214CBR::ExecuteCommand() 
    235215{ 
    236     int rc; 
    237216    char *zErrMsg = 0; 
    238217 
    239     rc = sqlite3_exec(db, command, callback, 0, &zErrMsg); 
    240     if( rc!=SQLITE_OK){ 
    241         fprintf(stderr, "SQL error: %s: %s\n", zErrMsg, command); 
     218    int32_t rc = sqlite3_exec(db, command.c_str(), callback, 0, &zErrMsg); 
     219    if(rc != SQLITE_OK) { 
     220        WARNING("SQL error: %s: %s\n", zErrMsg, command.c_str()); 
    242221        sqlite3_free(zErrMsg); 
    243222    } 
     
    250229CBR::ExecuteSearchCommand(float *_retvals) 
    251230{ 
    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) { 
    260237                _retvals[i] = sqlite3_column_double(pStatement, i); 
     238            } 
    261239        } else { 
    262                     printf("CBR:: No matched results returning default.\n"); 
    263                         rc=31337; 
     240                    LOG("CBR:: No matched results returning default.\n"); 
     241                        rc = 31337; 
    264242                } 
    265243    } else { 
    266                 printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,command); 
     244                WARNING("CBR:: Error executing SQL statement. rc = %i\n%s\n", rc, command.c_str()); 
    267245    } 
    268246 
     
    271249    return rc; 
    272250} 
     251 
     252 
    273253void  
    274254CBR::Print() 
    275255{ 
    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 
    284260    ExecuteCommand(); 
    285     printf("database %s, table %s:\n", filename, tablename); 
    286 } 
    287  
    288 int32_t  
    289 CBR::Search( 
    290     char *_names[], 
    291     int * _ops, 
    292     float *_vals, 
    293     unsigned int _n, 
    294     float *_retvals) 
     261    LOG("database %s, table %s:\n", filename.c_str(), tablename.c_str()); 
     262} 
     263 
     264 
     265int32_t  
     266CBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ 
     267        float *_retvals) 
    295268{    
    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 
    306271    char str_buffer[64]; 
    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]); 
    313280        } 
    314281 
    315         strcat(command, _names[i]); 
    316         strcat(command, ops_str[_ops[i]]); 
    317     printf("search command: %s\n", command); 
     282        command += _names[i] + ops_str[_ops[i]];; 
     283 
     284        LOG("CBR::Search - command: %s\n", command.c_str()); 
     285 
    318286        sprintf(str_buffer, "%E", _vals[i]); 
    319         strcat(command, str_buffer); 
    320  
    321  
    322         if (i<_n-1) 
    323             strcat(command, " AND "); 
     287        command += string(str_buffer); 
     288 
     289        if(i < _n - 1) 
     290            command += " AND "; 
    324291        else 
    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 
     299int32_t  
     300CBR::SearchSum(string _name, float *_retvals) 
     301{    
     302    command = "select SUM(" + tablename + "." + _name + ") from " + tablename + ";"; 
     303 
     304    return ExecuteSearchCommand(_retvals); 
     305} 
     306 
     307 
     308int32_t  
     309CBR::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 
     338int32_t  
     339CBR::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 += ";"; 
    330366     
    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 
     371int32_t  
     372CBR::AddRow(string _cols[], float *_vals, uint32_t _len) 
     373{ 
    379374    char str_buffer[64]; 
    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. 
    469388        // ???? how to fill the values if numColumns != _len 
    470389        // assume = in the following 
    471         sprintf(command, "%s%f", command, _vals[i]); 
    472         if (i != numColumns-1) // not last entry 
    473             strcat(command, ", "); 
    474     } 
    475     strcat(command, ");"); 
     390        sprintf(str_buffer, "%f", _vals[j]); 
     391        command += str_buffer; 
     392 
     393        if(j != numColumns - 1) 
     394            command += ", "; 
     395    } 
     396    command += ");"; 
    476397     
    477     // execute add command 
    478     ExecuteCommand(); 
    479  
    480     return 0; 
     398    return ExecuteCommand(); 
    481399} 
    482400