1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include "ecpgtype.h"
#include "ecpglib.h"
#include "ecpgerrno.h"
#include "extern.h"
#include "sqlca.h"
static struct connection *all_connections = NULL,
*actual_connection = NULL;
struct connection *
get_connection(const char *connection_name)
{
struct connection *con = all_connections;
if (connection_name == NULL || strcmp(connection_name, "CURRENT") == 0)
return actual_connection;
for (; con && strcmp(connection_name, con->name) != 0; con = con->next);
if (con)
return con;
else
return NULL;
}
static void
ecpg_finish(struct connection * act)
{
if (act != NULL)
{
struct ECPGtype_information_cache *cache, *ptr;
ECPGlog("ecpg_finish: finishing %s.\n", act->name);
PQfinish(act->connection);
/* remove act from the list */
if (act == all_connections)
all_connections = act->next;
else
{
struct connection *con;
for (con = all_connections; con->next && con->next != act; con = con->next);
if (con->next)
con->next = act->next;
}
if (actual_connection == act)
actual_connection = all_connections;
for (cache = act->cache_head; cache; ptr = cache, cache = cache->next, free(ptr));
free(act->name);
free(act);
}
else
ECPGlog("ecpg_finish: called an extra time.\n");
}
bool
ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
{
struct connection *con = get_connection(connection_name);
PGresult *results;
if (!ecpg_init(con, connection_name, lineno))
return (false);
ECPGlog("ECPGsetcommit line %d action = %s connection = %s\n", lineno, mode, con->name);
if (con->autocommit == true && strncmp(mode, "off", strlen("off")) == 0)
{
if (con->committed)
{
if ((results = PQexec(con->connection, "begin transaction")) == NULL)
{
ECPGraise(lineno, ECPG_TRANS, NULL);
return false;
}
PQclear(results);
con->committed = false;
}
con->autocommit = false;
}
else if (con->autocommit == false && strncmp(mode, "on", strlen("on")) == 0)
{
if (!con->committed)
{
if ((results = PQexec(con->connection, "commit")) == NULL)
{
ECPGraise(lineno, ECPG_TRANS, NULL);
return false;
}
PQclear(results);
con->committed = true;
}
con->autocommit = true;
}
return true;
}
bool
ECPGsetconn(int lineno, const char *connection_name)
{
struct connection *con = get_connection(connection_name);
if (!ecpg_init(con, connection_name, lineno))
return (false);
actual_connection = con;
return true;
}
static void
ECPGnoticeProcessor_raise(int code, const char *message)
{
sqlca.sqlcode = code;
strncpy(sqlca.sqlerrm.sqlerrmc, message, sizeof(sqlca.sqlerrm.sqlerrmc));
sqlca.sqlerrm.sqlerrmc[sizeof(sqlca.sqlerrm.sqlerrmc)-1]=0;
sqlca.sqlerrm.sqlerrml = strlen(sqlca.sqlerrm.sqlerrmc);
/* remove trailing newline */
if (sqlca.sqlerrm.sqlerrml
&& sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml-1]=='\n')
{
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml-1]=0;
sqlca.sqlerrm.sqlerrml--;
}
ECPGlog("raising sqlcode %d\n",code);
}
/*
* I know this is a mess, but we can't redesign the backend
*/
static void
ECPGnoticeProcessor(void *arg, const char *message)
{
/* these notices raise an error */
if (strncmp(message,"NOTICE: ",8))
{
ECPGlog("ECPGnoticeProcessor: strange notice '%s'\n", message);
ECPGnoticeProcessor_raise(ECPG_NOTICE_UNRECOGNIZED, message);
return;
}
message+=8;
while (*message==' ') message++;
ECPGlog("NOTICE: %s", message);
/* NOTICE: (transaction aborted): queries ignored until END */
/* NOTICE: current transaction is aborted, queries ignored until end of transaction block */
if (strstr(message,"queries ignored") && strstr(message,"transaction")
&& strstr(message,"aborted"))
{
ECPGnoticeProcessor_raise(ECPG_NOTICE_QUERY_IGNORED, message);
return;
}
/* NOTICE: PerformPortalClose: portal "*" not found */
if (!strncmp(message,"PerformPortalClose: portal",26)
&& strstr(message+26,"not found"))
{
ECPGnoticeProcessor_raise(ECPG_NOTICE_UNKNOWN_PORTAL, message);
return;
}
/* NOTICE: BEGIN: already a transaction in progress */
if (!strncmp(message,"BEGIN: already a transaction in progress",40))
{
ECPGnoticeProcessor_raise(ECPG_NOTICE_IN_TRANSACTION, message);
return;
}
/* NOTICE: AbortTransaction and not in in-progress state */
/* NOTICE: COMMIT: no transaction in progress */
/* NOTICE: ROLLBACK: no transaction in progress */
if (!strncmp(message,"AbortTransaction and not in in-progress state",45)
|| !strncmp(message,"COMMIT: no transaction in progress",34)
|| !strncmp(message,"ROLLBACK: no transaction in progress",36))
{
ECPGnoticeProcessor_raise(ECPG_NOTICE_NO_TRANSACTION, message);
return;
}
/* NOTICE: BlankPortalAssignName: portal * already exists */
if (!strncmp(message,"BlankPortalAssignName: portal",29)
&& strstr(message+29,"already exists"))
{
ECPGnoticeProcessor_raise(ECPG_NOTICE_PORTAL_EXISTS, message);
return;
}
/* these are harmless - do nothing */
/* NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index '*' for table '*' */
/* NOTICE: ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for FOREIGN KEY check(s) */
/* NOTICE: CREATE TABLE will create implicit sequence '*' for SERIAL column '*.*' */
/* NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) */
if ((!strncmp(message,"CREATE TABLE",12) || !strncmp(message,"ALTER TABLE",11))
&& strstr(message+11,"will create implicit"))
return;
/* NOTICE: QUERY PLAN: */
if (!strncmp(message,"QUERY PLAN:",11)) /* do we really see these? */
return;
/* NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "*" */
if (!strncmp(message,"DROP TABLE implicitly drops",27))
return;
/* NOTICE: Caution: DROP INDEX cannot be rolled back, so don't abort now */
if (strstr(message,"cannot be rolled back"))
return;
/* these and other unmentioned should set sqlca.sqlwarn[2] */
/* NOTICE: The ':' operator is deprecated. Use exp(x) instead. */
/* NOTICE: Rel *: Uninitialized page 0 - fixing */
/* NOTICE: PortalHeapMemoryFree: * not in alloc set! */
/* NOTICE: Too old parent tuple found - can't continue vc_repair_frag */
/* NOTICE: identifier "*" will be truncated to "*" */
/* NOTICE: InvalidateSharedInvalid: cache state reset */
/* NOTICE: RegisterSharedInvalid: SI buffer overflow */
sqlca.sqlwarn[2]='W';
sqlca.sqlwarn[0]='W';
}
bool
ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char *connection_name, int autocommit)
{
struct connection *this;
init_sqlca();
if ((this = (struct connection *) ecpg_alloc(sizeof(struct connection), lineno)) == NULL)
return false;
if (dbname == NULL && connection_name == NULL)
connection_name = "DEFAULT";
/* add connection to our list */
if (connection_name != NULL)
this->name = ecpg_strdup(connection_name, lineno);
else
this->name = ecpg_strdup(dbname, lineno);
this->cache_head = NULL;
if (all_connections == NULL)
this->next = NULL;
else
this->next = all_connections;
actual_connection = all_connections = this;
ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "");
this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd);
if (PQstatus(this->connection) == CONNECTION_BAD)
{
ecpg_finish(this);
ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "<DEFAULT>", user ? "for user " : "", user ? user : "", lineno);
ECPGraise(lineno, ECPG_CONNECT, dbname ? dbname : "<DEFAULT>");
return false;
}
this->committed = true;
this->autocommit = autocommit;
PQsetNoticeProcessor(this->connection,&ECPGnoticeProcessor,(void*)this);
return true;
}
bool
ECPGdisconnect(int lineno, const char *connection_name)
{
struct connection *con;
if (strcmp(connection_name, "ALL") == 0)
{
init_sqlca();
for (con = all_connections; con;)
{
struct connection *f = con;
con = con->next;
ecpg_finish(f);
}
}
else
{
con = get_connection(connection_name);
if (!ecpg_init(con, connection_name, lineno))
return (false);
else
ecpg_finish(con);
}
return true;
}
|