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 POSTGRES_ENABLED
00042
00043 #include <string.h>
00044 #include <oasys/debug/DebugUtils.h>
00045 #include <oasys/util/StringBuffer.h>
00046 #include "PostgresSQLImplementation.h"
00047
00048 namespace dtn {
00049
00050 PostgresSQLImplementation::PostgresSQLImplementation()
00051 : SQLImplementation("BYTEA", "BOOLEAN"),
00052 Logger("/storage/postgresql")
00053 {
00054 query_result_ = NULL;
00055 }
00056
00057 int
00058 PostgresSQLImplementation::connect(const char* dbName)
00059 {
00060 char *pghost;
00061 char *pgport;
00062 char *pgoptions;
00063 char *pgtty;
00064
00065 log_debug("connecting to database %s", dbName);
00066
00067
00068
00069
00070
00071
00072
00073
00074 pghost = NULL;
00075 pgport = NULL;
00076 pgoptions = NULL;
00077
00078 pgtty = NULL;
00079
00085 db_ = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
00086
00090 if (PQstatus(db_) == CONNECTION_BAD)
00091 {
00092 log_err("connection to database '%s' failed: %s",
00093 dbName, PQerrorMessage(db_));
00094 return -1;
00095 }
00096
00097 return 0;
00098 }
00099
00100 int
00101 PostgresSQLImplementation::close()
00102 {
00103 PQfinish(db_);
00104 return 0;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 const char*
00119 PostgresSQLImplementation::get_value(int tuple_no, int field_no)
00120 {
00121 const char* ret ;
00122 ASSERT(query_result_);
00123 ret = PQgetvalue(query_result_, tuple_no, field_no);
00124 return ret;
00125 }
00126
00127 bool
00128 PostgresSQLImplementation::has_table(const char* tablename)
00129 {
00130 bool retval = 0;
00131 oasys::StringBuffer query;
00132
00133 query.appendf("select * from pg_tables where tablename = '%s'", tablename);
00134 int ret = exec_query(query.c_str());
00135 ASSERT(ret == 0);
00136 if (num_tuples() == 1) retval = 1;
00137
00138 return retval;
00139 }
00140
00141 int
00142 PostgresSQLImplementation::num_tuples()
00143 {
00144 int ret = -1;
00145 ASSERT(query_result_);
00146 ret = PQntuples(query_result_);
00147 return ret;
00148 }
00149
00150 static int
00151 status_to_int(ExecStatusType t)
00152 {
00153 if (t == PGRES_COMMAND_OK) return 0;
00154 if (t == PGRES_TUPLES_OK) return 0;
00155 if (t == PGRES_EMPTY_QUERY) return 0;
00156 return -1;
00157 }
00158
00159 int
00160 PostgresSQLImplementation::exec_query(const char* query)
00161 {
00162 int ret = -1;
00163
00164 if (query_result_ != NULL) {
00165 PQclear(query_result_);
00166 query_result_ = NULL;
00167 }
00168
00169 query_result_ = PQexec(db_, query);
00170 ASSERT(query_result_);
00171 ExecStatusType t = PQresultStatus(query_result_);
00172 ret = status_to_int(t);
00173
00174 return ret;
00175 }
00176
00177 const char*
00178 PostgresSQLImplementation::escape_string(const char* from)
00179 {
00180 int length = strlen(from);
00181 char* to = (char *) malloc(2*length+1);
00182 PQescapeString (to,from,length);
00183 return to;
00184 }
00185
00186 const u_char*
00187 PostgresSQLImplementation::escape_binary(const u_char* from, int from_length)
00188 {
00189 size_t to_length;
00190 u_char* from1 = (u_char *) from ;
00191 u_char* to = PQescapeBytea(from1,from_length,&to_length);
00192 return to;
00193 }
00194
00195 const u_char*
00196 PostgresSQLImplementation::unescape_binary(const u_char* from)
00197 {
00198 u_char* from1 = (u_char *) from ;
00199 size_t to_length ;
00200 const u_char* to = PQunescapeBytea(from1,&to_length);
00201 return to;
00202 }
00203
00204 }
00205
00206 #endif