kexi

alter.h
1/* This file is part of the KDE project
2 Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KEXIDB_ALTER_H
21#define KEXIDB_ALTER_H
22
23#include "connection.h"
24
25#include <tqvaluelist.h>
26#include <tqasciidict.h>
27
28#include <kdebug.h>
29
30namespace KexiDB
31{
32class Connection;
33class ConnectionData;
34
36
111class KEXI_DB_EXPORT AlterTableHandler : public Object
112{
113 public:
115 class RemoveFieldAction;
116 class InsertFieldAction;
118
122 PhysicalAlteringRequired = 1,
123
126 DataConversionRequired = 2,
127
128 /* Changes to the main table schema (in kexi__fields) required,
129 this does not require physical changes for the table;
130 e.g. changing value of the "caption" or "description" property. */
131 MainSchemaAlteringRequired = 4,
132
133 /* Only changes to extended table schema required,
134 this does not require physical changes for the table;
135 e.g. changing value of the "visibleDecimalPlaces" property
136 or any of the custom properties. */
137 ExtendedSchemaAlteringRequired = 8,
138
140 SchemaAlteringRequired = ExtendedSchemaAlteringRequired | MainSchemaAlteringRequired
141 };
142
143 class ActionBase;
144 typedef TQAsciiDict<ActionBase> ActionDict;
145 typedef TQIntDict<ActionDict> ActionDictDict;
146 typedef TQAsciiDictIterator<ActionBase> ActionDictIterator;
147 typedef TQIntDictIterator<ActionDict> ActionDictDictIterator;
148 typedef TQPtrVector<ActionBase> ActionVector;
149
151 typedef TQPtrList<ActionBase> ActionList;
152
154 typedef TQPtrListIterator<ActionBase> ActionListIterator;
155
157 class KEXI_DB_EXPORT ActionBase {
158 public:
159 ActionBase(bool null = false);
160 virtual ~ActionBase();
161
162 ChangeFieldPropertyAction& toChangeFieldPropertyAction();
163 RemoveFieldAction& toRemoveFieldAction();
164 InsertFieldAction& toInsertFieldAction();
165 MoveFieldPositionAction& toMoveFieldPositionAction();
166
169 bool isNull() const { return m_null; }
170
173 {
174 public:
175 DebugOptions() : showUID(true), showFieldDebug(false) {}
176
178 bool showUID : 1;
179
183 };
184
185 virtual TQString debugString(const DebugOptions& debugOptions = DebugOptions()) {
186 Q_UNUSED(debugOptions); return "ActionBase"; }
187 void debug(const DebugOptions& debugOptions = DebugOptions()) {
188 KexiDBDbg << debugString(debugOptions)
189 << " (req = " << alteringRequirements() << ")" << endl; }
190
191 protected:
193 void setAlteringRequirements( int alteringRequirements )
194 { m_alteringRequirements = alteringRequirements; }
195
196 int alteringRequirements() const { return m_alteringRequirements; }
197
198 virtual void updateAlteringRequirements() {};
199
203 virtual void simplifyActions(ActionDictDict &fieldActions) { Q_UNUSED(fieldActions); }
204
209 virtual bool shouldBeRemoved(ActionDictDict &fieldActions) {
210 Q_UNUSED(fieldActions); return false; }
211
212 virtual tristate updateTableSchema(TableSchema &table, Field* field,
213 TQMap<TQString, TQString>& fieldMap)
214 { Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(fieldMap); return true; }
215
216 private:
218 virtual tristate execute(Connection& /*conn*/, TableSchema& /*table*/) { return true; }
219
221 int m_alteringRequirements;
222
224 int m_order;
225
226 bool m_null : 1;
227
228 friend class AlterTableHandler;
229 };
230
232 class KEXI_DB_EXPORT FieldActionBase : public ActionBase {
233 public:
234 FieldActionBase(const TQString& fieldName, int uid);
235 FieldActionBase(bool);
236 virtual ~FieldActionBase();
237
239 TQString fieldName() const { return m_fieldName; }
240
250 int uid() const { return m_fieldUID; }
251
253 void setFieldName(const TQString& fieldName) { m_fieldName = fieldName; }
254
255 protected:
256
259 private:
260 TQString m_fieldName;
261 };
262
271 class KEXI_DB_EXPORT ChangeFieldPropertyAction : public FieldActionBase {
272 public:
273 ChangeFieldPropertyAction(const TQString& fieldName,
274 const TQString& propertyName, const TQVariant& newValue, int uid);
276 ChangeFieldPropertyAction(bool null);
278
279 TQString propertyName() const { return m_propertyName; }
280 TQVariant newValue() const { return m_newValue; }
281 virtual TQString debugString(const DebugOptions& debugOptions = DebugOptions());
282
283 virtual void simplifyActions(ActionDictDict &fieldActions);
284
285 virtual bool shouldBeRemoved(ActionDictDict &fieldActions);
286
287 virtual tristate updateTableSchema(TableSchema &table, Field* field,
288 TQMap<TQString, TQString>& fieldMap);
289
290 protected:
291 virtual void updateAlteringRequirements();
292
294 virtual tristate execute(Connection &conn, TableSchema &table);
295
296 TQString m_propertyName;
297 TQVariant m_newValue;
298 };
299
301 class KEXI_DB_EXPORT RemoveFieldAction : public FieldActionBase {
302 public:
303 RemoveFieldAction(const TQString& fieldName, int uid);
304 RemoveFieldAction(bool);
305 virtual ~RemoveFieldAction();
306
307 virtual TQString debugString(const DebugOptions& debugOptions = DebugOptions());
308
309 virtual void simplifyActions(ActionDictDict &fieldActions);
310
311 virtual tristate updateTableSchema(TableSchema &table, Field* field,
312 TQMap<TQString, TQString>& fieldMap);
313
314 protected:
315 virtual void updateAlteringRequirements();
316
318 virtual tristate execute(Connection &conn, TableSchema &table);
319 };
320
322 class KEXI_DB_EXPORT InsertFieldAction : public FieldActionBase {
323 public:
324 InsertFieldAction(int fieldIndex, KexiDB::Field *newField, int uid);
325 //copy ctor
327 InsertFieldAction(bool);
328 virtual ~InsertFieldAction();
329
330 int index() const { return m_index; }
331 void setIndex( int index ) { m_index = index; }
332 KexiDB::Field& field() const { return *m_field; }
333 void setField(KexiDB::Field* field);
334 virtual TQString debugString(const DebugOptions& debugOptions = DebugOptions());
335
336 virtual void simplifyActions(ActionDictDict &fieldActions);
337
338 virtual tristate updateTableSchema(TableSchema &table, Field* field,
339 TQMap<TQString, TQString>& fieldMap);
340
341 protected:
342 virtual void updateAlteringRequirements();
343
345 virtual tristate execute(Connection &conn, TableSchema &table);
346
347 int m_index;
348
349 private:
350 KexiDB::Field *m_field;
351 };
352
355 class KEXI_DB_EXPORT MoveFieldPositionAction : public FieldActionBase {
356 public:
357 MoveFieldPositionAction(int fieldIndex, const TQString& fieldName, int uid);
359 virtual ~MoveFieldPositionAction();
360
361 int index() const { return m_index; }
362 virtual TQString debugString(const DebugOptions& debugOptions = DebugOptions());
363
364 virtual void simplifyActions(ActionDictDict &fieldActions);
365
366 protected:
367 virtual void updateAlteringRequirements();
368
370 virtual tristate execute(Connection &conn, TableSchema &table);
371
372 int m_index;
373 };
374
376
377 virtual ~AlterTableHandler();
378
380 void addAction(ActionBase* action);
381
383 AlterTableHandler& operator<< ( ActionBase* action );
384
386 void removeAction(int index);
387
389 void clear();
390
393 void setActions(const ActionList& actions);
394
397 const ActionList& actions() const;
398
401 public:
403 : debugString(0)
404 , requirements(0)
405 , result(false)
406 , simulate(false)
407 , onlyComputeRequirements(false)
408 {
409 }
411 TQString* debugString;
417 bool simulate : 1;
421 };
422
442 TableSchema* execute(const TQString& tableName, ExecutionArguments & args);
443
445 void debug();
446
449// tristate simulateExecution(const TQString& tableName, TQString& debugString);
450
457 static int alteringTypeForProperty(const TQCString& propertyName);
458
459 protected:
460// TableSchema* executeInternal(const TQString& tableName, tristate& result, bool simulate = false,
461// TQString* debugString = 0);
462
463 class Private;
464 Private *d;
465};
466}
467
468#endif
Controls debug options for actions. Used in debugString() and debug().
Definition alter.h:173
bool showFieldDebug
true if the field associated with the action (if exists) should be appended to the debug string (defa...
Definition alter.h:182
bool showUID
true if UID should be added to the action debug string (the default)
Definition alter.h:178
Abstract base class used for implementing all the AlterTable actions.
Definition alter.h:157
void setAlteringRequirements(int alteringRequirements)
Sets requirements for altering; used internally by AlterTableHandler object.
Definition alter.h:193
virtual void simplifyActions(ActionDictDict &fieldActions)
Definition alter.h:203
virtual bool shouldBeRemoved(ActionDictDict &fieldActions)
Definition alter.h:209
Arguments for AlterTableHandler::execute().
Definition alter.h:400
Abstract base class used for implementing table field-related actions.
Definition alter.h:232
int m_fieldUID
field's unique identifier,
Definition alter.h:258
void setFieldName(const TQString &fieldName)
Sets field name for this action.
Definition alter.h:253
Defines an action for inserting a single table field.
Definition alter.h:322
Defines an action for removing a single table field.
Definition alter.h:301
A tool for handling altering database table schema.
Definition alter.h:112
TQPtrList< ActionBase > ActionList
Defines a type for action list.
Definition alter.h:151
TQIntDict< ActionDict > ActionDictDict
for collecting groups of actions by field UID
Definition alter.h:145
TQPtrListIterator< ActionBase > ActionListIterator
Defines a type for action list's iterator.
Definition alter.h:154
TQAsciiDict< ActionBase > ActionDict
for collecting actions related to a single field
Definition alter.h:144
TQPtrVector< ActionBase > ActionVector
for collecting actions related to a single field
Definition alter.h:148
AlteringRequirements
Defines flags for possible altering requirements; can be combined.
Definition alter.h:120
Provides database connection, allowing queries and data modification.
Definition connection.h:62
Meta-data for a field.
Definition field.h:73
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT void debug(const QuerySchemaParameterList &list)
Shows debug information for list.