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"
42
/* Constraint types: */
43
#define XT_DD_UNKNOWN ((u_int) -1)
45
#define XT_DD_INDEX_UNIQUE 1
46
#define XT_DD_KEY_PRIMARY 2
47
#define XT_DD_KEY_FOREIGN 3
49
#define XT_KEY_ACTION_RESTRICT 1
50
#define XT_KEY_ACTION_CASCADE 2
51
#define XT_KEY_ACTION_SET_NULL 3
52
#define XT_KEY_ACTION_SET_DEFAULT 4
53
#define XT_KEY_ACTION_NO_ACTION 5 /* Like RESTRICT, but check at end of statement. */
55
class XTDDEnumerableColumn;
56
class XTDDColumnFactory;
58
class XTDDColumn : public XTObject {
62
XTDDColumn() : XTObject(),
75
virtual XTObject *factory(XTThreadPtr self) {
78
if (!(new_obj = new XTDDColumn()))
79
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
83
virtual void init(XTThreadPtr self) {
86
virtual void init(XTThreadPtr self, XTObject *obj);
87
virtual void finalize(XTThreadPtr self);
88
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
90
virtual XTDDEnumerableColumn *castToEnumerable() {
94
friend class XTDDColumnFactory;
98
* subclass for ENUMs and SETs
100
class XTDDEnumerableColumn : public XTDDColumn {
103
XTDDEnumerableColumn() : XTDDColumn(),
104
enum_size(0), is_enum(0) {
108
int enum_size; /* number of elements in the ENUM or SET */
109
xtBool is_enum; /* TRUE if this is ENUM, FALSE if SET */
111
virtual XTObject *factory(XTThreadPtr self) {
114
if (!(new_obj = new XTDDEnumerableColumn()))
115
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
119
virtual XTDDEnumerableColumn *castToEnumerable() {
123
friend class XTDDColumnFactory;
126
class XTDDColumnRef : public XTObject {
130
XTDDColumnRef() : XTObject(), cr_col_name(NULL) { }
132
virtual XTObject *factory(XTThreadPtr self) {
135
if (!(new_obj = new XTDDColumnRef()))
136
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
140
virtual void init(XTThreadPtr self) { XTObject::init(self); }
141
virtual void init(XTThreadPtr self, XTObject *obj);
142
virtual void finalize(XTThreadPtr self);
145
class XTDDConstraint : public XTObject {
147
class XTDDTable *co_table; /* The table of this constraint (non-referenced). */
151
XTList<XTDDColumnRef> co_cols;
153
XTDDConstraint(u_int t) : XTObject(),
160
virtual void init(XTThreadPtr self) { XTObject::init(self); }
161
virtual void init(XTThreadPtr self, XTObject *obj);
162
virtual void finalize(XTThreadPtr self) {
164
xt_free(self, co_name);
166
xt_free(self, co_ind_name);
167
co_cols.deleteAll(self);
168
XTObject::finalize(self);
170
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
171
virtual void alterColumnName(XTThreadPtr self, char *from_name, char *to_name);
172
void getColumnList(char *buffer, size_t size);
173
bool sameColumns(XTDDConstraint *co);
174
bool samePrefixColumns(XTDDConstraint *co);
175
bool attachColumns();
178
class XTDDTableRef : public XTObject {
180
class XTDDTableRef *tr_next; /* The next reference in the list. */
181
class XTDDForeignKey *tr_fkey; /* The foreign key that references this table (if not-NULL). */
183
XTDDTableRef() : XTObject(), tr_next(NULL), tr_fkey(NULL) { }
184
virtual void finalize(XTThreadPtr self);
185
bool modifyRow(struct XTOpenTable *tab, xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
186
bool checkReference(xtWord1 *before, XTThreadPtr thread);
187
void deleteAllRows(XTThreadPtr self);
190
class XTDDIndex : public XTDDConstraint {
194
XTDDIndex(u_int type) : XTDDConstraint(type), in_index((u_int) -1) { }
196
virtual XTObject *factory(XTThreadPtr self) {
199
if (!(new_obj = new XTDDIndex(XT_DD_UNKNOWN)))
200
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
204
virtual void init(XTThreadPtr self) { XTDDConstraint::init(self); };
205
virtual void init(XTThreadPtr self, XTObject *obj);
206
struct XTIndex *getIndexPtr();
210
* A foreign key is based on a local index.
212
class XTDDForeignKey : public XTDDIndex {
214
XTPathStrPtr fk_ref_tab_name;
215
XTDDTable *fk_ref_table;
216
u_int fk_ref_index; /* The index on which this foreign key references. */
217
XTList<XTDDColumnRef> fk_ref_cols;
221
XTDDForeignKey() : XTDDIndex(XT_DD_KEY_FOREIGN),
222
fk_ref_tab_name(NULL),
224
fk_ref_index(UINT_MAX),
229
virtual XTObject *factory(XTThreadPtr self) {
232
if (!(new_obj = new XTDDForeignKey()))
233
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
237
virtual void init(XTThreadPtr self) { XTDDIndex::init(self); }
238
virtual void init(XTThreadPtr self, XTObject *obj);
239
virtual void finalize(XTThreadPtr self);
240
virtual void loadString(XTThreadPtr self, XTStringBufferPtr sb);
241
void getReferenceList(char *buffer, size_t size);
242
struct XTIndex *getReferenceIndexPtr();
243
bool sameReferenceColumns(XTDDConstraint *co);
244
bool samePrefixReferenceColumns(XTDDConstraint *co);
245
bool checkReferencedTypes(XTDDTable *dt);
246
void removeReference(XTThreadPtr self);
247
bool insertRow(xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
248
bool updateRow(xtWord1 *before, xtWord1 *after, XTThreadPtr thread);
250
static const char *actionTypeToString(int action);
253
class XTDDTable : public XTObject {
257
struct XTTable *dt_table;
259
XTList<XTDDColumn> dt_cols;
260
XTList<XTDDIndex> dt_indexes;
262
xt_rwlock_type dt_ref_lock; /* The lock for adding and using references. */
263
XTList<XTDDForeignKey> dt_fkeys; /* The foreign keys on this table. */
264
XTDDTableRef *dt_trefs; /* A list of tables that reference this table. */
266
virtual XTObject *factory(XTThreadPtr self) {
269
if (!(new_obj = new XTDDTable()))
270
xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
274
virtual void init(XTThreadPtr self);
275
virtual void init(XTThreadPtr self, XTObject *obj);
276
virtual void finalize(XTThreadPtr self);
278
XTDDColumn *findColumn(char *name);
279
void loadString(XTThreadPtr self, XTStringBufferPtr sb);
280
void loadForeignKeyString(XTThreadPtr self, XTStringBufferPtr sb);
281
void checkForeignKeyReference(XTThreadPtr self, XTDDForeignKey *fk);
282
void attachReferences(XTThreadPtr self, struct XTDatabase *db);
283
void attachReference(XTThreadPtr self, XTDDForeignKey *fk);
284
void alterColumnName(XTThreadPtr self, char *from_name, char *to_name);
285
void attachReference(XTThreadPtr self, XTDDTable *dt);
286
void removeReferences(XTThreadPtr self);
287
void removeReference(XTThreadPtr self, XTDDForeignKey *fk);
288
void checkForeignKeys(XTThreadPtr self, bool temp_table);
289
XTDDIndex *findIndex(XTDDConstraint *co);
290
XTDDIndex *findReferenceIndex(XTDDForeignKey *fk);
291
bool insertRow(struct XTOpenTable *rec_ot, xtWord1 *buffer);
292
bool checkNoAction(struct XTOpenTable *ot, xtRecordID rec_id);
293
xtBool checkCanDrop(xtBool drop_db);
294
bool deleteRow(struct XTOpenTable *rec_ot, xtWord1 *buffer);
295
void deleteAllRows(XTThreadPtr self);
296
bool updateRow(struct XTOpenTable *rec_ot, xtWord1 *before, xtWord1 *after);
299
XTDDTable *xt_ri_create_table(XTThreadPtr self, bool convert, XTPathStrPtr tab_path, char *sql, XTDDTable *my_tab, struct XTDictionary *source_dic);