kexi

alter.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006-2007 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 
00203                 virtual void simplifyActions(ActionDictDict &fieldActions) { Q_UNUSED(fieldActions); }
00204 
00209                 virtual bool shouldBeRemoved(ActionDictDict &fieldActions) { 
00210                     Q_UNUSED(fieldActions); return false; }
00211 
00212                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00213                     QMap<QString, QString>& fieldMap) 
00214                     { Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(fieldMap); return true; }
00215 
00216             private:
00218                 virtual tristate execute(Connection& /*conn*/, TableSchema& /*table*/) { return true; }
00219 
00221                 int m_alteringRequirements;
00222 
00224                 int m_order;
00225 
00226                 bool m_null : 1;
00227 
00228             friend class AlterTableHandler;
00229         };
00230 
00232         class KEXI_DB_EXPORT FieldActionBase : public ActionBase {
00233             public:
00234                 FieldActionBase(const QString& fieldName, int uid);
00235                 FieldActionBase(bool);
00236                 virtual ~FieldActionBase();
00237 
00239                 QString fieldName() const { return m_fieldName; }
00240 
00250                 int uid() const { return m_fieldUID; }
00251 
00253                 void setFieldName(const QString& fieldName) { m_fieldName = fieldName; }
00254 
00255             protected:
00256 
00258                 int m_fieldUID;
00259             private:
00260                 QString m_fieldName;
00261         };
00262 
00271         class KEXI_DB_EXPORT ChangeFieldPropertyAction : public FieldActionBase {
00272             public:
00273                 ChangeFieldPropertyAction(const QString& fieldName, 
00274                     const QString& propertyName, const QVariant& newValue, int uid);
00276                 ChangeFieldPropertyAction(bool null);
00277                 virtual ~ChangeFieldPropertyAction();
00278 
00279                 QString propertyName() const { return m_propertyName; }
00280                 QVariant newValue() const { return m_newValue; }
00281                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00282 
00283                 virtual void simplifyActions(ActionDictDict &fieldActions);
00284 
00285                 virtual bool shouldBeRemoved(ActionDictDict &fieldActions);
00286 
00287                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00288                     QMap<QString, QString>& fieldMap);
00289 
00290             protected:
00291                 virtual void updateAlteringRequirements();
00292 
00294                 virtual tristate execute(Connection &conn, TableSchema &table);
00295 
00296                 QString m_propertyName;
00297                 QVariant m_newValue;
00298         };
00299 
00301         class KEXI_DB_EXPORT RemoveFieldAction : public FieldActionBase {
00302             public:
00303                 RemoveFieldAction(const QString& fieldName, int uid);
00304                 RemoveFieldAction(bool);
00305                 virtual ~RemoveFieldAction();
00306 
00307                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00308 
00309                 virtual void simplifyActions(ActionDictDict &fieldActions);
00310 
00311                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00312                     QMap<QString, QString>& fieldMap);
00313 
00314             protected:
00315                 virtual void updateAlteringRequirements();
00316 
00318                 virtual tristate execute(Connection &conn, TableSchema &table);
00319         };
00320 
00322         class KEXI_DB_EXPORT InsertFieldAction : public FieldActionBase {
00323             public:
00324                 InsertFieldAction(int fieldIndex, KexiDB::Field *newField, int uid);
00325                 //copy ctor
00326                 InsertFieldAction(const InsertFieldAction& action);
00327                 InsertFieldAction(bool);
00328                 virtual ~InsertFieldAction();
00329 
00330                 int index() const { return m_index; }
00331                 void setIndex( int index ) { m_index = index; }
00332                 KexiDB::Field& field() const { return *m_field; }
00333                 void setField(KexiDB::Field* field);
00334                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00335 
00336                 virtual void simplifyActions(ActionDictDict &fieldActions);
00337 
00338                 virtual tristate updateTableSchema(TableSchema &table, Field* field, 
00339                     QMap<QString, QString>& fieldMap);
00340 
00341             protected:
00342                 virtual void updateAlteringRequirements();
00343 
00345                 virtual tristate execute(Connection &conn, TableSchema &table);
00346 
00347                 int m_index;
00348 
00349             private:
00350                 KexiDB::Field *m_field;
00351         };
00352 
00355         class KEXI_DB_EXPORT MoveFieldPositionAction : public FieldActionBase {
00356             public:
00357                 MoveFieldPositionAction(int fieldIndex, const QString& fieldName, int uid);
00358                 MoveFieldPositionAction(bool);
00359                 virtual ~MoveFieldPositionAction();
00360 
00361                 int index() const { return m_index; }
00362                 virtual QString debugString(const DebugOptions& debugOptions = DebugOptions());
00363 
00364                 virtual void simplifyActions(ActionDictDict &fieldActions);
00365 
00366             protected:
00367                 virtual void updateAlteringRequirements();
00368 
00370                 virtual tristate execute(Connection &conn, TableSchema &table);
00371 
00372                 int m_index;
00373         };
00374 
00375         AlterTableHandler(Connection &conn);
00376 
00377         virtual ~AlterTableHandler();
00378 
00380         void addAction(ActionBase* action);
00381 
00383         AlterTableHandler& operator<< ( ActionBase* action );
00384 
00386         void removeAction(int index);
00387 
00389         void clear();
00390 
00393         void setActions(const ActionList& actions);
00394 
00397         const ActionList& actions() const;
00398 
00400         class ExecutionArguments {
00401             public:
00402                 ExecutionArguments()
00403                  : debugString(0)
00404                  , requirements(0)
00405                  , result(false)
00406                  , simulate(false)
00407                  , onlyComputeRequirements(false)
00408                 {
00409                 }
00411             QString* debugString;
00413             int requirements;
00415             tristate result;
00417             bool simulate : 1;
00420             bool onlyComputeRequirements;
00421         };
00422 
00442         TableSchema* execute(const QString& tableName, ExecutionArguments & args);
00443 
00445         void debug();
00446 
00449 //      tristate simulateExecution(const QString& tableName, QString& debugString);
00450 
00457         static int alteringTypeForProperty(const QCString& propertyName);
00458 
00459     protected:
00460 //      TableSchema* executeInternal(const QString& tableName, tristate& result, bool simulate = false,
00461 //          QString* debugString = 0);
00462 
00463         class Private;
00464         Private *d;
00465 };
00466 }
00467 
00468 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys