1
- /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.6 2003/06/13 10:50:57 meskes Exp $ */
1
+ /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.7 2003/06/15 04:07:58 momjian Exp $ */
2
2
3
+ #define POSTGRES_ECPG_INTERNAL
3
4
#include "postgres_fe.h"
4
5
6
+ #ifdef USE_THREADS
7
+ #include <pthread.h>
8
+ #endif
5
9
#include "ecpgtype.h"
6
10
#include "ecpglib.h"
7
11
#include "ecpgerrno.h"
8
12
#include "extern.h"
9
13
#include "sqlca.h"
10
14
11
- static struct connection * all_connections = NULL ,
12
- * actual_connection = NULL ;
15
+ #ifdef USE_THREADS
16
+ static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER ;
17
+ #endif
18
+ static struct connection * all_connections = NULL ;
19
+ static struct connection * actual_connection = NULL ;
13
20
14
21
struct connection *
15
22
ECPGget_connection (const char * connection_name )
16
23
{
17
- struct connection * con = all_connections ;
24
+ struct connection * ret = NULL ;
25
+
26
+ #ifdef USE_THREADS
27
+ pthread_mutex_lock (& connections_mutex );
28
+ #endif
29
+
30
+ if ( (connection_name == NULL ) || (strcmp (connection_name , "CURRENT" ) == 0 ) )
31
+ {
32
+ ret = actual_connection ;
33
+ }
34
+ else
35
+ {
36
+ struct connection * con ;
37
+
38
+ for ( con = all_connections ; con != NULL ; con = con -> next )
39
+ {
40
+ if ( strcmp (connection_name , con -> name ) == 0 )
41
+ break ;
42
+ }
43
+ ret = con ;
44
+ }
18
45
19
- if (connection_name == NULL || strcmp (connection_name , "CURRENT" ) == 0 )
20
- return actual_connection ;
46
+ #ifdef USE_THREADS
47
+ pthread_mutex_unlock (& connections_mutex );
48
+ #endif
21
49
22
- for (; con && strcmp (connection_name , con -> name ) != 0 ; con = con -> next );
23
- if (con )
24
- return con ;
25
- else
26
- return NULL ;
50
+ return ( ret );
27
51
}
28
52
29
53
static void
@@ -37,6 +61,10 @@ ecpg_finish(struct connection * act)
37
61
ECPGlog ("ecpg_finish: finishing %s.\n" , act -> name );
38
62
PQfinish (act -> connection );
39
63
64
+ /* no need to lock connections_mutex - we're always called
65
+ by ECPGdisconnect or ECPGconnect, which are holding
66
+ the lock */
67
+
40
68
/* remove act from the list */
41
69
if (act == all_connections )
42
70
all_connections = act -> next ;
@@ -118,17 +146,18 @@ ECPGsetconn(int lineno, const char *connection_name)
118
146
static void
119
147
ECPGnoticeProcessor_raise (int code , const char * message )
120
148
{
121
- sqlca .sqlcode = code ;
122
- strncpy (sqlca .sqlerrm .sqlerrmc , message , sizeof (sqlca .sqlerrm .sqlerrmc ));
123
- sqlca .sqlerrm .sqlerrmc [sizeof (sqlca .sqlerrm .sqlerrmc ) - 1 ] = 0 ;
124
- sqlca .sqlerrm .sqlerrml = strlen (sqlca .sqlerrm .sqlerrmc );
149
+ struct sqlca_t * sqlca = ECPGget_sqlca ();
150
+ sqlca -> sqlcode = code ;
151
+ strncpy (sqlca -> sqlerrm .sqlerrmc , message , sizeof (sqlca -> sqlerrm .sqlerrmc ));
152
+ sqlca -> sqlerrm .sqlerrmc [sizeof (sqlca -> sqlerrm .sqlerrmc ) - 1 ] = 0 ;
153
+ sqlca -> sqlerrm .sqlerrml = strlen (sqlca -> sqlerrm .sqlerrmc );
125
154
126
155
/* remove trailing newline */
127
- if (sqlca . sqlerrm .sqlerrml
128
- && sqlca . sqlerrm .sqlerrmc [sqlca . sqlerrm .sqlerrml - 1 ] == '\n' )
156
+ if (sqlca -> sqlerrm .sqlerrml
157
+ && sqlca -> sqlerrm .sqlerrmc [sqlca -> sqlerrm .sqlerrml - 1 ] == '\n' )
129
158
{
130
- sqlca . sqlerrm .sqlerrmc [sqlca . sqlerrm .sqlerrml - 1 ] = 0 ;
131
- sqlca . sqlerrm .sqlerrml -- ;
159
+ sqlca -> sqlerrm .sqlerrmc [sqlca -> sqlerrm .sqlerrml - 1 ] = 0 ;
160
+ sqlca -> sqlerrm .sqlerrml -- ;
132
161
}
133
162
134
163
ECPGlog ("raising sqlcode %d\n" , code );
@@ -141,6 +170,8 @@ ECPGnoticeProcessor_raise(int code, const char *message)
141
170
static void
142
171
ECPGnoticeProcessor (void * arg , const char * message )
143
172
{
173
+ struct sqlca_t * sqlca = ECPGget_sqlca ();
174
+
144
175
/* these notices raise an error */
145
176
if (strncmp (message , "WARNING: " , 9 ))
146
177
{
@@ -245,22 +276,23 @@ ECPGnoticeProcessor(void *arg, const char *message)
245
276
if (strstr (message , "cannot be rolled back" ))
246
277
return ;
247
278
248
- /* these and other unmentioned should set sqlca. sqlwarn[2] */
279
+ /* these and other unmentioned should set sqlca-> sqlwarn[2] */
249
280
/* WARNING: The ':' operator is deprecated. Use exp(x) instead. */
250
281
/* WARNING: Rel *: Uninitialized page 0 - fixing */
251
282
/* WARNING: PortalHeapMemoryFree: * not in alloc set! */
252
283
/* WARNING: Too old parent tuple found - can't continue vc_repair_frag */
253
284
/* WARNING: identifier "*" will be truncated to "*" */
254
285
/* WARNING: InvalidateSharedInvalid: cache state reset */
255
286
/* WARNING: RegisterSharedInvalid: SI buffer overflow */
256
- sqlca . sqlwarn [2 ] = 'W' ;
257
- sqlca . sqlwarn [0 ] = 'W' ;
287
+ sqlca -> sqlwarn [2 ] = 'W' ;
288
+ sqlca -> sqlwarn [0 ] = 'W' ;
258
289
}
259
290
260
291
/* this contains some quick hacks, needs to be cleaned up, but it works */
261
292
bool
262
293
ECPGconnect (int lineno , const char * name , const char * user , const char * passwd , const char * connection_name , int autocommit )
263
294
{
295
+ struct sqlca_t * sqlca = ECPGget_sqlca ();
264
296
struct connection * this ;
265
297
char * dbname = strdup (name ),
266
298
* host = NULL ,
@@ -269,7 +301,7 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
269
301
* realname = NULL ,
270
302
* options = NULL ;
271
303
272
- ECPGinit_sqlca ();
304
+ ECPGinit_sqlca (sqlca );
273
305
274
306
if ((this = (struct connection * ) ECPGalloc (sizeof (struct connection ), lineno )) == NULL )
275
307
return false;
@@ -394,6 +426,9 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
394
426
realname = strdup (dbname );
395
427
396
428
/* add connection to our list */
429
+ #ifdef USE_THREADS
430
+ pthread_mutex_lock (& connections_mutex );
431
+ #endif
397
432
if (connection_name != NULL )
398
433
this -> name = ECPGstrdup (connection_name , lineno );
399
434
else
@@ -424,6 +459,9 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
424
459
425
460
set_backend_err (errmsg , lineno );
426
461
ecpg_finish (this );
462
+ #ifdef USE_THREADS
463
+ pthread_mutex_unlock (& connections_mutex );
464
+ #endif
427
465
ECPGlog ("connect: could not open database %s on %s port %s %s%s%s%s in line %d\n\t%s\n" ,
428
466
db ,
429
467
host ? host : "<DEFAULT>" ,
@@ -445,6 +483,9 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
445
483
ECPGfree (dbname );
446
484
return false;
447
485
}
486
+ #ifdef USE_THREADS
487
+ pthread_mutex_unlock (& connections_mutex );
488
+ #endif
448
489
449
490
if (host )
450
491
ECPGfree (host );
@@ -468,11 +509,16 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
468
509
bool
469
510
ECPGdisconnect (int lineno , const char * connection_name )
470
511
{
512
+ struct sqlca_t * sqlca = ECPGget_sqlca ();
471
513
struct connection * con ;
472
514
515
+ #ifdef USE_THREADS
516
+ pthread_mutex_lock (& connections_mutex );
517
+ #endif
518
+
473
519
if (strcmp (connection_name , "ALL" ) == 0 )
474
520
{
475
- ECPGinit_sqlca ();
521
+ ECPGinit_sqlca (sqlca );
476
522
for (con = all_connections ; con ;)
477
523
{
478
524
struct connection * f = con ;
@@ -486,10 +532,19 @@ ECPGdisconnect(int lineno, const char *connection_name)
486
532
con = ECPGget_connection (connection_name );
487
533
488
534
if (!ECPGinit (con , connection_name , lineno ))
489
- return (false);
535
+ {
536
+ #ifdef USE_THREADS
537
+ pthread_mutex_unlock (& connections_mutex );
538
+ #endif
539
+ return (false);
540
+ }
490
541
else
491
- ecpg_finish (con );
542
+ ecpg_finish (con );
492
543
}
493
544
545
+ #ifdef USE_THREADS
546
+ pthread_mutex_unlock (& connections_mutex );
547
+ #endif
548
+
494
549
return true;
495
550
}
0 commit comments