00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
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
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
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
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 }
00192
00193 #endif