kexi

alter.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KEXIDB_ALTER_H
00021 #define KEXIDB_ALTER_H
00022 
00023 #include "connection.h"
00024 
00025 #include <qvaluelist.h>
00026 #include <qasciidict.h>
00027 
00028 #include <kdebug.h>
00029 
00030 namespace KexiDB
00031 {
00032 class Connection;
00033 class ConnectionData;
00034 
00036 
00111 class KEXI_DB_EXPORT AlterTableHandler : public Object
00112 {
00113     public:
00114         class ChangeFieldPropertyAction;
00115         class RemoveFieldAction;
00116         class InsertFieldAction;
00117         class MoveFieldPositionAction;
00118 
00120         enum AlteringRequirements {
00122             PhysicalAlteringRequired = 1,
00123 
00126             DataConversionRequired = 2,
00127 
00128             /* Changes to the main table schema (in kexi__fields) required,
00129              this does not require physical changes for the table; 
00130              e.g. changing value of the "caption" or "description" property. */
00131             MainSchemaAlteringRequired = 4,
00132 
00133             /* Only changes to extended table schema required,
00134              this does not require physical changes for the table; 
00135              e.g. changing value of the "visibleDecimalPlaces" property
00136              or any of the custom properties. */
00137             ExtendedSchemaAlteringRequired = 8,
00138 
00140             SchemaAlteringRequired = ExtendedSchemaAlteringRequired | MainSchemaAlteringRequired
00141         };
00142 
00143         class ActionBase;
00144         typedef QAsciiDict<ActionBase> ActionDict; 
00145         typedef QIntDict<ActionDict> ActionDictDict; 
00146         typedef QAsciiDictIterator<ActionBase> ActionDictIterator;
00147         typedef QIntDictIterator<ActionDict> ActionDictDictIterator;
00148         typedef QPtrVector<ActionBase> ActionVector; 
00149 
00151         typedef QPtrList<ActionBase> ActionList;
00152 
00154         typedef QPtrListIterator<ActionBase> ActionListIterator;
00155 
00157         class KEXI_DB_EXPORT ActionBase {
00158             public:
00159                 ActionBase(bool null = false);
00160                 virtual ~ActionBase();
00161 
00162                 ChangeFieldPropertyAction& toChangeFieldPropertyAction();
00163                 RemoveFieldAction& toRemoveFieldAction();
00164                 InsertFieldAction& toInsertFieldAction();
00165                 MoveFieldPositionAction& toMoveFieldPositionAction();
00166                 
00169                 bool isNull() const { return m_null; }
00170 
00172                 class DebugOptions
00173                 {
00174                     public:
00175                         DebugOptions() : showUID(true), showFieldDebug(false) {}
00176 
00178                         bool showUID : 1;
00179 
00182                         bool showFieldDebug : 1;
00183                 };
00184 
00185                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions()) { 
00186                     Q_UNUSED(debugOptions); return "ActionBase"; }
00187                 void debug(const DebugOptions& debugOptions = DebugOptions()) { 
00188                     KexiDBDbg << debugString(debugOptions) 
00189                         << " (req = " << alteringRequirements() << ")" << endl; }
00190 
00191             protected:
00193                 void setAlteringRequirements( int alteringRequirements )
00194                     { m_alteringRequirements = alteringRequirements; }
00195 
00196                 int alteringRequirements() const { return m_alteringRequirements; }
00197 
00198                 virtual void updateAlteringRequirements() {};
00199 
00200                 virtual void simplifyActions(ActionDictDict &fieldActions);
00201 
00202                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00203                     QMap<QString, QString>& fieldMap) 
00204                     { Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(fieldMap); return true; }
00205 
00206             private:
00208                 virtual tristate execute(Connection& /*conn*/, TableSchema& /*table*/) { return true; }
00209 
00211                 int m_alteringRequirements;
00212 
00214                 int m_order;
00215 
00216                 bool m_null : 1;
00217 
00218             friend class AlterTableHandler;
00219         };
00220 
00222         class KEXI_DB_EXPORT FieldActionBase : public ActionBase {
00223             public:
00224                 FieldActionBase(const QString& fieldName, int uid);
00225                 FieldActionBase(bool);
00226                 virtual ~FieldActionBase();
00227 
00229                 QString fieldName() const { return m_fieldName; }
00230 
00240                 int uid() const { return m_fieldUID; }
00241 
00243                 void setFieldName(const QString& fieldName) { m_fieldName = fieldName; }
00244 
00245             protected:
00246 
00248                 int m_fieldUID;
00249             private:
00250                 QString m_fieldName;
00251         };
00252 
00261         class KEXI_DB_EXPORT ChangeFieldPropertyAction : public FieldActionBase {
00262             public:
00263                 ChangeFieldPropertyAction(const QString& fieldName, 
00264                     const QString& propertyName, const QVariant& newValue, int uid);
00266                 ChangeFieldPropertyAction(bool null);
00267                 virtual ~ChangeFieldPropertyAction();
00268 
00269                 QString propertyName() const { return m_propertyName; }
00270                 QVariant newValue() const { return m_newValue; }
00271                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00272 
00273                 virtual void simplifyActions(ActionDictDict &fieldActions);
00274 
00275                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00276                     QMap<QString, QString>& fieldMap);
00277 
00278             protected:
00279                 virtual void updateAlteringRequirements();
00280 
00282                 virtual tristate execute(Connection &conn, TableSchema &table);
00283 
00284                 QString m_propertyName;
00285                 QVariant m_newValue;
00286         };
00287 
00289         class KEXI_DB_EXPORT RemoveFieldAction : public FieldActionBase {
00290             public:
00291                 RemoveFieldAction(const QString& fieldName, int uid);
00292                 RemoveFieldAction(bool);
00293                 virtual ~RemoveFieldAction();
00294 
00295                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00296 
00297                 virtual void simplifyActions(ActionDictDict &fieldActions);
00298 
00299                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00300                     QMap<QString, QString>& fieldMap);
00301 
00302             protected:
00303                 virtual void updateAlteringRequirements();
00304 
00306                 virtual tristate execute(Connection &conn, TableSchema &table);
00307         };
00308 
00310         class KEXI_DB_EXPORT InsertFieldAction : public FieldActionBase {
00311             public:
00312                 InsertFieldAction(int fieldIndex, KexiDB::Field *newField, int uid);
00313                 //copy ctor
00314                 InsertFieldAction(const InsertFieldAction& action);
00315                 InsertFieldAction(bool);
00316                 virtual ~InsertFieldAction();
00317 
00318                 int index() const { return m_index; }
00319                 void setIndex( int index ) { m_index = index; }
00320                 KexiDB::Field& field() const { return *m_field; }
00321                 void setField(KexiDB::Field* field);
00322                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00323 
00324                 virtual void simplifyActions(ActionDictDict &fieldActions);
00325 
00326                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00327                     QMap<QString, QString>& fieldMap);
00328 
00329             protected:
00330                 virtual void updateAlteringRequirements();
00331 
00333                 virtual tristate execute(Connection &conn, TableSchema &table);
00334 
00335                 int m_index;
00336 
00337             private:
00338                 KexiDB::Field *m_field;
00339         };
00340 
00343         class KEXI_DB_EXPORT MoveFieldPositionAction : public FieldActionBase {
00344             public:
00345                 MoveFieldPositionAction(int fieldIndex, const QString& fieldName, int uid);
00346                 MoveFieldPositionAction(bool);
00347                 virtual ~MoveFieldPositionAction();
00348 
00349                 int index() const { return m_index; }
00350                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00351 
00352                 virtual void simplifyActions(ActionDictDict &fieldActions);
00353 
00354             protected:
00355                 virtual void updateAlteringRequirements();
00356 
00358                 virtual tristate execute(Connection &conn, TableSchema &table);
00359 
00360                 int m_index;
00361         };
00362 
00363         AlterTableHandler(Connection &conn);
00364 
00365         virtual ~AlterTableHandler();
00366 
00368         void addAction(ActionBase* action);
00369 
00371         AlterTableHandler& operator<< ( ActionBase* action );
00372 
00374         void removeAction(int index);
00375 
00377         void clear();
00378 
00381         void setActions(const ActionList& actions);
00382 
00385         const ActionList& actions() const;
00386 
00406         TableSchema* execute(const QString& tableName, tristate& result, bool simulate = false);
00407 
00408         void debug();
00409 
00412         tristate simulateExecution(const QString& tableName, QString& debugString);
00413 
00414     protected:
00415         TableSchema* executeInternal(const QString& tableName, tristate& result, bool simulate = false,
00416             QString* debugString = 0);
00417 
00418         class Private;
00419         Private *d;
00420 };
00421 }
00422 
00423 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys