00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef HAVE_CONFIG_H
00019 # include <dtn-config.h>
00020 #endif
00021
00022 #if MYSQL_ENABLED
00023
00024 #include <oasys/debug/DebugUtils.h>
00025 #include <oasys/debug/Log.h>
00026
00027 #include "MysqlSQLImplementation.h"
00028
00029 namespace dtn {
00030
00031 MysqlSQLImplementation::MysqlSQLImplementation()
00032 : SQLImplementation("BLOB", "BOOLEAN"),
00033 Logger("/storage/mysql")
00034 {
00035 query_result_ = NULL;
00036 }
00037
00038 int
00039 MysqlSQLImplementation::connect(const char* dbName)
00040 {
00041 db_ = mysql_init(NULL);
00042
00043 log_debug("connecting to database %s", dbName);
00044
00045
00046 if (!mysql_real_connect(db_, NULL,
00047 NULL, NULL, dbName, 0, NULL, 0)) {
00048 log_err("error connecting to database %s: %s",
00049 dbName, mysql_error(db_));
00050 return -1;
00051 }
00052
00053 return 0;
00054 }
00055
00056 const char*
00057 MysqlSQLImplementation::get_value(int tuple_no, int field_no)
00058 {
00059 const char* ret;
00060
00061 ASSERT(query_result_);
00062 mysql_data_seek(query_result_, tuple_no);
00063
00064 MYSQL_ROW r = mysql_fetch_row(query_result_);
00065 ret = r[field_no];
00066 return ret;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 int
00085 MysqlSQLImplementation::close()
00086 {
00087 mysql_close(db_);
00088 db_ = NULL;
00089 return 0;
00090 }
00091
00092 bool
00093 MysqlSQLImplementation::has_table(const char* tablename)
00094 {
00095 bool ret = false;
00096
00097 if (query_result_ != 0) {
00098 mysql_free_result(query_result_);
00099 query_result_ = 0;
00100 }
00101
00102 query_result_ = mysql_list_tables(db_, tablename);
00103
00104 if (mysql_num_rows(query_result_) == 1) {
00105 ret = true;
00106 }
00107
00108 mysql_free_result(query_result_);
00109 query_result_ = NULL;
00110
00111 return ret;
00112 }
00113
00114 int
00115 MysqlSQLImplementation::num_tuples()
00116 {
00117 int ret = -1;
00118 ASSERT(query_result_);
00119 ret = mysql_num_rows(query_result_);
00120 return ret;
00121 }
00122
00123 int
00124 MysqlSQLImplementation::exec_query(const char* query)
00125 {
00126 int ret = -1;
00127
00128
00129 if (query_result_ != NULL) {
00130 mysql_free_result(query_result_);
00131 query_result_ = NULL;
00132 }
00133
00134 ret = mysql_query(db_, query);
00135
00136 if (ret == 1) {
00137 return ret;
00138 }
00139
00140 query_result_ = mysql_store_result(db_);
00141
00142 return ret;
00143 }
00144
00145 const char*
00146 MysqlSQLImplementation::escape_string(const char* from)
00147 {
00148 int length = strlen(from);
00149
00150 char* to = (char *) malloc(2*length+1);
00151 mysql_real_escape_string(db_,to,from,length);
00152
00153 return to;
00154 }
00155
00156 const u_char*
00157 MysqlSQLImplementation::escape_binary(const u_char* from, int from_length)
00158 {
00159 int length = from_length;
00160
00161 char* to = (char *) malloc(2*length+1);
00162 mysql_real_escape_string(db_,to,(const char*)from,length);
00163 return (const u_char*) to;
00164 }
00165
00166 const u_char*
00167 MysqlSQLImplementation::unescape_binary(const u_char* from)
00168 {
00169 return from;
00170 }
00171
00172 }
00173
00174 #endif