DCL 3.7.4
Loading...
Searching...
No Matches
PqConnection Class Reference

#include <PqConnection.h>

Inheritance diagram for PqConnection:
SQL::Connection Object

Public Member Functions

PGconn * connHandle () const
int cursor () const
int stmtNo ()
void setErrorMessage (const ByteString &_message, const wchar_t *_filename, int _line)
virtual void destroy ()
 PqConnection (const wchar_t *_serverTitle)
virtual ~PqConnection ()
virtual bool __open (const char *_conns, size_t _connslen)
virtual bool __close ()
virtual bool __execute (const char *_sql, size_t _sqllen)
virtual bool __startTrans ()
virtual bool __commitTrans ()
virtual bool __rollbackTrans ()
virtual bool __createQueryInstance (SQL::Query **_queryHandleOut)
virtual bool __getErrorMessage (char *_buf, size_t *_buflen)
virtual bool __getServerInfo (char *_buf, size_t *_buflen)
Public Member Functions inherited from SQL::Connection
long refCount () const
long addRef ()
long release ()
bool open (const char *_conn, size_t _connlen)
bool close ()
bool execute (const char *_sql, size_t _sqllen)
bool startTrans ()
bool commitTrans ()
bool rollbackTrans ()
bool getErrorMessage (char *_buf, size_t *_buflen)
bool getServerInfo (char *_buf, size_t *_buflen)
bool createQueryInstance (Query **_queryHandle)
bool destroyQueryInstance (Query *_queryHandle)
Error errorCode () const
bool canTransact () const
const wchar_t * serverTitle () const
bool inState (unsigned int _state) const
Public Member Functions inherited from Object
virtual String toString () const
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const

Additional Inherited Members

Public Types inherited from SQL::Connection
enum  State { stClosed = 0x0001 , stOpenned = 0x0002 , stInTransaction = 0x0004 }
Static Public Member Functions inherited from SQL::Connection
static size_t splitConnStr (const char *_connstr, size_t _strlen, ListedByteStringToByteStringMap &_results)
Public Attributes inherited from SQL::Connection
_PROTECTED : void setErrorStatus( Error _errorCode
_PROTECTED const wchar_t * _filename
_PROTECTED const wchar_t int _line
Protected Member Functions inherited from SQL::Connection
 Connection (const wchar_t *_serverTitle)
virtual ~Connection ()
Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()
Protected Attributes inherited from SQL::Connection
bool __canTransact
const wchar_t * __serverTitle
Error __errorCode
const wchar_t * __errorFileName
int __errorLine
unsigned int __states

Detailed Description

Definition at line 6 of file PqConnection.h.

Constructor & Destructor Documentation

◆ PqConnection()

PqConnection::PqConnection ( const wchar_t * _serverTitle)

◆ ~PqConnection()

PqConnection::~PqConnection ( )
virtual

Definition at line 58 of file PqConnection.cpp.

59{
60 if (__conn) {
61 __DCL_TRACE0_N(L"Warning!! The connection was not closed\n");
62 close();
63 }
64}
#define __DCL_TRACE0_N(fmt)

Member Function Documentation

◆ __close()

bool PqConnection::__close ( )
virtual

Implements SQL::Connection.

Definition at line 170 of file PqConnection.cpp.

171{
172 if (!__conn) {
174 return false;
175 }
176
177 PQfinish(__conn);
178 __conn = NULL;
179
180 return true;
181}
#define NULL
Definition Config.h:312
#define __SET_ERROR(_errorCode)
Definition SQLCore.cpp:149
@ eNotConnected
Definition SQLCore.h:33

◆ __commitTrans()

bool PqConnection::__commitTrans ( )
virtual

Implements SQL::Connection.

Definition at line 244 of file PqConnection.cpp.

245{
246 return __execute("COMMIT", (size_t)-1);
247}
virtual bool __execute(const char *_sql, size_t _sqllen)

◆ __createQueryInstance()

bool PqConnection::__createQueryInstance ( SQL::Query ** _queryHandleOut)
virtual

Implements SQL::Connection.

Definition at line 254 of file PqConnection.cpp.

255{
256 __DCL_ASSERT(_queryHandleOut != NULL);
257
258 SQL::Query* newQuery = new PqQuery(this);
259 if (!newQuery) {
261 return false;
262 }
263
264 *_queryHandleOut = newQuery;
265
266 return true;
267}
#define __DCL_ASSERT(expr)
Definition Object.h:394
@ eOutOfMemory
Definition SQLCore.h:24

◆ __execute()

bool PqConnection::__execute ( const char * _sql,
size_t _sqllen )
virtual

Implements SQL::Connection.

Definition at line 214 of file PqConnection.cpp.

215{
216 PGresult* res = PQexec(__conn, _sql);
217 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
218 __SET_ERROR_MSG(PQerrorMessage(connHandle()));
219 PQclear(res);
220 return false;
221 }
222 PQclear(res);
223
224 switch(__GetStmtType(_sql)) {
225 case StmtTransBegin :
227 break;
228 case StmtTransEnd :
230 break;
231 case StmtOther :
232 default :
233 ;
234 }
235
236 return true;
237}
#define __SET_ERROR_MSG(_message)
@ StmtTransEnd
@ StmtOther
@ StmtTransBegin
#define __UNSET_STATE(state)
Definition SQLCore.h:483
#define __SET_STATE(state)
Definition SQLCore.h:482
PGconn * connHandle() const

◆ __getErrorMessage()

bool PqConnection::__getErrorMessage ( char * _buf,
size_t * _buflen )
virtual

Implements SQL::Connection.

Definition at line 269 of file PqConnection.cpp.

270{
271 __DCL_ASSERT(Connection::__errorCode == SQL::eServerError);
272 if (__lastErrorMessage.length() < *_buflen) {
273 *_buflen = __lastErrorMessage.length();
274 *(_buf + *_buflen) = '\0';
275 }
276 strncpy(_buf, __lastErrorMessage.data(), *_buflen);
277 return true;
278}
@ eServerError
Definition SQLCore.h:21

◆ __getServerInfo()

bool PqConnection::__getServerInfo ( char * _buf,
size_t * _buflen )
virtual

Implements SQL::Connection.

Definition at line 280 of file PqConnection.cpp.

281{
282 int serverVersion = PQserverVersion(__conn);
283 int protocolVersion =
284#ifdef LIBPQ_HAS_FULL_PROTOCOL_VERSION
285 PQfullProtocolVersion(__conn)
286#else
287 PQprotocolVersion(__conn) * 10000
288#endif
289 ;
290 int libVersion = PQlibVersion();
291
292 ByteString s = ByteString::format("PostgreSQL %d.%d/%d.%d/%d.%d",
293 serverVersion / 10000, serverVersion % 10000,
294 protocolVersion / 10000, protocolVersion % 10000,
295 libVersion / 10000, libVersion % 10000
296 );
297 if (s.length() < *_buflen) {
298 *_buflen = s.length();
299 *(_buf + *_buflen) = '\0';
300 }
301 strncpy(_buf, s.data(), *_buflen);
302 return true;
303}

◆ __open()

bool PqConnection::__open ( const char * _conns,
size_t _connslen )
virtual

Implements SQL::Connection.

Definition at line 79 of file PqConnection.cpp.

80{
81 ByteString conns(_conns, _connslen);
82 const char* regex = "CURSOR *= *[^&;]*[&;]*";
83 if (conns.searches(regex, true)) {
84 ByteString cursor = conns.substring(regex, true);
85 cursor = cursor.substring(cursor.indexOf('=') + 1).trim();
86 char* endptr;
87 __cursor = strtol(cursor, &endptr, 10);
88
89 conns = conns.replace_r(regex, "", true);
90 }
91
92 ListedByteStringToByteStringMap map;
93 Connection::splitConnStr(conns, conns.length(), map);
94
95 ByteString _USER = map["USER"];
96 ByteString _PASSWORD = map["PASSWORD"];
97 ByteString _SERVER = map["SERVER"];
98 ByteString _PORT = map["PORT"];
99 ByteString _DATABASE = map["DATABASE"];
100 ByteString _CONNECT_TIMEOUT = map["CONNECT_TIMEOUT"];
101 ByteString _APPLICATION = map["APPLICATION"];
102
103 ByteString uri;
104 if (!(_USER.isEmpty() || _SERVER.isEmpty() || _DATABASE.isEmpty())) {
105 // URI를 만든다.
106 ByteStringBuilder sb("postgresql://");
107 sb.append(_USER);
108 if (!_PASSWORD.isEmpty()) {
109 sb.append(":").append(_PASSWORD);
110 }
111 sb.append("@").append(_SERVER);
112 if (!_PORT.isEmpty()) {
113 sb.append(":").append(_PORT);
114 }
115 sb.append("/").append(_DATABASE)
116 .append("?connect_timeout=")
117 .append(_CONNECT_TIMEOUT.isEmpty() ? "10" : _CONNECT_TIMEOUT)
118 .append("&application_name=")
119 .append(_APPLICATION.isEmpty() ? "DCL" : _APPLICATION)
120 ;
121 uri = sb.toByteString();
122 }
123 else {
124 // _conns가 URI이다.
125 uri = conns.replace_r("client_encoding=[^&]*&*", "", true);
126 if (uri.indexOf('?') == (size_t)-1) {
127 uri = uri + '?';
128 }
129 regex = "connect_timeout=[^&]*&*";
130 if (!uri.searches(regex, true)) {
131 uri = uri + "&connect_timeout=10";
132 }
133 regex = "application_name=[^&]*&*";
134 if (!uri.searches(regex, true)) {
135 uri = uri + "&application_name=DCL";
136 }
137
138 uri = uri.replace_r("&{2,}", "&", true)
139 .replace_r("\\?&", "?", true)
140 .trimRight("&")
141 ;
142 }
143 uri = uri + "&client_encoding=UTF8";
144 __DCL_TRACE2_N(L"connection URI [%hs] cursor[%ls]\n",
145 uri.data(), String::valueOf(__cursor).data()
146 );
147
148 PGconn* conn = PQconnectdb(uri.data());
149 if (PQstatus(conn) != CONNECTION_OK) {
150 __SET_ERROR_MSG(PQerrorMessage(conn));
151 return false;
152 }
153
154 __conn = conn;
155
156 if (!__execute("SET DateStyle TO 'ISO'; SET IntervalStyle TO 'iso_8601'", (size_t)-1))
157 return false;
158
159 __DCL_TRACE1_N(L"client_encoding [%hs]\n", PQparameterStatus(conn, "client_encoding"));
160 __DCL_TRACE1_N(L"DateStyle [%hs]\n", PQparameterStatus(conn, "DateStyle"));
161 __DCL_TRACE1_N(L"integer_datetimes [%hs]\n", PQparameterStatus(conn, "integer_datetimes"));
162 __DCL_TRACE1_N(L"IntervalStyle [%hs]\n", PQparameterStatus(conn, "IntervalStyle"));;
163 __DCL_TRACE1_N(L"server_encoding [%hs]\n", PQparameterStatus(conn, "server_encoding"));
164 __DCL_TRACE1_N(L"server_version [%hs]\n", PQparameterStatus(conn, "server_version"));
165 __DCL_TRACE1_N(L"TimeZone [%hs]\n", PQparameterStatus(conn, "TimeZone"));
166
167 return true;
168}
#define __DCL_TRACE1_N(fmt, arg)
#define __DCL_TRACE2_N(fmt, arg1, arg2)
int cursor() const

◆ __rollbackTrans()

bool PqConnection::__rollbackTrans ( )
virtual

Implements SQL::Connection.

Definition at line 249 of file PqConnection.cpp.

250{
251 return __execute("ROLLBACK", (size_t)-1);
252}

◆ __startTrans()

bool PqConnection::__startTrans ( )
virtual

Implements SQL::Connection.

Definition at line 239 of file PqConnection.cpp.

240{
241 return __execute("BEGIN", (size_t)-1);
242}

◆ connHandle()

PGconn * PqConnection::connHandle ( ) const
inline

Definition at line 41 of file PqConnection.h.

42{
43 return __conn;
44}

◆ cursor()

int PqConnection::cursor ( ) const
inline

Definition at line 46 of file PqConnection.h.

47{
48 return __cursor;
49}

◆ destroy()

void PqConnection::destroy ( )
virtual

파생클래스에서 new 연산자를 override했거나, 추가적인 행위가 필요하다면 이것도 override하라

Implements SQL::Connection.

Definition at line 66 of file PqConnection.cpp.

67{
68 delete this;
69}

◆ setErrorMessage()

void PqConnection::setErrorMessage ( const ByteString & _message,
const wchar_t * _filename,
int _line )
inline

Definition at line 56 of file PqConnection.h.

60{
61 Connection::setErrorStatus(SQL::eServerError, _filename, _line);
62 __lastErrorMessage = _message;
63}
_PROTECTED const wchar_t * _filename
Definition SQLCore.h:439
_PROTECTED const wchar_t int _line
Definition SQLCore.h:441

◆ stmtNo()

int PqConnection::stmtNo ( )
inline

Definition at line 51 of file PqConnection.h.

52{
53 return ++__stmtNo;
54}

The documentation for this class was generated from the following files: