Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/sql/mymoneystoragesql_p.h
Context not available. | |||||
45 | #include <QColor> | 45 | #include <QColor> | ||
---|---|---|---|---|---|
46 | #include <QDebug> | 46 | #include <QDebug> | ||
47 | #include <QStack> | 47 | #include <QStack> | ||
48 | #include <QXmlStreamReader> | ||||
48 | 49 | | |||
49 | // ---------------------------------------------------------------------------- | 50 | // ---------------------------------------------------------------------------- | ||
50 | // KDE Includes | 51 | // KDE Includes | ||
Context not available. | |||||
516 | while (query.next()) dbList.append(query.value(0).toString()); | 517 | while (query.next()) dbList.append(query.value(0).toString()); | ||
517 | 518 | | |||
518 | MyMoneyTransactionFilter filter; | 519 | MyMoneyTransactionFilter filter; | ||
520 | filter.clear(); // to get rid of default origin filter | ||||
519 | filter.setReportAllSplits(false); | 521 | filter.setReportAllSplits(false); | ||
520 | QList<MyMoneyTransaction> list; | 522 | QList<MyMoneyTransaction> list; | ||
521 | m_storage->transactionList(list, filter); | 523 | m_storage->transactionList(list, filter); | ||
Context not available. | |||||
1109 | query.bindValue(":entryDate", tx.entryDate().toString(Qt::ISODate)); | 1111 | query.bindValue(":entryDate", tx.entryDate().toString(Qt::ISODate)); | ||
1110 | query.bindValue(":currencyId", tx.commodity()); | 1112 | query.bindValue(":currencyId", tx.commodity()); | ||
1111 | query.bindValue(":bankId", tx.bankID()); | 1113 | query.bindValue(":bankId", tx.bankID()); | ||
1114 | query.bindValue(":origin", tx.origin()); | ||||
1112 | 1115 | | |||
1113 | if (!query.exec()) // krazy:exclude=crashy | 1116 | if (!query.exec()) // krazy:exclude=crashy | ||
1114 | throw MYMONEYEXCEPTIONSQL("writing Transaction"); // krazy:exclude=crashy | 1117 | throw MYMONEYEXCEPTIONSQL("writing Transaction"); // krazy:exclude=crashy | ||
Context not available. | |||||
1685 | //s.setPostDate(GETDATETIME(postDateCol)); // FIXME - when Tom puts date into split object | 1688 | //s.setPostDate(GETDATETIME(postDateCol)); // FIXME - when Tom puts date into split object | ||
1686 | s.setBankID(GETSTRING(bankIdCol)); | 1689 | s.setBankID(GETSTRING(bankIdCol)); | ||
1687 | 1690 | | |||
1691 | QStringList kvpIdList {QString::fromLatin1("%1%2").arg(GETSTRING(transactionIdCol), | ||||
1692 | GETSTRING(splitIdCol))}; | ||||
1693 | | ||||
1694 | // get the kvps | ||||
1695 | auto kvpMap = readKeyValuePairs("SPLIT", kvpIdList); | ||||
1696 | s.setPairs(kvpMap.value(kvpIdList.first()).pairs()); | ||||
1697 | const auto hasMatchTransaction = !s.MyMoneyKeyValueContainer::value("kmm-match-transaction").isEmpty(); | ||||
1698 | const auto hasMatchSplit = !s.MyMoneyKeyValueContainer::value("kmm-match-split").isEmpty(); | ||||
1699 | if (hasMatchTransaction && hasMatchSplit) | ||||
1700 | s.addMatch(); | ||||
1701 | else | ||||
1702 | s.removeMatch(); // MyMoneyStorageSql::fetchTransactions enter this method multiple times with old split argument, so we must remove match explicitly | ||||
1703 | | ||||
1688 | return; | 1704 | return; | ||
1689 | } | 1705 | } | ||
1690 | 1706 | | |||
Context not available. | |||||
2133 | if ((rc = upgradeToV12()) != 0) return (1); | 2149 | if ((rc = upgradeToV12()) != 0) return (1); | ||
2134 | ++m_dbVersion; | 2150 | ++m_dbVersion; | ||
2135 | break; | 2151 | break; | ||
2152 | case 12: | ||||
2153 | if ((rc = upgradeToV13()) != 0) return (1); | ||||
2154 | ++m_dbVersion; | ||||
2155 | break; | ||||
2136 | default: | 2156 | default: | ||
2137 | qWarning("Unknown version number in database - %d", m_dbVersion); | 2157 | qWarning("Unknown version number in database - %d", m_dbVersion); | ||
2138 | } | 2158 | } | ||
Context not available. | |||||
2497 | return 0; | 2517 | return 0; | ||
2498 | } | 2518 | } | ||
2499 | 2519 | | |||
2520 | /** | ||||
2521 | * @brief upgradeToV13 | ||||
2522 | * Changes (2018-08-19): | ||||
2523 | * 1) make two separate transactions out of two matched transactions | ||||
2524 | * 2) create a new tramsaction as a result of matching | ||||
2525 | * 3) add origin (imported, typed, matched) of transaction | ||||
2526 | * 4) add kmm-match-transaction containing two matched transaction IDs to every matched split | ||||
2527 | * 5) add kmm-match-split containing two matched split IDs to every matched split | ||||
2528 | * @return 0 if successful, 1 if not successful | ||||
2529 | */ | ||||
2530 | int upgradeToV13() | ||||
2531 | { | ||||
2532 | Q_Q(MyMoneyStorageSql); | ||||
2533 | MyMoneyDbTransaction dbtrans(*q, Q_FUNC_INFO); | ||||
2534 | | ||||
2535 | switch(haveColumnInTable(QLatin1String("kmmTransactions"), QLatin1String("origin"))) { | ||||
2536 | case -1: | ||||
2537 | return 1; | ||||
2538 | case 1: // column exists, nothing to do | ||||
2539 | break; | ||||
2540 | case 0: // need update of kmmTransactions | ||||
2541 | if (!alterTable(m_db.m_tables["kmmTransactions"], m_dbVersion)) | ||||
2542 | return 1; | ||||
2543 | break; | ||||
2544 | } | ||||
2545 | | ||||
2546 | QSqlQuery selectQuery(*q); | ||||
2547 | QSqlQuery updateQuery(*q); | ||||
2548 | QSqlQuery deleteQuery(*q); | ||||
2549 | QSqlQuery insertQuery(*q); | ||||
2550 | | ||||
2551 | // we need to know the highest unoccupied ID to know what IDs can we assign to new (reorganized) transactions | ||||
2552 | selectQuery.prepare(QString::fromLatin1("SELECT id FROM kmmTransactions ORDER BY id DESC")); | ||||
2553 | if (!selectQuery.exec()) { | ||||
2554 | buildError(selectQuery, Q_FUNC_INFO, QString("Error retrieving highest transaction id.")); | ||||
2555 | return 1; | ||||
2556 | } | ||||
2557 | | ||||
2558 | // no transactions = no problem | ||||
2559 | if (!selectQuery.next()) | ||||
2560 | return 0; | ||||
2561 | auto transactionRecord = selectQuery.record(); | ||||
2562 | auto lastTransactionID = transactionRecord.value(0).toString().mid(1).toULong(); | ||||
2563 | | ||||
2564 | // set default transaction origin for every transaction | ||||
2565 | updateQuery.prepare(QString::fromLatin1("UPDATE kmmTransactions SET origin='%1'").arg( | ||||
2566 | QString::number(static_cast<int>(eMyMoney::Transaction::Origin::Typed)))); | ||||
2567 | if (!updateQuery.exec()) { | ||||
2568 | buildError(updateQuery, Q_FUNC_INFO, QString("Error updating transaction origins.")); | ||||
2569 | return 1; | ||||
2570 | } | ||||
2571 | | ||||
2572 | // correct transaction origin if it has been imported | ||||
2573 | selectQuery.prepare("SELECT * FROM kmmKeyValuePairs WHERE kvpType='TRANSACTION' AND kvpKey='Imported'"); | ||||
2574 | if (!selectQuery.exec()) { | ||||
2575 | buildError(selectQuery, Q_FUNC_INFO, QString("Error retrieving key value pairs about imported transactions.")); | ||||
2576 | return 1; | ||||
2577 | } | ||||
2578 | | ||||
2579 | while (selectQuery.next()) { | ||||
2580 | auto record = selectQuery.record(); | ||||
2581 | auto transactionID = record.value("kvpID").toString(); | ||||
2582 | | ||||
2583 | updateQuery.prepare(QString::fromLatin1("UPDATE kmmTransactions SET origin='%1' WHERE id='%2'").arg( | ||||
2584 | QString::number(static_cast<int>(eMyMoney::Transaction::Origin::Imported)), | ||||
2585 | transactionID)); | ||||
2586 | if (!updateQuery.exec()) { | ||||
2587 | buildError(updateQuery, Q_FUNC_INFO, QString("Error assigning transaction's origin to imported.")); | ||||
2588 | return 1; | ||||
2589 | } | ||||
2590 | } | ||||
2591 | | ||||
2592 | deleteQuery.prepare("DELETE FROM kmmKeyValuePairs WHERE kvpKey='Imported'"); | ||||
2593 | if (!deleteQuery.exec()) { | ||||
2594 | buildError(deleteQuery, Q_FUNC_INFO, QString("Error deleting key value pairs about imported transactions.")); | ||||
2595 | return 1; | ||||
2596 | } | ||||
2597 | | ||||
2598 | // search for embedded tranactions | ||||
2599 | QSqlQuery queryMatchedTX(*q); | ||||
2600 | queryMatchedTX.prepare("SELECT * FROM kmmKeyValuePairs WHERE kvpType='SPLIT' AND kvpKey='kmm-matched-tx'"); | ||||
2601 | if (!queryMatchedTX.exec()) { | ||||
2602 | buildError(queryMatchedTX, Q_FUNC_INFO, QString("Error retrieving key value pairs about matched transactions.")); | ||||
2603 | return 1; | ||||
2604 | } | ||||
2605 | | ||||
2606 | while (queryMatchedTX.next()) { | ||||
2607 | auto record = queryMatchedTX.record(); | ||||
2608 | auto transactionIDAndSplitID = record.value("kvpID").toString(); | ||||
2609 | auto transactionID = transactionIDAndSplitID; | ||||
2610 | transactionID = transactionID.left(19); // transaction id size | ||||
2611 | | ||||
2612 | // unembedd imported transaction | ||||
2613 | auto embeddedTransactionXML = record.value("kvpData").toString(); | ||||
2614 | embeddedTransactionXML.replace(QLatin1String("<"), QLatin1String("<")); | ||||
2615 | | ||||
2616 | QXmlStreamReader reader(embeddedTransactionXML); | ||||
2617 | QXmlStreamAttributes embeddedTransactionAttributes; | ||||
2618 | QString embeddedTransactionID; | ||||
2619 | | ||||
2620 | auto isEmbeddedTransactionImported = false; | ||||
2621 | // start parsing transaction in XML | ||||
2622 | reader.readNextStartElement(); // this reads container | ||||
2623 | while (reader.readNextStartElement()) { | ||||
2624 | if (reader.name() == "TRANSACTION") { | ||||
2625 | embeddedTransactionAttributes = reader.attributes(); | ||||
2626 | embeddedTransactionID = QString::fromLatin1("T%1").arg(QString::number(++lastTransactionID).rightJustified(18 , '0')); | ||||
2627 | | ||||
2628 | while (reader.readNextStartElement()) { | ||||
2629 | if (reader.name() == "SPLITS") { | ||||
2630 | while (reader.readNextStartElement()) { | ||||
2631 | if (reader.name() == "SPLIT") { | ||||
2632 | auto embeddedTransactionSplitAttributes = reader.attributes(); | ||||
2633 | auto embeddedSplitIDConvertedToSQL = QString::number(embeddedTransactionSplitAttributes.value("id").toString().mid(1).toInt() - 1); | ||||
2634 | | ||||
2635 | // SPLIT node has only key-value pairs node as subnode, so read it before all | ||||
2636 | while (reader.readNextStartElement()) { | ||||
2637 | if (reader.name() == "KEYVALUEPAIRS") { | ||||
2638 | while (reader.readNextStartElement()) { | ||||
2639 | // inserting all key-value pairs of embedded transaction without filtering anything out might be dangerous | ||||
2640 | if (reader.name() == "PAIR") { | ||||
2641 | auto embeddedTransactionSplitKVPAttributes = reader.attributes(); | ||||
2642 | const QStringList columnValuesInKVP = { | ||||
2643 | "SPLIT", | ||||
2644 | QString::fromLatin1("%1%2").arg(embeddedTransactionID, embeddedSplitIDConvertedToSQL), | ||||
2645 | embeddedTransactionSplitKVPAttributes.value("key").toString(), | ||||
2646 | embeddedTransactionSplitKVPAttributes.value("value").toString() | ||||
2647 | }; | ||||
2648 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmKeyValuePairs VALUES ('%1')").arg(columnValuesInKVP.join("','"))); | ||||
2649 | if (!insertQuery.exec()) { | ||||
2650 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting key-value pairs of splits of embedded transaction.")); | ||||
2651 | return 1; | ||||
2652 | } | ||||
2653 | } | ||||
2654 | reader.skipCurrentElement(); | ||||
2655 | } | ||||
2656 | } else { | ||||
2657 | reader.skipCurrentElement(); | ||||
2658 | } | ||||
2659 | } | ||||
2660 | | ||||
2661 | // we use highest precision for formatted values because we don't know precise precision yet | ||||
2662 | const auto valueFormatted = MyMoneyMoney(embeddedTransactionSplitAttributes.value("value").toString()).formatMoney(QString(), -1, false).replace(QChar(','), QChar('.')); | ||||
2663 | const auto priceFormatted = MyMoneyMoney(embeddedTransactionSplitAttributes.value("price").toString()).formatMoney(QString(), 8, false).replace(QChar(','), QChar('.')); | ||||
2664 | const auto sharesFormatted = MyMoneyMoney(embeddedTransactionSplitAttributes.value("shares").toString()).formatMoney(QString(), 8, false).replace(QChar(','), QChar('.')); | ||||
2665 | | ||||
2666 | // we unembedd split 1:1 except its ID an transaction ID | ||||
2667 | const QStringList columnValuesInSplit = { | ||||
2668 | embeddedTransactionID, | ||||
2669 | "N", | ||||
2670 | embeddedSplitIDConvertedToSQL, | ||||
2671 | embeddedTransactionSplitAttributes.value("payee").toString(), | ||||
2672 | embeddedTransactionSplitAttributes.value("reconciledate").toString(), | ||||
2673 | embeddedTransactionSplitAttributes.value("action").toString(), | ||||
2674 | embeddedTransactionSplitAttributes.value("reconcileflag").toString(), | ||||
2675 | embeddedTransactionSplitAttributes.value("value").toString(), | ||||
2676 | valueFormatted, | ||||
2677 | embeddedTransactionSplitAttributes.value("shares").toString(), | ||||
2678 | sharesFormatted, | ||||
2679 | embeddedTransactionSplitAttributes.value("price").toString(), | ||||
2680 | priceFormatted, | ||||
2681 | embeddedTransactionSplitAttributes.value("memo").toString(), | ||||
2682 | embeddedTransactionSplitAttributes.value("account").toString(), | ||||
2683 | QString(), | ||||
2684 | QString(), | ||||
2685 | embeddedTransactionAttributes.value("postdate").toString(), | ||||
2686 | embeddedTransactionAttributes.value("bankid").toString() | ||||
2687 | }; | ||||
2688 | | ||||
2689 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmSplits VALUES ('%1')").arg(columnValuesInSplit.join("','"))); | ||||
2690 | if (!insertQuery.exec()) { | ||||
2691 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting splits of embedded transaction.")); | ||||
2692 | return 1; | ||||
2693 | } | ||||
2694 | } else { | ||||
2695 | reader.skipCurrentElement(); | ||||
2696 | } | ||||
2697 | } | ||||
2698 | | ||||
2699 | // key-value pairs node as a subnode of transaction | ||||
2700 | } else if (reader.name() == "KEYVALUEPAIRS") { | ||||
2701 | while (reader.readNextStartElement()) { | ||||
2702 | if (reader.name() == "PAIR") { | ||||
2703 | auto embeddedTransactionKVPAttributes = reader.attributes(); | ||||
2704 | const QStringList columnValuesInKVP = { | ||||
2705 | "TRANSACTION", | ||||
2706 | embeddedTransactionID, | ||||
2707 | embeddedTransactionKVPAttributes.value("key").toString(), | ||||
2708 | embeddedTransactionKVPAttributes.value("value").toString() | ||||
2709 | }; | ||||
2710 | | ||||
2711 | // we don't want Imported key to be unembedded because it's replaced by origin attribute | ||||
2712 | if (embeddedTransactionKVPAttributes.value("key") == "Imported") { | ||||
2713 | isEmbeddedTransactionImported = true; | ||||
2714 | } else { | ||||
2715 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmKeyValuePairs VALUES ('%1')").arg(columnValuesInKVP.join("','"))); | ||||
2716 | if (!insertQuery.exec()) { | ||||
2717 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting key-value pairs of embedded transaction.")); | ||||
2718 | return 1; | ||||
2719 | } | ||||
2720 | } | ||||
2721 | } else { | ||||
2722 | reader.skipCurrentElement(); | ||||
2723 | } | ||||
2724 | | ||||
2725 | } | ||||
2726 | } else { | ||||
2727 | reader.skipCurrentElement(); | ||||
2728 | } | ||||
2729 | } | ||||
2730 | } else { | ||||
2731 | reader.skipCurrentElement(); | ||||
2732 | } | ||||
2733 | } | ||||
2734 | | ||||
2735 | // inserting embedded transaction as 1:1 except its ID | ||||
2736 | QStringList columnValuesInTransaction = { | ||||
2737 | embeddedTransactionID, | ||||
2738 | "N", | ||||
2739 | embeddedTransactionAttributes.value("postdate").toString(), | ||||
2740 | embeddedTransactionAttributes.value("memo").toString(), | ||||
2741 | embeddedTransactionAttributes.value("entrydate").toString(), | ||||
2742 | embeddedTransactionAttributes.value("commodity").toString(), | ||||
2743 | QString(), | ||||
2744 | isEmbeddedTransactionImported ? | ||||
2745 | QString::number(static_cast<int>(eMyMoney::Transaction::Origin::Imported | eMyMoney::Transaction::Origin::MatchingInput)) : | ||||
2746 | QString::number(static_cast<int>(eMyMoney::Transaction::Origin::Typed | eMyMoney::Transaction::Origin::MatchingInput)) | ||||
2747 | }; | ||||
2748 | | ||||
2749 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmTransactions VALUES ('%1')").arg(columnValuesInTransaction.join("','"))); | ||||
2750 | | ||||
2751 | if (!insertQuery.exec()) { | ||||
2752 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting embedded transaction.")); | ||||
2753 | return 1; | ||||
2754 | } | ||||
2755 | | ||||
2756 | // we already unembedded end transaction that served as input for matching | ||||
2757 | // and now we'll restore original stand of start transaction that also served as input for matching | ||||
2758 | // from matched transaction that is output from matching | ||||
2759 | selectQuery.prepare(QString::fromLatin1("SELECT * FROM kmmTransactions WHERE id='%1'").arg(transactionID)); | ||||
2760 | | ||||
2761 | if (!selectQuery.exec() || !selectQuery.next()) { | ||||
2762 | buildError(selectQuery, Q_FUNC_INFO, QString("Error retrieving matched transaction.")); | ||||
2763 | return 1; | ||||
2764 | } | ||||
2765 | auto sourceTransactionRecord = selectQuery.record(); | ||||
2766 | auto sourceTransactionID = QString::fromLatin1("T%1").arg(QString::number(++lastTransactionID).rightJustified(18 , '0')); | ||||
2767 | sourceTransactionRecord.setValue("id", QVariant(sourceTransactionID)); | ||||
2768 | auto sourceTransactionOrigin = sourceTransactionRecord.value("origin").toInt(); | ||||
2769 | sourceTransactionOrigin = sourceTransactionOrigin | eMyMoney::Transaction::Origin::MatchingInput; | ||||
2770 | sourceTransactionRecord.setValue("origin", QVariant(sourceTransactionOrigin)); | ||||
2771 | | ||||
2772 | // in matched transaction splits there are backup values of start transaction, so iterate over them | ||||
2773 | selectQuery.prepare(QString::fromLatin1("SELECT * FROM kmmSplits WHERE transactionId='%1'").arg(transactionID)); | ||||
2774 | if (!selectQuery.exec()) { | ||||
2775 | buildError(selectQuery, Q_FUNC_INFO, QString("Error retrieving splits.")); | ||||
2776 | return 1; | ||||
2777 | } | ||||
2778 | | ||||
2779 | while (selectQuery.next()) { | ||||
2780 | auto sourceTransactionSplitRecord = selectQuery.record(); | ||||
2781 | auto transactionSplitID = sourceTransactionSplitRecord.value("splitId").toInt(); | ||||
2782 | sourceTransactionSplitRecord.setValue("transactionId", QVariant(sourceTransactionID)); | ||||
2783 | | ||||
2784 | // backup values of start transaction are stored in key-value pairs of split | ||||
2785 | QSqlQuery kvpQuery(*q); | ||||
2786 | kvpQuery.prepare(QString::fromLatin1("SELECT * FROM kmmKeyValuePairs WHERE kvpType='SPLIT' AND kvpId='%1%2'").arg(transactionID, QString::number(transactionSplitID))); | ||||
2787 | if (!kvpQuery.exec()) { | ||||
2788 | buildError(kvpQuery, Q_FUNC_INFO, QString("Error retrieving key-value pairs about matched transaction.")); | ||||
2789 | return 1; | ||||
2790 | } | ||||
2791 | | ||||
2792 | // store key-value pairs because we will deltete them from database now and will be filtering them out later on | ||||
2793 | QMap<QString, QVariant> keyValueMap; | ||||
2794 | while (kvpQuery.next()) { | ||||
2795 | auto kvpKey = kvpQuery.value("kvpKey").toString(); | ||||
2796 | auto kvpData = kvpQuery.value("kvpData"); | ||||
2797 | keyValueMap.insert(kvpKey, kvpData); | ||||
2798 | } | ||||
2799 | | ||||
2800 | deleteQuery.prepare(QString::fromLatin1("DELETE FROM kmmKeyValuePairs WHERE kvpType='SPLIT' AND kvpId='%1%2'").arg(transactionID, QString::number(transactionSplitID))); | ||||
2801 | if (!deleteQuery.exec()) { | ||||
2802 | buildError(deleteQuery, Q_FUNC_INFO, QString("Error deleting key-value pairs.")); | ||||
2803 | return 1; | ||||
2804 | } | ||||
2805 | | ||||
2806 | // split containg kmm-match-split means that it was matched | ||||
2807 | // otherwise it's a generic split and will be written only with new transaction ID | ||||
2808 | if (keyValueMap.contains("kmm-match-split")) { | ||||
2809 | | ||||
2810 | // restore all original values from backup for source transaction and its split | ||||
2811 | if (keyValueMap.contains("kmm-orig-postdate")) { | ||||
2812 | sourceTransactionRecord.setValue("postDate", keyValueMap.value("kmm-orig-postdate")); | ||||
2813 | keyValueMap.remove("kmm-orig-postdate"); | ||||
2814 | } | ||||
2815 | | ||||
2816 | auto sourceTransactionSplitID = keyValueMap.value("kmm-match-split").toString().mid(1).toInt() - 1; | ||||
2817 | if (transactionSplitID == sourceTransactionSplitID) { | ||||
2818 | if (keyValueMap.contains("kmm-orig-memo")) { | ||||
2819 | sourceTransactionSplitRecord.setValue("memo", keyValueMap.value("kmm-orig-memo")); | ||||
2820 | keyValueMap.remove("kmm-orig-memo"); | ||||
2821 | } | ||||
2822 | if (keyValueMap.contains("kmm-orig-payee")) { | ||||
2823 | sourceTransactionSplitRecord.setValue("payee", keyValueMap.value("kmm-orig-payee")); | ||||
2824 | keyValueMap.remove("kmm-orig-payee"); | ||||
2825 | } | ||||
2826 | } | ||||
2827 | | ||||
2828 | // add key-value pairs according to new way of storing information about matched split | ||||
2829 | QStringList columnValuesInKVP = { | ||||
2830 | "SPLIT", | ||||
2831 | QString::fromLatin1("%1%2").arg(transactionID, QString::number(transactionSplitID)), | ||||
2832 | "kmm-match-transaction", | ||||
2833 | QString::fromLatin1("%1;%2").arg(sourceTransactionID, embeddedTransactionID) | ||||
2834 | }; | ||||
2835 | | ||||
2836 | | ||||
2837 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmKeyValuePairs VALUES ('%1')").arg(columnValuesInKVP.join("','"))); | ||||
2838 | if (!insertQuery.exec()) { | ||||
2839 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting kmm-match-transaction information.")); | ||||
2840 | return 1; | ||||
2841 | } | ||||
2842 | | ||||
2843 | columnValuesInKVP = QStringList { | ||||
2844 | "SPLIT", | ||||
2845 | QString::fromLatin1("%1%2").arg(transactionID, QString::number(transactionSplitID)), | ||||
2846 | "kmm-match-split", | ||||
2847 | QString::fromLatin1("%1;%2").arg(QString::number(transactionSplitID), QString::number(sourceTransactionSplitID)) | ||||
2848 | }; | ||||
2849 | | ||||
2850 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmKeyValuePairs VALUES ('%1')").arg(columnValuesInKVP.join("','"))); | ||||
2851 | if (!insertQuery.exec()) { | ||||
2852 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting kmm-match-split information.")); | ||||
2853 | return 1; | ||||
2854 | } | ||||
2855 | | ||||
2856 | } | ||||
2857 | | ||||
2858 | QStringList columnValuesInSplit; | ||||
2859 | for (auto i = 0; i < sourceTransactionSplitRecord.count(); ++i) | ||||
2860 | columnValuesInSplit.append(sourceTransactionSplitRecord.value(i).toString()); | ||||
2861 | | ||||
2862 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmSplits VALUES ('%1')").arg(columnValuesInSplit.join("','"))); | ||||
2863 | if (!insertQuery.exec()) { | ||||
2864 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting split.")); | ||||
2865 | return 1; | ||||
2866 | } | ||||
2867 | | ||||
2868 | // all key-value pairs were deleted from the database, so write only the usefull ones back | ||||
2869 | QStringList columnValuesInKVP { | ||||
2870 | "SPLIT", | ||||
2871 | QString::fromLatin1("%1%2").arg(sourceTransactionID, QString::number(transactionSplitID)), | ||||
2872 | QString(), | ||||
2873 | QString() | ||||
2874 | }; | ||||
2875 | | ||||
2876 | for (auto it = keyValueMap.cbegin(); it != keyValueMap.cend(); ++it) { | ||||
2877 | if (it.key() == "kmm-matched-tx" || it.key() == "kmm-match-split") | ||||
2878 | continue; | ||||
2879 | columnValuesInKVP.replace(2, it.key()); | ||||
2880 | columnValuesInKVP.replace(3, it.value().toString()); | ||||
2881 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmKeyValuePairs VALUES ('%1')").arg(columnValuesInKVP.join("','"))); | ||||
2882 | if (!insertQuery.exec()) { | ||||
2883 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting key-value pair.")); | ||||
2884 | return 1; | ||||
2885 | } | ||||
2886 | } | ||||
2887 | } | ||||
2888 | | ||||
2889 | // inserting source transaction as it was just before matching | ||||
2890 | columnValuesInTransaction = QStringList(); | ||||
2891 | for (auto i = 0; i < sourceTransactionRecord.count(); ++i) | ||||
2892 | columnValuesInTransaction.append(sourceTransactionRecord.value(i).toString()); | ||||
2893 | | ||||
2894 | insertQuery.prepare(QString::fromLatin1("INSERT INTO kmmTransactions VALUES ('%1')").arg(columnValuesInTransaction.join("','"))); | ||||
2895 | if (!insertQuery.exec()) { | ||||
2896 | buildError(insertQuery, Q_FUNC_INFO, QString("Error inserting source transaction.")); | ||||
2897 | return 1; | ||||
2898 | } | ||||
2899 | } | ||||
2900 | return 0; | ||||
2901 | } | ||||
2902 | | ||||
2903 | | ||||
2500 | int createTables() | 2904 | int createTables() | ||
2501 | { | 2905 | { | ||
2502 | Q_Q(MyMoneyStorageSql); | 2906 | Q_Q(MyMoneyStorageSql); | ||
Context not available. |