7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.4 1996/07/23 03:35:12 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.4.2.1 1996/08/19 13:23:19 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -66,16 +66,16 @@ static void closePGconn(PGconn *conn);
66
66
PGconn *
67
67
PQsetdb (char * pghost , char * pgport , char * pgoptions , char * pgtty , char * dbName )
68
68
{
69
- PGconn * conn ;
70
- char * tmp ;
69
+ PGconn * conn ;
70
+ char * tmp ;
71
71
72
- conn = (PGconn * )malloc (sizeof (PGconn ));
73
-
74
- if (!conn ) {
75
- fprintf (stderr ,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn" );
76
- return (PGconn * )NULL ;
77
- }
72
+ conn = (PGconn * )malloc (sizeof (PGconn ));
78
73
74
+ if (conn == NULL ) {
75
+ fprintf (stderr ,
76
+ "FATAL: PQsetdb() -- unable to allocate memory for a PGconn" );
77
+ return (PGconn * )NULL ;
78
+ }
79
79
conn -> Pfout = NULL ;
80
80
conn -> Pfin = NULL ;
81
81
conn -> Pfdebug = NULL ;
@@ -113,39 +113,36 @@ PQsetdb(char *pghost, char* pgport, char* pgoptions, char* pgtty, char* dbName)
113
113
conn -> pgoptions = strdup (tmp );
114
114
} else
115
115
conn -> pgoptions = strdup (pgoptions );
116
- #if 0
117
- if (!dbName || dbName [0 ] == '\0' ) {
116
+ if (((tmp = dbName ) && (dbName [0 ] != '\0' )) ||
117
+ ((tmp = getenv ("PGDATABASE" )))) {
118
+ conn -> dbName = strdup (tmp );
119
+ } else {
118
120
char errorMessage [ERROR_MSG_LENGTH ];
119
- if (!(tmp = getenv ("PGDATABASE" )) &&
120
- !(tmp = fe_getauthname (errorMessage ))) {
121
+ if ((tmp = fe_getauthname (errorMessage )) != 0 ) {
122
+ conn -> dbName = strdup (tmp );
123
+ free ((char * )tmp );
124
+ } else {
121
125
sprintf (conn -> errorMessage ,
122
126
"FATAL: PQsetdb: Unable to determine a database name!\n" );
123
- /* pqdebug("%s", conn->errorMessage); */
124
127
conn -> dbName = NULL ;
125
128
return conn ;
126
129
}
127
- conn -> dbName = strdup (tmp );
128
- } else
129
- conn -> dbName = strdup (dbName );
130
- #endif
131
- if (((tmp = dbName ) && (dbName [0 ] != '\0' )) ||
132
- ((tmp = getenv ("PGDATABASE" )))) {
133
- conn -> dbName = strdup (tmp );
134
- } else {
135
- char errorMessage [ERROR_MSG_LENGTH ];
136
- if (tmp = fe_getauthname (errorMessage )) {
137
- conn -> dbName = strdup (tmp );
138
- free (tmp );
139
- } else {
140
- sprintf (conn -> errorMessage ,
141
- "FATAL: PQsetdb: Unable to determine a database name!\n" );
142
- /* pqdebug("%s", conn->errorMessage); */
143
- conn -> dbName = NULL ;
144
- return conn ;
145
- }
146
130
}
147
131
conn -> status = connectDB (conn );
148
- return conn ;
132
+ if (conn -> status == CONNECTION_OK ) {
133
+ PGresult * res ;
134
+ /* Send a blank query to make sure everything works; in particular, that
135
+ the database exists.
136
+ */
137
+ res = PQexec (conn ," " );
138
+ if (res == NULL || res -> resultStatus != PGRES_EMPTY_QUERY ) {
139
+ /* PQexec has put error message in conn->errorMessage */
140
+ closePGconn (conn );
141
+ }
142
+ PQclear (res );
143
+ }
144
+ }
145
+ return conn ;
149
146
}
150
147
151
148
/*
@@ -165,7 +162,6 @@ connectDB(PGconn *conn)
165
162
int laddrlen = sizeof (struct sockaddr );
166
163
Port * port = conn -> port ;
167
164
int portno ;
168
- PGresult * res ;
169
165
170
166
char * user ;
171
167
/*
@@ -274,14 +270,6 @@ connectDB(PGconn *conn)
274
270
275
271
conn -> port = port ;
276
272
277
- /* we have a connection now,
278
- send a blank query down to make sure the database exists*/
279
- res = PQexec (conn ," " );
280
- if (res == NULL || res -> resultStatus != PGRES_EMPTY_QUERY ) {
281
- /* error will already be in conn->errorMessage */
282
- goto connect_errReturn ;
283
- }
284
- free (res );
285
273
return CONNECTION_OK ;
286
274
287
275
connect_errReturn :
@@ -318,6 +306,7 @@ closePGconn(PGconn *conn)
318
306
if (conn -> Pfout ) fclose (conn -> Pfout );
319
307
if (conn -> Pfin ) fclose (conn -> Pfin );
320
308
if (conn -> Pfdebug ) fclose (conn -> Pfdebug );
309
+ conn -> status = CONNECTION_BAD ; /* Well, not really _bad_ - just absent */
321
310
}
322
311
323
312
/*
0 commit comments