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