2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
CField.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-01-13 04:29:42 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CFIELD_C
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include "CField.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int valid_field(CFIELD *_object)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
return !THIS->table || !THIS->table->conn || !THIS->table->conn->db.handle;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool exist_field(CTABLE *table, const char *name)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
DB_FIELD *fp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!name || !*name)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (table->create)
|
|
|
|
{
|
|
|
|
for (fp = table->new_fields; fp; fp = fp->next)
|
|
|
|
{
|
2018-01-08 13:12:15 +01:00
|
|
|
if (!strcasecmp(fp->name, name))
|
|
|
|
return TRUE;
|
2014-09-22 04:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return table->driver->Field.Exist(&table->conn->db, table->name, (char *)name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_field(CTABLE *table, char *name, bool must_exist)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
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;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static bool check_type(int type)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
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;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CFIELD *make_field(CTABLE *table, const char *name, bool must_exist)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
CFIELD *_object;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (check_field(table, (char *)name, must_exist))
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
_object = GB.New(GB.FindClass("Field"), NULL, NULL);
|
|
|
|
THIS->table = table;
|
|
|
|
THIS->driver = table->conn->driver;
|
|
|
|
THIS->name = GB.NewZeroString(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
return _object;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CFIELD_free_info(DB_FIELD *info)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.FreeString(&info->name);
|
|
|
|
GB.FreeString(&info->collation);
|
|
|
|
GB.StoreVariant(NULL, &info->def);
|
|
|
|
info->type = GB_T_NULL;
|
|
|
|
info->length = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_new_field(CTABLE *table, DB_FIELD *field)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
DB_FIELD **fp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
fp = &table->new_fields;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (!*fp)
|
|
|
|
{
|
|
|
|
*fp = field;
|
|
|
|
field->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
fp = &((*fp)->next);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *CFIELD_get(CTABLE *table, const char *name)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
CFIELD *field = make_field(table, name, TRUE);
|
|
|
|
table->driver->Field.Info(&table->conn->db, table->name, (char *)name, &field->info);
|
|
|
|
return field;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CFIELD_exist(CTABLE *table, const char *name)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
return exist_field(table, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFIELD_list(CTABLE *table, char ***list)
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
table->driver->Field.List(&table->conn->db, table->name, list);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFIELD_release(CTABLE *table, void *_object)
|
|
|
|
{
|
|
|
|
THIS->table = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
Field
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_METHOD_VOID(Field_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!valid_field(THIS))
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_SubCollectionRemove(THIS->table->fields, THIS->name, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.FreeString(&THIS->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
CFIELD_free_info(&THIS->info);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*BEGIN_METHOD_VOID(CFIELD_delete)
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
THIS->table->conn->driver->User.Delete(THIS->conn->handle, THIS->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD*/
|
|
|
|
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_PROPERTY(Field_Name)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.ReturnString(THIS->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_PROPERTY(Field_Type)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.ReturnInteger(THIS->info.type);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_PROPERTY(Field_Length)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.ReturnInteger(THIS->info.length);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_PROPERTY(Field_Default)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.ReturnVariant(&THIS->info.def);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_PROPERTY(Field_Table)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.ReturnObject(THIS->table);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(Field_Collation)
|
|
|
|
|
|
|
|
GB.ReturnString(THIS->info.collation);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CFieldDesc[] =
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_DECLARE("Field", sizeof(CFIELD)),
|
|
|
|
GB_NOT_CREATABLE(),
|
|
|
|
GB_HOOK_CHECK(valid_field),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_METHOD("_free", NULL, Field_free, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
//GB_METHOD("Delete", NULL, CFIELD_delete, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
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),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
.Table.Fields
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#undef THIS
|
2012-11-14 23:24:15 +01:00
|
|
|
#define THIS ((CSUBCOLLECTION *)_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
BEGIN_METHOD(Field_Add, GB_STRING name; GB_INTEGER type; GB_INTEGER length; GB_VARIANT def; GB_STRING collation)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
CTABLE *table = GB_SubCollectionContainer(THIS);
|
|
|
|
char *name = GB.ToZeroString(ARG(name));
|
|
|
|
DB_FIELD new_field, *info;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (!table->create)
|
|
|
|
{
|
|
|
|
GB.Error("Table already exists");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (DB_CheckName(name, "field"))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (check_field(table, name, FALSE))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
new_field.next = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
new_field.type = VARG(type);
|
|
|
|
if (check_type(new_field.type))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
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;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB.Alloc(POINTER(&info), sizeof(DB_FIELD));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
info->next = NULL;
|
|
|
|
info->type = new_field.type;
|
|
|
|
info->length = new_field.length;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
info->def.type = GB_T_NULL;
|
|
|
|
if (!MISSING(def))
|
|
|
|
GB.StoreVariant(ARG(def), &info->def);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
info->name = GB.NewString(STRING(name), LENGTH(name));
|
|
|
|
//DB_LowerString(info->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
if (info->type == GB_T_STRING && !MISSING(collation) && LENGTH(collation) > 0)
|
|
|
|
info->collation = GB.NewString(STRING(collation), LENGTH(collation));
|
|
|
|
else
|
|
|
|
info->collation = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
add_new_field(table, info);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CTableFieldsDesc[] =
|
|
|
|
{
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_DECLARE(".Table.Fields", 0), GB_INHERITS(".SubCollection"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_METHOD("Add", NULL, Field_Add, "(Name)s(Type)i[(Length)i(Default)v(Collation)s]"),
|
|
|
|
//GB_METHOD("Remove", NULL, CFIELD_remove, "(Name)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-09-22 04:52:05 +02:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|