@@ -140,12 +140,25 @@ get_connection(const char *connection_name)
140140return NULL ;
141141}
142142
143+ static bool
144+ ecpg_init (const struct connection * con ,const char * connection_name ,const int lineno )
145+ {
146+ memcpy ((char * )& sqlca , (char * )& sqlca_init ,sizeof (sqlca ));
147+ if (con == NULL )
148+ {
149+ register_error (ECPG_NO_CONN ,"No such connection %s in line %d." ,connection_name ?connection_name :"NULL" ,lineno );
150+ return (false);
151+ }
152+
153+ return (true);
154+ }
155+
143156static void
144- ECPGfinish (struct connection * act )
157+ ecpg_finish (struct connection * act )
145158{
146159if (act != NULL )
147160{
148- ECPGlog ("ECPGfinish : finishing %s.\n" ,act -> name );
161+ ECPGlog ("ecpg_finish : finishing %s.\n" ,act -> name );
149162PQfinish (act -> connection );
150163/* remove act from the list */
151164if (act == all_connections )
@@ -166,7 +179,7 @@ ECPGfinish(struct connection * act)
166179free (act );
167180}
168181else
169- ECPGlog ("ECPGfinish : called an extra time.\n" );
182+ ECPGlog ("ecpg_finish : called an extra time.\n" );
170183}
171184
172185static char *
@@ -383,8 +396,6 @@ ECPGexecute(struct statement * stmt)
383396PGnotify * notify ;
384397struct variable * var ;
385398
386- memcpy ((char * )& sqlca , (char * )& sqlca_init ,sizeof (sqlca ));
387-
388399copiedquery = ecpg_strdup (stmt -> command ,stmt -> lineno );
389400
390401/*
@@ -1029,11 +1040,8 @@ ECPGdo(int lineno, const char *connection_name, char *query,...)
10291040struct connection * con = get_connection (connection_name );
10301041bool status ;
10311042
1032- if (con == NULL )
1033- {
1034- register_error (ECPG_NO_CONN ,"No such connection %s in line %d." ,connection_name ?connection_name :"NULL" ,lineno );
1035- return (false);
1036- }
1043+ if (!ecpg_init (con ,connection_name ,lineno ))
1044+ return (false);
10371045
10381046va_start (args ,query );
10391047if (create_statement (lineno ,con ,& stmt ,query ,args )== false)
@@ -1058,11 +1066,8 @@ ECPGstatus(int lineno, const char *connection_name)
10581066{
10591067struct connection * con = get_connection (connection_name );
10601068
1061- if (con == NULL )
1062- {
1063- register_error (ECPG_NO_CONN ,"No such connection %s in line %d" ,connection_name ?connection_name :"NULL" ,lineno );
1064- return (false);
1065- }
1069+ if (!ecpg_init (con ,connection_name ,lineno ))
1070+ return (false);
10661071
10671072/* are we connected? */
10681073if (con -> connection == NULL )
@@ -1081,11 +1086,8 @@ ECPGtrans(int lineno, const char *connection_name, const char *transaction)
10811086PGresult * res ;
10821087struct connection * con = get_connection (connection_name );
10831088
1084- if (con == NULL )
1085- {
1086- register_error (ECPG_NO_CONN ,"No such connection %s in line %d" ,connection_name ?connection_name :"NULL" ,lineno );
1087- return (false);
1088- }
1089+ if (!ecpg_init (con ,connection_name ,lineno ))
1090+ return (false);
10891091
10901092ECPGlog ("ECPGtrans line %d action = %s connection = %s\n" ,lineno ,transaction ,con -> name );
10911093
@@ -1124,41 +1126,36 @@ ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
11241126struct connection * con = get_connection (connection_name );
11251127PGresult * results ;
11261128
1127- if (con )
1129+ if (!ecpg_init (con ,connection_name ,lineno ))
1130+ return (false);
1131+
1132+ if (con -> autocommit == true&& strncmp (mode ,"OFF" ,strlen ("OFF" ))== 0 )
11281133{
1129- if (con -> autocommit == true && strncmp ( mode , "OFF" , strlen ( "OFF" )) == 0 )
1134+ if (con -> committed )
11301135{
1131- if (con -> committed )
1136+ if (( results = PQexec ( con -> connection , "begin transaction" )) == NULL )
11321137{
1133- if ((results = PQexec (con -> connection ,"begin transaction" ))== NULL )
1134- {
1135- register_error (ECPG_TRANS ,"Error in transaction processing line %d." ,lineno );
1136- return false;
1137- }
1138- PQclear (results );
1139- con -> committed = false;
1138+ register_error (ECPG_TRANS ,"Error in transaction processing line %d." ,lineno );
1139+ return false;
11401140}
1141- con -> autocommit = false;
1141+ PQclear (results );
1142+ con -> committed = false;
11421143}
1143- else if (con -> autocommit == false&& strncmp (mode ,"ON" ,strlen ("ON" ))== 0 )
1144+ con -> autocommit = false;
1145+ }
1146+ else if (con -> autocommit == false&& strncmp (mode ,"ON" ,strlen ("ON" ))== 0 )
1147+ {
1148+ if (!con -> committed )
11441149{
1145- if (! con -> committed )
1150+ if (( results = PQexec ( con -> connection , "commit" )) == NULL )
11461151{
1147- if ((results = PQexec (con -> connection ,"commit" ))== NULL )
1148- {
1149- register_error (ECPG_TRANS ,"Error in transaction processing line %d." ,lineno );
1150- return false;
1151- }
1152- PQclear (results );
1153- con -> committed = true;
1152+ register_error (ECPG_TRANS ,"Error in transaction processing line %d." ,lineno );
1153+ return false;
11541154}
1155- con -> autocommit = true;
1155+ PQclear (results );
1156+ con -> committed = true;
11561157}
1157- }
1158- else
1159- {
1160- register_error (ECPG_NO_CONN ,"No such connection %s in line %d" ,connection_name ?connection_name :"NULL" ,lineno );
1161- return false;
1158+ con -> autocommit = true;
11621159}
11631160
11641161return true;
@@ -1169,24 +1166,22 @@ ECPGsetconn(int lineno, const char *connection_name)
11691166{
11701167struct connection * con = get_connection (connection_name );
11711168
1172- if (con )
1173- {
1174- actual_connection = con ;
1175- return true;
1176- }
1177- else
1178- {
1179- register_error (ECPG_NO_CONN ,"No such connection %s in line %d" ,connection_name ?connection_name :"NULL" ,lineno );
1180- return false;
1181- }
1169+ if (!ecpg_init (con ,connection_name ,lineno ))
1170+ return (false);
1171+
1172+ actual_connection = con ;
1173+ return true;
11821174}
11831175
11841176bool
11851177ECPGconnect (int lineno ,const char * dbname ,const char * user ,const char * passwd ,const char * connection_name ,int autocommit )
11861178{
1187- struct connection * this = ( struct connection * ) ecpg_alloc ( sizeof ( struct connection ), lineno ) ;
1179+ struct connection * this ;
11881180
1189- if (!this )
1181+
1182+ memcpy ((char * )& sqlca , (char * )& sqlca_init ,sizeof (sqlca ));
1183+
1184+ if ((this = (struct connection * )ecpg_alloc (sizeof (struct connection ),lineno ))== NULL )
11901185return false;
11911186
11921187if (dbname == NULL && connection_name == NULL )
@@ -1213,7 +1208,7 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
12131208
12141209if (PQstatus (this -> connection )== CONNECTION_BAD )
12151210{
1216- ECPGfinish (this );
1211+ ecpg_finish (this );
12171212ECPGlog ("connect: could not open database %s %s%s in line %d\n" ,dbname ?dbname :"<DEFAULT>" ,user ?"for user " :"" ,user ?user :"" ,lineno );
12181213register_error (ECPG_CONNECT ,"connect: could not open database %s." ,dbname ?dbname :"<DEFAULT>" );
12191214return false;
@@ -1237,21 +1232,17 @@ ECPGdisconnect(int lineno, const char *connection_name)
12371232struct connection * f = con ;
12381233
12391234con = con -> next ;
1240- ECPGfinish (f );
1235+ ecpg_finish (f );
12411236}
12421237}
12431238else
12441239{
12451240con = get_connection (connection_name );
12461241
1247- if (con == NULL )
1248- {
1249- ECPGlog ("disconnect: not connected to connection %s\n" ,connection_name ?connection_name :"NULL" );
1250- register_error (ECPG_NO_CONN ,"No such connection %s in line %d" ,connection_name ?connection_name :"NULL" ,lineno );
1251- return false;
1252- }
1242+ if (!ecpg_init (con ,connection_name ,lineno ))
1243+ return (false);
12531244else
1254- ECPGfinish (con );
1245+ ecpg_finish (con );
12551246}
12561247
12571248return true;