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