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
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifdef HAVE_PGSQL
00065 #include <libpq-fe.h>
00066
00067
00068 #define add1(str, value) \
00069 if (value != NULL && octstr_len(value) > 0) { \
00070 tmp = octstr_format(str, value); \
00071 octstr_append(cs, tmp); \
00072 octstr_destroy(tmp); \
00073 }
00074
00075
00076 static void *pgsql_open_conn(const DBConf *db_conf)
00077 {
00078 PGconn *conn = NULL;
00079 PgSQLConf *conf = db_conf->pgsql;
00080 Octstr *tmp, *cs;
00081
00082
00083 if (conf == NULL)
00084 return NULL;
00085
00086 cs = octstr_create("");
00087 add1(" host=%S", conf->host);
00088
00089
00090
00091
00092
00093
00094 if (conf->port > 0) {
00095 octstr_append_cstr(cs, " port=");
00096 octstr_append_decimal(cs, conf->port);
00097 }
00098 add1(" user=%S", conf->username);
00099 add1(" password=%S", conf->password);
00100 add1(" dbname=%S", conf->database);
00101
00102 #if 0
00103
00104 info(0, "PGSQL: Using connection string: %s.", octstr_get_cstr(cs));
00105 #endif
00106
00107 conn = PQconnectdb(octstr_get_cstr(cs));
00108
00109 octstr_destroy(cs);
00110 if (conn == NULL)
00111 goto failed;
00112
00113 gw_assert(conn != NULL);
00114
00115 if (PQstatus(conn) == CONNECTION_BAD) {
00116 error(0, "PGSQL: connection to database '%s' failed!", octstr_get_cstr(conf->database));
00117 panic(0, "PGSQL: %s", PQerrorMessage(conn));
00118 goto failed;
00119 }
00120
00121 info(0, "PGSQL: Connected to server at '%s'.", octstr_get_cstr(conf->host));
00122
00123 return conn;
00124
00125 failed:
00126 PQfinish(conn);
00127 return NULL;
00128 }
00129
00130
00131 static void pgsql_close_conn(void *conn)
00132 {
00133 if (conn == NULL)
00134 return;
00135
00136 PQfinish(conn);
00137 return;
00138 }
00139
00140
00141 static int pgsql_check_conn(void *conn)
00142 {
00143 if (conn == NULL)
00144 return -1;
00145
00146 if (PQstatus(conn) == CONNECTION_BAD) {
00147 error(0, "PGSQL: Database check failed!");
00148 error(0, "PGSQL: %s", PQerrorMessage(conn));
00149 return -1;
00150 }
00151
00152 return 0;
00153 }
00154
00155
00156 static void pgsql_conf_destroy(DBConf *db_conf)
00157 {
00158 PgSQLConf *conf = db_conf->pgsql;
00159
00160 octstr_destroy(conf->host);
00161 octstr_destroy(conf->username);
00162 octstr_destroy(conf->password);
00163 octstr_destroy(conf->database);
00164
00165 gw_free(conf);
00166 gw_free(db_conf);
00167 }
00168
00169
00170 static int pgsql_update(void *theconn, const Octstr *sql, List *binds)
00171 {
00172 int rows;
00173 PGresult *res = NULL;
00174 PGconn *conn = (PGconn*) theconn;
00175
00176 res = PQexec(conn, octstr_get_cstr(sql));
00177 if (res == NULL)
00178 return -1;
00179
00180 switch (PQresultStatus(res)) {
00181 case PGRES_BAD_RESPONSE:
00182 case PGRES_NONFATAL_ERROR:
00183 case PGRES_FATAL_ERROR:
00184 error(0, "PGSQL: %s", octstr_get_cstr(sql));
00185 error(0, "PGSQL: %s", PQresultErrorMessage(res));
00186 PQclear(res);
00187 return -1;
00188 default:
00189 break;
00190 }
00191 rows = atoi(PQcmdTuples(res));
00192 PQclear(res);
00193
00194 return rows;
00195 }
00196
00197
00198 static int pgsql_select(void *theconn, const Octstr *sql, List *binds, List **list)
00199 {
00200 int nTuples, nFields, row_loop, field_loop;
00201 PGresult *res = NULL;
00202 List *fields;
00203 PGconn *conn = (PGconn*) theconn;
00204
00205 gw_assert(list != NULL);
00206 *list = NULL;
00207
00208 res = PQexec(conn, octstr_get_cstr(sql));
00209 if (res == NULL)
00210 return -1;
00211
00212 switch (PQresultStatus(res)) {
00213 case PGRES_EMPTY_QUERY:
00214 case PGRES_BAD_RESPONSE:
00215 case PGRES_NONFATAL_ERROR:
00216 case PGRES_FATAL_ERROR:
00217 error(0, "PGSQL: %s", octstr_get_cstr(sql));
00218 error(0, "PGSQL: %s", PQresultErrorMessage(res));
00219 PQclear(res);
00220 return -1;
00221 default:
00222 break;
00223 }
00224
00225 nTuples = PQntuples(res);
00226 nFields = PQnfields(res);
00227 *list = gwlist_create();
00228 for (row_loop = 0; row_loop < nTuples; row_loop++) {
00229 fields = gwlist_create();
00230 for (field_loop = 0; field_loop < nFields; field_loop++) {
00231 if (PQgetisnull(res, row_loop, field_loop))
00232 gwlist_produce(fields, octstr_create(""));
00233 else
00234 gwlist_produce(fields, octstr_create(PQgetvalue(res, row_loop, field_loop)));
00235 }
00236 gwlist_produce(*list, fields);
00237 }
00238 PQclear(res);
00239
00240 return 0;
00241 }
00242
00243
00244 static struct db_ops pgsql_ops = {
00245 .open = pgsql_open_conn,
00246 .close = pgsql_close_conn,
00247 .check = pgsql_check_conn,
00248 .conf_destroy = pgsql_conf_destroy,
00249 .update = pgsql_update,
00250 .select = pgsql_select
00251 };
00252
00253 #endif
00254
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.