File indexing completed on 2024-04-28 16:30:31

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGDOCUMENT_H
0007 #define SKGDOCUMENT_H
0008 /** @file
0009  * This file defines classes SKGDocument.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qdatetime.h>
0014 #include <qfuturewatcher.h>
0015 #include <qhash.h>
0016 #include <qicon.h>
0017 #include <qsqldatabase.h>
0018 #include <qstringlist.h>
0019 #include <qvariant.h>
0020 
0021 #include <functional>
0022 
0023 #include "skgbasemodeler_export.h"
0024 #include "skgobjectbase.h"
0025 #include "skgservices.h"
0026 
0027 class SKGObjectBase;
0028 class SKGError;
0029 class SKGPropertyObject;
0030 class SKGReport;
0031 class SKGDocumentPrivate;
0032 
0033 using FuncSelect = std::function<void (const SKGStringListList&)>;  // NOLINT(whitespace/parens)
0034 using FuncExist =  std::function<void (bool)>;  // NOLINT(whitespace/parens)
0035 using FuncProgress = std::function<int (int, qint64, const QString&, void*)>;  // NOLINT(whitespace/parens)
0036 /**
0037 * This class manages skg documents
0038 */
0039 class SKGBASEMODELER_EXPORT SKGDocument : public QObject
0040 {
0041     Q_OBJECT
0042     Q_CLASSINFO("D-Bus Interface", "org.skg.SKGDocument")
0043 
0044     using progressFunction = int (*)(int, qint64, const QString&, void*);
0045     /**
0046      * The unique identifier
0047      */
0048     Q_PROPERTY(QString uuid READ getUniqueIdentifier NOTIFY modified)
0049 
0050     /**
0051      * The current file name
0052      */
0053     Q_PROPERTY(QString fileName READ getCurrentFileName NOTIFY modified)
0054 
0055     /**
0056      * To know if the document is read only
0057      */
0058     Q_PROPERTY(bool readOnly READ isReadOnly NOTIFY modified)
0059 
0060     /**
0061      * To know if the document is modifier
0062      */
0063     Q_PROPERTY(bool modified READ isFileModified NOTIFY transactionSuccessfullyEnded)
0064 
0065 public:
0066     /**
0067      * This enumerate defines a type of modification
0068      */
0069     enum ModificationType {
0070         U, /**< Update */
0071         I, /**< Insert */
0072         D  /**< Delete */
0073     };
0074     /**
0075      * This enumerate defines a type of modification
0076      */
0077     Q_ENUM(ModificationType)
0078 
0079     /**
0080      * This enumerate defines a type of message
0081      */
0082     enum MessageType {
0083         Positive,     /**< Positive */
0084         Information,  /**< Information */
0085         Warning,      /**< Warning */
0086         Error,        /**< Error */
0087         Hidden        /**< Hidden */
0088     };
0089 
0090     /**
0091      * This enumerate defines a type of message
0092      */
0093     Q_ENUM(MessageType)
0094 
0095     /**
0096      * Describe a modification of an object
0097      */
0098     struct SKGObjectModification {
0099         QString uuid;               /**< The uuid of the object */
0100         int id{};                     /**< The id of the object */
0101         QString table;              /**< The table of the object */
0102         ModificationType type;      /**< The type of modification */
0103     };
0104 
0105     /**
0106      * Describe a modification of an object
0107      */
0108     struct SKGMessage {
0109         QString Text;               /**< The text of the message */
0110         MessageType Type;           /**< The type of the message */
0111         QString Action;             /**< The action associated */
0112     };
0113 
0114     /**
0115      * This structure represents a template to display data
0116      */
0117     struct SKGModelTemplate {
0118         QString id;         /**< Identifier of the schema */
0119         QString name;       /**< The nls name */
0120         QString icon;       /**< The icon */
0121         QString schema;     /**< The schema. The format of this string is the following one: attribute name[|visibility Y or N[|size]];attribute name[|visibility Y or N[|size]];… */
0122     };
0123 
0124     /**
0125      * A list of SKGModelTemplate ==> SKGModelTemplateList
0126      */
0127     using SKGModelTemplateList = QVector<SKGModelTemplate>;
0128 
0129     /**
0130      * A list of SKGObjectModification ==> SKGObjectModificationList
0131      */
0132     using SKGObjectModificationList = QVector<SKGObjectModification>;
0133 
0134     /**
0135      * A list of SKGMessage ==> SKGObjectModificationList
0136      */
0137     using SKGMessageList = QVector<SKGMessage>;
0138 
0139     /**
0140      * This enumerate defines the direction of the UNDO / REDO mechanism
0141      */
0142     enum UndoRedoMode {
0143         UNDOLASTSAVE,   /**< To do/get an undo=cancel of the last successfully extecuted transactions until last save */
0144         UNDO,       /**< To do/get an undo=cancel of the last successfully extecuted transaction */
0145         REDO        /**< To do/get a redo=replay the last cancelled transaction */
0146     };
0147     /**
0148      * This enumerate defines the direction of the UNDO / REDO mechanism
0149      */
0150     Q_ENUM(UndoRedoMode)
0151 
0152     /**
0153      * Constructor
0154      */
0155     explicit SKGDocument();
0156 
0157     /**
0158      * Destructor
0159      */
0160     ~SKGDocument() override;
0161 
0162     /**
0163      * Set the callback function to follow the progress of the transaction.
0164      * the first parameter is the progress between 0% and 100%.
0165      * the callback must return 0 to continue and !=0 to cancel the transaction.
0166      * @param iProgressFunction the pointer of the function
0167      * @param iData the data for the progress call back
0168      * @return an object managing the error
0169      *   @see SKGError
0170      */
0171     virtual SKGError setProgressCallback(FuncProgress iProgressFunction, void* iData);
0172 
0173     /**
0174      * Add a check function at the end of successfully executed transaction.
0175      * If an error is returned, the transaction will be cancelled.
0176      * @param iCheckFunction the pointer of the function
0177      * @return an object managing the error
0178      *   @see SKGError
0179      */
0180     virtual SKGError addEndOfTransactionCheck(SKGError(*iCheckFunction)(SKGDocument*));
0181 
0182     /**
0183      * Get unique identifier
0184      * @return the unique identifier
0185      */
0186     virtual QString getUniqueIdentifier() const;
0187 
0188     /**
0189      * Get database identifier
0190      * @return the database identifier
0191      */
0192     virtual QString getDatabaseIdentifier() const;
0193 
0194     /**
0195      * Get message attached to a transaction.
0196      * @param iIdTransaction the identifier of a transaction
0197      * @param oMessages the messages
0198      * @param iAll to get all message (true) or only non hidden messages (false)
0199      * @return an object managing the error
0200      *   @see SKGError
0201      */
0202     virtual SKGError getMessages(int iIdTransaction, SKGMessageList& oMessages, bool iAll = true);
0203 
0204     /**
0205      * Remove all message attached to a transaction.
0206      * @param iIdTransaction the identifier of a transaction
0207      * @return an object managing the error
0208      *   @see SKGError
0209      */
0210     virtual SKGError removeMessages(int iIdTransaction);
0211 
0212     /**
0213      * Get list of direct modifications done in a transaction
0214      * @param iIdTransaction the identifier of a transaction
0215      * @param oModifications list of modifications
0216      * @return an object managing the error
0217      *   @see SKGError
0218      */
0219     virtual SKGError getModifications(int iIdTransaction, SKGObjectModificationList& oModifications) const;
0220 
0221     /**
0222      * Undo or redo the last transaction.
0223      * @param iMode the mode
0224      * @return an object managing the error
0225      *   @see SKGError
0226      */
0227     virtual SKGError undoRedoTransaction(UndoRedoMode iMode = SKGDocument::UNDO);
0228 
0229     /**
0230      * Group transactions
0231      * @param iFrom the first id transaction of the group, it will be the master of the group.
0232      * @param iTo the last id transaction of the group.
0233      * @return an object managing the error
0234      *   @see SKGError
0235      */
0236     virtual SKGError groupTransactions(int iFrom, int iTo);
0237 
0238     /**
0239      * Return the number of transaction stored including the current one
0240     * @param iMode the mode
0241     * @return the number of transaction
0242      */
0243     virtual int getNbTransaction(UndoRedoMode iMode = SKGDocument::UNDO) const;
0244 
0245     /**
0246      * Return the internal identifier of the transaction
0247      * which must be treated for an undo or a redo
0248      * @param iMode the mode
0249      * @param oName if you want also the name of the transaction
0250      * @param oSaveStep if you want also to know if it is a save step
0251      * @param oDate if you want also the date of the transaction
0252      * @param oRefreshViews if you want also to know if views must be refreshed
0253      * @return the internal identifier of the last transaction
0254      * 0 if no transaction found
0255      */
0256     virtual int getTransactionToProcess(UndoRedoMode iMode, QString* oName = nullptr, bool* oSaveStep = nullptr, QDateTime* oDate = nullptr, bool* oRefreshViews = nullptr) const;
0257 
0258     /**
0259      * Return the identifier of the current transaction
0260      * @return the internal identifier of the current transaction
0261      * 0 if no transaction found
0262      */
0263     virtual int getCurrentTransaction() const;
0264 
0265     /**
0266      * Return the depth of the current transaction
0267      * @return the depth
0268      */
0269     virtual int getDepthTransaction() const;
0270 
0271     /**
0272      * To know if a transaction is opened or not
0273      * @return an object managing the error.
0274      *   @see SKGError
0275      */
0276     virtual SKGError checkExistingTransaction() const;
0277 
0278     /**
0279      * Get the current password.
0280      * @return the password
0281      */
0282     virtual QString getPassword() const;
0283 
0284     /**
0285      * To know if the current document has been modified or not.
0286      * @return true: the document has been modified, save is possible/needed.
0287      *         false: the document hasn't been modified, save is not needed.
0288      */
0289     virtual bool isFileModified() const;
0290 
0291     /**
0292      * To know if the document is loaded in read only.
0293      * @return true: the document is loaded in read only.
0294      *         false: the document is loaded in read write.
0295      */
0296     virtual bool isReadOnly() const;
0297 
0298     /**
0299      * Return the file name of the current document.
0300      * To set it, you must use saveAs.
0301      * @return the file name of the current document.
0302      */
0303     virtual QString getCurrentFileName() const;
0304 
0305     /**
0306      * Set a parameter.
0307      * WARNING: This method must be used in a transaction.
0308      * @see beginTransaction
0309      * @param iName the parameter unique identifier.
0310      * @param iValue the parameter value.
0311      * @param iFileName the file name.
0312      * @param iParentUUID the unique identifier of the object owning this parameter.
0313      * @param oObjectCreated the parameter object created. Can be nullptr
0314      * @return an object managing the error.
0315      *   @see SKGError
0316      */
0317     virtual SKGError setParameter(const QString& iName, const QString& iValue,
0318                                   const QString& iFileName = QString(),
0319                                   const QString& iParentUUID = QStringLiteral("document"),
0320                                   SKGPropertyObject* oObjectCreated = nullptr) const;
0321 
0322     /**
0323      * Set a parameter.
0324      * WARNING: This method must be used in a transaction.
0325      * @see beginTransaction
0326      * @param iName the parameter unique identifier.
0327      * @param iValue the parameter value.
0328      * @param iBlob the parameter blob.
0329      * @param iParentUUID the unique identifier of the object owning this parameter.
0330      * @param oObjectCreated the parameter object created. Can be nullptr
0331      * @return an object managing the error.
0332      *   @see SKGError
0333      */
0334     virtual SKGError setParameter(const QString& iName, const QString& iValue,
0335                                   const QVariant& iBlob,
0336                                   const QString& iParentUUID = QStringLiteral("document"),
0337                                   SKGPropertyObject* oObjectCreated = nullptr) const;
0338 
0339     /**
0340      * Get the list of parameters
0341      * @param iParentUUID the unique identifier of the object owning parameters.
0342      * @param iWhereClause the additional where clause.
0343      * @return the list of parameters.
0344      */
0345     virtual QStringList getParameters(const QString& iParentUUID, const QString& iWhereClause = QString());
0346 
0347     /**
0348      * Get a parameter value
0349      * @param iName the parameter unique identifier.
0350      * @param iParentUUID the unique identifier of the object owning this parameter.
0351      * @return the value.
0352      */
0353     virtual QString getParameter(const QString& iName, const QString& iParentUUID = QStringLiteral("document")) const;
0354 
0355     /**
0356      * Get a parameter blob
0357      * @param iName the parameter unique identifier.
0358      * @param iParentUUID the unique identifier of the object owning this parameter.
0359      * @return the blob.
0360      */
0361     virtual QVariant getParameterBlob(const QString& iName, const QString& iParentUUID = QStringLiteral("document")) const;
0362 
0363     /**
0364      * Get the database pointer.
0365      * This is the database connection of the main thread
0366      * @return the database pointer.
0367      *   MUST NOT BE REMOVED
0368      */
0369     QSqlDatabase* getMainDatabase() const;
0370 
0371     /**
0372      * Get the database pointer.
0373      * This is the database connection of the current thread
0374      * @return the database pointer.
0375      */
0376     virtual QSqlDatabase getThreadDatabase() const;
0377 
0378     /**
0379      * dump the document in the std output.
0380      * It is useful for debug.
0381      * @param iMode the select what you want to dump.
0382      * @code
0383      * document->dump (DUMPPARAMETERS|DUMPTRANSACTIONS);
0384      * @endcode
0385      * @return an object managing the error.
0386      *   @see SKGError
0387      */
0388     virtual SKGError dump(int iMode = DUMPPARAMETERS | DUMPTRANSACTIONS) const;
0389 
0390     /**
0391      * Create a consolidated view
0392      * @param iTable Table name
0393      * @param iAsColumn Attribute used as column names
0394      * @param iAsRow Attribute used as lines names
0395      * @param iAttribute Attribute
0396      * @param iOpAtt Transaction to apply on @p iAttribute
0397      * @param iWhereClause Where clause
0398      * @param oTable the consolidated view
0399      * @param iMissingValue value to put if no value found
0400      * @return an object managing the error.
0401      *   @see SKGError
0402      */
0403     virtual SKGError getConsolidatedView(const QString& iTable,
0404                                          const QString& iAsColumn,
0405                                          const QString& iAsRow,
0406                                          const QString& iAttribute,
0407                                          const QString& iOpAtt,
0408                                          const QString& iWhereClause,
0409                                          SKGStringListList& oTable,
0410                                          const QString& iMissingValue = QStringLiteral("0")) const;
0411 
0412     /**
0413      * Get the display string for any modeler object (table, attribute)
0414      * @param iString the name of the object (example: v_operation, v_unit.t_name)
0415      * @return the display string
0416      */
0417     virtual QString getDisplay(const QString& iString) const;
0418 
0419     /**
0420      * Get display schemas
0421      * @param iRealTable the real table name
0422      * @return list of schemas
0423      */
0424     virtual SKGDocument::SKGModelTemplateList getDisplaySchemas(const QString& iRealTable) const;
0425 
0426     /**
0427      * Get the icon for attribute
0428      * @param iString the name of the attribute
0429      * @return the icon name
0430      */
0431     virtual QString getIconName(const QString& iString) const;
0432 
0433     /**
0434      * Get the icon for attribute
0435      * @param iString the name of the attribute
0436      * @return the icon
0437      */
0438     virtual QIcon getIcon(const QString& iString) const;
0439 
0440     /**
0441      * Get the real attribute
0442      * @param iString the name of the attribute (something like t_BANK)
0443      * @return the real attribute (something like bank.rd_bank_id.t_name)
0444      */
0445     virtual QString getRealAttribute(const QString& iString) const;
0446 
0447     /**
0448      * Get the attribute type
0449      * @param iAttributeName the name of an attribute
0450      * @return the type
0451      */
0452     virtual SKGServices::AttributeType getAttributeType(const QString& iAttributeName) const;
0453 
0454     /**
0455      * Get unit. WARNING: This value can be not uptodated in a transaction.
0456      * @param iPrefixInCache the prefix of the unit in the cache
0457      * @return unit.
0458      */
0459     virtual SKGServices::SKGUnitInfo getUnit(const QString& iPrefixInCache) const;
0460 
0461     /**
0462      * Get the cached sql result
0463      * @param iKey key
0464      * @return the sql result
0465      */
0466     virtual SKGStringListList getCachedSqlResult(const QString& iKey) const;
0467 
0468     /**
0469      * Get the cached value
0470      * @param iKey key
0471      * @return the value
0472      */
0473     virtual QString getCachedValue(const QString& iKey) const;
0474 
0475     /**
0476      * Get the corresponding backup file
0477      * @param iFileName the file to save
0478      * @return the corresponding backup file
0479      */
0480     virtual QString getBackupFile(const QString& iFileName) const;
0481 
0482     /**
0483      * Get the temporary file for a file
0484      * @param iFileName the file
0485      * @param iForceReadOnly force the read only mode
0486      * @return the temporary file
0487      */
0488     static QString getTemporaryFile(const QString& iFileName, bool iForceReadOnly = false);
0489 
0490     /**
0491      * Get the temporary file for a document
0492      * @return the temporary file
0493      */
0494     virtual QString getCurrentTemporaryFile() const;
0495 
0496     /**
0497      * Get the file extension for this kind of document (must be overwritten)
0498      * @return file extension (like skg)
0499      */
0500     virtual QString getFileExtension() const;
0501 
0502     /**
0503      * Get the header of the file (useful for magic mime type).
0504      * @return document header
0505      */
0506     virtual QString getDocumentHeader() const;
0507 
0508     /**
0509      * Return a number of objects (@p oNbObjects) corresponding to a where clause (@p iWhereClause )
0510      * @param iTable the table where to search
0511      * @param iWhereClause the where clause
0512      * @param oNbObjects the result
0513      * @return an object managing the error
0514      *   @see SKGError
0515      */
0516     virtual SKGError getNbObjects(const QString& iTable, const QString& iWhereClause, int& oNbObjects) const;
0517 
0518     /**
0519      * To execute a function when at least one object exist corresponding to a where clause (@p iWhereClause)
0520      * @param iTable the table where to search
0521      * @param iWhereClause the where clause
0522      * @param iFunction the function to call
0523      * @param iExecuteInMainThread to execute the function in the main thread or not
0524      */
0525     virtual void concurrentExistObjects(const QString& iTable, const QString& iWhereClause, const FuncExist& iFunction, bool iExecuteInMainThread = true);
0526 
0527     /**
0528      * To know if at least one object exist corresponding to a where clause (@p iWhereClause)
0529      * @param iTable the table where to search
0530      * @param iWhereClause the where clause
0531      * @param oExist the result
0532      * @return an object managing the error
0533      *   @see SKGError
0534      */
0535     virtual SKGError existObjects(const QString& iTable, const QString& iWhereClause, bool& oExist) const;
0536 
0537     /**
0538      * Return a list of objects (@p oListObject) corresponding to a where clause (@p iWhereClause )
0539      * @param iTable the table where to search
0540      * @param iWhereClause the where clause
0541      * @param oListObject the result
0542      * @return an object managing the error
0543      *   @see SKGError
0544      */
0545     virtual SKGError getObjects(const QString& iTable, const QString& iWhereClause, SKGObjectBase::SKGListSKGObjectBase& oListObject) const;
0546 
0547     /**
0548      * Return the object (@p oObject) corresponding to a where clause (@p iWhereClause ).
0549      * If more than one objects are returned by the query, then an error is generated
0550      * If 0 object is returned by the query, then an error is generated
0551      * @param iTable the table where to search
0552      * @param iWhereClause the where clause
0553      * @param oObject the result
0554      * @return an object managing the error
0555      *   @see SKGError
0556      */
0557     virtual SKGError getObject(const QString& iTable, const QString& iWhereClause, SKGObjectBase& oObject) const;
0558 
0559     /**
0560      * Return the object (@p oObject) corresponding to an id (@p iId ).
0561      * If more than one objects are returned by the query, then an error is generated
0562      * If 0 object is returned by the query, then an error is generated
0563      * @param iTable the table where to search
0564      * @param iId id of the object in @p iTable
0565      * @param oObject the result
0566      * @return an object managing the error
0567      *   @see SKGError
0568      */
0569     virtual SKGError getObject(const QString& iTable, int iId, SKGObjectBase& oObject) const;
0570 
0571     /**
0572      * Retrieve list of tables
0573      * @param oResult the output result
0574      * @return An object managing the error
0575      *   @see SKGError
0576      */
0577     virtual SKGError getTablesList(QStringList& oResult) const;
0578 
0579     /**
0580      * Retrieve all distinct values of an attribute of a table
0581      * @param iTable the table where to look for
0582      * @param iAttribute the attribute wanted (only one)
0583      * @param iWhereClause a whereclause
0584      * @param oResult the output result
0585      * @return An object managing the error
0586      *   @see SKGError
0587      */
0588     virtual SKGError getDistinctValues(const QString& iTable, const QString& iAttribute, const QString& iWhereClause, QStringList& oResult) const;
0589 
0590     /**
0591      * Retrieve all distinct values of an attribute of a table
0592      * @param iTable the table where to look for
0593      * @param iAttribute the attribute wanted (only one)
0594      * @param oResult the output result
0595      * @return An object managing the error
0596      *   @see SKGError
0597      */
0598     virtual SKGError getDistinctValues(const QString& iTable, const QString& iAttribute, QStringList& oResult) const;
0599 
0600     /**
0601      * Execute a sqlite orders
0602      * @param iSqlOrders the sql orders
0603      * @return An object managing the error
0604      *   @see SKGError
0605      */
0606     virtual SKGError executeSqliteOrders(const QStringList& iSqlOrders) const;
0607 
0608     /**
0609      * Execute a sqlite order
0610      * @param iSqlOrder the sql order
0611      * @param iBind the binded variables and values
0612      * @param iLastId to retrieve the id of the last created object
0613      * @return An object managing the error
0614      *   @see SKGError
0615      */
0616     virtual SKGError executeSqliteOrder(const QString& iSqlOrder, const QMap<QString, QVariant>& iBind, int* iLastId) const;
0617 
0618     /**
0619      * Execute a sqlite order
0620      * @param iSqlOrder the sql order
0621      * @param iLastId to retrieve the id of the last created object. Can be nullptr
0622      * @return An object managing the error
0623      *   @see SKGError
0624      */
0625     virtual SKGError executeSqliteOrder(const QString& iSqlOrder, int* iLastId) const;
0626 
0627     /**
0628      * Execute a sqlite order
0629      * @param iSqlOrder the sql order
0630      * @return An object managing the error
0631      *   @see SKGError
0632      */
0633     virtual SKGError executeSqliteOrder(const QString& iSqlOrder) const;
0634 
0635     /**
0636      * Execute a select sqlite order returning one value and return the result in @p oResult
0637      * @param iSqlOrder the sql order
0638      * @param oResult the result of the select
0639      * @param iUseCache to use the cache
0640      * @return An object managing the error
0641      *   @see SKGError
0642      */
0643     virtual SKGError executeSingleSelectSqliteOrder(const QString& iSqlOrder, QString& oResult, bool iUseCache = true) const;
0644 
0645     /**
0646     * Execute a select sqlite order and return the result in @p oResult
0647     * @param iSqlOrder the sql order
0648     * @param oResult the result of the select. It is a vector of vector of QString
0649     * @param iUseCache to use the cache
0650     * @return An object managing the error
0651     *   @see SKGError
0652     */
0653     virtual SKGError executeSelectSqliteOrder(const QString& iSqlOrder, SKGStringListList& oResult, bool iUseCache = true) const;
0654 
0655 
0656     /**
0657     * Execute a select sqlite order and pass the result to the function @p iFunction. This is done in another thread.
0658     * @param iSqlOrder the sql order
0659     * @param iFunction the function to call
0660     * @param iExecuteInMainThread to execute the function in the main thread or not
0661     */
0662     virtual void concurrentExecuteSelectSqliteOrder(const QString& iSqlOrder, const FuncSelect& iFunction, bool iExecuteInMainThread = true) const;
0663 
0664     /**
0665     * dump a select sqlite order
0666     * @param iSqlOrder the sql order
0667     * @param oStream the output stream, nullptr for std output (cout)
0668     * @param iMode dump mode
0669     * @return An object managing the error
0670     *   @see SKGError
0671     */
0672     virtual SKGError dumpSelectSqliteOrder(const QString& iSqlOrder, QTextStream* oStream = nullptr, SKGServices::DumpMode iMode = SKGServices::DUMP_TEXT) const;
0673 
0674     /**
0675      * dump a select sqlite order
0676      * @param iSqlOrder the sql order
0677      * @param oResult the output
0678      * @param iMode dump mode
0679      * @return An object managing the error
0680      *   @see SKGError
0681      */
0682     virtual SKGError dumpSelectSqliteOrder(const QString& iSqlOrder, QString& oResult, SKGServices::DumpMode iMode = SKGServices::DUMP_TEXT) const;
0683 
0684     /**
0685      * dump a select sqlite order
0686      * @param iSqlOrder the sql order
0687      * @param oResult the output
0688      * @param iMode dump mode
0689      * @return An object managing the error
0690      *   @see SKGError
0691      */
0692     virtual SKGError dumpSelectSqliteOrder(const QString& iSqlOrder, QStringList& oResult, SKGServices::DumpMode iMode = SKGServices::DUMP_TEXT) const;
0693 
0694     /**
0695      * Retrieve description of each attribute of a table
0696      * @param iTable the table where to look for
0697      * @param oResult the output result
0698      * @return An object managing the error
0699      *   @see SKGError
0700      */
0701     virtual SKGError getAttributesDescription(const QString& iTable, SKGServices::SKGAttributesList& oResult) const;
0702 
0703     /**
0704      * Retrieve list of attributes
0705      * @param iTable the table where to look for
0706      * @param oResult the output result
0707      * @return An object managing the error
0708      *   @see SKGError
0709      */
0710     virtual SKGError getAttributesList(const QString& iTable, QStringList& oResult) const;
0711 
0712     /**
0713      * Get the report
0714      * Do not forget to delete the pointer
0715      * @return the report
0716      */
0717     virtual SKGReport* getReport() const;
0718 
0719     /**
0720      * Copy a document into an JSON document.
0721      * @param oDocument the json document
0722      * @return An object managing the error
0723      *   @see SKGError
0724      */
0725     virtual SKGError copyToJson(QString& oDocument) const;
0726 
0727     /**
0728      * Refresh all views and indexes in the database
0729      * @param iForce force the refresh
0730      * @return an object managing the error.
0731      *   @see SKGError
0732      */
0733     virtual SKGError refreshViewsIndexesAndTriggers(bool iForce = false) const;
0734 
0735     /**
0736      * Get formated money
0737      * @param iValue value
0738      * @param iUnit unit
0739      * @param iHtml colored output
0740      * @return formated value in red or black
0741      */
0742     Q_INVOKABLE virtual QString formatMoney(double iValue, const SKGServices::SKGUnitInfo& iUnit, bool iHtml = true) const;
0743 
0744     /**
0745      * Get formated money in primary unit
0746      * @param iValue value
0747      * @param iForcedNbOfDigit the number of digit (-1 means nb of digit of the unit)
0748      * @return formated value in red or black
0749      */
0750     Q_INVOKABLE virtual QString formatPrimaryMoney(double iValue, int iForcedNbOfDigit = -1) const;
0751 
0752     /**
0753      * Get formated money in primary unit
0754      * @param iValue value
0755      * @param iForcedNbOfDigit the number of digit (-1 means nb of digit of the unit)
0756      * @return formated value in red or black
0757      */
0758     Q_INVOKABLE virtual QString formatSecondaryMoney(double iValue, int iForcedNbOfDigit = -1) const;
0759 
0760     /**
0761      * Get formated percentage
0762      * @param iValue value
0763      * @param iInvertColors to set positive value in red and negative values in green
0764      * @return formated value in red or black
0765      */
0766     Q_INVOKABLE virtual QString formatPercentage(double iValue, bool iInvertColors = false) const;
0767 
0768 public Q_SLOTS:
0769 
0770     /**
0771      * Call the progress callstack.
0772      * @param iPosition the position in the current transaction.
0773      * The value must be between 0 and the value passed to beginTransaction.
0774      * @param iText the text to display. If empty then the name of the last transaction is used.
0775      * @return an object managing the error
0776      *   @see SKGError
0777      */
0778     virtual SKGError stepForward(int iPosition, const QString& iText = QString());
0779 
0780     /**
0781      * Start a transaction.
0782      * A transaction is needed to modify the SKGDocument.
0783      * This transaction is also used to manage the undo/redo.
0784      * @see endTransaction
0785      * @param iName the name of the transaction
0786      * @param iNbStep the number of step in this transaction.
0787      * It is used to call the progress callback.
0788      * @param iDate date of the transaction.
0789      * @param iRefreshViews at the end of the transaction, computed views will be recomputed.
0790      * @return an object managing the error
0791      *   @see SKGError
0792      */
0793     virtual SKGError beginTransaction(const QString& iName,
0794                                       int iNbStep = 0,
0795                                       const QDateTime& iDate = QDateTime::currentDateTime(),
0796                                       bool iRefreshViews = true);
0797 
0798     /**
0799      * Close the current transaction.
0800      * A transaction is needed to modify the SKGDocument.
0801      * This transaction is also used to manage the undo/redo.
0802      * @see beginTransaction
0803      * @param succeedded : true to indicate that current transaction has been successfully executed
0804      *                   : false to indicate that current transaction has failed
0805      * @return an object managing the error
0806      *   @see SKGError
0807      */
0808     virtual SKGError endTransaction(bool succeedded);
0809 
0810     /**
0811      * Remove all transactions of the history.
0812      * @return an object managing the error
0813      *   @see SKGError
0814      */
0815     virtual SKGError removeAllTransactions();
0816 
0817     /**
0818      * Send a message attached to the current transaction.
0819      * @param iMessage the message
0820      * @param iMessageType the message type
0821      * @param iAction the associated action
0822      * @return an object managing the error
0823      *   @see SKGError
0824      */
0825     virtual SKGError sendMessage(const QString& iMessage, SKGDocument::MessageType iMessageType = SKGDocument::Information, const QString& iAction = QString());
0826 
0827     /**
0828      * Change the passord of the document.
0829      * WARNING: This method must NOT be used in a transaction.
0830      * @see beginTransaction
0831      * @param iNewPassword the new password
0832      * @return an object managing the error
0833      *   @see SKGError
0834      */
0835     virtual SKGError changePassword(const QString& iNewPassword);
0836 
0837     /**
0838      * Change the language of the document.
0839      * @param iLanguage the new language
0840      * @return an object managing the error
0841      *   @see SKGError
0842      */
0843     virtual SKGError setLanguage(const QString& iLanguage);
0844 
0845     /**
0846      * Initialize a new document.
0847      * WARNING: This method must NOT be used in a transaction.
0848      * @see endTransaction
0849      * @return an object managing the error
0850      *   @see SKGError
0851      */
0852     virtual SKGError initialize();
0853 
0854     /**
0855      * Recover a corrupted file.
0856      * WARNING: This method must NOT be used in a transaction.
0857      * @see endTransaction
0858      * @param iName the file name to load.
0859      * @param iPassword the password of the SKGDocument.
0860      * @param oRecoveredFile the recovered file.
0861      * @return an object managing the error.
0862      *   @see SKGError
0863      */
0864     virtual SKGError recover(const QString& iName, const QString& iPassword, QString& oRecoveredFile);
0865 
0866     /**
0867      * Load an existing document.
0868      * WARNING: This method must NOT be used in a transaction.
0869      * @see endTransaction
0870      * @param iName the file name to load.
0871      * @param iPassword the password of the SKGDocument.
0872      * @param iRestoreTmpFile restore the temporary file if existing.
0873      * @param iForceReadOnly force the read only mode.
0874      * @return an object managing the error.
0875      *   @see SKGError
0876      */
0877     virtual SKGError load(const QString& iName, const QString& iPassword = QString(), bool iRestoreTmpFile = false, bool iForceReadOnly = false);
0878 
0879     /**
0880      * Set the file not modified.
0881      */
0882     virtual void setFileNotModified() const;
0883 
0884     /**
0885      * save the current SKGDocument.
0886      * WARNING: This method must NOT be used in a transaction.
0887      * @see endTransaction.
0888      * @return an object managing the error.
0889      *   @see SKGError
0890      */
0891     virtual SKGError save();
0892 
0893     /**
0894      * save the current SKGDocument.
0895      * WARNING: This method must NOT be used in a transaction.
0896      * @see endTransaction
0897      * @param iName the file name to save.
0898      * @param iOverwrite to authorize the overwrite or not.
0899      * @return an object managing the error.
0900      *   @see SKGError
0901      */
0902     virtual SKGError saveAs(const QString& iName, bool iOverwrite = false);
0903 
0904     /**
0905      * close the current SKGDocument.
0906      * @return an object managing the error.
0907      *   @see SKGError
0908      */
0909     SKGError close();
0910 
0911     /**
0912      * Add a sql result in the cache. Cache is cleaned after each transaction
0913      * @param iKey the key
0914      * @param iValue the sql result
0915      */
0916     virtual void addSqlResultInCache(const QString& iKey, const SKGStringListList& iValue) const;
0917 
0918     /**
0919      * Add a value in the cache. Cache is cleaned after each transaction
0920      * @param iKey the key
0921      * @param iValue the value
0922      */
0923     virtual void addValueInCache(const QString& iKey, const QString& iValue) const;
0924 
0925     /**
0926      * Refresh the case.
0927      * @param iTable the modified table triggering the cache refresh.
0928      */
0929     virtual void refreshCache(const QString& iTable) const;
0930 
0931     /**
0932      * Set backup parameters.
0933      * The key word \<DATE\> is supported.
0934      * Call setBackupParameters() to avoid backup creation.
0935      * @param iPrefix the prefix for the backup file
0936      * @param iSuffix the suffix for backup file
0937      */
0938     void setBackupParameters(const QString& iPrefix = QString(), const QString& iSuffix = QString()) const;
0939 
0940 Q_SIGNALS:
0941     /**
0942      * This signal is launched by endTransaction on all tables modified when a huge modification occures on the model
0943      * @param iTableName the name of the modified table. iTableName="" if all tables must be refreshed
0944      * @param iIdTransaction the id of the transaction for direct modifications of the table (update of modify objects is enough)
0945      * @param iLightTransaction to if the transaction is light
0946      *or 0 in case of modifications by impact (full table must be refreshed)
0947      */
0948     void tableModified(const QString& iTableName, int iIdTransaction, bool iLightTransaction);
0949 
0950     /**
0951      * This signal is launched by endTransaction when a transaction is successfully ended
0952      * @param iIdTransaction the id of the transaction for direct modifications of the table (update of modify objects is enough)
0953      */
0954     void transactionSuccessfullyEnded(int iIdTransaction);
0955 
0956     /**
0957      * This signal is launched when the status of the document is changed (read only, modified)
0958      */
0959     void modified();
0960 
0961 protected:
0962     /**
0963      * Drop views and indexes attached to a list of tables
0964      * @param iTables the list of tables.
0965      * @return an object managing the error.
0966      *   @see SKGError
0967      */
0968     virtual SKGError dropViewsAndIndexes(const QStringList& iTables) const;
0969 
0970     /**
0971      * Migrate the current SKGDocument to the latest version of the data model.
0972      * WARNING: This method must be used in a transaction.
0973      * @see beginTransaction
0974      * @param oMigrationDone to know if a migration has been done or not.
0975      * @return an object managing the error.
0976      *   @see SKGError
0977      */
0978     virtual SKGError migrate(bool& oMigrationDone);
0979 
0980     /**
0981      * Create dynamic triggers for "undoable" tables.
0982      * @return an object managing the error.
0983      *   @see SKGError
0984      */
0985     virtual SKGError createUndoRedoTemporaryTriggers() const;
0986 
0987     /**
0988      * This list must contain a list of object
0989      * where the undo / redo is NOT applicable.
0990      * For a full table, the syntax is: T.nameofthetable
0991      * For an attribute, the syntax is: A.nameofthetable.nameoftheattribute
0992      */
0993     QStringList SKGListNotUndoable;
0994 
0995     /**
0996      * Get impacted views if one object of @p iTable is modifier.
0997      * @param iTable name of a table
0998      * @return impacted views
0999      */
1000     virtual QStringList getImpactedViews(const QString& iTable) const;
1001 
1002     /**
1003      * Compute all materialized views.
1004      * @param iTable Compute only materialized views linked to this table. If empty then compute all materialized views
1005      * @return an object managing the error.
1006      *   @see SKGError
1007      */
1008     virtual SKGError computeMaterializedViews(const QString& iTable = QString()) const;
1009 
1010 private:
1011     Q_DISABLE_COPY(SKGDocument)
1012 
1013     SKGMessageList m_unTransactionnalMessages;
1014     SKGDocumentPrivate* d;
1015 };
1016 /**
1017  * Declare the class
1018  */
1019 Q_DECLARE_TYPEINFO(SKGDocument, Q_MOVABLE_TYPE);
1020 #endif