00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #if POSTGRES_ENABLED
00022
00023 #include <string.h>
00024 #include <oasys/debug/DebugUtils.h>
00025 #include <oasys/util/StringBuffer.h>
00026 #include "PostgresSQLImplementation.h"
00027
00028 namespace dtn {
00029
00030 PostgresSQLImplementation::PostgresSQLImplementation()
00031 : SQLImplementation("BYTEA", "BOOLEAN"),
00032 Logger("/storage/postgresql")
00033 {
00034 query_result_ = NULL;
00035 }
00036
00037 int
00038 PostgresSQLImplementation::connect(const char* dbName)
00039 {
00040 char *pghost;
00041 char *pgport;
00042 char *pgoptions;
00043 char *pgtty;
00044
00045 log_debug("connecting to database %s", dbName);
00046
00047
00048
00049
00050
00051
00052
00053
00054 pghost = NULL;
00055 pgport = NULL;
00056 pgoptions = NULL;
00057
00058 pgtty = NULL;
00059
00065 db_ = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
00066
00070 if (PQstatus(db_) == CONNECTION_BAD)
00071 {
00072 log_err("connection to database '%s' failed: %s",
00073 dbName, PQerrorMessage(db_));
00074 return -1;
00075 }
00076
00077 return 0;
00078 }
00079
00080 int
00081 PostgresSQLImplementation::close()
00082 {
00083 PQfinish(db_);
00084 return 0;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 const char*
00099 PostgresSQLImplementation::get_value(int tuple_no, int field_no)
00100 {
00101 const char* ret ;
00102 ASSERT(query_result_);
00103 ret = PQgetvalue(query_result_, tuple_no, field_no);
00104 return ret;
00105 }
00106
00107 bool
00108 PostgresSQLImplementation::has_table(const char* tablename)
00109 {
00110 bool retval = 0;
00111 oasys::StringBuffer query;
00112
00113 query.appendf("select * from pg_tables where tablename = '%s'", tablename);
00114 int ret = exec_query(query.c_str());
00115 ASSERT(ret == 0);
00116 if (num_tuples() == 1) retval = 1;
00117
00118 return retval;
00119 }
00120
00121 int
00122 PostgresSQLImplementation::num_tuples()
00123 {
00124 int ret = -1;
00125 ASSERT(query_result_);
00126 ret = PQntuples(query_result_);
00127 return ret;
00128 }
00129
00130 static int
00131 status_to_int(ExecStatusType t)
00132 {
00133 if (t == PGRES_COMMAND_OK) return 0;
00134 if (t == PGRES_TUPLES_OK) return 0;
00135 if (t == PGRES_EMPTY_QUERY) return 0;
00136 return -1;
00137 }
00138
00139 int
00140 PostgresSQLImplementation::exec_query(const char* query)
00141 {
00142 int ret = -1;
00143
00144 if (query_result_ != NULL) {
00145 PQclear(query_result_);
00146 query_result_ = NULL;
00147 }
00148
00149 query_result_ = PQexec(db_, query);
00150 ASSERT(query_result_);
00151 ExecStatusType t = PQresultStatus(query_result_);
00152 ret = status_to_int(t);
00153
00154 return ret;
00155 }
00156
00157 const char*
00158 PostgresSQLImplementation::escape_string(const char* from)
00159 {
00160 int length = strlen(from);
00161 char* to = (char *) malloc(2*length+1);
00162 PQescapeString (to,from,length);
00163 return to;
00164 }
00165
00166 const u_char*
00167 PostgresSQLImplementation::escape_binary(const u_char* from, int from_length)
00168 {
00169 size_t to_length;
00170 u_char* from1 = (u_char *) from ;
00171 u_char* to = PQescapeBytea(from1,from_length,&to_length);
00172 return to;
00173 }
00174
00175 const u_char*
00176 PostgresSQLImplementation::unescape_binary(const u_char* from)
00177 {
00178 u_char* from1 = (u_char *) from ;
00179 size_t to_length ;
00180 const u_char* to = PQunescapeBytea(from1,&to_length);
00181 return to;
00182 }
00183
00184 }
00185
00186 #endif