Changeset 443

Show
Ignore:
Timestamp:
08/26/09 18:49:00 (15 years ago)
Author:
bhilburn
Message:

Code cleanup in the sqlite DB creation functions.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp

    r442 r443  
    157157 
    158158    miss = new Mission[10]; 
    159     for(size_t i = 0; i < 10; i++) 
     159    for(size_t i = 0; i < 10; i++) { 
    160160        miss[i].services = new Service[30]; 
     161    } 
    161162 
    162163    Current_ID = 0; 
     
    177178ServiceManagementLayer::CreateServicesDB() 
    178179{ 
    179     sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     180    sqlite3_stmt *ppStmt;   /* OUT: Statement handle */ 
    180181    const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
    181  
    182     //_services_DB = (services_DB) malloc(sizeof(struct services_s)); 
     182    int32_t rc;             /* sqlite command return code */ 
     183 
    183184    _services_DB = new services_s; 
    184     // copy filename 
    185185    _services_DB->filename="Services_Table"; 
    186     // execute create database command 
    187     // database handle 
    188     //_services_DB->db = NULL; 
    189186    sqlite3_open(_services_DB->filename.c_str(), &(_services_DB->db)); 
     187 
    190188    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
    191189 
    192  
    193     // copy tablename 
    194190    _services_DB->tablename="Services"; 
    195191 
    196     //If program execution ends in anything other than a ordered shutdown, DB's will still be there for next run 
    197     //Need to get rid of it so that old data isn't inadvertantly used in the next execution cycle 
     192    /* If program execution ends in anything other than a ordered shutdown, DB's will still  
     193     * be there for next run. Need to get rid of it so that old data isn't inadvertantly  
     194     * used in the next execution cycle. */ 
    198195    _services_DB->command = "DROP TABLE IF EXISTS Services;";      
    199196 
    200     int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), 128, &ppStmt, &pzTail); 
    201     if( rc!=SQLITE_OK && rc!=101 ) 
    202         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     197    rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), 128, &ppStmt, &pzTail); 
     198    if((rc != SQLITE_OK) && (rc != 101)) 
     199        WARNING("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     200 
    203201    rc = sqlite3_step(ppStmt); 
    204     if( rc!=SQLITE_OK && rc!=101 ) 
    205         printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
    206  
    207     // number of columns in the table 
     202    if((rc != SQLITE_OK) && (rc != 101)) 
     203        WARNING("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
     204 
    208205    _services_DB->num_columns = 2; 
    209206 
    210     // generate command 
     207    /* Generate command */ 
    211208    _services_DB->command="CREATE TABLE "; 
    212209    _services_DB->command.append(_services_DB->tablename); 
     
    217214    _services_DB->command.append(" TEXT);"); 
    218215 
    219     // execute create table command 
    220  
     216    /* Execute create table command */ 
    221217    rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), 128, &ppStmt, &pzTail); 
    222     if( rc!=SQLITE_OK && rc!=101 ) 
    223         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     218    if((rc != SQLITE_OK) && (rc != 101)) 
     219        WARNING("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     220 
    224221    rc = sqlite3_step(ppStmt); 
    225     if( rc!=SQLITE_OK && rc!=101 ) 
    226         printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
     222    if((rc != SQLITE_OK) && (rc != 101)) 
     223        WARNING("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
    227224} 
    228225 
     
    236233ServiceManagementLayer::CreateDataDB() 
    237234{ 
    238     sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     235    sqlite3_stmt *ppStmt;   /* OUT: Statement handle */ 
    239236    const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
    240  
    241     _data_DB = new data_s;//(data_DB) malloc(sizeof(struct data_s)); 
    242  
    243     // create database 
    244  
    245     // copy filename 
     237    int32_t rc;             /* sqlite command return code */ 
     238 
     239    _data_DB = new data_s; 
     240 
    246241    _data_DB->filename="Data_Table"; 
    247     // execute create database command 
    248     // database handle 
    249     //_services_DB->db = NULL; 
    250242    sqlite3_open(_data_DB->filename.c_str(), &(_data_DB->db)); 
     243 
    251244    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
    252245 
    253     // create table 
    254  
    255     // copy tablename 
    256246    _data_DB->tablename="Data"; 
    257247    _data_DB->command="DROP TABLE IF EXISTS Data;";      
    258248 
    259     int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail); 
    260     if( rc!=SQLITE_OK && rc!=101 ) 
    261         printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     249    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail); 
     250    if((rc != SQLITE_OK) && (rc != 101)) 
     251        WARNING("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
     252 
    262253    rc = sqlite3_step(ppStmt); 
    263     if( rc!=SQLITE_OK && rc!=101 ) 
    264         printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
    265  
    266  
    267     // number of columns in the table 
     254    if((rc != SQLITE_OK) && (rc != 101)) 
     255        WARNING("ServiceManagementLayer::CreateDataDB 'step' error\n"); 
     256 
    268257    _data_DB->num_columns = 2; 
    269258 
    270     // generate command 
     259    /* Generate command */ 
    271260    _data_DB->command="CREATE TABLE "; 
    272261    _data_DB->command.append(_data_DB->tablename); 
    273262    _data_DB->command.append("("); 
    274263    _data_DB->command.append(cols[0]); 
    275     //First column is the name of the data (coresponding to the name of the output/input pair) 
    276     //It is the primary key so any subsequent data with the same name will replace the row 
     264 
     265    /* First column is the name of the data (corresponding to the name of the output/input pair) 
     266     * It is the primary key so any subsequent data with the same name will replace the row. */ 
    277267    _data_DB->command.append(" TEXT PRIMARY KEY ON CONFLICT REPLACE, "); 
    278268    _data_DB->command.append(cols[1]); 
    279269    _data_DB->command.append(" TEXT);"); 
    280270 
    281     // execute create table command 
    282  
    283271    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail); 
    284     if( rc!=SQLITE_OK && rc!=101 ) 
    285         printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
     272    if((rc != SQLITE_OK) && (rc != 101)) 
     273        WARNING("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
     274 
    286275    rc = sqlite3_step(ppStmt); 
    287     if( rc!=SQLITE_OK && rc!=101 ) 
    288         printf("ServiceManagementLayer::CreateDataDB 'step' error\n"); 
     276    if((rc != SQLITE_OK) && (rc != 101)) 
     277        WARNING("ServiceManagementLayer::CreateDataDB 'step' error\n"); 
    289278} 
    290279