File: CField.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 (329 lines) | stat: -rw-r--r-- 6,524 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
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
/***************************************************************************

	CField.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 __CFIELD_C

#include "main.h"

#include "CField.h"


static int valid_field(CFIELD *_object)
{
	return !THIS->table || !THIS->table->conn || !THIS->table->conn->db.handle;
}

static bool exist_field(CTABLE *table, const char *name)
{
	DB_FIELD *fp;

	if (!name || !*name)
		return FALSE;

	if (table->create)
	{
		for (fp = table->new_fields; fp; fp = fp->next)
		{
			if (!strcasecmp(fp->name, name))
				return TRUE;
		}

		return FALSE;
	}
	else
		return table->driver->Field.Exist(&table->conn->db, table->name, (char *)name);
}

static bool check_field(CTABLE *table, char *name, bool must_exist)
{
	bool exist = exist_field(table, name);

	if (must_exist)
	{
		if (!exist)
		{
			GB.Error("Unknown field: &1.&2", table->name, name);
			return TRUE;
		}
	}
	else
	{
		if (exist)
		{
			GB.Error("Field already exists: &1.&2", table->name, name);
			return TRUE;
		}
	}

	return FALSE;
}


static bool check_type(int type)
{
	if (type == GB_T_BOOLEAN
			|| type == GB_T_INTEGER
			|| type == GB_T_LONG
			|| type == GB_T_FLOAT
			|| type == GB_T_DATE
			|| type == GB_T_STRING
			|| type == DB_T_SERIAL
			|| type == DB_T_BLOB)
		return FALSE;

	GB.Error("Bad field type");
	return TRUE;
}


static CFIELD *make_field(CTABLE *table, const char *name, bool must_exist)
{
	CFIELD *_object;

	if (check_field(table, (char *)name, must_exist))
		return NULL;

	_object = GB.New(GB.FindClass("Field"), NULL, NULL);
	THIS->table = table;
	THIS->driver = table->conn->driver;
	THIS->name = GB.NewZeroString(name);

	return _object;
}


void CFIELD_free_info(DB_FIELD *info)
{
	GB.FreeString(&info->name);
	GB.FreeString(&info->collation);
	GB.StoreVariant(NULL, &info->def);
	info->type = GB_T_NULL;
	info->length = 0;
}

static void add_new_field(CTABLE *table, DB_FIELD *field)
{
	DB_FIELD **fp;

	fp = &table->new_fields;

	for(;;)
	{
		if (!*fp)
		{
			*fp = field;
			field->next = NULL;
			return;
		}

		fp = &((*fp)->next);
	}
}


void *CFIELD_get(CTABLE *table, const char *name)
{
	CFIELD *field = make_field(table, name, TRUE);
	table->driver->Field.Info(&table->conn->db, table->name, (char *)name, &field->info);
	return field;
}


int CFIELD_exist(CTABLE *table, const char *name)
{
	return exist_field(table, name);
}

void CFIELD_list(CTABLE *table, char ***list)
{
	table->driver->Field.List(&table->conn->db, table->name, list);
}

void CFIELD_release(CTABLE *table, void *_object)
{
	THIS->table = NULL;
}


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

	Field

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

BEGIN_METHOD_VOID(Field_free)

	if (!valid_field(THIS))
		GB_SubCollectionRemove(THIS->table->fields, THIS->name, 0);

	GB.FreeString(&THIS->name);

	CFIELD_free_info(&THIS->info);

END_METHOD



/*BEGIN_METHOD_VOID(CFIELD_delete)

	THIS->table->conn->driver->User.Delete(THIS->conn->handle, THIS->name);

END_METHOD*/


BEGIN_PROPERTY(Field_Name)

	GB.ReturnString(THIS->name);

END_PROPERTY


BEGIN_PROPERTY(Field_Type)

	GB.ReturnInteger(THIS->info.type);

END_PROPERTY


BEGIN_PROPERTY(Field_Length)

	GB.ReturnInteger(THIS->info.length);

END_PROPERTY


BEGIN_PROPERTY(Field_Default)

	GB.ReturnVariant(&THIS->info.def);

END_PROPERTY


BEGIN_PROPERTY(Field_Table)

	GB.ReturnObject(THIS->table);

END_PROPERTY


BEGIN_PROPERTY(Field_Collation)

	GB.ReturnString(THIS->info.collation);

END_PROPERTY


GB_DESC CFieldDesc[] =
{
	GB_DECLARE("Field", sizeof(CFIELD)),
	GB_NOT_CREATABLE(),
	GB_HOOK_CHECK(valid_field),

	GB_METHOD("_free", NULL, Field_free, NULL),

	//GB_METHOD("Delete", NULL, CFIELD_delete, NULL),

	GB_PROPERTY_READ("Name", "s", Field_Name),
	GB_PROPERTY_READ("Type", "i", Field_Type),
	GB_PROPERTY_READ("Length", "i", Field_Length),
	GB_PROPERTY_READ("Default", "v", Field_Default),
	GB_PROPERTY_READ("Table", "Table", Field_Table),
	GB_PROPERTY_READ("Collation", "s", Field_Collation),

	GB_END_DECLARE
};


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

	.Table.Fields

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

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

BEGIN_METHOD(Field_Add, GB_STRING name; GB_INTEGER type; GB_INTEGER length; GB_VARIANT def; GB_STRING collation)

	CTABLE *table = GB_SubCollectionContainer(THIS);
	char *name = GB.ToZeroString(ARG(name));
	DB_FIELD new_field, *info;

	if (!table->create)
	{
		GB.Error("Table already exists");
		return;
	}

	if (DB_CheckName(name, "field"))
		return;

	if (check_field(table, name, FALSE))
		return;

	new_field.next = NULL;

	new_field.type = VARG(type);
	if (check_type(new_field.type))
		return;

	new_field.length = VARGOPT(length, 0);
	if (new_field.length < 0)
		new_field.length = 0;
	else if (new_field.length > 65535)
		new_field.length = 65535;

	GB.Alloc(POINTER(&info), sizeof(DB_FIELD));

	info->next = NULL;
	info->type = new_field.type;
	info->length = new_field.length;

	info->def.type = GB_T_NULL;
	if (!MISSING(def))
		GB.StoreVariant(ARG(def), &info->def);

	info->name = GB.NewString(STRING(name), LENGTH(name));
	//DB_LowerString(info->name);

	if (info->type == GB_T_STRING && !MISSING(collation) && LENGTH(collation) > 0)
		info->collation = GB.NewString(STRING(collation), LENGTH(collation));
	else
		info->collation = NULL;

	add_new_field(table, info);

END_METHOD


GB_DESC CTableFieldsDesc[] =
{
	GB_DECLARE(".Table.Fields", 0), GB_INHERITS(".SubCollection"),

	GB_METHOD("Add", NULL, Field_Add, "(Name)s(Type)i[(Length)i(Default)v(Collation)s]"),
	//GB_METHOD("Remove", NULL, CFIELD_remove, "(Name)s"),

	GB_END_DECLARE
};