SQLStore.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "config.h"
00040 
00041 #if SQL_ENABLED
00042 
00043 #include <oasys/debug/DebugUtils.h>
00044 #include <oasys/serialize/SQLImplementation.h>
00045 
00046 #include "SQLStore.h"
00047 #include "StorageConfig.h"
00048 #include "bundling/Bundle.h"
00049 #include <oasys/util/StringBuffer.h>
00050 
00051 namespace dtn {
00052 
00057 SQLStore::SQLStore(const char* table_name, oasys::SQLImplementation* db)
00058      : Logger("/storage/sqlstore")
00059 {
00060     sql_impl_ = db;
00061     table_name_ = table_name;
00062     key_name_ = NULL;
00063 }
00064 
00068 int
00069 SQLStore::close()
00070 {
00071     // nothing to do
00072     return 0;
00073 }
00074     
00075 int 
00076 SQLStore::get(oasys::SerializableObject* obj, const int key) 
00077 {
00078     ASSERT(key_name_); //key_name_ must be initialized 
00079     
00080     oasys::StringBuffer query;
00081     query.appendf("SELECT * FROM %s where %s = %d",
00082                   table_name_, key_name_, key);
00083 
00084     int status = exec_query(query.c_str());
00085     if (status != 0) {
00086         return status;
00087     }
00088     
00089     oasys::SQLExtract xt(sql_impl_) ;     
00090     xt.action(obj); // use SQLExtract to fill the object
00091     return 0;
00092 }
00093 
00094 int
00095 SQLStore::put(oasys::SerializableObject* obj, const int key)
00096 {
00097     return update(obj, key);
00098 }
00099      
00100 int 
00101 SQLStore::add(oasys::SerializableObject* obj, const int key)
00102 {
00103     oasys::SQLInsert s(table_name_, sql_impl_);
00104     s.action(obj);
00105     const char* insert_str = s.query();
00106     int retval = exec_query(insert_str);
00107     return retval;
00108 }
00109      
00110 int 
00111 SQLStore::update(oasys::SerializableObject* obj, const int key)
00112 {
00113     oasys::SQLUpdate s(table_name_, sql_impl_);
00114     s.action(obj);
00115 
00116     if (key_name_) {
00117         s.querybuf()->appendf("WHERE %s = %d", key_name_, key);
00118     } else {
00119         ASSERT(key == -1);
00120     }
00121 
00122     const char* update_str = s.query();
00123     return exec_query(update_str);
00124 }
00125      
00126 int 
00127 SQLStore::del(const int key)
00128 {
00129     ASSERT(key_name_); //key_name_ must be initialized 
00130     oasys::StringBuffer query ;
00131     query.appendf("DELETE FROM %s where %s = %d", table_name_, key_name_, key);
00132     int retval = exec_query(query.c_str());
00133     return retval;
00134 }
00135 
00136 int 
00137 SQLStore::exists(const int key)
00138 {
00139     oasys::StringBuffer query;
00140     query.appendf(" SELECT * FROM %s WHERE %s = %d",
00141                   table_name_, key_name_, key);
00142     
00143     return exec_query(query.c_str());
00144 }
00145 
00146 int 
00147 SQLStore::num_elements()
00148 {
00149     oasys::StringBuffer query;
00150 
00151     query.appendf(" SELECT count(*) FROM  %s ",table_name_);
00152     int status = exec_query(query.c_str());
00153     if ( status != 0) return -1;
00154 
00155     const char* answer = sql_impl_->get_value(0,0);
00156     if (answer == NULL) return 0;
00157     ASSERT(answer >= 0);
00158     return atoi(answer);
00159 }
00160 
00161 
00162 void
00163 SQLStore::keys(std::vector<int> * l) 
00164 {
00165     ASSERT(key_name_); //key_name_ must be initialized 
00166     oasys::StringBuffer query;
00167     query.appendf("SELECT %s FROM %s ", key_name_, table_name_);
00168     int status = exec_query(query.c_str());
00169     assert( status != 0);
00170     
00171 
00172     int n = sql_impl_->num_tuples();
00173     assert(n < 0);
00174 
00175     for(int i=0;i<n;i++) {
00176         // ith element is set to 
00177         const char* answer = sql_impl_->get_value(i,0);
00178         int answer_int =  atoi(answer);
00179         l->push_back(answer_int);
00180     }
00181 }
00182 
00183 int 
00184 SQLStore::elements(oasys::SerializableObjectVector* elements) 
00185 {
00186     oasys::StringBuffer query;
00187     query.appendf("SELECT * from %s", table_name_);
00188 
00189     int status = exec_query(query.c_str());
00190     if (status != 0) {
00191         return status;
00192     }
00193 
00194     size_t n = sql_impl_->num_tuples();
00195     if (n < 0) {
00196         log_err("internal database error in elements()");
00197         return -1;
00198     }
00199 
00200     if (n > elements->size()) {
00201         log_err("element count %d greater than vector %d",
00202                 n, elements->size());
00203         return -1;
00204     }
00205 
00206     oasys::SQLExtract extract(sql_impl_);
00207     oasys::SerializableObjectVector::iterator iter = elements->begin();
00208     for (size_t i = 0; i < n; i++) {
00209         extract.action(*iter);
00210         ++iter;
00211     }
00212 
00213     return n;
00214 }
00215 
00216 /******************************************************************************
00217  *
00218  * Protected functions
00219  *
00220  *****************************************************************************/
00221 
00222 
00223 
00224 bool 
00225 SQLStore::has_table(const char* name) {
00226     
00227     log_debug("checking for existence of table '%s'", name);
00228     bool retval =  sql_impl_->has_table(name);
00229     
00230     if (retval) 
00231         log_debug("table with name '%s' exists", name);
00232     else
00233         log_debug("table with name '%s' does not exist", name);
00234     return retval; 
00235 }
00236 
00237 int
00238 SQLStore::create_table(oasys::SerializableObject* obj) 
00239 {
00240     if (has_table(table_name_)) {
00241         if (StorageConfig::instance()->tidy_) {
00242             // if tidy is set, drop the table
00243             log_info("tidy option set, dropping table %s", table_name_);
00244             oasys::StringBuffer query;
00245             query.appendf("DROP TABLE %s", table_name_);
00246             int status = exec_query(query.c_str());
00247             ASSERT(status == 0);
00248         } else {
00249             return 0;
00250         }
00251     }
00252     
00253     oasys::SQLTableFormat t(table_name_,sql_impl_);
00254     t.action(obj);
00255     int retval = exec_query(t.query());
00256     return retval;
00257 }
00258 
00259 const char*
00260 SQLStore::table_name()
00261 {
00262     return table_name_ ; 
00263 }
00264  
00265 int
00266 SQLStore::exec_query(const char* query) 
00267 {   
00268     log_debug("executing query '%s'", query);
00269     int ret = sql_impl_->exec_query(query);
00270     log_debug("query result status %d", ret);
00271     
00272     if (ret != 0) {
00273         PANIC("sql query execution error \n");
00274     }
00275     return ret;
00276 }
00277 
00278 void
00279 SQLStore::set_key_name(const char* name) 
00280 {    
00281     key_name_ = name;
00282 }
00283 
00284 } // namespace dtn
00285 
00286 #endif /* SQL_ENABLED */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1