2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
CTable.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01: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
|
|
|
|
2012-02-25 02:35:55 +01: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
|
|
|
|
2012-02-25 02:35:55 +01: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 __CTABLE_C
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include "CField.h"
|
|
|
|
#include "CIndex.h"
|
|
|
|
#include "CTable.h"
|
|
|
|
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
static SUBCOLLECTION_DESC _fields_desc =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
".Table.Fields",
|
|
|
|
(void *)CFIELD_get,
|
|
|
|
(void *)CFIELD_exist,
|
|
|
|
(void *)CFIELD_list,
|
|
|
|
(void *)CFIELD_release
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
static SUBCOLLECTION_DESC _indexes_desc =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
".Table.Indexes",
|
|
|
|
(void *)CINDEX_get,
|
|
|
|
(void *)CINDEX_exist,
|
|
|
|
(void *)CINDEX_list,
|
|
|
|
(void *)CINDEX_release
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int valid_table(CTABLE *_object)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
return !THIS->conn || !THIS->conn->db.handle;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool check_table(CCONNECTION *conn, char *name, bool must_exist)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
bool exist = conn->driver->Table.Exist(&conn->db, name);
|
|
|
|
|
|
|
|
if (must_exist)
|
|
|
|
{
|
|
|
|
if (!exist)
|
|
|
|
{
|
|
|
|
GB.Error("Unknown table: &1", name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (exist)
|
|
|
|
{
|
|
|
|
GB.Error("Table already exists: &1", name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CTABLE *make_table(CCONNECTION *conn, const char *name, bool must_exist)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
CTABLE *_object;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (check_table(conn, (char *)name, must_exist))
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "make_table: '%s'\n", name);
|
2012-02-25 02:35:55 +01:00
|
|
|
|
|
|
|
_object = GB.New(GB.FindClass("Table"), NULL, NULL);
|
|
|
|
THIS->conn = conn;
|
|
|
|
//GB.Ref(conn);
|
|
|
|
THIS->driver = conn->driver;
|
|
|
|
THIS->name = GB.NewZeroString(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "make_table: -> %p '%s'\n", THIS, THIS->name);
|
2012-02-25 02:35:55 +01:00
|
|
|
|
|
|
|
return _object;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *CTABLE_get(CCONNECTION *conn, const char *name)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
return make_table(conn, name, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CTABLE_exist(CCONNECTION *conn, const char *name)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
return conn->driver->Table.Exist(&conn->db, (char *)name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CTABLE_list(CCONNECTION *conn, char ***list)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
conn->driver->Table.List(&conn->db, list);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTABLE_release(CCONNECTION *conn, void *_object)
|
|
|
|
{
|
|
|
|
THIS->conn = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
Table
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
static void free_new_fields(CTABLE *_object)
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
DB_FIELD *fp;
|
|
|
|
DB_FIELD *next;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
for (fp = THIS->new_fields; fp; fp = next)
|
|
|
|
{
|
|
|
|
next = fp->next;
|
|
|
|
CFIELD_free_info(fp);
|
|
|
|
GB.Free(POINTER(&fp));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
THIS->new_fields = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_primary_key)
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_ARRAY primary;
|
|
|
|
int i, n;
|
|
|
|
char *field;
|
|
|
|
|
|
|
|
if (THIS->create)
|
|
|
|
{
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
if (!THIS->primary)
|
|
|
|
{
|
|
|
|
GB.ReturnNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB.ReturnObject(DB_StringArrayToGambasArray(THIS->primary));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
primary = (GB_ARRAY)VPROP(GB_OBJECT);
|
|
|
|
if (primary)
|
|
|
|
n = GB.Array.Count(primary);
|
|
|
|
else
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
field = *((char **)GB.Array.Get(primary, i));
|
|
|
|
if (!CFIELD_exist(THIS, field))
|
|
|
|
{
|
|
|
|
GB.Error("Unknown field: &1", field);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DB_FreeStringArray(&THIS->primary);
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
GB.NewArray(&THIS->primary, sizeof(char *), n);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
THIS->primary[i] = GB.NewZeroString(*((char **)GB.Array.Get(primary, i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
if (THIS->driver->Table.PrimaryKey(&THIS->conn->db, THIS->name, &THIS->primary))
|
|
|
|
return;
|
|
|
|
|
|
|
|
GB.ReturnObject(DB_StringArrayToGambasArray(THIS->primary));
|
|
|
|
DB_FreeStringArray(&THIS->primary);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
GB.Error("Read-only property");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_name)
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.ReturnString(THIS->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_system)
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.ReturnBoolean(THIS->driver->Table.IsSystem(&THIS->conn->db, THIS->name));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_type)
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
char *type;
|
|
|
|
|
|
|
|
if (THIS->create)
|
|
|
|
{
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnString(THIS->type);
|
|
|
|
else
|
|
|
|
GB.StoreString(PROP(GB_STRING), &THIS->type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
type = THIS->driver->Table.Type(&THIS->conn->db, THIS->name, NULL);
|
|
|
|
if (type)
|
|
|
|
GB.ReturnNewZeroString(type);
|
|
|
|
else
|
|
|
|
GB.ReturnVoidString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
THIS->driver->Table.Type(&THIS->conn->db, THIS->name, GB.ToZeroString(PROP(GB_STRING)));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CTABLE_update)
|
|
|
|
|
|
|
|
DB_FIELD *fp;
|
|
|
|
DB_FIELD *fp_serial = NULL;
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (!THIS->new_fields)
|
|
|
|
{
|
|
|
|
GB.Error("No field");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (fp = THIS->new_fields; fp; fp = fp->next)
|
|
|
|
{
|
|
|
|
if (fp->type == DB_T_SERIAL)
|
|
|
|
{
|
|
|
|
if (THIS->conn->db.flags.no_serial)
|
|
|
|
{
|
|
|
|
GB.Error("Serial fields are not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fp_serial)
|
|
|
|
{
|
|
|
|
GB.Error("Only one serial field is allowed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fp_serial = fp;
|
|
|
|
}
|
|
|
|
else if (fp->type == DB_T_BLOB)
|
|
|
|
{
|
|
|
|
if (THIS->conn->db.flags.no_blob)
|
|
|
|
{
|
|
|
|
GB.Error("Blob fields are not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (fp_serial)
|
|
|
|
{
|
|
|
|
if (!(THIS->primary && GB.Count(THIS->primary) == 1 && strcmp(THIS->primary[0], fp_serial->name) == 0))
|
|
|
|
{
|
|
|
|
GB.Error("The serial field must be the primary key");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
/*if (!THIS->primary || GB.Count(THIS->primary) == 0)
|
|
|
|
{
|
|
|
|
GB.Error("No primary key");
|
|
|
|
return;
|
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (THIS->driver->Table.Create(&THIS->conn->db, THIS->name, THIS->new_fields, THIS->primary, THIS->type))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
free_new_fields(THIS);
|
|
|
|
DB_FreeStringArray(&THIS->primary);
|
|
|
|
THIS->create = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
//GB.Unref(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CTABLE_free)
|
|
|
|
|
|
|
|
//fprintf(stderr, "CTABLE_free: %p '%s'\n", THIS, THIS->name);
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (!valid_table(THIS))
|
2012-11-14 23:24:15 +01:00
|
|
|
GB_SubCollectionRemove(THIS->conn->tables, THIS->name, 0);
|
2012-02-25 02:35:55 +01:00
|
|
|
//GB.Unref(POINTER(&THIS->conn));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.FreeString(&THIS->name);
|
|
|
|
GB.FreeString(&THIS->type);
|
|
|
|
DB_FreeStringArray(&THIS->primary);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.Unref(POINTER(&THIS->fields));
|
|
|
|
GB.Unref(POINTER(&THIS->indexes));
|
|
|
|
|
|
|
|
free_new_fields(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_fields)
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
GB_SubCollectionNew(&THIS->fields, &_fields_desc, THIS);
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.ReturnObject(THIS->fields);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_indexes)
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
GB_SubCollectionNew(&THIS->indexes, &_indexes_desc, THIS);
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.ReturnObject(THIS->indexes);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CTABLE_connection)
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB.ReturnObject(THIS->conn);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CTableDesc[] =
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_DECLARE("Table", sizeof(CTABLE)),
|
|
|
|
GB_NOT_CREATABLE(),
|
|
|
|
GB_HOOK_CHECK(valid_table),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_METHOD("_free", NULL, CTABLE_free, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
//GB_METHOD("AddField", NULL, CTABLE_add_field, "(Name)s(Type)i[(Length)i(Default)v"])
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_PROPERTY_READ("Name", "s", CTABLE_name),
|
|
|
|
GB_PROPERTY_READ("System", "b", CTABLE_system),
|
|
|
|
GB_PROPERTY("PrimaryKey", "String[]", CTABLE_primary_key),
|
|
|
|
GB_PROPERTY("Type", "s", CTABLE_type),
|
|
|
|
GB_PROPERTY_READ("Connection", "Connection", CTABLE_connection),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_METHOD("Update", NULL, CTABLE_update, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_PROPERTY_READ("Fields", ".Table.Fields", CTABLE_fields),
|
|
|
|
GB_PROPERTY_READ("Indexes", ".Table.Indexes", CTABLE_indexes),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
.Connection.Tables
|
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
|
|
|
|
|
|
|
BEGIN_METHOD(CTABLE_add, GB_STRING name; GB_STRING type)
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
CCONNECTION *conn = GB_SubCollectionContainer(THIS);
|
2012-02-25 02:35:55 +01:00
|
|
|
CTABLE *table;
|
|
|
|
char *name = GB.ToZeroString(ARG(name));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (DB_CheckNameWith(name, "table", "."))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
table = make_table(conn, name, FALSE);
|
|
|
|
if (!table)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
GB_SubCollectionAdd(THIS, STRING(name), LENGTH(name), table);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (!MISSING(type))
|
|
|
|
GB.StoreString(ARG(type), &table->type);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
table->create = TRUE;
|
|
|
|
GB.ReturnObject(table);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CTABLE_remove, GB_STRING name)
|
|
|
|
|
2012-11-14 23:24:15 +01:00
|
|
|
CCONNECTION *conn = GB_SubCollectionContainer(THIS);
|
2012-02-25 02:35:55 +01:00
|
|
|
char *name = GB.ToZeroString(ARG(name));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-11-20 02:37:26 +01:00
|
|
|
GB_SubCollectionRemove(THIS, STRING(name), LENGTH(name));
|
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
if (check_table(conn, name, TRUE))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
conn->driver->Table.Delete(&conn->db, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
GB_DESC CConnectionTablesDesc[] =
|
|
|
|
{
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_DECLARE(".Connection.Tables", 0), GB_INHERITS(".SubCollection"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_METHOD("Add", "Table", CTABLE_add, "(Name)s[(Type)s]"),
|
|
|
|
GB_METHOD("Remove", NULL, CTABLE_remove, "(Name)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-02-25 02:35:55 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|