Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/sql/mymoneystoragesql_p.h
Context not available. | |||||
50 | // KDE Includes | 50 | // KDE Includes | ||
---|---|---|---|---|---|
51 | 51 | | |||
52 | #include <KLocalizedString> | 52 | #include <KLocalizedString> | ||
53 | #include <KServiceTypeTrader> | | |||
54 | 53 | | |||
55 | // ---------------------------------------------------------------------------- | 54 | // ---------------------------------------------------------------------------- | ||
56 | // Project Includes | 55 | // Project Includes | ||
57 | 56 | | |||
58 | #include "mymoneystoragemgr.h" | 57 | #include "mymoneystoragemgr.h" | ||
59 | #include "kmymoneystorageplugin.h" | | |||
60 | #include "onlinejobadministration.h" | 58 | #include "onlinejobadministration.h" | ||
61 | #include "payeeidentifier/payeeidentifierloader.h" | 59 | #include "payeeidentifier/payeeidentifierloader.h" | ||
62 | #include "onlinetasks/interfaces/tasks/onlinetask.h" | 60 | #include "onlinetasks/interfaces/tasks/onlinetask.h" | ||
Context not available. | |||||
2669 | return false; | 2667 | return false; | ||
2670 | QString sqlIID; | 2668 | QString sqlIID; | ||
2671 | 2669 | | |||
2670 | MyMoneyDbTransaction t(*q, Q_FUNC_INFO); | ||||
2671 | auto rc = false; | ||||
2672 | if (iid == payeeIdentifiers::ibanBic::staticPayeeIdentifierIid()) | 2672 | if (iid == payeeIdentifiers::ibanBic::staticPayeeIdentifierIid()) | ||
2673 | sqlIID = QString::fromLatin1("org.kmymoney.payeeIdentifier.ibanbic.sqlStoragePlugin"); | 2673 | rc = setupIBANBIC(*q); | ||
2674 | else if (iid == payeeIdentifiers::nationalAccount::staticPayeeIdentifierIid()) | 2674 | else if (iid == payeeIdentifiers::nationalAccount::staticPayeeIdentifierIid()) | ||
2675 | sqlIID = QLatin1String("org.kmymoney.payeeIdentifier.nationalAccount.sqlStoragePlugin"); | 2675 | rc = setupNationalAccount(*q); | ||
2676 | else if (iid == sepaOnlineTransferImpl::name()) | 2676 | else if (iid == sepaOnlineTransferImpl::name()) | ||
2677 | sqlIID = QLatin1String("org.kmymoney.creditTransfer.sepa.sqlStoragePlugin"); | 2677 | rc = setupSepaOnlineTransfer(*q); | ||
2678 | else | 2678 | else | ||
2679 | rc = false; | ||||
2680 | return rc; | ||||
2681 | } | ||||
2682 | | ||||
2683 | bool setupIBANBIC(QSqlDatabase connection) | ||||
2684 | { | ||||
2685 | auto iid = QLatin1String("org.kmymoney.payeeIdentifier.ibanbic.sqlStoragePlugin"); | ||||
2686 | // Get current version | ||||
2687 | QSqlQuery query = QSqlQuery(connection); | ||||
2688 | query.prepare("SELECT versionMajor FROM kmmPluginInfo WHERE iid = ?"); | ||||
2689 | query.bindValue(0, iid); | ||||
2690 | if (!query.exec()) { | ||||
2691 | qWarning("Could not execute query for ibanBicStoragePlugin: %s", qPrintable(query.lastError().text())); | ||||
2679 | return false; | 2692 | return false; | ||
2693 | } | ||||
2680 | 2694 | | |||
2681 | QString errorMsg; | 2695 | int currentVersion = 0; | ||
2682 | KMyMoneyPlugin::storagePlugin* plugin = KServiceTypeTrader::createInstanceFromQuery<KMyMoneyPlugin::storagePlugin>( | 2696 | if (query.next()) | ||
2683 | QLatin1String("KMyMoney/sqlStoragePlugin"), | 2697 | currentVersion = query.value(0).toInt(); | ||
2684 | QString("'%1' ~in [X-KMyMoney-PluginIid]").arg(sqlIID.replace(QLatin1Char('\''), QLatin1String("\\'"))), | | |||
2685 | 0, | | |||
2686 | QVariantList(), | | |||
2687 | &errorMsg | | |||
2688 | ); | | |||
2689 | 2698 | | |||
2690 | if (plugin == 0) | 2699 | // Create database in it's most recent version if version is 0 | ||
2691 | throw MYMONEYEXCEPTION(QString::fromLatin1("Could not load sqlStoragePlugin '%1', (error: %2)").arg(sqlIID, errorMsg)); | 2700 | // (version 0 means the database was not installed) | ||
2701 | if (currentVersion == 0) { | ||||
2702 | // If the database is recreated the table may be still there. So drop it if needed. No error handling needed | ||||
2703 | // as this step is not necessary - only the creation is important. | ||||
2704 | if (!query.exec("DROP TABLE IF EXISTS kmmIbanBic;")) | ||||
2705 | return false; | ||||
2692 | 2706 | | |||
2693 | MyMoneyDbTransaction t(*q, Q_FUNC_INFO); | 2707 | if (!query.exec( | ||
2694 | if (plugin->setupDatabase(*q)) { | 2708 | "CREATE TABLE kmmIbanBic (" | ||
2695 | m_loadedStoragePlugins.insert(sqlIID); | 2709 | " id varchar(32) NOT NULL PRIMARY KEY REFERENCES kmmPayeeIdentifier( id ) ON DELETE CASCADE ON UPDATE CASCADE," | ||
2696 | return true; | 2710 | " iban varchar(32)," | ||
2711 | " bic char(11) CHECK(length(bic) = 11 OR bic IS NULL)," | ||||
2712 | " name text" | ||||
2713 | " );" | ||||
2714 | )) { | ||||
2715 | qWarning("Could not create table for ibanBicStoragePlugin: %s", qPrintable(query.lastError().text())); | ||||
2716 | return false; | ||||
2717 | } | ||||
2718 | | ||||
2719 | query.prepare("INSERT INTO kmmPluginInfo (iid, versionMajor, versionMinor, uninstallQuery) VALUES(?, ?, ?, ?)"); | ||||
2720 | query.bindValue(0, iid); | ||||
2721 | query.bindValue(1, 1); | ||||
2722 | query.bindValue(2, 0); | ||||
2723 | query.bindValue(3, "DROP TABLE kmmIbanBic;"); | ||||
2724 | if (query.exec()) | ||||
2725 | return true; | ||||
2726 | qWarning("Could not save plugin info for ibanBicStoragePlugin (%s): %s", qPrintable(iid), qPrintable(query.lastError().text())); | ||||
2727 | return false; | ||||
2697 | } | 2728 | } | ||
2698 | 2729 | | |||
2699 | throw MYMONEYEXCEPTION(QString::fromLatin1("Could not install sqlStoragePlugin '%1' in database.").arg(sqlIID)); | 2730 | // Check if version is valid with this plugin | ||
2731 | switch (currentVersion) { | ||||
2732 | case 1: return true; | ||||
2733 | } | ||||
2734 | | ||||
2735 | return false; | ||||
2736 | } | ||||
2737 | | ||||
2738 | bool setupNationalAccount(QSqlDatabase connection) | ||||
2739 | { | ||||
2740 | auto iid = QLatin1String("org.kmymoney.payeeIdentifier.nationalAccount.sqlStoragePlugin"); | ||||
2741 | // Get current version | ||||
2742 | QSqlQuery query = QSqlQuery(connection); | ||||
2743 | query.prepare("SELECT versionMajor FROM kmmPluginInfo WHERE iid = ?"); | ||||
2744 | query.bindValue(0, iid); | ||||
2745 | if (!query.exec()) { | ||||
2746 | qWarning("Could not execute query for nationalAccountStoragePlugin: %s", qPrintable(query.lastError().text())); | ||||
2747 | return false; | ||||
2748 | } | ||||
2749 | | ||||
2750 | int currentVersion = 0; | ||||
2751 | if (query.next()) | ||||
2752 | currentVersion = query.value(0).toInt(); | ||||
2753 | | ||||
2754 | // Create database in it's most recent version if version is 0 | ||||
2755 | // (version 0 means the database was not installed) | ||||
2756 | if (currentVersion == 0) { | ||||
2757 | // If the database is recreated the table may be still there. So drop it if needed. No error handling needed | ||||
2758 | // as this step is not necessary - only the creation is important. | ||||
2759 | if (!query.exec("DROP TABLE IF EXISTS kmmNationalAccountNumber;")) | ||||
2760 | return false; | ||||
2761 | | ||||
2762 | if (!query.exec( | ||||
2763 | "CREATE TABLE kmmNationalAccountNumber (" | ||||
2764 | " id varchar(32) NOT NULL PRIMARY KEY REFERENCES kmmPayeeIdentifier( id ) ON DELETE CASCADE ON UPDATE CASCADE," | ||||
2765 | " countryCode varchar(3)," | ||||
2766 | " accountNumber TEXT," | ||||
2767 | " bankCode TEXT," | ||||
2768 | " name TEXT" | ||||
2769 | " );" | ||||
2770 | )) { | ||||
2771 | qWarning("Could not create table for nationalAccountStoragePlugin: %s", qPrintable(query.lastError().text())); | ||||
2772 | return false; | ||||
2773 | } | ||||
2774 | | ||||
2775 | query.prepare("INSERT INTO kmmPluginInfo (iid, versionMajor, versionMinor, uninstallQuery) VALUES(?, ?, ?, ?)"); | ||||
2776 | query.bindValue(0, iid); | ||||
2777 | query.bindValue(1, 1); | ||||
2778 | query.bindValue(2, 0); | ||||
2779 | query.bindValue(3, "DROP TABLE kmmNationalAccountNumber;"); | ||||
2780 | if (query.exec()) | ||||
2781 | return true; | ||||
2782 | qWarning("Could not save plugin info for nationalAccountStoragePlugin (%s): %s", qPrintable(iid), qPrintable(query.lastError().text())); | ||||
2783 | return false; | ||||
2784 | } | ||||
2785 | | ||||
2786 | // Check if version is valid with this plugin | ||||
2787 | switch (currentVersion) { | ||||
2788 | case 1: return true; | ||||
2789 | } | ||||
2790 | | ||||
2791 | return false; | ||||
2792 | } | ||||
2793 | | ||||
2794 | bool setupSepaOnlineTransfer(QSqlDatabase connection) | ||||
2795 | { | ||||
2796 | auto iid = QLatin1String("org.kmymoney.creditTransfer.sepa.sqlStoragePlugin"); | ||||
2797 | // Get current version | ||||
2798 | QSqlQuery query = QSqlQuery(connection); | ||||
2799 | query.prepare("SELECT versionMajor FROM kmmPluginInfo WHERE iid = ?"); | ||||
2800 | query.bindValue(0, iid); | ||||
2801 | if (!query.exec()) { | ||||
2802 | qWarning("Could not execute query for sepaStoragePlugin: %s", qPrintable(query.lastError().text())); | ||||
2803 | return false; | ||||
2804 | } | ||||
2805 | | ||||
2806 | int currentVersion = 0; | ||||
2807 | if (query.next()) | ||||
2808 | currentVersion = query.value(0).toInt(); | ||||
2809 | | ||||
2810 | // Create database in it's most recent version if version is 0 | ||||
2811 | // (version 0 means the database was not installed) | ||||
2812 | if (currentVersion <= 1) { | ||||
2813 | // If the database is recreated the table may be still there. So drop it if needed. No error handling needed | ||||
2814 | // as this step is not necessary - only the creation is important. | ||||
2815 | if (!query.exec("DROP TABLE IF EXISTS kmmSepaOrders;")) | ||||
2816 | return false; | ||||
2817 | | ||||
2818 | if (!query.exec( | ||||
2819 | "CREATE TABLE kmmSepaOrders (" | ||||
2820 | " id varchar(32) NOT NULL PRIMARY KEY REFERENCES kmmOnlineJobs( id ) ON UPDATE CASCADE ON DELETE CASCADE," | ||||
2821 | " originAccount varchar(32) REFERENCES kmmAccounts( id ) ON UPDATE CASCADE ON DELETE SET NULL," | ||||
2822 | " value text DEFAULT '0'," | ||||
2823 | " purpose text," | ||||
2824 | " endToEndReference varchar(35)," | ||||
2825 | " beneficiaryName varchar(27)," | ||||
2826 | " beneficiaryIban varchar(32)," | ||||
2827 | " beneficiaryBic char(11)," | ||||
2828 | " textKey int," | ||||
2829 | " subTextKey int" | ||||
2830 | " );" | ||||
2831 | )) { | ||||
2832 | qWarning("Error while creating table kmmSepaOrders: %s", qPrintable(query.lastError().text())); | ||||
2833 | return false; | ||||
2834 | } | ||||
2835 | | ||||
2836 | query.prepare("DELETE FROM kmmPluginInfo WHERE iid = ?;"); | ||||
2837 | query.bindValue(0, iid); | ||||
2838 | query.exec(); | ||||
2839 | | ||||
2840 | query.prepare("INSERT INTO kmmPluginInfo (iid, versionMajor, versionMinor, uninstallQuery) VALUES(?, ?, ?, ?)"); | ||||
2841 | query.bindValue(0, iid); | ||||
2842 | query.bindValue(1, 2); | ||||
2843 | query.bindValue(2, 0); | ||||
2844 | query.bindValue(3, "DROP TABLE kmmSepaOrders;"); | ||||
2845 | if (query.exec()) | ||||
2846 | return true; | ||||
2847 | qWarning("Error while inserting kmmPluginInfo for '%s': %s", qPrintable(iid), qPrintable(query.lastError().text())); | ||||
2848 | return false; | ||||
2849 | } | ||||
2850 | | ||||
2851 | // Check if version is valid with this plugin | ||||
2852 | switch (currentVersion) { | ||||
2853 | case 2: return true; | ||||
2854 | } | ||||
2855 | | ||||
2856 | return false; | ||||
2700 | } | 2857 | } | ||
2701 | 2858 | | |||
2702 | bool actOnIBANBICObjectInSQL(SQLAction action, const payeeIdentifier &obj) | 2859 | bool actOnIBANBICObjectInSQL(SQLAction action, const payeeIdentifier &obj) | ||
Context not available. |