4 * ODBC driver module for GnuGk
6 * Copyright (c) 2006, Simon Horne
8 * This work is published under the GNU Public License (GPL)
9 * see file COPYING for details.
10 * We also explicitely grant the right to link this code
11 * with the OpenH323 library.
18 #include <ptlib/podbc.h>
21 /** Class that encapsulates SQL query result for odbc backend.
22 It does not provide any multithread safety, so should be accessed
23 from a single thread at time.
25 class GkodbcResult : public GkSQLResult
28 /// Build the result from SELECT type query
30 /// SELECT type query result
31 PGresult* selectResult
34 /// Build the result from INSERT, DELETE or UPDATE query
36 /// number of rows affected by the query
40 /// Build the empty result and store query execution error information
42 /// odbc specific error code
43 unsigned int errorCode,
44 /// odbc specific error message text
48 virtual ~GkodbcResult();
51 Backend specific error message, if the query failed.
53 virtual PString GetErrorMessage();
56 Backend specific error code, if the query failed.
58 virtual long GetErrorCode();
61 True if rows can be fetched in random access order, false if
62 rows have to be fethed sequentially and can be retrieved only once.
64 virtual bool HasRandomAccess();
66 /** Fetch a single row from the result set. After each row is fetched,
67 cursor position is moved to a next row.
70 True if the row has been fetched, false if no more rows are available.
72 virtual bool FetchRow(
73 /// array to be filled with string representations of the row fields
76 virtual bool FetchRow(
77 /// array to be filled with string representations of the row fields
82 True if the column at the index #fieldOffset# is NULL in the row
83 fetched most recently.
85 virtual bool IsNullField(
86 /// index of the column to check
90 /** Fetch a single row from the result set. This function requires
91 that the backend supports random row access.
94 True if the row has been fetched, false if a row at the given offset
95 does not exists or SQL backend does not support random row access.
97 virtual bool FetchRow(
98 /// array to be filled with string representations of the row fields
100 /// index (0 based) of the row to fetch
103 virtual bool FetchRow(
104 /// array to be filled with string representations of the row fields
106 /// index (0 based) of the row to fetch
111 True if the column at the index #fieldOffset# is NULL in the row
112 at the specified index.
114 virtual bool IsNullField(
115 /// index of the column to check
117 /// index (0 based) of the row to check
123 GkodbcResult(const GkodbcResult&);
124 GkodbcResult& operator=(const GkodbcResult&);
127 /// query result for SELECT type queries, NULL otherwise
128 PGresult* m_sqlResult;
129 /// the most recent row returned by fetch operation
131 /// PgSQL specific error code (if the query failed)
132 unsigned int m_errorCode;
133 /// PgSQL specific error message text (if the query failed)
134 PString m_errorMessage;
137 /// odbc backend connection implementation.
138 class GkodbcConnection : public GkSQLConnection
141 /// Build a new PgSQL connection object
143 /// name to use in the log
144 const char* name = "odbc"
147 virtual ~GkodbcConnection();
150 class odbcConnWrapper : public GkSQLConnection::SQLConnWrapper
154 /// unique identifier for this connection
156 /// host:port this connection is made to
158 /// odbc connection object
160 ) : SQLConnWrapper(id, host), m_conn(conn) {}
162 virtual ~odbcConnWrapper();
166 odbcConnWrapper(const odbcConnWrapper&);
167 odbcConnWrapper& operator=(const odbcConnWrapper&);
173 /** Create a new SQL connection using parameters stored in this object.
174 When the connection is to be closed, the object is simply deleted
175 using delete operator.
178 NULL if database connection could not be established
179 or an object of odbcConnWrapper class.
181 virtual SQLConnPtr CreateNewConnection(
182 /// unique identifier for this connection
186 /** Execute the query using specified SQL connection.
189 Query execution result.
191 virtual GkSQLResult* ExecuteQuery(
192 /// SQL connection to use for query execution
195 const char* queryStr,
196 /// maximum time (ms) for the query execution, -1 means infinite
200 /** Escape any special characters in the string, so it can be used in a SQL query.
205 virtual PString EscapeString(
206 /// SQL connection to get escaping parameters from
208 /// string to be escaped
213 GkodbcConnection(const GkodbcConnection&);
214 GkodbcConnection& operator=(const GkodbcConnection&);
218 GkodbcResult::GkodbcResult(
219 /// SELECT type query result
220 PGresult* selectResult
222 : GkSQLResult(false), m_sqlResult(selectResult), m_sqlRow(-1),
226 m_numRows = PQntuples(m_sqlResult);
227 m_numFields = PQnfields(m_sqlResult);
234 GkodbcResult::GkodbcResult(
235 /// number of rows affected by the query
238 : GkSQLResult(false), m_sqlResult(NULL), m_sqlRow(-1),
241 m_numRows = numRowsAffected;
242 m_selectType = false;
245 GkodbcResult::GkodbcResult(
246 /// odbc specific error code
247 unsigned int errorCode,
248 /// odbc specific error message text
251 : GkSQLResult(true), m_sqlResult(NULL), m_sqlRow(-1),
252 m_errorCode(errorCode), m_errorMessage(errorMsg)
256 GkodbcResult::~GkodbcResult()
260 bool GkodbcResult::HasRandomAccess()
265 PString GkodbcResult::GetErrorMessage()
267 return m_errorMessage;
270 long GkodbcResult::GetErrorCode()
275 bool GkodbcResult::FetchRow(
276 /// array to be filled with string representations of the row fields
283 bool GkodbcResult::FetchRow(
284 /// array to be filled with string representations of the row fields
291 bool GkodbcResult::IsNullField(
292 /// index of the column to check
299 bool GkodbcResult::FetchRow(
300 /// array to be filled with string representations of the row fields
301 PStringArray& result,
302 /// index (0 based) of the row to fetch
309 bool GkodbcResult::FetchRow(
310 /// array to be filled with string representations of the row fields
312 /// index (0 based) of the row to fetch
319 bool GkodbcResult::IsNullField(
320 /// index of the column to check
322 /// index (0 based) of the row to check
330 GkodbcConnection::GkodbcConnection(
331 /// name to use in the log
333 ) : GkSQLConnection(name)
337 GkodbcConnection::~GkodbcConnection()
341 GkodbcConnection::odbcConnWrapper::~odbcConnWrapper()
345 GkSQLConnection::SQLConnPtr GkodbcConnection::CreateNewConnection(
346 /// unique identifier for this connection
353 GkSQLResult* GkodbcConnection::ExecuteQuery(
354 /// SQL connection to use for query execution
355 GkSQLConnection::SQLConnPtr conn,
357 const char* queryStr,
358 /// maximum time (ms) for the query execution, -1 means infinite
363 return new GkodbcResult(resultInfo, PQresultErrorMessage(result));
367 PString GkodbcConnection::EscapeString(
368 /// SQL connection to get escaping parameters from
370 /// string to be escaped
380 GkSQLCreator<GkodbcConnection> PgSQLCreator("odbc");
383 #endif /* HAS_PGSQL */