PostgresSQLImplementation.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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      * begin, by setting the parameters for a backend connection if
00048      * the parameters are null, then the system will try to use
00049      * reasonable defaults by looking up environment variables or,
00050      * failing that, using hardwired constants
00051      */
00052 
00053     pghost = NULL;      /* host name of the backend server */
00054     pgport = NULL;      /* port of the backend server */
00055     pgoptions = NULL;   /* special options to start up the backend
00056                          * server */
00057     pgtty = NULL;       /* debugging tty for the backend server */
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 size_t
00088 PostgresSQLImplementation::get_value_length(int tuple_no, int field_no)
00089 {
00090     size_t retval;
00091     const char* val = get_value(tuple_no,field_no);
00092     unescape_binary((const u_char*)val,&retval);
00093     return retval;
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 } // namespace dtn
00184 
00185 #endif /* POSTGRES_ENABLED */

Generated on Sat Sep 8 08:43:31 2007 for DTN Reference Implementation by  doxygen 1.5.3