PostgresSQLImplementation.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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      * begin, by setting the parameters for a backend connection if
00069      * the parameters are null, then the system will try to use
00070      * reasonable defaults by looking up environment variables or,
00071      * failing that, using hardwired constants
00072      */
00073 
00074     pghost = NULL;      /* host name of the backend server */
00075     pgport = NULL;      /* port of the backend server */
00076     pgoptions = NULL;   /* special options to start up the backend
00077                          * server */
00078     pgtty = NULL;       /* debugging tty for the backend server */
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 size_t
00109 PostgresSQLImplementation::get_value_length(int tuple_no, int field_no)
00110 {
00111     size_t retval;
00112     const char* val = get_value(tuple_no,field_no);
00113     unescape_binary((const u_char*)val,&retval);
00114     return retval;
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 } // namespace dtn
00205 
00206 #endif /* POSTGRES_ENABLED */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1