Add xaccTransCopyNoKvm

To avoid having to expose the KVM pointer in order to clear it.
Also rename xaccDupeTransaction to dupe_trans and make it static,
because it doesn't commit the new transaction.
This commit is contained in:
John Ralls
2013-10-16 16:40:05 -07:00
parent f29ea9fbe9
commit c748f33b92
5 changed files with 46 additions and 34 deletions

View File

@@ -1035,7 +1035,7 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
as not finding the approrpiate Accounts and not being able to
parse the formula|credit/debit strings. */
new_txn = xaccTransClone(template_txn);
new_txn = xaccTransCloneNoKvp(template_txn);
xaccTransBeginEdit(new_txn);
g_debug("creating template txn desc [%s] for sx [%s]",
@@ -1044,9 +1044,6 @@ create_each_transaction_helper(Transaction *template_txn, void *user_data)
g_debug("template txn currency is %s", gnc_commodity_get_mnemonic(xaccTransGetCurrency (template_txn)));
/* clear any copied KVP data */
qof_instance_set_slots(QOF_INSTANCE(new_txn), kvp_frame_new());
/* Bug#500427: copy the notes, if any */
if (xaccTransGetNotes(template_txn) != NULL)
{

View File

@@ -554,9 +554,8 @@ xaccTransSortSplits (Transaction *trans)
* This routine is prone to programmer snafu if not used correctly.
* It is used only by the edit-rollback code.
*/
/* Actually, it *is* public, and used by Period.c */
Transaction *
xaccDupeTransaction (const Transaction *from)
static Transaction *
dupe_trans (const Transaction *from)
{
Transaction *to;
GList *node;
@@ -593,10 +592,11 @@ xaccDupeTransaction (const Transaction *from)
/********************************************************************\
* Use this routine to externally duplicate a transaction. It creates
* a full fledged transaction with unique guid, splits, etc.
* a full fledged transaction with unique guid, splits, etc. and
* writes it to the database.
\********************************************************************/
Transaction *
xaccTransClone (const Transaction *from)
xaccTransCloneNoKvp (const Transaction *from)
{
Transaction *to;
Split *split;
@@ -615,9 +615,8 @@ xaccTransClone (const Transaction *from)
to->orig = NULL;
qof_instance_init_data (&to->inst, GNC_ID_TRANS, qof_instance_get_book(from));
kvp_frame_delete (to->inst.kvp_data);
to->inst.kvp_data = kvp_frame_copy (from->inst.kvp_data);
qof_instance_init_data (&to->inst, GNC_ID_TRANS,
qof_instance_get_book(from));
xaccTransBeginEdit(to);
for (node = from->splits; node; node = node->next)
@@ -633,11 +632,21 @@ xaccTransClone (const Transaction *from)
return to;
}
Transaction *
xaccTransClone (const Transaction *from)
{
Transaction *to = xaccTransCloneNoKvp (from);
xaccTransBeginEdit (to);
to->inst.kvp_data = kvp_frame_copy (from->inst.kvp_data);
xaccTransCommitEdit (to);
return to;
}
/*################## Added for Reg2 #################*/
/********************************************************************\
* Copy a transaction to the 'clipboard' transaction using
* xaccDupeTransaction. The 'clipboard' transaction must never
* dupe_trans. The 'clipboard' transaction must never
* be dereferenced.
\********************************************************************/
Transaction * xaccTransCopyToClipBoard(const Transaction *from_trans)
@@ -647,7 +656,7 @@ Transaction * xaccTransCopyToClipBoard(const Transaction *from_trans)
if (!from_trans)
return NULL;
to_trans = xaccDupeTransaction(from_trans);
to_trans = dupe_trans(from_trans);
return to_trans;
}
@@ -664,7 +673,7 @@ xaccTransCopyOnto(const Transaction *from_trans, Transaction *to_trans)
/********************************************************************\
* This function explicitly must robustly handle some unusual input.
*
* 'from_trans' may be a duped trans (see xaccDupeTransaction), so its
* 'from_trans' may be a duped trans (see dupe_trans), so its
* splits may not really belong to the accounts that they say they do.
*
* 'from_acc' need not be a valid account. It may be an already freed
@@ -1330,7 +1339,7 @@ xaccTransBeginEdit (Transaction *trans)
/* Make a clone of the transaction; we will use this
* in case we need to roll-back the edit. */
trans->orig = xaccDupeTransaction (trans);
trans->orig = dupe_trans (trans);
}
/********************************************************************\
@@ -2774,6 +2783,7 @@ _utest_trans_fill_functions (void)
func->trans_on_error = trans_on_error;
func->trans_cleanup_commit = trans_cleanup_commit;
func->xaccTransScrubGainsDate = xaccTransScrubGainsDate;
func->dupe_trans = dupe_trans;
return func;
}

View File

@@ -156,6 +156,12 @@ void xaccTransDestroy (Transaction *trans);
*/
Transaction * xaccTransClone (const Transaction *t);
/**
The xaccTransCloneNoKvp() method will create a complete copy of an
existing transaction except that the KVP slots will be empty.
*/
Transaction * xaccTransCloneNoKvp (const Transaction *t);
/** Equality.
*
* @param ta First transaction to compare
@@ -223,7 +229,7 @@ Transaction * xaccTransLookup (const GncGUID *guid, QofBook *book);
/*################## Added for Reg2 #################*/
/** Copy a transaction to the 'clipboard' transaction using
* xaccDupeTransaction. The 'clipboard' transaction must never
* dupe_transaction. The 'clipboard' transaction must never
* be dereferenced.
*/
Transaction * xaccTransCopyToClipBoard(const Transaction *from_trans);

View File

@@ -186,6 +186,7 @@ typedef struct
void (*trans_on_error)(Transaction*, QofBackendError);
void (*trans_cleanup_commit)(Transaction*);
void (*xaccTransScrubGainsDate)(Transaction*);
Transaction *(*dupe_trans)(const Transaction*);
} TransTestFunctions;

View File

@@ -569,12 +569,12 @@ test_xaccTransSortSplits (Fixture *fixture, gconstpointer pData)
xaccTransCommitEdit (txn);
}
/* xaccDupeTransaction
Transaction *
xaccDupeTransaction (const Transaction *from)// Local: 1:0:0
/* dupe_trans
static Transaction *
dupe_trans (const Transaction *from)// Local: 1:0:0
*/
static void
test_xaccDupeTransaction (Fixture *fixture, gconstpointer pData)
test_dupe_trans (Fixture *fixture, gconstpointer pData)
{
Timespec posted = gnc_dmy2timespec (12, 7, 2011);
Timespec entered = gnc_dmy2timespec (14, 7, 2011);
@@ -587,7 +587,7 @@ test_xaccDupeTransaction (Fixture *fixture, gconstpointer pData)
kvp_frame_set_string (old->inst.kvp_data, "/foo/bar/baz",
"The Great Waldo Pepper");
new = xaccDupeTransaction (old);
new = fixture->func->dupe_trans (old);
g_assert_cmpstr (new->num, ==, old->num);
g_assert_cmpstr (new->description, ==, old->description);
@@ -1398,29 +1398,27 @@ void
xaccTransDestroy (Transaction *trans)// C: 26 in 15 SCM: 4 in 4 Local: 3:0:0
*/
static void
test_xaccTransDestroy ()
test_xaccTransDestroy (Fixture *fixture, gconstpointer pData)
{
QofBook *book = qof_book_new ();
Transaction *txn = xaccMallocTransaction (book);
Transaction *dupe = xaccDupeTransaction (txn);
Transaction *txn = fixture->txn;
QofBook *book = qof_instance_get_book (QOF_INSTANCE (txn));
Transaction *dupe = xaccTransClone (txn);
xaccTransBeginEdit (txn);
g_assert (!qof_instance_get_destroying (QOF_INSTANCE (txn)));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, TRUE, TRUE));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, FALSE, TRUE));
xaccTransDestroy (txn);
g_assert (qof_instance_get_destroying (QOF_INSTANCE (txn)));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, TRUE, TRUE));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, FALSE, TRUE));
xaccTransRollbackEdit (txn);
qof_book_mark_readonly (book);
xaccTransBeginEdit (txn);
xaccTransDestroy (txn);
g_assert (qof_instance_get_destroying (QOF_INSTANCE (txn)));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, TRUE, TRUE));
g_assert (xaccTransEqual (txn, dupe, FALSE, TRUE, FALSE, TRUE));
xaccTransRollbackEdit (txn);
test_destroy (txn);
test_destroy (dupe);
test_destroy (book);
}
/* destroy_gains
static void
@@ -1790,7 +1788,7 @@ static void
test_xaccTransOrder_num_action (Fixture *fixture, gconstpointer pData)
{
Transaction *txnA = fixture->txn;
Transaction *txnB = xaccDupeTransaction (txnA);
Transaction *txnB = fixture->func->dupe_trans (txnA);
g_assert_cmpint (xaccTransOrder_num_action (txnA, NULL, NULL, NULL), ==, -1);
g_assert_cmpint (xaccTransOrder_num_action (NULL, NULL, txnA, NULL), ==, 1);
@@ -2042,7 +2040,7 @@ test_suite_transaction (void)
GNC_TEST_ADD (suitename, "gnc transaction set/get property", Fixture, NULL, setup, test_gnc_transaction_set_get_property, teardown);
GNC_TEST_ADD (suitename, "xaccMallocTransaction", Fixture, NULL, setup, test_xaccMallocTransaction, teardown);
GNC_TEST_ADD (suitename, "xaccTransSortSplits", Fixture, NULL, setup, test_xaccTransSortSplits, teardown);
GNC_TEST_ADD (suitename, "xaccDupeTransaction", Fixture, NULL, setup, test_xaccDupeTransaction, teardown);
GNC_TEST_ADD (suitename, "dupe_trans", Fixture, NULL, setup, test_dupe_trans, teardown);
GNC_TEST_ADD (suitename, "xaccTransClone", Fixture, NULL, setup, test_xaccTransClone, teardown);
GNC_TEST_ADD (suitename, "xaccTransCopyFromClipBoard", Fixture, NULL, setup, test_xaccTransCopyFromClipBoard, teardown);
GNC_TEST_ADD (suitename, "xaccTransCopyFromClipBoard No-Start", Fixture, NULL, setup, test_xaccTransCopyFromClipBoard_no_start, teardown);
@@ -2063,7 +2061,7 @@ test_suite_transaction (void)
GNC_TEST_ADD (suitename, "xaccTransSetCurrency", Fixture, NULL, setup, test_xaccTransSetCurrency, teardown);
GNC_TEST_ADD_FUNC (suitename, "xaccTransBeginEdit", test_xaccTransBeginEdit);
GNC_TEST_ADD_FUNC (suitename, "xaccTransDestroy", test_xaccTransDestroy);
GNC_TEST_ADD (suitename, "xaccTransDestroy", Fixture, NULL, setup, test_xaccTransDestroy, teardown);
GNC_TEST_ADD (suitename, "destroy gains", GainsFixture, NULL, setup_with_gains, test_destroy_gains, teardown_with_gains);
GNC_TEST_ADD (suitename, "do destroy", GainsFixture, NULL, setup_with_gains, test_do_destroy, teardown_with_gains);
GNC_TEST_ADD (suitename, "was trans emptied", Fixture, NULL, setup, test_was_trans_emptied, teardown);