7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.6 1996/08/10 00:22:44 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.7 1996/08/19 13:25:40 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -67,91 +67,89 @@ static void closePGconn(PGconn *conn);
67
67
PGconn *
68
68
PQsetdb (const char * pghost , const char * pgport , const char * pgoptions , const char * pgtty , const char * dbName )
69
69
{
70
- PGconn * conn ;
71
- const char * tmp ;
70
+ PGconn * conn ;
71
+ const char * tmp ;
72
72
73
- conn = (PGconn * )malloc (sizeof (PGconn ));
74
-
75
- if (!conn ) {
76
- fprintf (stderr ,"FATAL: PQsetdb() -- unable to allocate memory for a PGconn" );
77
- return (PGconn * )NULL ;
78
- }
73
+ conn = (PGconn * )malloc (sizeof (PGconn ));
79
74
75
+ if (conn == NULL )
76
+ fprintf (stderr ,
77
+ "FATAL: PQsetdb() -- unable to allocate memory for a PGconn" );
78
+ else {
80
79
conn -> Pfout = NULL ;
81
80
conn -> Pfin = NULL ;
82
81
conn -> Pfdebug = NULL ;
83
82
conn -> port = NULL ;
84
83
conn -> notifyList = DLNewList ();
85
-
84
+
86
85
if (!pghost || pghost [0 ] == '\0' ) {
87
- if (!(tmp = getenv ("PGHOST" ))) {
88
- tmp = DefaultHost ;
89
- }
90
- conn -> pghost = strdup (tmp );
86
+ if (!(tmp = getenv ("PGHOST" ))) {
87
+ tmp = DefaultHost ;
88
+ }
89
+ conn -> pghost = strdup (tmp );
91
90
} else
92
- conn -> pghost = strdup (pghost );
93
-
91
+ conn -> pghost = strdup (pghost );
92
+
94
93
if (!pgport || pgport [0 ] == '\0' ) {
95
- if (!(tmp = getenv ("PGPORT" ))) {
96
- tmp = POSTPORT ;
97
- }
98
- conn -> pgport = strdup (tmp );
94
+ if (!(tmp = getenv ("PGPORT" ))) {
95
+ tmp = POSTPORT ;
96
+ }
97
+ conn -> pgport = strdup (tmp );
99
98
} else
100
- conn -> pgport = strdup (pgport );
101
-
99
+ conn -> pgport = strdup (pgport );
100
+
102
101
if (!pgtty || pgtty [0 ] == '\0' ) {
103
- if (!(tmp = getenv ("PGTTY" ))) {
104
- tmp = DefaultTty ;
105
- }
106
- conn -> pgtty = strdup (tmp );
102
+ if (!(tmp = getenv ("PGTTY" ))) {
103
+ tmp = DefaultTty ;
104
+ }
105
+ conn -> pgtty = strdup (tmp );
107
106
} else
108
- conn -> pgtty = strdup (pgtty );
109
-
107
+ conn -> pgtty = strdup (pgtty );
108
+
110
109
if (!pgoptions || pgoptions [0 ] == '\0' ) {
111
- if (!(tmp = getenv ("PGOPTIONS" ))) {
112
- tmp = DefaultOption ;
113
- }
114
- conn -> pgoptions = strdup (tmp );
115
- } else
116
- conn -> pgoptions = strdup (pgoptions );
117
- #if 0
118
- if (!dbName || dbName [0 ] == '\0' ) {
119
- char errorMessage [ERROR_MSG_LENGTH ];
120
- if (!(tmp = getenv ("PGDATABASE" )) &&
121
- !(tmp = fe_getauthname (errorMessage ))) {
122
- sprintf (conn -> errorMessage ,
123
- "FATAL: PQsetdb: Unable to determine a database name!\n" );
124
- /* pqdebug("%s", conn->errorMessage); */
125
- conn -> dbName = NULL ;
126
- return conn ;
127
- }
128
- conn -> dbName = strdup (tmp );
110
+ if (!(tmp = getenv ("PGOPTIONS" ))) {
111
+ tmp = DefaultOption ;
112
+ }
113
+ conn -> pgoptions = strdup (tmp );
129
114
} else
130
- conn -> dbName = strdup (dbName );
131
- #endif
115
+ conn -> pgoptions = strdup (pgoptions );
132
116
if (((tmp = dbName ) && (dbName [0 ] != '\0' )) ||
133
- ((tmp = getenv ("PGDATABASE" )))) {
117
+ ((tmp = getenv ("PGDATABASE" )))) {
134
118
conn -> dbName = strdup (tmp );
135
119
} else {
136
120
char errorMessage [ERROR_MSG_LENGTH ];
137
121
if ((tmp = fe_getauthname (errorMessage )) != 0 ) {
138
122
conn -> dbName = strdup (tmp );
139
- free (tmp );
123
+ free (( char * ) tmp );
140
124
} else {
141
125
sprintf (conn -> errorMessage ,
142
- "FATAL: PQsetdb: Unable to determine a database name!\n" );
143
- /* pqdebug("%s", conn->errorMessage); */
126
+ "FATAL: PQsetdb: Unable to determine a database name!\n" );
144
127
conn -> dbName = NULL ;
145
128
return conn ;
146
129
}
147
130
}
148
131
conn -> status = connectDB (conn );
149
- 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 ;
150
146
}
151
147
148
+
152
149
/*
153
150
* connectDB -
154
- * make a connection to the database, returns 1 if successful or 0 if not
151
+ * make a connection to the backend so it is ready to receive queries.
152
+ * return CONNECTION_OK if successful, CONNECTION_BAD if not.
155
153
*
156
154
*/
157
155
static ConnStatusType
@@ -166,7 +164,6 @@ connectDB(PGconn *conn)
166
164
int laddrlen = sizeof (struct sockaddr );
167
165
Port * port = conn -> port ;
168
166
int portno ;
169
- PGresult * res ;
170
167
171
168
char * user ;
172
169
/*
@@ -275,14 +272,6 @@ connectDB(PGconn *conn)
275
272
276
273
conn -> port = port ;
277
274
278
- /* we have a connection now,
279
- send a blank query down to make sure the database exists*/
280
- res = PQexec (conn ," " );
281
- if (res == NULL || res -> resultStatus != PGRES_EMPTY_QUERY ) {
282
- /* error will already be in conn->errorMessage */
283
- goto connect_errReturn ;
284
- }
285
- free (res );
286
275
return CONNECTION_OK ;
287
276
288
277
connect_errReturn :
@@ -319,6 +308,7 @@ closePGconn(PGconn *conn)
319
308
if (conn -> Pfout ) fclose (conn -> Pfout );
320
309
if (conn -> Pfin ) fclose (conn -> Pfin );
321
310
if (conn -> Pfdebug ) fclose (conn -> Pfdebug );
311
+ conn -> status = CONNECTION_BAD ; /* Well, not really _bad_ - just absent */
322
312
}
323
313
324
314
/*
0 commit comments