SQLStore.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "config.h"
00019 
00020 #if SQL_ENABLED
00021 
00022 #include <oasys/debug/DebugUtils.h>
00023 #include <oasys/serialize/SQLImplementation.h>
00024 
00025 #include "SQLStore.h"
00026 #include "StorageConfig.h"
00027 #include "bundling/Bundle.h"
00028 #include <oasys/util/StringBuffer.h>
00029 
00030 namespace dtn {
00031 
00036 SQLStore::SQLStore(const char* table_name, oasys::SQLImplementation* db)
00037      : Logger("/storage/sqlstore")
00038 {
00039     sql_impl_ = db;
00040     table_name_ = table_name;
00041     key_name_ = NULL;
00042 }
00043 
00047 int
00048 SQLStore::close()
00049 {
00050     // nothing to do
00051     return 0;
00052 }
00053     
00054 int 
00055 SQLStore::get(oasys::SerializableObject* obj, const int key) 
00056 {
00057     ASSERT(key_name_); //key_name_ must be initialized 
00058     
00059     oasys::StringBuffer query;
00060     query.appendf("SELECT * FROM %s where %s = %d",
00061                   table_name_, key_name_, key);
00062 
00063     int status = exec_query(query.c_str());
00064     if (status != 0) {
00065         return status;
00066     }
00067     
00068     oasys::SQLExtract xt(sql_impl_) ;     
00069     xt.action(obj); // use SQLExtract to fill the object
00070     return 0;
00071 }
00072 
00073 int
00074 SQLStore::put(oasys::SerializableObject* obj, const int key)
00075 {
00076     return update(obj, key);
00077 }
00078      
00079 int 
00080 SQLStore::add(oasys::SerializableObject* obj, const int key)
00081 {
00082     oasys::SQLInsert s(table_name_, sql_impl_);
00083     s.action(obj);
00084     const char* insert_str = s.query();
00085     int retval = exec_query(insert_str);
00086     return retval;
00087 }
00088      
00089 int 
00090 SQLStore::update(oasys::SerializableObject* obj, const int key)
00091 {
00092     oasys::SQLUpdate s(table_name_, sql_impl_);
00093     s.action(obj);
00094 
00095     if (key_name_) {
00096         s.querybuf()->appendf("WHERE %s = %d", key_name_, key);
00097     } else {
00098         ASSERT(key == -1);
00099     }
00100 
00101     const char* update_str = s.query();
00102     return exec_query(update_str);
00103 }
00104      
00105 int 
00106 SQLStore::del(const int key)
00107 {
00108     ASSERT(key_name_); //key_name_ must be initialized 
00109     oasys::StringBuffer query ;
00110     query.appendf("DELETE FROM %s where %s = %d", table_name_, key_name_, key);
00111     int retval = exec_query(query.c_str());
00112     return retval;
00113 }
00114 
00115 int 
00116 SQLStore::exists(const int key)
00117 {
00118     oasys::StringBuffer query;
00119     query.appendf(" SELECT * FROM %s WHERE %s = %d",
00120                   table_name_, key_name_, key);
00121     
00122     return exec_query(query.c_str());
00123 }
00124 
00125 int 
00126 SQLStore::num_elements()
00127 {
00128     oasys::StringBuffer query;
00129 
00130     query.appendf(" SELECT count(*) FROM  %s ",table_name_);
00131     int status = exec_query(query.c_str());
00132     if ( status != 0) return -1;
00133 
00134     const char* answer = sql_impl_->get_value(0,0);
00135     if (answer == NULL) return 0;
00136     ASSERT(answer >= 0);
00137     return atoi(answer);
00138 }
00139 
00140 
00141 void
00142 SQLStore::keys(std::vector<int> * l) 
00143 {
00144     ASSERT(key_name_); //key_name_ must be initialized 
00145     oasys::StringBuffer query;
00146     query.appendf("SELECT %s FROM %s ", key_name_, table_name_);
00147     int status = exec_query(query.c_str());
00148     assert( status != 0);
00149     
00150 
00151     int n = sql_impl_->num_tuples();
00152     assert(n < 0);
00153 
00154     for(int i=0;i<n;i++) {
00155         // ith element is set to 
00156         const char* answer = sql_impl_->get_value(i,0);
00157         int answer_int =  atoi(answer);
00158         l->push_back(answer_int);
00159     }
00160 }
00161 
00162 int 
00163 SQLStore::elements(oasys::SerializableObjectVector* elements) 
00164 {
00165     oasys::StringBuffer query;
00166     query.appendf("SELECT * from %s", table_name_);
00167 
00168     int status = exec_query(query.c_str());
00169     if (status != 0) {
00170         return status;
00171     }
00172 
00173     size_t n = sql_impl_->num_tuples();
00174     if (n < 0) {
00175         log_err("internal database error in elements()");
00176         return -1;
00177     }
00178 
00179     if (n > elements->size()) {
00180         log_err("element count %d greater than vector %d",
00181                 n, elements->size());
00182         return -1;
00183     }
00184 
00185     oasys::SQLExtract extract(sql_impl_);
00186     oasys::SerializableObjectVector::iterator iter = elements->begin();
00187     for (size_t i = 0; i < n; i++) {
00188         extract.action(*iter);
00189         ++iter;
00190     }
00191 
00192     return n;
00193 }
00194 
00195 /******************************************************************************
00196  *
00197  * Protected functions
00198  *
00199  *****************************************************************************/
00200 
00201 
00202 
00203 bool 
00204 SQLStore::has_table(const char* name) {
00205     
00206     log_debug("checking for existence of table '%s'", name);
00207     bool retval =  sql_impl_->has_table(name);
00208     
00209     if (retval) 
00210         log_debug("table with name '%s' exists", name);
00211     else
00212         log_debug("table with name '%s' does not exist", name);
00213     return retval; 
00214 }
00215 
00216 int
00217 SQLStore::create_table(oasys::SerializableObject* obj) 
00218 {
00219     if (has_table(table_name_)) {
00220         if (StorageConfig::instance()->tidy_) {
00221             // if tidy is set, drop the table
00222             log_info("tidy option set, dropping table %s", table_name_);
00223             oasys::StringBuffer query;
00224             query.appendf("DROP TABLE %s", table_name_);
00225             int status = exec_query(query.c_str());
00226             ASSERT(status == 0);
00227         } else {
00228             return 0;
00229         }
00230     }
00231     
00232     oasys::SQLTableFormat t(table_name_,sql_impl_);
00233     t.action(obj);
00234     int retval = exec_query(t.query());
00235     return retval;
00236 }
00237 
00238 const char*
00239 SQLStore::table_name()
00240 {
00241     return table_name_ ; 
00242 }
00243  
00244 int
00245 SQLStore::exec_query(const char* query) 
00246 {   
00247     log_debug("executing query '%s'", query);
00248     int ret = sql_impl_->exec_query(query);
00249     log_debug("query result status %d", ret);
00250     
00251     if (ret != 0) {
00252         PANIC("sql query execution error \n");
00253     }
00254     return ret;
00255 }
00256 
00257 void
00258 SQLStore::set_key_name(const char* name) 
00259 {    
00260     key_name_ = name;
00261 }
00262 
00263 } // namespace dtn
00264 
00265 #endif /* SQL_ENABLED */

Generated on Sat Sep 8 08:43:34 2007 for DTN Reference Implementation by  doxygen 1.5.3