aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/lib/dynamic.c
blob: 12aac32a4584afc80bc318a8352a4834f24ffefa (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* dynamic SQL support routines
 *
 * Copyright (c) 2000, Christof Petig <christof.petig@wtal.de>
 *
 * $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/dynamic.c,v 1.5 2000/02/22 19:57:05 meskes Exp $
 */

/* I borrowed the include files from ecpglib.c, maybe we don't need all of them */

#include <sql3types.h>

static struct descriptor
{
	char *name;
	PGresult *result;
	struct descriptor *next;
} *all_descriptors=NULL;

PGconn *ECPG_internal_get_connection(char *name);

unsigned int ECPGDynamicType(Oid type)
{
	switch(type)
	{	case 16:	return SQL3_BOOLEAN;	/* bool */
		case 21:	return SQL3_SMALLINT;	/* int2 */
		case 23:	return SQL3_INTEGER;	/* int4 */
		case 25:	return SQL3_CHARACTER;	/* text */
		case 700:	return SQL3_REAL;		/* float4 */
		case 701:	return SQL3_DOUBLE_PRECISION;	/* float8 */
		case 1042:	return SQL3_CHARACTER;	/* bpchar */
		case 1043:	return SQL3_CHARACTER_VARYING;	/* varchar */
		case 1082:	return SQL3_DATE_TIME_TIMESTAMP;	/* date */
		case 1083:	return SQL3_DATE_TIME_TIMESTAMP;	/* time */
		case 1184:	return SQL3_DATE_TIME_TIMESTAMP;	/* datetime */
		case 1296:	return SQL3_DATE_TIME_TIMESTAMP;	/* timestamp */
		case 1700:	return SQL3_NUMERIC;	/* numeric */
		default:
			return -type;
	}
}

unsigned int ECPGDynamicType_DDT(Oid type)
{	switch(type)
	{	
		case 1082:	return SQL3_DDT_DATE;	/* date */
		case 1083:	return SQL3_DDT_TIME;	/* time */
		case 1184:	return SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE;	/* datetime */
		case 1296:	return SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE;	/* timestamp */
		default:
			return SQL3_DDT_ILLEGAL;
	}
}

// like ECPGexecute
static bool execute_descriptor(int lineno,const char *query
							,struct connection *con,PGresult **resultptr)
{
	bool	status = false;
	PGresult   *results;
	PGnotify   *notify;
	
	/* Now the request is built. */

	if (con->committed && !con->autocommit)
	{
		if ((results = PQexec(con->connection, "begin transaction")) == NULL)
		{
			register_error(ECPG_TRANS, "Error in transaction processing line %d.", lineno);
			return false;
		}
		PQclear(results);
		con->committed = false;
	}

	ECPGlog("execute_descriptor line %d: QUERY: %s on connection %s\n", lineno, query, con->name);
	results = PQexec(con->connection, query);

	if (results == NULL)
	{
		ECPGlog("ECPGexecute line %d: error: %s", lineno,
				PQerrorMessage(con->connection));
		register_error(ECPG_PGSQL, "Postgres error: %s line %d.",
			 PQerrorMessage(con->connection), lineno);
	}
	else
	{	*resultptr=results;
		switch (PQresultStatus(results))
		{	int ntuples;
			case PGRES_TUPLES_OK:
				status = true;
				sqlca.sqlerrd[2] = ntuples = PQntuples(results);
				if (ntuples < 1)
				{
					ECPGlog("execute_descriptor line %d: Incorrect number of matches: %d\n",
							lineno, ntuples);
					register_error(ECPG_NOT_FOUND, "No data found line %d.", lineno);
					status = false;
					break;
				}
				break;
#if 1 /* strictly these are not needed (yet) */
			case PGRES_EMPTY_QUERY:
				/* do nothing */
				register_error(ECPG_EMPTY, "Empty query line %d.", lineno);
				break;
			case PGRES_COMMAND_OK:
				status = true;
				sqlca.sqlerrd[1] = atol(PQoidStatus(results));
				sqlca.sqlerrd[2] = atol(PQcmdTuples(results));
				ECPGlog("ECPGexecute line %d Ok: %s\n", lineno, PQcmdStatus(results));
				break;
			case PGRES_COPY_OUT:
				ECPGlog("ECPGexecute line %d: Got PGRES_COPY_OUT ... tossing.\n", lineno);
				PQendcopy(con->connection);
				break;
			case PGRES_COPY_IN:
				ECPGlog("ECPGexecute line %d: Got PGRES_COPY_IN ... tossing.\n", lineno);
				PQendcopy(con->connection);
				break;
#else
			case PGRES_EMPTY_QUERY:
			case PGRES_COMMAND_OK:
			case PGRES_COPY_OUT:
			case PGRES_COPY_IN:
				break;
#endif
			case PGRES_NONFATAL_ERROR:
			case PGRES_FATAL_ERROR:
			case PGRES_BAD_RESPONSE:
				ECPGlog("ECPGexecute line %d: Error: %s",
						lineno, PQerrorMessage(con->connection));
				register_error(ECPG_PGSQL, "Postgres error: %s line %d.",
							   PQerrorMessage(con->connection), lineno);
				status = false;
				break;
			default:
				ECPGlog("ECPGexecute line %d: Got something else, postgres error.\n",
						lineno);
				register_error(ECPG_PGSQL, "Postgres error: %s line %d.",
							   PQerrorMessage(con->connection), lineno);
				status = false;
				break;
		}
	}

	/* check for asynchronous returns */
	notify = PQnotifies(con->connection);
	if (notify)
	{
		ECPGlog("ECPGexecute line %d: ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
				lineno, notify->relname, notify->be_pid);
		free(notify);
	}
	return status;
}

/* like ECPGdo */
static bool do_descriptor2(int lineno,const char *connection_name,
					PGresult **resultptr, const char *query)
{
	struct connection *con = get_connection(connection_name);
	bool		status=true;
	char *locale = setlocale(LC_NUMERIC, NULL);

	/* Make sure we do NOT honor the locale for numeric input/output */
	/* since the database wants teh standard decimal point */
	setlocale(LC_NUMERIC, "C");

	if (!ecpg_init(con, connection_name, lineno))
	{	setlocale(LC_NUMERIC, locale);
		return(false);
	}

	/* are we connected? */
	if (con == NULL || con->connection == NULL)
	{
		ECPGlog("ECPGdo: not connected to %s\n", con->name);
		register_error(ECPG_NOT_CONN, "Not connected in line %d.", lineno);
		setlocale(LC_NUMERIC, locale);
		return false;
	}

	status = execute_descriptor(lineno,query,con,resultptr);

	/* and reset locale value so our application is not affected */
	setlocale(LC_NUMERIC, locale);
	return (status);
}

bool ECPGdo_descriptor(int line,const char *connection,
							const char *descriptor,const char *query)
{
	struct descriptor *i;
	for (i=all_descriptors;i!=NULL;i=i->next)
	{	if (!strcmp(descriptor,i->name)) 
	    {	
			bool status;

			/* free previous result */
			if (i->result) PQclear(i->result);
		    	i->result=NULL;
	    	
			status=do_descriptor2(line,connection,&i->result,query);
			
			if (!i->result) PQmakeEmptyPGresult(NULL, 0);
			return (status);
	    }
	}
	
	ECPGraise(line, ECPG_UNKNOWN_DESCRIPTOR, descriptor);
	return false;
}
							
PGresult *ECPGresultByDescriptor(int line,const char *name)
{
	struct descriptor *i;
	
	for (i = all_descriptors; i != NULL; i = i->next)
	{
		if (!strcmp(name, i->name)) return i->result;
	}
	
	ECPGraise(line, ECPG_UNKNOWN_DESCRIPTOR, name);
	
	return NULL;
} 


bool ECPGdeallocate_desc(int line,const char *name)
{
	struct descriptor *i;
	struct descriptor **lastptr=&all_descriptors;
	for (i=all_descriptors;i;lastptr=&i->next,i=i->next)
	{	if (!strcmp(name,i->name))
		{	*lastptr=i->next;
			free(i->name);
			PQclear(i->result);
			free(i);
			return true;
		}
	}
	ECPGraise(line, ECPG_UNKNOWN_DESCRIPTOR, name);
	return false;
} 

bool ECPGallocate_desc(int line,const char *name)
{
	struct descriptor *new=(struct descriptor *)malloc(sizeof(struct descriptor));
	
	new->next=all_descriptors;
	new->name=malloc(strlen(name)+1);
	new->result=PQmakeEmptyPGresult(NULL, 0);
	strcpy(new->name,name);
	all_descriptors=new;
	return true;
}

void
ECPGraise(int line, int code, const char *str)
{
	struct auto_mem *am;
	       
	sqlca.sqlcode = code;
	switch (code)
	{ 
		case ECPG_NOT_FOUND: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"No data found line %d.", line);
			break;
			
		case ECPG_OUT_OF_MEMORY: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"Out of memory in line %d.", line);
			break;
			
		case ECPG_UNSUPPORTED: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"Unsupported type %s in line %d.", str, line);
			break;
			
		case ECPG_TOO_MANY_ARGUMENTS: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"Too many arguments in line %d.", line);
			break;
		
		case ECPG_TOO_FEW_ARGUMENTS: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"Too few arguments in line %d.", line);
			break;
			
		case ECPG_MISSING_INDICATOR: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"NULL value without indicator, line %d.", line);
			break;
			
		case ECPG_UNKNOWN_DESCRIPTOR: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"descriptor %s not found, line %d.", str, line);
			break;
			
		case ECPG_INVALID_DESCRIPTOR_INDEX: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"descriptor index out of range, line %d.", line);
			break;
		
		case ECPG_UNKNOWN_DESCRIPTOR_ITEM: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"unknown descriptor item %s, line %d.", str, line);
			break;
		
		case ECPG_VAR_NOT_NUMERIC: 
			snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"variable is not a numeric type, line %d.", line);
			break;
			
		default:
		    	snprintf(sqlca.sqlerrm.sqlerrmc,sizeof(sqlca.sqlerrm.sqlerrmc),
				"SQL error #%d, line %d.",code, line);
			break;
	}
	
        /* free all memory we have allocated for the user */
        for (am = auto_allocs; am;)
        {
        	struct auto_mem *act = am;
	        
	        am = am->next;
	        free(act->pointer);
	        free(act);
	}

        auto_allocs = NULL;
}