File indexing completed on 2024-05-26 05:09:26

0001 /*
0002     SPDX-FileCopyrightText: 2005 Tony Bloomfield <tonybloom@users.sourceforge.net>
0003     Copyright (C)      Fernando Vilas <fvilas@iname.com>
0004     SPDX-FileCopyrightText: 2014 Christian Dávid <christian-david@web.de>
0005     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef MYMONEYSTORAGESQL_H
0010 #define MYMONEYSTORAGESQL_H
0011 
0012 #include <QSharedData>
0013 #include <QSharedPointer>
0014 #include <QSqlDatabase>
0015 
0016 #include "imymoneystorageformat.h"
0017 #include "mymoneyunittestable.h"
0018 
0019 // This is a convenience functor to make it easier to use STL algorithms
0020 // It will return false if the MyMoneyTransaction DOES match the filter.
0021 // This functor may disappear when all filtering can be handled in SQL.
0022 
0023 class QUrl;
0024 class QDate;
0025 class QIODevice;
0026 
0027 class MyMoneyInstitution;
0028 class MyMoneyAccount;
0029 class MyMoneySecurity;
0030 class MyMoneyCostCenter;
0031 class MyMoneyMoney;
0032 class MyMoneySchedule;
0033 class MyMoneyPayee;
0034 class MyMoneyTag;
0035 class MyMoneySplit;
0036 class MyMoneyTransaction;
0037 class MyMoneyTransactionFilter;
0038 class MyMoneyBudget;
0039 class MyMoneyReport;
0040 class MyMoneyPrice;
0041 class payeeIdentifier;
0042 class onlineJob;
0043 class MyMoneyStorageSql;
0044 class MyMoneyStorageMgr;
0045 class MyMoneyFile;
0046 
0047 template <class T1, class T2> struct QPair;
0048 template <class Key, class Value> class QMap;
0049 
0050 typedef QPair<QString, QString> MyMoneySecurityPair;
0051 typedef QMap<QDate, MyMoneyPrice> MyMoneyPriceEntries;
0052 typedef QMap<MyMoneySecurityPair, MyMoneyPriceEntries> MyMoneyPriceList;
0053 
0054 namespace eMyMoney {
0055 namespace TransactionFilter {
0056 enum class State;
0057 }
0058 }
0059 
0060 /**
0061   * The MyMoneyDbColumn class is a base type for generic db columns.
0062   * Derived types exist for several common column types.
0063   *
0064   * @todo Remove unneeded columns which store the row count of tables from kmmFileInfo
0065   */
0066 
0067 class MyMoneyStorageSqlPrivate;
0068 class MyMoneyStorageSql : public IMyMoneyOperationsFormat, public QSqlDatabase, public QSharedData
0069 {
0070     Q_DISABLE_COPY(MyMoneyStorageSql)
0071     friend class MyMoneyDbDef;
0072     KMM_MYMONEY_UNIT_TESTABLE
0073 
0074 public:
0075     explicit MyMoneyStorageSql(MyMoneyFile* file, const QUrl&);
0076     ~MyMoneyStorageSql() override;
0077 
0078     uint currentVersion() const;
0079 
0080     /**
0081     * MyMoneyStorageSql - open database file
0082     *
0083     * @param url pseudo-URL of database to be opened
0084     * @param openMode open mode, same as for QFile::open
0085     * @param clear whether existing data can be deleted
0086 
0087     * @return 0 - database successfully opened
0088     * @return 1 - database not opened, use lastError function for reason
0089     * @return -1 - output database not opened, contains data, clean not specified
0090     *
0091      */
0092     int open(const QUrl &url, int openMode, bool clear = false);
0093     /**
0094      * MyMoneyStorageSql close the database
0095      *
0096      * @return void
0097      *
0098      */
0099     void close(bool logoff = true);
0100     /**
0101      * MyMoneyStorageSql read all the database into storage
0102      *
0103      * @return void
0104      *
0105      */
0106     bool readFile();
0107     /**
0108      * MyMoneyStorageSql write/update the database from storage
0109      *
0110      * @return void
0111      *
0112      */
0113     bool writeFile();
0114 
0115     /**
0116      * MyMoneyStorageSql generalized error routine
0117      *
0118      * @return : error message to be displayed
0119      *
0120      */
0121     QString lastError() const;
0122 
0123     /**
0124      * This method is used when a database file is open, and the data is to
0125      * be saved in a different file or format. It will ensure that all data
0126      * from the database is available in memory to enable it to be written.
0127      */
0128     // virtual void fillStorage();
0129 
0130     /**
0131       * The following functions correspond to the identically named (usually) functions
0132       * within the Storage Manager, and are called to update the database
0133       */
0134     void modifyUserInfo(const MyMoneyPayee& payee);
0135     void addInstitution(const MyMoneyInstitution& inst);
0136     void modifyInstitution(const MyMoneyInstitution& inst);
0137     void removeInstitution(const MyMoneyInstitution& inst);
0138     void addPayee(const MyMoneyPayee& payee);
0139     void modifyPayee(MyMoneyPayee payee);
0140     void removePayee(const MyMoneyPayee& payee);
0141     void addTag(const MyMoneyTag& tag);
0142     void modifyTag(const MyMoneyTag& tag);
0143     void removeTag(const MyMoneyTag& tag);
0144     void addAccount(const MyMoneyAccount& acc);
0145     void modifyAccount(const MyMoneyAccount& acc);
0146     void removeAccount(const MyMoneyAccount& acc);
0147     void addTransaction(const MyMoneyTransaction& tx);
0148     void modifyTransaction(const MyMoneyTransaction& tx);
0149     void removeTransaction(const MyMoneyTransaction& tx);
0150     void addSchedule(const MyMoneySchedule& sch);
0151     void modifySchedule(const MyMoneySchedule& sch);
0152     void removeSchedule(const MyMoneySchedule& sch);
0153     void addSecurity(const MyMoneySecurity& sec);
0154     void modifySecurity(const MyMoneySecurity& sec);
0155     void removeSecurity(const MyMoneySecurity& sec);
0156     void addPrice(const MyMoneyPrice& p);
0157     void removePrice(const MyMoneyPrice& p);
0158     void addCurrency(const MyMoneySecurity& sec);
0159     void modifyCurrency(const MyMoneySecurity& sec);
0160     void removeCurrency(const MyMoneySecurity& sec);
0161     void addReport(const MyMoneyReport& rep);
0162     void modifyReport(const MyMoneyReport& rep);
0163     void removeReport(const MyMoneyReport& rep);
0164     void addBudget(const MyMoneyBudget& bud);
0165     void modifyBudget(const MyMoneyBudget& bud);
0166     void removeBudget(const MyMoneyBudget& bud);
0167     void addOnlineJob(const onlineJob& job);
0168     void modifyOnlineJob(const onlineJob& job);
0169     void removeOnlineJob(const onlineJob& job);
0170     void addPayeeIdentifier(payeeIdentifier& ident);
0171     void modifyPayeeIdentifier(const payeeIdentifier& ident);
0172     void removePayeeIdentifier(const payeeIdentifier& ident);
0173 
0174     ulong transactionCount(const QString& aid) const;
0175     QHash<QString, ulong> transactionCountMap() const;
0176 
0177     /**
0178      * The following functions are perform the same operations as the
0179      * above functions, but on a QList of the items.
0180      * This reduces db round-trips, so should be the preferred method when
0181      * such a function exists.
0182      */
0183     void modifyAccountList(const QList<MyMoneyAccount>& acc);
0184 
0185     /**
0186       * the storage manager also needs the following read entry points
0187       */
0188     QMap<QString, MyMoneyAccount> fetchAccounts(const QStringList& idList, bool forUpdate = false) const;
0189     QMap<QString, MyMoneyAccount> fetchAccounts() const;
0190 
0191     QMap<QString, MyMoneyMoney> fetchBalance(const QStringList& id, const QDate& date) const;
0192 
0193     QMap<QString, MyMoneyBudget> fetchBudgets(const QStringList& idList, bool forUpdate = false) const;
0194     QMap<QString, MyMoneyBudget> fetchBudgets() const;
0195 
0196     QMap<QString, MyMoneySecurity> fetchCurrencies(const QStringList& idList, bool forUpdate = false) const;
0197     QMap<QString, MyMoneySecurity> fetchCurrencies() const;
0198 
0199     QMap<QString, MyMoneyInstitution> fetchInstitutions(const QStringList& idList, bool forUpdate = false) const;
0200     QMap<QString, MyMoneyInstitution> fetchInstitutions() const;
0201 
0202     QMap<QString, MyMoneyPayee> fetchPayees(const QStringList& idList, bool forUpdate = false) const;
0203     QMap<QString, MyMoneyPayee> fetchPayees() const;
0204 
0205     QMap<QString, MyMoneyTag> fetchTags(const QStringList& idList, bool forUpdate = false) const;
0206     QMap<QString, MyMoneyTag> fetchTags() const;
0207 
0208     QMap<QString, onlineJob> fetchOnlineJobs(const QStringList& idList, bool forUpdate = false) const;
0209     QMap<QString, onlineJob> fetchOnlineJobs() const;
0210 
0211     QMap<QString, MyMoneyCostCenter> fetchCostCenters(const QStringList& idList, bool forUpdate = false) const;
0212     QMap<QString, MyMoneyCostCenter> fetchCostCenters() const;
0213 
0214     MyMoneyPriceList fetchPrices(const QStringList& fromIdList, const QStringList& toIdList, bool forUpdate = false) const;
0215     MyMoneyPriceList fetchPrices() const;
0216 
0217     MyMoneyPrice fetchSinglePrice(const QString& fromId, const QString& toId, const QDate& date_, bool exactDate, bool = false) const;
0218 
0219     QMap<QString, MyMoneyReport> fetchReports(const QStringList& idList, bool forUpdate = false) const;
0220     QMap<QString, MyMoneyReport> fetchReports() const;
0221 
0222     QMap<QString, MyMoneySchedule> fetchSchedules(const QStringList& idList, bool forUpdate = false) const;
0223     QMap<QString, MyMoneySchedule> fetchSchedules() const;
0224 
0225     QMap<QString, MyMoneySecurity> fetchSecurities(const QStringList& idList, bool forUpdate = false) const;
0226     QMap<QString, MyMoneySecurity> fetchSecurities() const;
0227 
0228     QMap<QString, MyMoneyTransaction> fetchTransactions(const QString& tidList, const QString& dateClause, bool forUpdate = false) const;
0229     QMap<QString, MyMoneyTransaction> fetchTransactions(const QString& tidList) const;
0230     QMap<QString, QSharedPointer<MyMoneyTransaction>> fetchTransactions() const;
0231 
0232     QMap<QString, MyMoneyTransaction> fetchTransactions(const MyMoneyTransactionFilter& filter) const;
0233     payeeIdentifier fetchPayeeIdentifier(const QString& id) const;
0234 
0235     QMap<QString, payeeIdentifier> fetchPayeeIdentifiers(const QStringList& idList) const;
0236     QMap<QString, payeeIdentifier> fetchPayeeIdentifiers() const;
0237 
0238     bool isReferencedByTransaction(const QString& id) const;
0239 
0240     void readPayees(const QString&);
0241     void readPayees(const QList<QString>& payeeList);
0242     void readPayees();
0243 
0244     void readTags(const QString&);
0245     void readTags(const QList<QString>& tagList);
0246     void readTags();
0247 
0248     void readTransactions(const MyMoneyTransactionFilter& filter);
0249     void setProgressCallback(void(*callback)(int, int, const QString&)) override;
0250 
0251     void readFile(QIODevice* s, MyMoneyFile* file) override;
0252     void writeFile(QIODevice* s, MyMoneyFile* file) override;
0253 
0254     void startCommitUnit(const QString& callingFunction);
0255     bool endCommitUnit(const QString& callingFunction);
0256     void cancelCommitUnit(const QString& callingFunction);
0257 
0258     ulong getRecCount(const QString& table) const;
0259     ulong getNextBudgetId() const;
0260     ulong getNextAccountId() const;
0261     ulong getNextInstitutionId() const;
0262     ulong getNextPayeeId() const;
0263     ulong getNextTagId() const;
0264     ulong getNextOnlineJobId() const;
0265     ulong getNextPayeeIdentifierId() const;
0266     ulong getNextReportId() const;
0267     ulong getNextScheduleId() const;
0268     ulong getNextSecurityId() const;
0269     ulong getNextTransactionId() const;
0270     ulong getNextCostCenterId() const;
0271 
0272     ulong incrementBudgetId();
0273     ulong incrementAccountId();
0274     ulong incrementInstitutionId();
0275     ulong incrementPayeeId();
0276     ulong incrementTagId();
0277     ulong incrementReportId();
0278     ulong incrementScheduleId();
0279     ulong incrementSecurityId();
0280     ulong incrementTransactionId();
0281     ulong incrementOnlineJobId();
0282     ulong incrementPayeeIdentfierId();
0283     ulong incrementCostCenterId();
0284 
0285     void loadAccountId(ulong id);
0286     void loadTransactionId(ulong id);
0287     void loadPayeeId(ulong id);
0288     void loadTagId(ulong id);
0289     void loadInstitutionId(ulong id);
0290     void loadScheduleId(ulong id);
0291     void loadSecurityId(ulong id);
0292     void loadReportId(ulong id);
0293     void loadBudgetId(ulong id);
0294     void loadOnlineJobId(ulong id);
0295     void loadPayeeIdentifierId(ulong id);
0296     void loadCostCenterId(ulong id);
0297 
0298     /**
0299       * This method allows to modify the precision with which prices
0300       * are handled within the object. The default of the precision is 4.
0301       */
0302     static void setPrecision(int prec);
0303 
0304     /**
0305       * This method allows to modify the start date for transaction retrieval
0306       * The default of the precision is Jan 1st, 1900.
0307       */
0308     static void setStartDate(const QDate &startDate);
0309 
0310 private:
0311     MyMoneyStorageSqlPrivate* const d_ptr;
0312     Q_DECLARE_PRIVATE(MyMoneyStorageSql)
0313 };
0314 #endif // MYMONEYSTORAGESQL_H