77 *
88 *
99 * 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 $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -67,91 +67,89 @@ static void closePGconn(PGconn *conn);
6767PGconn *
6868PQsetdb (const char * pghost ,const char * pgport ,const char * pgoptions ,const char * pgtty ,const char * dbName )
6969{
70- PGconn * conn ;
71- const char * tmp ;
70+ PGconn * conn ;
71+ const char * tmp ;
7272
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 ));
7974
75+ if (conn == NULL )
76+ fprintf (stderr ,
77+ "FATAL: PQsetdb() -- unable to allocate memory for a PGconn" );
78+ else {
8079conn -> Pfout = NULL ;
8180conn -> Pfin = NULL ;
8281conn -> Pfdebug = NULL ;
8382conn -> port = NULL ;
8483conn -> notifyList = DLNewList ();
85-
84+
8685if (!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 );
9190 }else
92- conn -> pghost = strdup (pghost );
93-
91+ conn -> pghost = strdup (pghost );
92+
9493if (!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 );
9998 }else
100- conn -> pgport = strdup (pgport );
101-
99+ conn -> pgport = strdup (pgport );
100+
102101if (!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 );
107106 }else
108- conn -> pgtty = strdup (pgtty );
109-
107+ conn -> pgtty = strdup (pgtty );
108+
110109if (!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 );
129114 }else
130- conn -> dbName = strdup (dbName );
131- #endif
115+ conn -> pgoptions = strdup (pgoptions );
132116if (((tmp = dbName )&& (dbName [0 ]!= '\0' ))||
133- ((tmp = getenv ("PGDATABASE" )))) {
117+ ((tmp = getenv ("PGDATABASE" )))) {
134118conn -> dbName = strdup (tmp );
135119 }else {
136120char errorMessage [ERROR_MSG_LENGTH ];
137121if ((tmp = fe_getauthname (errorMessage ))!= 0 ) {
138122conn -> dbName = strdup (tmp );
139- free (tmp );
123+ free (( char * ) tmp );
140124 }else {
141125sprintf (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" );
144127conn -> dbName = NULL ;
145128return conn ;
146129 }
147130 }
148131conn -> 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 ;
150146}
151147
148+
152149/*
153150 * 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.
155153 *
156154 */
157155static ConnStatusType
@@ -166,7 +164,6 @@ connectDB(PGconn *conn)
166164int laddrlen = sizeof (struct sockaddr );
167165Port * port = conn -> port ;
168166int portno ;
169- PGresult * res ;
170167
171168char * user ;
172169/*
@@ -275,14 +272,6 @@ connectDB(PGconn *conn)
275272
276273conn -> port = port ;
277274
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- gotoconnect_errReturn ;
284- }
285- free (res );
286275return CONNECTION_OK ;
287276
288277connect_errReturn :
@@ -319,6 +308,7 @@ closePGconn(PGconn *conn)
319308if (conn -> Pfout )fclose (conn -> Pfout );
320309if (conn -> Pfin )fclose (conn -> Pfin );
321310if (conn -> Pfdebug )fclose (conn -> Pfdebug );
311+ conn -> status = CONNECTION_BAD ;/* Well, not really _bad_ - just absent */
322312}
323313
324314/*