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