MysqlSQLImplementation.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 MYSQL_ENABLED
00042 
00043 #include <oasys/debug/DebugUtils.h>
00044 #include <oasys/debug/Log.h>
00045 
00046 #include "MysqlSQLImplementation.h"
00047 
00048 namespace dtn {
00049 
00050 MysqlSQLImplementation::MysqlSQLImplementation()
00051     : SQLImplementation("BLOB", "BOOLEAN"),
00052       Logger("/storage/mysql")
00053 {
00054     query_result_ = NULL;
00055 }
00056 
00057 int
00058 MysqlSQLImplementation::connect(const char* dbName)
00059 {
00060     db_ = mysql_init(NULL);
00061 
00062     log_debug("connecting to database %s", dbName);
00063  
00064     /* connect to database */
00065     if (!mysql_real_connect(db_, NULL,
00066                             NULL, NULL, dbName, 0, NULL, 0)) {
00067         log_err("error connecting to database %s: %s",
00068                 dbName, mysql_error(db_));
00069         return -1;
00070     }
00071 
00072     return 0;
00073 }
00074 
00075 const char*
00076 MysqlSQLImplementation::get_value(int tuple_no, int field_no)
00077 {
00078     const char* ret;
00079   
00080     ASSERT(query_result_);
00081     mysql_data_seek(query_result_, tuple_no);
00082 
00083     MYSQL_ROW r = mysql_fetch_row(query_result_);
00084     ret = r[field_no];
00085     return ret;
00086 }
00087 
00088 /*
00089 size_t
00090 MysqlSQLImplementation::get_value_length(int tuple_no, int field_no)
00091 {
00092     unsigned long *lengths;
00093   
00094     ASSERT(query_result_);
00095     mysql_data_seek(query_result_, tuple_no);
00096 
00097     mysql_fetch_row(query_result_);
00098     lengths = mysql_fetch_lengths(query_result_);
00099     return (size_t)lengths[field_no];
00100 }
00101 */
00102 
00103 int
00104 MysqlSQLImplementation::close()
00105 {
00106     mysql_close(db_);
00107     db_ = NULL;
00108     return 0;
00109 }
00110 
00111 bool
00112 MysqlSQLImplementation::has_table(const char* tablename)
00113 {
00114     bool ret = false;
00115  
00116     if (query_result_ != 0) {
00117         mysql_free_result(query_result_);
00118         query_result_ = 0;
00119     }
00120 
00121     query_result_ = mysql_list_tables(db_, tablename);
00122     
00123     if (mysql_num_rows(query_result_) == 1) {
00124         ret = true;
00125     }
00126     
00127     mysql_free_result(query_result_);
00128     query_result_ = NULL;
00129 
00130     return ret;
00131 }
00132 
00133 int
00134 MysqlSQLImplementation::num_tuples()
00135 {
00136     int ret = -1;
00137     ASSERT(query_result_);
00138     ret = mysql_num_rows(query_result_);
00139     return ret;
00140 }
00141 
00142 int
00143 MysqlSQLImplementation::exec_query(const char* query)
00144 {
00145     int ret = -1;
00146     
00147     // free previous result state
00148     if (query_result_ != NULL) {
00149         mysql_free_result(query_result_);
00150         query_result_ = NULL;
00151     }
00152     
00153     ret = mysql_query(db_, query);
00154     
00155     if (ret == 1) {
00156         return ret;
00157     }
00158     
00159     query_result_ = mysql_store_result(db_);
00160     
00161     return ret;
00162 }
00163 
00164 const char* 
00165 MysqlSQLImplementation::escape_string(const char* from) 
00166 {
00167     int length = strlen(from);
00168     // XXX/demmer fix memory leaks
00169     char* to = (char *) malloc(2*length+1);
00170     mysql_real_escape_string(db_,to,from,length);
00171 
00172     return to;
00173 }
00174 
00175 const u_char* 
00176 MysqlSQLImplementation::escape_binary(const u_char* from, int from_length) 
00177 {
00178     int length = from_length;
00179     // XXX/demmer fix memory leaks
00180     char* to = (char *) malloc(2*length+1);
00181     mysql_real_escape_string(db_,to,(const char*)from,length);
00182     return (const u_char*) to;
00183 }
00184 
00185 const u_char* 
00186 MysqlSQLImplementation::unescape_binary(const u_char* from) 
00187 {
00188     return from; 
00189 }
00190 
00191 } // namespace dtn
00192 
00193 #endif /* MYSQL_ENABLED */

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1