File: CResultField.c

package info (click to toggle)
gambas3 3.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,984 kB
  • sloc: ansic: 197,178; cpp: 124,076; sh: 18,999; javascript: 7,761; sql: 5,399; makefile: 2,354; perl: 1,397; xml: 490; python: 335
file content (258 lines) | stat: -rw-r--r-- 5,410 bytes parent folder | download | duplicates (3)
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
/***************************************************************************

	CResultField.c

	(c) 2000-2017 BenoƮt Minisini <benoit.minisini@gambas-basic.org>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
	MA 02110-1301, USA.

***************************************************************************/

#define __CRESULTFIELD_C

#include <stdlib.h>

#include "main.h"

#include "CResultField.h"


static int valid_result_field(CRESULTFIELD *_object)
{
	return !THIS->result || !THIS->result->conn || !THIS->result->conn->db.handle;
}


int CRESULTFIELD_find(CRESULT *result, const char *name, bool error)
{
	int index;
	char *p;

	if (!name || !*name)
		return -1;

	index = strtol(name, &p, 10);
	if (*name && *p == 0)
	{
		if (index < 0 || index >= result->info.nfield)
		{
			if (error)
				GB.Error("Bad field index");
			index = -1;
		}
	}
	else
	{
		if (result->handle)
			index = result->driver->Result.Field.Index(result->handle, (char *)name, &result->conn->db);
		else
		{
			for (index = 0; index < result->info.nfield; index++)
			{
				if (strcasecmp(name, result->info.field[index].name) == 0)
					break;
			}
		}

		if (index < 0 || index >= result->info.nfield)
		{
			if (error)
				GB.Error("Unknown field: &1", name);
			index = -1;
		}
	}

	return index;
}


static CRESULTFIELD *make_result_field(CRESULT *result, int index)
{
	CRESULTFIELD *_object;

	_object = GB.New(GB.FindClass("ResultField"), NULL, NULL);
	THIS->result = result;
	THIS->driver = result->conn->driver;
	THIS->index = index;

	return _object;
}


void *CRESULTFIELD_get(CRESULT *result, const char *name)
{
	int index;

	if ((intptr_t)name >> 16)
		index = CRESULTFIELD_find(result, name, TRUE);
	else
		index = (int)(intptr_t)name;

	if (index < 0)
		return NULL;
	else
		return make_result_field(result, index);
}


int CRESULTFIELD_exist(CRESULT *result, const char *name)
{
	return CRESULTFIELD_find(result, name, FALSE) >= 0;
}

char *CRESULTFIELD_key(CRESULT *result, int index)
{
	if (result->handle)
		return result->driver->Result.Field.Name(result->handle, index);
	else
		return result->info.field[index].name;
}

void CRESULTFIELD_release(CRESULT *result, void *_object)
{
	THIS->result = NULL;
}




/***************************************************************************

	ResultField

***************************************************************************/

BEGIN_METHOD_VOID(CRESULTFIELD_free)

	if (!valid_result_field(THIS))
		GB_SubCollectionRemove(THIS->result->fields, CRESULTFIELD_key(THIS->result, THIS->index), 0);

END_METHOD


BEGIN_PROPERTY(CRESULTFIELD_name)

	GB.ReturnNewZeroString(CRESULTFIELD_key(THIS->result, THIS->index));

END_PROPERTY


BEGIN_PROPERTY(CRESULTFIELD_type)

	CRESULT *result = THIS->result;

	if (result->handle)
		GB.ReturnInteger(result->driver->Result.Field.Type(result->handle, THIS->index));
	else
		GB.ReturnInteger(result->info.field[THIS->index].type);

END_PROPERTY


BEGIN_PROPERTY(CRESULTFIELD_length)

	CRESULT *result = THIS->result;

	if (result->handle)
		GB.ReturnInteger(result->driver->Result.Field.Length(result->handle, THIS->index));
	else
		GB.ReturnInteger(result->info.field[THIS->index].length);

END_PROPERTY


/*BEGIN_PROPERTY(CRESULTFIELD_default)

	GB.Error("No default value");

END_PROPERTY*/

BEGIN_PROPERTY(CRESULTFIELD_result)

	GB.ReturnObject(THIS->result);

END_PROPERTY



GB_DESC CResultFieldDesc[] =
{
	GB_DECLARE("ResultField", sizeof(CRESULTFIELD)),
	GB_NOT_CREATABLE(),
	GB_HOOK_CHECK(valid_result_field),

	GB_METHOD("_free", NULL, CRESULTFIELD_free, NULL),

	GB_PROPERTY_READ("Name", "s", CRESULTFIELD_name),
	GB_PROPERTY_READ("Type", "i", CRESULTFIELD_type),
	GB_PROPERTY_READ("Length", "i", CRESULTFIELD_length),

	GB_PROPERTY_READ("Result", "Result", CRESULTFIELD_result),

	GB_END_DECLARE
};



/***************************************************************************

	.Result.Fields

***************************************************************************/

#undef THIS
#define THIS ((CSUBCOLLECTION *)_object)

BEGIN_PROPERTY(CRESULTFIELD_count)

	CRESULT *result = GB_SubCollectionContainer(THIS);
	GB.ReturnInteger(result->info.nfield);

END_PROPERTY


BEGIN_METHOD_VOID(CRESULTFIELD_next)

	CRESULT *result = GB_SubCollectionContainer(THIS);
	int *index = (int *)GB.GetEnum();
	CRESULTFIELD *rf;

	if (*index >= result->info.nfield)
		GB.StopEnum();
	else
	{
		rf = GB_SubCollectionGet(THIS, CRESULTFIELD_key(result, *index), 0);
		(*index)++;
		GB.ReturnObject(rf);
	}

END_METHOD



GB_DESC CResultFieldsDesc[] =
{
	GB_DECLARE(".Result.Fields", 0), GB_INHERITS(".SubCollection"),

	GB_PROPERTY_READ("Count", "i", CRESULTFIELD_count),
	//GB_PROPERTY_READ("Length", "i", CRESULTFIELD_count),
	GB_METHOD("_next", "ResultField", CRESULTFIELD_next, NULL),

	GB_END_DECLARE
};