80{
81 ByteString conns(_conns, _connslen);
82 const char* regex = "CURSOR *= *[^&;]*[&;]*";
83 if (conns.searches(regex, true)) {
84 ByteString
cursor = conns.substring(regex,
true);
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
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
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";
145 uri.data(), String::valueOf(__cursor).data()
146 );
147
148 PGconn* conn = PQconnectdb(uri.data());
149 if (PQstatus(conn) != CONNECTION_OK) {
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)