1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* 2004-01-03 Paul McCullagh
23
* Implementation of the PBXT internal data dictionary.
26
#ifndef __datadic_xt_h__
27
#define __datadic_xt_h__
32
#include "ccutils_xt.h"
41
/* Constraint types: */
42
#define XT_DD_UNKNOWN ((u_int) -1)
44
#define XT_DD_INDEX_UNIQUE 1
45
#define XT_DD_KEY_PRIMARY 2
46
#define XT_DD_KEY_FOREIGN 3
48
#define XT_KEY_ACTION_RESTRICT 1
49
#define XT_KEY_ACTION_CASCADE 2
50
#define XT_KEY_ACTION_SET_NULL 3
51
#define XT_KEY_ACTION_SET_DEFAULT 4
52
#define XT_KEY_ACTION_NO_ACTION 5 /* Like RESTRICT, but check at end of statement. */
54
class XTDDEnumerableColumn;
55
class XTDDColumnFactory;
57
class XTDDColumn : public XTObject {
61
XTDDColumn() : XTObject(),
74
virtual XTObject *factory(XTThreadPtr self) {
77
if (!(new_obj = new XTDDColumn()))
78
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
82
virtual void init(XTThreadPtr self) {
85
virtual void init(XTThreadPtr self, XTObject *obj);
86
virtual void finalize(XTThreadPtr self);
87
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
89
virtual XTDDEnumerableColumn *castToEnumerable() {
93
friend class XTDDColumnFactory;
97
* subclass for ENUMs and SETs
99
class XTDDEnumerableColumn : public XTDDColumn {
102
XTDDEnumerableColumn() : XTDDColumn(),
103
enum_size(0), is_enum(0) {
107
int enum_size; /* number of elements in the ENUM or SET */
108
xtBool is_enum; /* TRUE if this is ENUM, FALSE if SET */
110
virtual XTObject *factory(XTThreadPtr self) {
113
if (!(new_obj = new XTDDEnumerableColumn()))
114
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
118
virtual XTDDEnumerableColumn *castToEnumerable() {
122
friend class XTDDColumnFactory;
125
class XTDDColumnRef : public XTObject {
129
XTDDColumnRef() : XTObject(), cr_col_name(NULL) { }
131
virtual XTObject *factory(XTThreadPtr self) {
134
if (!(new_obj = new XTDDColumnRef()))
135
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
139
virtual void init(XTThreadPtr self) { XTObject::init(self); }
140
virtual void init(XTThreadPtr self, XTObject *obj);
141
virtual void finalize(XTThreadPtr self);
144
class XTDDConstraint : public XTObject {
146
class XTDDTable *co_table; /* The table of this constraint (non-referenced). */
150
XTList<XTDDColumnRef> co_cols;
152
XTDDConstraint(u_int t) : XTObject(),
159
virtual void init(XTThreadPtr self) { XTObject::init(self); }
160
virtual void init(XTThreadPtr self, XTObject *obj);
161
virtual void finalize(XTThreadPtr self) {
163
xt_free(self, co_name);
165
xt_free(self, co_ind_name);
166
co_cols.deleteAll(self);
167
XTObject::finalize(self);
169
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
170
virtual void alterColumnName(XTThreadPtr self, char *from_name, char *to_name);
171
void getColumnList(char *buffer, size_t size);
172
bool sameColumns(XTDDConstraint *co);
173
bool samePrefixColumns(XTDDConstraint *co);
174
bool attachColumns();
177
class XTDDTableRef : public XTObject {
179
class XTDDTableRef *tr_next; /* The next reference in the list. */
180
class XTDDForeignKey *tr_fkey; /* The foreign key that references this table (if not-NULL). */
182
XTDDTableRef() : XTObject(), tr_next(NULL), tr_fkey(NULL) { }
183
virtual void finalize(XTThreadPtr self);
184
bool modifyRow(struct XTOpenTable *tab, xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
185
bool checkReference(xtWord1 *before, XTThreadPtr thread);
186
void deleteAllRows(XTThreadPtr self);
189
class XTDDIndex : public XTDDConstraint {
193
XTDDIndex(u_int type) : XTDDConstraint(type), in_index((u_int) -1) { }
195
virtual XTObject *factory(XTThreadPtr self) {
198
if (!(new_obj = new XTDDIndex(XT_DD_UNKNOWN)))
199
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
203
virtual void init(XTThreadPtr self) { XTDDConstraint::init(self); };
204
virtual void init(XTThreadPtr self, XTObject *obj);
205
struct XTIndex *getIndexPtr();
209
* A foreign key is based on a local index.
211
class XTDDForeignKey : public XTDDIndex {
213
XTPathStrPtr fk_ref_tab_name;
214
XTDDTable *fk_ref_table;
215
u_int fk_ref_index; /* The index on which this foreign key references. */
216
XTList<XTDDColumnRef> fk_ref_cols;
220
XTDDForeignKey() : XTDDIndex(XT_DD_KEY_FOREIGN),
221
fk_ref_tab_name(NULL),
223
fk_ref_index(UINT_MAX),
228
virtual XTObject *factory(XTThreadPtr self) {
231
if (!(new_obj = new XTDDForeignKey()))
232
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
236
virtual void init(XTThreadPtr self) { XTDDIndex::init(self); }
237
virtual void init(XTThreadPtr self, XTObject *obj);
238
virtual void finalize(XTThreadPtr self);
239
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
240
void getReferenceList(char *buffer, size_t size);
241
struct XTIndex *getReferenceIndexPtr();
242
bool sameReferenceColumns(XTDDConstraint *co);
243
bool samePrefixReferenceColumns(XTDDConstraint *co);
244
bool checkReferencedTypes(XTDDTable *dt);
245
void removeReference(XTThreadPtr self);
246
bool insertRow(xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
247
bool updateRow(xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
249
static const char *actionTypeToString(int action);
252
class XTDDTable : public XTObject {
256
struct XTTable *dt_table;
258
XTList<XTDDColumn> dt_cols;
259
XTList<XTDDIndex> dt_indexes;
261
XTRecurRWLockRec dt_ref_lock; /* The lock for adding and using references. */
262
XTList<XTDDForeignKey> dt_fkeys; /* The foreign keys on this table. */
263
XTDDTableRef *dt_trefs; /* A list of tables that reference this table. */
265
virtual XTObject *factory(XTThreadPtr self) {
268
if (!(new_obj = new XTDDTable()))
269
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
273
virtual void init(XTThreadPtr self);
274
virtual void init(XTThreadPtr self, XTObject *obj);
275
virtual void finalize(XTThreadPtr self);
277
XTDDColumn *findColumn(char *name);
278
void loadString(XTThreadPtr self, XTStringBufferPtr sb);
279
void loadForeignKeyString(XTThreadPtr self, XTStringBufferPtr sb);
280
void checkForeignKeyReference(XTThreadPtr self, XTDDForeignKey *fk);
281
void attachReferences(XTThreadPtr self, struct XTDatabase *db);
282
void attachReference(XTThreadPtr self, XTDDForeignKey *fk);
283
void alterColumnName(XTThreadPtr self, char *from_name, char *to_name);
284
void attachReference(XTThreadPtr self, XTDDTable *dt);
285
void removeReferences(XTThreadPtr self);
286
void removeReference(XTThreadPtr self, XTDDForeignKey *fk);
287
void checkForeignKeys(XTThreadPtr self, bool temp_table);
288
XTDDIndex *findIndex(XTDDConstraint *co);
289
XTDDIndex *findReferenceIndex(XTDDForeignKey *fk);
290
bool insertRow(struct XTOpenTable *rec_ot, xtWord1 *buffer);
291
bool checkNoAction(struct XTOpenTable *ot, xtRecordID rec_id);
292
xtBool checkCanDrop(xtBool drop_db);
293
bool deleteRow(struct XTOpenTable *rec_ot, xtWord1 *buffer);
294
void deleteAllRows(XTThreadPtr self);
295
bool updateRow(struct XTOpenTable *rec_ot, xtWord1 *before, xtWord1 *after);
298
XTDDTable *xt_ri_create_table(XTThreadPtr self, bool convert, XTPathStrPtr tab_path, char *sql, XTDDTable *my_tab);