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