| 1 |
/* src/interfaces/ecpg/ecpglib/connect.c */ |
| 2 |
|
| 3 |
#define POSTGRES_ECPG_INTERNAL |
| 4 |
#include "postgres_fe.h" |
| 5 |
|
| 6 |
#include "ecpg-pthread-win32.h" |
| 7 |
#include "ecpgerrno.h" |
| 8 |
#include "ecpglib.h" |
| 9 |
#include "ecpglib_extern.h" |
| 10 |
#include "ecpgtype.h" |
| 11 |
#include "sqlca.h" |
| 12 |
|
| 13 |
#ifdef HAVE_USELOCALE |
| 14 |
locale_t ecpg_clocale = (locale_t) 0; |
| 15 |
#endif |
| 16 |
|
| 17 |
static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 18 |
static pthread_key_t actual_connection_key; |
| 19 |
static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT; |
| 20 |
static struct connection *actual_connection = NULL; |
| 21 |
static struct connection *all_connections = NULL; |
| 22 |
|
| 23 |
static void |
| 24 |
ecpg_actual_connection_init(void) |
| 25 |
{ |
| 26 |
pthread_key_create(&actual_connection_key, NULL); |
| 27 |
} |
| 28 |
|
| 29 |
void |
| 30 |
ecpg_pthreads_init(void) |
| 31 |
{ |
| 32 |
pthread_once(&actual_connection_key_once, ecpg_actual_connection_init); |
| 33 |
} |
| 34 |
|
| 35 |
static struct connection * |
| 36 |
ecpg_get_connection_nr(const char *connection_name) |
| 37 |
{ |
| 38 |
struct connection *ret = NULL; |
| 39 |
|
| 40 |
if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) |
| 41 |
{ |
| 42 |
ecpg_pthreads_init(); /* ensure actual_connection_key is valid */ |
| 43 |
|
| 44 |
ret = pthread_getspecific(actual_connection_key); |
| 45 |
|
| 46 |
/* |
| 47 |
* if no connection in TSD for this thread, get the global default |
| 48 |
* connection and hope the user knows what they're doing (i.e. using |
| 49 |
* their own mutex to protect that connection from concurrent accesses |
| 50 |
*/ |
| 51 |
if (ret == NULL) |
| 52 |
/* no TSD connection, going for global */ |
| 53 |
ret = actual_connection; |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
struct connection *con; |
| 58 |
|
| 59 |
for (con = all_connections; con != NULL; con = con->next) |
| 60 |
{ |
| 61 |
/* |
| 62 |
* Check for the case of a NULL connection name, stored as such in |
| 63 |
* the connection information by ECPGconnect() when the database |
| 64 |
* name is not specified by its caller. |
| 65 |
*/ |
| 66 |
if (con->name != NULL && strcmp(connection_name, con->name) == 0) |
| 67 |
break; |
| 68 |
} |
| 69 |
ret = con; |
| 70 |
} |
| 71 |
|
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
struct connection * |
| 76 |
ecpg_get_connection(const char *connection_name) |
| 77 |
{ |
| 78 |
struct connection *ret = NULL; |
| 79 |
|
| 80 |
if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) |
| 81 |
{ |
| 82 |
ecpg_pthreads_init(); /* ensure actual_connection_key is valid */ |
| 83 |
|
| 84 |
ret = pthread_getspecific(actual_connection_key); |
| 85 |
|
| 86 |
/* |
| 87 |
* if no connection in TSD for this thread, get the global default |
| 88 |
* connection and hope the user knows what they're doing (i.e. using |
| 89 |
* their own mutex to protect that connection from concurrent accesses |
| 90 |
*/ |
| 91 |
if (ret == NULL) |
| 92 |
/* no TSD connection here either, using global */ |
| 93 |
ret = actual_connection; |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
pthread_mutex_lock(&connections_mutex); |
| 98 |
|
| 99 |
ret = ecpg_get_connection_nr(connection_name); |
| 100 |
|
| 101 |
pthread_mutex_unlock(&connections_mutex); |
| 102 |
} |
| 103 |
|
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
ecpg_finish(struct connection *act) |
| 109 |
{ |
| 110 |
if (act != NULL) |
| 111 |
{ |
| 112 |
struct ECPGtype_information_cache *cache, |
| 113 |
*ptr; |
| 114 |
|
| 115 |
ecpg_deallocate_all_conn(0, ECPG_COMPAT_PGSQL, act); |
| 116 |
PQfinish(act->connection); |
| 117 |
|
| 118 |
/* |
| 119 |
* no need to lock connections_mutex - we're always called by |
| 120 |
* ECPGdisconnect or ECPGconnect, which are holding the lock |
| 121 |
*/ |
| 122 |
|
| 123 |
/* remove act from the list */ |
| 124 |
if (act == all_connections) |
| 125 |
all_connections = act->next; |
| 126 |
else |
| 127 |
{ |
| 128 |
struct connection *con; |
| 129 |
|
| 130 |
for (con = all_connections; con->next && con->next != act; con = con->next); |
| 131 |
if (con->next) |
| 132 |
con->next = act->next; |
| 133 |
} |
| 134 |
|
| 135 |
if (pthread_getspecific(actual_connection_key) == act) |
| 136 |
pthread_setspecific(actual_connection_key, all_connections); |
| 137 |
if (actual_connection == act) |
| 138 |
actual_connection = all_connections; |
| 139 |
|
| 140 |
ecpg_log("ecpg_finish: connection %s closed\n", act->name ? act->name : "(null)"); |
| 141 |
|
| 142 |
for (cache = act->cache_head; cache; ptr = cache, cache = cache->next, ecpg_free(ptr)); |
| 143 |
ecpg_free(act->name); |
| 144 |
ecpg_free(act); |
| 145 |
/* delete cursor variables when last connection gets closed */ |
| 146 |
if (all_connections == NULL) |
| 147 |
{ |
| 148 |
struct var_list *iv_ptr; |
| 149 |
|
| 150 |
for (; ivlist; iv_ptr = ivlist, ivlist = ivlist->next, ecpg_free(iv_ptr)); |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
ecpg_log("ecpg_finish: called an extra time\n"); |
| 155 |
} |
| 156 |
|
| 157 |
bool |
| 158 |
ECPGsetcommit(int lineno, const char *mode, const char *connection_name) |
| 159 |
{ |
| 160 |
struct connection *con = ecpg_get_connection(connection_name); |
| 161 |
PGresult *results; |
| 162 |
|
| 163 |
if (!ecpg_init(con, connection_name, lineno)) |
| 164 |
return false; |
| 165 |
|
| 166 |
ecpg_log("ECPGsetcommit on line %d: action \"%s\"; connection \"%s\"\n", lineno, mode, con->name); |
| 167 |
|
| 168 |
if (con->autocommit && strncmp(mode, "off", strlen("off")) == 0) |
| 169 |
{ |
| 170 |
if (PQtransactionStatus(con->connection) == PQTRANS_IDLE) |
| 171 |
{ |
| 172 |
results = PQexec(con->connection, "begin transaction"); |
| 173 |
if (!ecpg_check_PQresult(results, lineno, con->connection, ECPG_COMPAT_PGSQL)) |
| 174 |
return false; |
| 175 |
PQclear(results); |
| 176 |
} |
| 177 |
con->autocommit = false; |
| 178 |
} |
| 179 |
else if (!con->autocommit && strncmp(mode, "on", strlen("on")) == 0) |
| 180 |
{ |
| 181 |
if (PQtransactionStatus(con->connection) != PQTRANS_IDLE) |
| 182 |
{ |
| 183 |
results = PQexec(con->connection, "commit"); |
| 184 |
if (!ecpg_check_PQresult(results, lineno, con->connection, ECPG_COMPAT_PGSQL)) |
| 185 |
return false; |
| 186 |
PQclear(results); |
| 187 |
} |
| 188 |
con->autocommit = true; |
| 189 |
} |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
bool |
| 195 |
ECPGsetconn(int lineno, const char *connection_name) |
| 196 |
{ |
| 197 |
struct connection *con = ecpg_get_connection(connection_name); |
| 198 |
|
| 199 |
if (!ecpg_init(con, connection_name, lineno)) |
| 200 |
return false; |
| 201 |
|
| 202 |
pthread_setspecific(actual_connection_key, con); |
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
static void |
| 208 |
ECPGnoticeReceiver(void *arg, const PGresult *result) |
| 209 |
{ |
| 210 |
char *sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE); |
| 211 |
char *message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY); |
| 212 |
struct sqlca_t *sqlca = ECPGget_sqlca(); |
| 213 |
int sqlcode; |
| 214 |
|
| 215 |
if (sqlca == NULL) |
| 216 |
{ |
| 217 |
ecpg_log("out of memory"); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
(void) arg; /* keep the compiler quiet */ |
| 222 |
if (sqlstate == NULL) |
| 223 |
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR; |
| 224 |
|
| 225 |
if (message == NULL) /* Shouldn't happen, but need to be sure */ |
| 226 |
message = ecpg_gettext("empty message text"); |
| 227 |
|
| 228 |
/* these are not warnings */ |
| 229 |
if (strncmp(sqlstate, "00", 2) == 0) |
| 230 |
return; |
| 231 |
|
| 232 |
ecpg_log("ECPGnoticeReceiver: %s\n", message); |
| 233 |
|
| 234 |
/* map to SQLCODE for backward compatibility */ |
| 235 |
if (strcmp(sqlstate, ECPG_SQLSTATE_INVALID_CURSOR_NAME) == 0) |
| 236 |
sqlcode = ECPG_WARNING_UNKNOWN_PORTAL; |
| 237 |
else if (strcmp(sqlstate, ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION) == 0) |
| 238 |
sqlcode = ECPG_WARNING_IN_TRANSACTION; |
| 239 |
else if (strcmp(sqlstate, ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION) == 0) |
| 240 |
sqlcode = ECPG_WARNING_NO_TRANSACTION; |
| 241 |
else if (strcmp(sqlstate, ECPG_SQLSTATE_DUPLICATE_CURSOR) == 0) |
| 242 |
sqlcode = ECPG_WARNING_PORTAL_EXISTS; |
| 243 |
else |
| 244 |
sqlcode = 0; |
| 245 |
|
| 246 |
strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate)); |
| 247 |
sqlca->sqlcode = sqlcode; |
| 248 |
sqlca->sqlwarn[2] = 'W'; |
| 249 |
sqlca->sqlwarn[0] = 'W'; |
| 250 |
|
| 251 |
strncpy(sqlca->sqlerrm.sqlerrmc, message, sizeof(sqlca->sqlerrm.sqlerrmc)); |
| 252 |
sqlca->sqlerrm.sqlerrmc[sizeof(sqlca->sqlerrm.sqlerrmc) - 1] = 0; |
| 253 |
sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc); |
| 254 |
|
| 255 |
ecpg_log("raising sqlcode %d\n", sqlcode); |
| 256 |
} |
| 257 |
|
| 258 |
/* this contains some quick hacks, needs to be cleaned up, but it works */ |
| 259 |
bool |
| 260 |
ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit) |
| 261 |
{ |
| 262 |
struct sqlca_t *sqlca = ECPGget_sqlca(); |
| 263 |
enum COMPAT_MODE compat = c; |
| 264 |
struct connection *this; |
| 265 |
int i, |
| 266 |
connect_params = 0; |
| 267 |
char *dbname = name ? ecpg_strdup(name, lineno) : NULL, |
| 268 |
*host = NULL, |
| 269 |
*tmp, |
| 270 |
*port = NULL, |
| 271 |
*realname = NULL, |
| 272 |
*options = NULL; |
| 273 |
const char **conn_keywords; |
| 274 |
const char **conn_values; |
| 275 |
|
| 276 |
if (sqlca == NULL) |
| 277 |
{ |
| 278 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, |
| 279 |
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 280 |
ecpg_free(dbname); |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
ecpg_init_sqlca(sqlca); |
| 285 |
|
| 286 |
/* |
| 287 |
* clear auto_mem structure because some error handling functions might |
| 288 |
* access it |
| 289 |
*/ |
| 290 |
ecpg_clear_auto_mem(); |
| 291 |
|
| 292 |
if (INFORMIX_MODE(compat)) |
| 293 |
{ |
| 294 |
char *envname; |
| 295 |
|
| 296 |
/* |
| 297 |
* Informix uses an environment variable DBPATH that overrides the |
| 298 |
* connection parameters given here. We do the same with PG_DBPATH as |
| 299 |
* the syntax is different. |
| 300 |
*/ |
| 301 |
envname = getenv("PG_DBPATH"); |
| 302 |
if (envname) |
| 303 |
{ |
| 304 |
ecpg_free(dbname); |
| 305 |
dbname = ecpg_strdup(envname, lineno); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if (dbname == NULL && connection_name == NULL) |
| 310 |
connection_name = "DEFAULT"; |
| 311 |
|
| 312 |
ecpg_pthreads_init(); |
| 313 |
|
| 314 |
/* check if the identifier is unique */ |
| 315 |
if (ecpg_get_connection(connection_name)) |
| 316 |
{ |
| 317 |
ecpg_free(dbname); |
| 318 |
ecpg_log("ECPGconnect: connection identifier %s is already in use\n", |
| 319 |
connection_name); |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
if ((this = (struct connection *) ecpg_alloc(sizeof(struct connection), lineno)) == NULL) |
| 324 |
{ |
| 325 |
ecpg_free(dbname); |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
if (dbname != NULL) |
| 330 |
{ |
| 331 |
/* get the detail information from dbname */ |
| 332 |
if (strncmp(dbname, "tcp:", 4) == 0 || strncmp(dbname, "unix:", 5) == 0) |
| 333 |
{ |
| 334 |
int offset = 0; |
| 335 |
|
| 336 |
/* |
| 337 |
* only allow protocols tcp and unix |
| 338 |
*/ |
| 339 |
if (strncmp(dbname, "tcp:", 4) == 0) |
| 340 |
offset = 4; |
| 341 |
else if (strncmp(dbname, "unix:", 5) == 0) |
| 342 |
offset = 5; |
| 343 |
|
| 344 |
if (strncmp(dbname + offset, "postgresql://", strlen("postgresql://")) == 0) |
| 345 |
{ |
| 346 |
|
| 347 |
/*------ |
| 348 |
* new style: |
| 349 |
* <tcp|unix>:postgresql://server[:port][/db-name][?options] |
| 350 |
*------ |
| 351 |
*/ |
| 352 |
offset += strlen("postgresql://"); |
| 353 |
|
| 354 |
tmp = strrchr(dbname + offset, '?'); |
| 355 |
if (tmp != NULL) /* options given */ |
| 356 |
{ |
| 357 |
options = ecpg_strdup(tmp + 1, lineno); |
| 358 |
*tmp = '\0'; |
| 359 |
} |
| 360 |
|
| 361 |
tmp = last_dir_separator(dbname + offset); |
| 362 |
if (tmp != NULL) /* database name given */ |
| 363 |
{ |
| 364 |
if (tmp[1] != '\0') /* non-empty database name */ |
| 365 |
{ |
| 366 |
realname = ecpg_strdup(tmp + 1, lineno); |
| 367 |
connect_params++; |
| 368 |
} |
| 369 |
*tmp = '\0'; |
| 370 |
} |
| 371 |
|
| 372 |
tmp = strrchr(dbname + offset, ':'); |
| 373 |
if (tmp != NULL) /* port number given */ |
| 374 |
{ |
| 375 |
*tmp = '\0'; |
| 376 |
port = ecpg_strdup(tmp + 1, lineno); |
| 377 |
connect_params++; |
| 378 |
} |
| 379 |
|
| 380 |
if (strncmp(dbname, "unix:", 5) == 0) |
| 381 |
{ |
| 382 |
/* |
| 383 |
* The alternative of using "127.0.0.1" here is deprecated |
| 384 |
* and undocumented; we'll keep it for backward |
| 385 |
* compatibility's sake, but not extend it to allow IPv6. |
| 386 |
*/ |
| 387 |
if (strcmp(dbname + offset, "localhost") != 0 && |
| 388 |
strcmp(dbname + offset, "127.0.0.1") != 0) |
| 389 |
{ |
| 390 |
ecpg_log("ECPGconnect: non-localhost access via sockets on line %d\n", lineno); |
| 391 |
ecpg_raise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, realname ? realname : ecpg_gettext("<DEFAULT>")); |
| 392 |
if (host) |
| 393 |
ecpg_free(host); |
| 394 |
if (port) |
| 395 |
ecpg_free(port); |
| 396 |
if (options) |
| 397 |
ecpg_free(options); |
| 398 |
if (realname) |
| 399 |
ecpg_free(realname); |
| 400 |
if (dbname) |
| 401 |
ecpg_free(dbname); |
| 402 |
free(this); |
| 403 |
return false; |
| 404 |
} |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
if (*(dbname + offset) != '\0') |
| 409 |
{ |
| 410 |
host = ecpg_strdup(dbname + offset, lineno); |
| 411 |
connect_params++; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
else |
| 417 |
{ |
| 418 |
/* old style: dbname[@server][:port] */ |
| 419 |
tmp = strrchr(dbname, ':'); |
| 420 |
if (tmp != NULL) /* port number given */ |
| 421 |
{ |
| 422 |
port = ecpg_strdup(tmp + 1, lineno); |
| 423 |
connect_params++; |
| 424 |
*tmp = '\0'; |
| 425 |
} |
| 426 |
|
| 427 |
tmp = strrchr(dbname, '@'); |
| 428 |
if (tmp != NULL) /* host name given */ |
| 429 |
{ |
| 430 |
host = ecpg_strdup(tmp + 1, lineno); |
| 431 |
connect_params++; |
| 432 |
*tmp = '\0'; |
| 433 |
} |
| 434 |
|
| 435 |
if (strlen(dbname) > 0) |
| 436 |
{ |
| 437 |
realname = ecpg_strdup(dbname, lineno); |
| 438 |
connect_params++; |
| 439 |
} |
| 440 |
else |
| 441 |
realname = NULL; |
| 442 |
} |
| 443 |
} |
| 444 |
else |
| 445 |
realname = NULL; |
| 446 |
|
| 447 |
/* |
| 448 |
* Count options for the allocation done below (this may produce an |
| 449 |
* overestimate, it's ok). |
| 450 |
*/ |
| 451 |
if (options) |
| 452 |
for (i = 0; options[i]; i++) |
| 453 |
if (options[i] == '=') |
| 454 |
connect_params++; |
| 455 |
|
| 456 |
if (user && strlen(user) > 0) |
| 457 |
connect_params++; |
| 458 |
if (passwd && strlen(passwd) > 0) |
| 459 |
connect_params++; |
| 460 |
|
| 461 |
/* |
| 462 |
* Allocate enough space for all connection parameters. These allocations |
| 463 |
* are done before manipulating the list of connections to ease the error |
| 464 |
* handling on failure. |
| 465 |
*/ |
| 466 |
conn_keywords = (const char **) ecpg_alloc((connect_params + 1) * sizeof(char *), lineno); |
| 467 |
conn_values = (const char **) ecpg_alloc(connect_params * sizeof(char *), lineno); |
| 468 |
if (conn_keywords == NULL || conn_values == NULL) |
| 469 |
{ |
| 470 |
if (host) |
| 471 |
ecpg_free(host); |
| 472 |
if (port) |
| 473 |
ecpg_free(port); |
| 474 |
if (options) |
| 475 |
ecpg_free(options); |
| 476 |
if (realname) |
| 477 |
ecpg_free(realname); |
| 478 |
if (dbname) |
| 479 |
ecpg_free(dbname); |
| 480 |
if (conn_keywords) |
| 481 |
ecpg_free(conn_keywords); |
| 482 |
if (conn_values) |
| 483 |
ecpg_free(conn_values); |
| 484 |
free(this); |
| 485 |
return false; |
| 486 |
} |
| 487 |
|
| 488 |
/* add connection to our list */ |
| 489 |
pthread_mutex_lock(&connections_mutex); |
| 490 |
|
| 491 |
/* |
| 492 |
* ... but first, make certain we have created ecpg_clocale. Rely on |
| 493 |
* holding connections_mutex to ensure this is done by only one thread. |
| 494 |
*/ |
| 495 |
#ifdef HAVE_USELOCALE |
| 496 |
if (!ecpg_clocale) |
| 497 |
{ |
| 498 |
ecpg_clocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); |
| 499 |
if (!ecpg_clocale) |
| 500 |
{ |
| 501 |
pthread_mutex_unlock(&connections_mutex); |
| 502 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, |
| 503 |
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 504 |
if (host) |
| 505 |
ecpg_free(host); |
| 506 |
if (port) |
| 507 |
ecpg_free(port); |
| 508 |
if (options) |
| 509 |
ecpg_free(options); |
| 510 |
if (realname) |
| 511 |
ecpg_free(realname); |
| 512 |
if (dbname) |
| 513 |
ecpg_free(dbname); |
| 514 |
if (conn_keywords) |
| 515 |
ecpg_free(conn_keywords); |
| 516 |
if (conn_values) |
| 517 |
ecpg_free(conn_values); |
| 518 |
free(this); |
| 519 |
return false; |
| 520 |
} |
| 521 |
} |
| 522 |
#endif |
| 523 |
|
| 524 |
if (connection_name != NULL) |
| 525 |
this->name = ecpg_strdup(connection_name, lineno); |
| 526 |
else |
| 527 |
this->name = ecpg_strdup(realname, lineno); |
| 528 |
|
| 529 |
this->cache_head = NULL; |
| 530 |
this->prep_stmts = NULL; |
| 531 |
|
| 532 |
if (all_connections == NULL) |
| 533 |
this->next = NULL; |
| 534 |
else |
| 535 |
this->next = all_connections; |
| 536 |
|
| 537 |
all_connections = this; |
| 538 |
pthread_setspecific(actual_connection_key, all_connections); |
| 539 |
actual_connection = all_connections; |
| 540 |
|
| 541 |
ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s %s%s\n", |
| 542 |
realname ? realname : "<DEFAULT>", |
| 543 |
host ? host : "<DEFAULT>", |
| 544 |
port ? (ecpg_internal_regression_mode ? "<REGRESSION_PORT>" : port) : "<DEFAULT>", |
| 545 |
options ? "with options " : "", options ? options : "", |
| 546 |
(user && strlen(user) > 0) ? "for user " : "", user ? user : ""); |
| 547 |
|
| 548 |
i = 0; |
| 549 |
if (realname) |
| 550 |
{ |
| 551 |
conn_keywords[i] = "dbname"; |
| 552 |
conn_values[i] = realname; |
| 553 |
i++; |
| 554 |
} |
| 555 |
if (host) |
| 556 |
{ |
| 557 |
conn_keywords[i] = "host"; |
| 558 |
conn_values[i] = host; |
| 559 |
i++; |
| 560 |
} |
| 561 |
if (port) |
| 562 |
{ |
| 563 |
conn_keywords[i] = "port"; |
| 564 |
conn_values[i] = port; |
| 565 |
i++; |
| 566 |
} |
| 567 |
if (user && strlen(user) > 0) |
| 568 |
{ |
| 569 |
conn_keywords[i] = "user"; |
| 570 |
conn_values[i] = user; |
| 571 |
i++; |
| 572 |
} |
| 573 |
if (passwd && strlen(passwd) > 0) |
| 574 |
{ |
| 575 |
conn_keywords[i] = "password"; |
| 576 |
conn_values[i] = passwd; |
| 577 |
i++; |
| 578 |
} |
| 579 |
if (options) |
| 580 |
{ |
| 581 |
char *str; |
| 582 |
|
| 583 |
/* |
| 584 |
* The options string contains "keyword=value" pairs separated by |
| 585 |
* '&'s. We must break this up into keywords and values to pass to |
| 586 |
* libpq (it's okay to scribble on the options string). We ignore |
| 587 |
* spaces just before each keyword or value. (The preprocessor used |
| 588 |
* to add spaces around '&'s, making it necessary to ignore spaces |
| 589 |
* before keywords here. While it no longer does that, we still must |
| 590 |
* skip spaces to support code compiled with older preprocessors.) |
| 591 |
*/ |
| 592 |
for (str = options; *str;) |
| 593 |
{ |
| 594 |
int e, |
| 595 |
a; |
| 596 |
char *token1, |
| 597 |
*token2; |
| 598 |
|
| 599 |
/* Skip spaces before keyword */ |
| 600 |
for (token1 = str; *token1 == ' '; token1++) |
| 601 |
/* skip */ ; |
| 602 |
/* Find end of keyword */ |
| 603 |
for (e = 0; token1[e] && token1[e] != '='; e++) |
| 604 |
/* skip */ ; |
| 605 |
if (token1[e]) /* found "=" */ |
| 606 |
{ |
| 607 |
token1[e] = '\0'; |
| 608 |
/* Skip spaces before value */ |
| 609 |
for (token2 = token1 + e + 1; *token2 == ' '; token2++) |
| 610 |
/* skip */ ; |
| 611 |
/* Find end of value */ |
| 612 |
for (a = 0; token2[a] && token2[a] != '&'; a++) |
| 613 |
/* skip */ ; |
| 614 |
if (token2[a]) /* found "&" => another option follows */ |
| 615 |
{ |
| 616 |
token2[a] = '\0'; |
| 617 |
str = token2 + a + 1; |
| 618 |
} |
| 619 |
else |
| 620 |
str = token2 + a; |
| 621 |
|
| 622 |
conn_keywords[i] = token1; |
| 623 |
conn_values[i] = token2; |
| 624 |
i++; |
| 625 |
} |
| 626 |
else |
| 627 |
{ |
| 628 |
/* Bogus options syntax ... ignore trailing garbage */ |
| 629 |
str = token1 + e; |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
Assert(i <= connect_params); |
| 635 |
conn_keywords[i] = NULL; /* terminator */ |
| 636 |
|
| 637 |
this->connection = PQconnectdbParams(conn_keywords, conn_values, 0); |
| 638 |
|
| 639 |
if (host) |
| 640 |
ecpg_free(host); |
| 641 |
if (port) |
| 642 |
ecpg_free(port); |
| 643 |
if (options) |
| 644 |
ecpg_free(options); |
| 645 |
if (dbname) |
| 646 |
ecpg_free(dbname); |
| 647 |
ecpg_free(conn_values); |
| 648 |
ecpg_free(conn_keywords); |
| 649 |
|
| 650 |
if (PQstatus(this->connection) == CONNECTION_BAD) |
| 651 |
{ |
| 652 |
const char *errmsg = PQerrorMessage(this->connection); |
| 653 |
const char *db = realname ? realname : ecpg_gettext("<DEFAULT>"); |
| 654 |
|
| 655 |
/* PQerrorMessage's result already has a trailing newline */ |
| 656 |
ecpg_log("ECPGconnect: %s", errmsg); |
| 657 |
|
| 658 |
ecpg_finish(this); |
| 659 |
pthread_mutex_unlock(&connections_mutex); |
| 660 |
|
| 661 |
ecpg_raise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, db); |
| 662 |
if (realname) |
| 663 |
ecpg_free(realname); |
| 664 |
|
| 665 |
return false; |
| 666 |
} |
| 667 |
|
| 668 |
if (realname) |
| 669 |
ecpg_free(realname); |
| 670 |
|
| 671 |
pthread_mutex_unlock(&connections_mutex); |
| 672 |
|
| 673 |
this->autocommit = autocommit; |
| 674 |
|
| 675 |
PQsetNoticeReceiver(this->connection, &ECPGnoticeReceiver, this); |
| 676 |
|
| 677 |
return true; |
| 678 |
} |
| 679 |
|
| 680 |
bool |
| 681 |
ECPGdisconnect(int lineno, const char *connection_name) |
| 682 |
{ |
| 683 |
struct sqlca_t *sqlca = ECPGget_sqlca(); |
| 684 |
struct connection *con; |
| 685 |
|
| 686 |
if (sqlca == NULL) |
| 687 |
{ |
| 688 |
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, |
| 689 |
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL); |
| 690 |
return false; |
| 691 |
} |
| 692 |
|
| 693 |
pthread_mutex_lock(&connections_mutex); |
| 694 |
|
| 695 |
if (strcmp(connection_name, "ALL") == 0) |
| 696 |
{ |
| 697 |
ecpg_init_sqlca(sqlca); |
| 698 |
for (con = all_connections; con;) |
| 699 |
{ |
| 700 |
struct connection *f = con; |
| 701 |
|
| 702 |
con = con->next; |
| 703 |
ecpg_finish(f); |
| 704 |
} |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
con = ecpg_get_connection_nr(connection_name); |
| 709 |
|
| 710 |
if (!ecpg_init(con, connection_name, lineno)) |
| 711 |
{ |
| 712 |
pthread_mutex_unlock(&connections_mutex); |
| 713 |
return false; |
| 714 |
} |
| 715 |
else |
| 716 |
ecpg_finish(con); |
| 717 |
} |
| 718 |
|
| 719 |
pthread_mutex_unlock(&connections_mutex); |
| 720 |
|
| 721 |
return true; |
| 722 |
} |
| 723 |
|
| 724 |
PGconn * |
| 725 |
ECPGget_PGconn(const char *connection_name) |
| 726 |
{ |
| 727 |
struct connection *con; |
| 728 |
|
| 729 |
con = ecpg_get_connection(connection_name); |
| 730 |
if (con == NULL) |
| 731 |
return NULL; |
| 732 |
|
| 733 |
return con->connection; |
| 734 |
} |