@@ -140,12 +140,25 @@ get_connection(const char *connection_name)
140
140
return NULL ;
141
141
}
142
142
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
+
143
156
static void
144
- ECPGfinish (struct connection * act )
157
+ ecpg_finish (struct connection * act )
145
158
{
146
159
if (act != NULL )
147
160
{
148
- ECPGlog ("ECPGfinish : finishing %s.\n" ,act -> name );
161
+ ECPGlog ("ecpg_finish : finishing %s.\n" ,act -> name );
149
162
PQfinish (act -> connection );
150
163
/* remove act from the list */
151
164
if (act == all_connections )
@@ -166,7 +179,7 @@ ECPGfinish(struct connection * act)
166
179
free (act );
167
180
}
168
181
else
169
- ECPGlog ("ECPGfinish : called an extra time.\n" );
182
+ ECPGlog ("ecpg_finish : called an extra time.\n" );
170
183
}
171
184
172
185
static char *
@@ -383,8 +396,6 @@ ECPGexecute(struct statement * stmt)
383
396
PGnotify * notify ;
384
397
struct variable * var ;
385
398
386
- memcpy ((char * )& sqlca , (char * )& sqlca_init ,sizeof (sqlca ));
387
-
388
399
copiedquery = ecpg_strdup (stmt -> command ,stmt -> lineno );
389
400
390
401
/*
@@ -1029,11 +1040,8 @@ ECPGdo(int lineno, const char *connection_name, char *query,...)
1029
1040
struct connection * con = get_connection (connection_name );
1030
1041
bool status ;
1031
1042
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);
1037
1045
1038
1046
va_start (args ,query );
1039
1047
if (create_statement (lineno ,con ,& stmt ,query ,args )== false)
@@ -1058,11 +1066,8 @@ ECPGstatus(int lineno, const char *connection_name)
1058
1066
{
1059
1067
struct connection * con = get_connection (connection_name );
1060
1068
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);
1066
1071
1067
1072
/* are we connected? */
1068
1073
if (con -> connection == NULL )
@@ -1081,11 +1086,8 @@ ECPGtrans(int lineno, const char *connection_name, const char *transaction)
1081
1086
PGresult * res ;
1082
1087
struct connection * con = get_connection (connection_name );
1083
1088
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);
1089
1091
1090
1092
ECPGlog ("ECPGtrans line %d action = %s connection = %s\n" ,lineno ,transaction ,con -> name );
1091
1093
@@ -1124,41 +1126,36 @@ ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
1124
1126
struct connection * con = get_connection (connection_name );
1125
1127
PGresult * results ;
1126
1128
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 )
1128
1133
{
1129
- if (con -> autocommit == true && strncmp ( mode , "OFF" , strlen ( "OFF" )) == 0 )
1134
+ if (con -> committed )
1130
1135
{
1131
- if (con -> committed )
1136
+ if (( results = PQexec ( con -> connection , "begin transaction" )) == NULL )
1132
1137
{
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;
1140
1140
}
1141
- con -> autocommit = false;
1141
+ PQclear (results );
1142
+ con -> committed = false;
1142
1143
}
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 )
1144
1149
{
1145
- if (! con -> committed )
1150
+ if (( results = PQexec ( con -> connection , "commit" )) == NULL )
1146
1151
{
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;
1154
1154
}
1155
- con -> autocommit = true;
1155
+ PQclear (results );
1156
+ con -> committed = true;
1156
1157
}
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;
1162
1159
}
1163
1160
1164
1161
return true;
@@ -1169,24 +1166,22 @@ ECPGsetconn(int lineno, const char *connection_name)
1169
1166
{
1170
1167
struct connection * con = get_connection (connection_name );
1171
1168
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;
1182
1174
}
1183
1175
1184
1176
bool
1185
1177
ECPGconnect (int lineno ,const char * dbname ,const char * user ,const char * passwd ,const char * connection_name ,int autocommit )
1186
1178
{
1187
- struct connection * this = ( struct connection * ) ecpg_alloc ( sizeof ( struct connection ), lineno ) ;
1179
+ struct connection * this ;
1188
1180
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 )
1190
1185
return false;
1191
1186
1192
1187
if (dbname == NULL && connection_name == NULL )
@@ -1213,7 +1208,7 @@ ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd
1213
1208
1214
1209
if (PQstatus (this -> connection )== CONNECTION_BAD )
1215
1210
{
1216
- ECPGfinish (this );
1211
+ ecpg_finish (this );
1217
1212
ECPGlog ("connect: could not open database %s %s%s in line %d\n" ,dbname ?dbname :"<DEFAULT>" ,user ?"for user " :"" ,user ?user :"" ,lineno );
1218
1213
register_error (ECPG_CONNECT ,"connect: could not open database %s." ,dbname ?dbname :"<DEFAULT>" );
1219
1214
return false;
@@ -1237,21 +1232,17 @@ ECPGdisconnect(int lineno, const char *connection_name)
1237
1232
struct connection * f = con ;
1238
1233
1239
1234
con = con -> next ;
1240
- ECPGfinish (f );
1235
+ ecpg_finish (f );
1241
1236
}
1242
1237
}
1243
1238
else
1244
1239
{
1245
1240
con = get_connection (connection_name );
1246
1241
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);
1253
1244
else
1254
- ECPGfinish (con );
1245
+ ecpg_finish (con );
1255
1246
}
1256
1247
1257
1248
return true;