|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #if POSTGRES_ENABLED |
|
22 |
|
23 #include <string.h> |
|
24 #include <oasys/debug/DebugUtils.h> |
|
25 #include <oasys/util/StringBuffer.h> |
|
26 #include "PostgresSQLImplementation.h" |
|
27 |
|
28 namespace dtn { |
|
29 |
|
30 PostgresSQLImplementation::PostgresSQLImplementation() |
|
31 : SQLImplementation("BYTEA", "BOOLEAN"), |
|
32 Logger("/storage/postgresql") |
|
33 { |
|
34 query_result_ = NULL; |
|
35 } |
|
36 |
|
37 int |
|
38 PostgresSQLImplementation::connect(const char* dbName) |
|
39 { |
|
40 char *pghost; |
|
41 char *pgport; |
|
42 char *pgoptions; |
|
43 char *pgtty; |
|
44 |
|
45 log_debug("connecting to database %s", dbName); |
|
46 |
|
47 /* |
|
48 * begin, by setting the parameters for a backend connection if |
|
49 * the parameters are null, then the system will try to use |
|
50 * reasonable defaults by looking up environment variables or, |
|
51 * failing that, using hardwired constants |
|
52 */ |
|
53 |
|
54 pghost = NULL; /* host name of the backend server */ |
|
55 pgport = NULL; /* port of the backend server */ |
|
56 pgoptions = NULL; /* special options to start up the backend |
|
57 * server */ |
|
58 pgtty = NULL; /* debugging tty for the backend server */ |
|
59 |
|
60 /** |
|
61 * make a connection to the database |
|
62 * |
|
63 */ |
|
64 |
|
65 db_ = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); |
|
66 |
|
67 /** |
|
68 * check to see that the backend connection was successfully made |
|
69 */ |
|
70 if (PQstatus(db_) == CONNECTION_BAD) |
|
71 { |
|
72 log_err("connection to database '%s' failed: %s", |
|
73 dbName, PQerrorMessage(db_)); |
|
74 return -1; |
|
75 } |
|
76 |
|
77 return 0; |
|
78 } |
|
79 |
|
80 int |
|
81 PostgresSQLImplementation::close() |
|
82 { |
|
83 PQfinish(db_); |
|
84 return 0; |
|
85 } |
|
86 |
|
87 /* |
|
88 size_t |
|
89 PostgresSQLImplementation::get_value_length(int tuple_no, int field_no) |
|
90 { |
|
91 size_t retval; |
|
92 const char* val = get_value(tuple_no,field_no); |
|
93 unescape_binary((const u_char*)val,&retval); |
|
94 return retval; |
|
95 } |
|
96 */ |
|
97 |
|
98 const char* |
|
99 PostgresSQLImplementation::get_value(int tuple_no, int field_no) |
|
100 { |
|
101 const char* ret ; |
|
102 ASSERT(query_result_); |
|
103 ret = PQgetvalue(query_result_, tuple_no, field_no); |
|
104 return ret; |
|
105 } |
|
106 |
|
107 bool |
|
108 PostgresSQLImplementation::has_table(const char* tablename) |
|
109 { |
|
110 bool retval = 0; |
|
111 oasys::StringBuffer query; |
|
112 |
|
113 query.appendf("select * from pg_tables where tablename = '%s'", tablename); |
|
114 int ret = exec_query(query.c_str()); |
|
115 ASSERT(ret == 0); |
|
116 if (num_tuples() == 1) retval = 1; |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
|
121 int |
|
122 PostgresSQLImplementation::num_tuples() |
|
123 { |
|
124 int ret = -1; |
|
125 ASSERT(query_result_); |
|
126 ret = PQntuples(query_result_); |
|
127 return ret; |
|
128 } |
|
129 |
|
130 static int |
|
131 status_to_int(ExecStatusType t) |
|
132 { |
|
133 if (t == PGRES_COMMAND_OK) return 0; |
|
134 if (t == PGRES_TUPLES_OK) return 0; |
|
135 if (t == PGRES_EMPTY_QUERY) return 0; |
|
136 return -1; |
|
137 } |
|
138 |
|
139 int |
|
140 PostgresSQLImplementation::exec_query(const char* query) |
|
141 { |
|
142 int ret = -1; |
|
143 |
|
144 if (query_result_ != NULL) { |
|
145 PQclear(query_result_); |
|
146 query_result_ = NULL; |
|
147 } |
|
148 |
|
149 query_result_ = PQexec(db_, query); |
|
150 ASSERT(query_result_); |
|
151 ExecStatusType t = PQresultStatus(query_result_); |
|
152 ret = status_to_int(t); |
|
153 |
|
154 return ret; |
|
155 } |
|
156 |
|
157 const char* |
|
158 PostgresSQLImplementation::escape_string(const char* from) |
|
159 { |
|
160 int length = strlen(from); |
|
161 char* to = (char *) malloc(2*length+1); |
|
162 PQescapeString (to,from,length); |
|
163 return to; |
|
164 } |
|
165 |
|
166 const u_char* |
|
167 PostgresSQLImplementation::escape_binary(const u_char* from, int from_length) |
|
168 { |
|
169 size_t to_length; |
|
170 u_char* from1 = (u_char *) from ; |
|
171 u_char* to = PQescapeBytea(from1,from_length,&to_length); |
|
172 return to; |
|
173 } |
|
174 |
|
175 const u_char* |
|
176 PostgresSQLImplementation::unescape_binary(const u_char* from) |
|
177 { |
|
178 u_char* from1 = (u_char *) from ; |
|
179 size_t to_length ; |
|
180 const u_char* to = PQunescapeBytea(from1,&to_length); |
|
181 return to; |
|
182 } |
|
183 |
|
184 } // namespace dtn |
|
185 |
|
186 #endif /* POSTGRES_ENABLED */ |