Hi all, I'd like to follow the example shown at https://wiki.gnucash.org/wiki/Using_the_API, which demonstrates creating a transaction. And I'd like to do it with a freshly-created Postgres backend.
That tutorial starts from a point at which you've already got an `Account`, and therefore can easily get a `QofBook` from it, which gets passed to `xaccMallocTransaction()`. However, I'm having trouble getting that far. I've got some code put together, which you can see here: https://github.com/feuGeneA/a-gnucash-integration/pull/2 This email is largely a copy/paste of the PR description there. Feel free to reply here, and/or comment on code directly there, whatever works for you. For the sake of completeness, I've attached my source and my debug logs to this email, but all of that is visible in the PR (source code in the PR itself, and logs in the CI runs (GitHub Checks)). Thank you in advance for any feedback! Gene ----- I've got the GnuCash engine connecting to Postgres, but I'm not sure what to do next, and I've got errors: * One error is coming from the `qof_session_destroy()` routine, and it doesn't seem to make any sense at all. - You can see this error in the output of the `ci/circleci:build` Check on the commit `Start and stop the engine`. - Note the error message `qof session error message: (-484235720). func: qof_session_destroy`. - This is coming from my own routine `qof_error_check()` at `a-gnucash-integration.cpp:11`, which in this case is being called from `a-gnucash-integration.cpp:49`. - Note that `qof_session_get_error_message(session)` is returning an empty string, and `qof_session_get_error(session)` is returning a ridiculous value. - Am I using these routines right? * Another error is coming from my attempt to get the session's book and seed an `Account` with it. - See what I introduced in the commit `Get QofBook and create a root Account`. - Below are the `ERROR` logs are being emitted. You can see the full debug log in the `ci/circleci:build` Check on the commit. ``` * 05:37:54 ERROR <gnc.engine> Account* xaccMallocAccount(QofBook*): assertion 'book' failed * 05:37:54 ERROR <GLib-GObject> g_type_instance_get_private: assertion 'instance != NULL && instance->g_class != NULL' failed * 05:37:54 ERROR <gnc.engine> void xaccAccountBeginEdit(Account*): assertion 'acc' failed ``` It feels like I'm not using these routines properly. Where am I going wrong? Other questions: * How do i properly do error checking? Is my `qof_error_check()` routine the right approach? What about for non-`qof_`-prefixed routines, eg `xaccMallocTransaction()` or `gnc_account_create_root()`? * When do i need to call `qof_session_save()` or `qof_session_load()`? * What else should I be aware of as I continue trying to connect the dots between what I have here and what's shown in the API tutorial?
#include <cassert> #include <iostream> #include <gnucash/Account.h> #include <gnucash/gnc-engine.h> #include <gnucash/qofsession.h> #include <gnucash/Transaction.h> using std::cout; using std::endl; void qof_error_check(QofSession *session, const char* func = "") { while( qof_session_events_pending(session) || qof_session_save_in_progress(session) ) { cout << "waiting..." << endl; continue; } QofBackendError error = qof_session_get_error(session); if (error != QofBackendError::ERR_BACKEND_NO_ERR) { cout << "qof session error message: " << qof_session_get_error_message(session) << "(" << error << "). " << "func: " << func << endl; exit(error); } } const char* postgresUrl = "postgres://gnc:gnc@127.0.0.1:5432/gnucash"; class QofSessionRAII { public: QofSession *session; QofSessionRAII() { session = qof_session_new(NULL); qof_error_check(session, "qof_session_new"); qof_session_begin(session, postgresUrl, SessionOpenMode::SESSION_NEW_OVERWRITE); qof_error_check(session, "qof_session_begin"); } ~QofSessionRAII() { qof_session_end(session); qof_error_check(session, "qof_session_end"); qof_session_destroy(session); qof_error_check(session, "qof_session_destroy"); } }; class GncEngineRAII { public: GncEngineRAII() { cout << "initializing engine..." << endl; gnc_engine_init(0, { NULL }); while (!gnc_engine_is_initialized()) { cout << "waiting for engine to initialize..." << endl; } }; ~GncEngineRAII() { cout << "shutting down engine..." << endl; while (gnc_engine_is_initialized()) { cout << "waiting for engine to shut down..." << endl; } gnc_engine_shutdown(); } }; int main() { qof_log_init(); qof_log_set_level ("", QOF_LOG_DEBUG); qof_log_set_level ("qof", QOF_LOG_DEBUG); qof_log_set_level ("gnc", QOF_LOG_DEBUG); GncEngineRAII engine; QofSessionRAII sessionRaii; QofSession * session = sessionRaii.session; QofBook *book = qof_session_get_book(session); qof_error_check(session, "qof_session_get_book"); Account *rootAccount = gnc_account_create_root(book); /* Account *account = xaccMallocAccount(book); Transaction *transaction = xaccMallocTransaction(book); */ return 0; }
* 05:11:33 DEBUG <qof.query> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofquery.cpp:qof_query_init()] * 05:11:33 DEBUG <qof.query> [leave qof_query_init()] Completed initialization of QofQuery * 05:11:33 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] GNC_DBD_DIR not set: using libdbi built-in default * 05:11:33 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] 1 DBD drivers found * 05:11:33 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] Driver: pgsql * 05:11:33 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::begin()] sess=0x560cd45e6e90 mode=3, URI=postgres://gnc:gnc@127.0.0.1:5432/gnucash * 05:11:33 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::load_backend()] list=3 * 05:11:33 INFO <qof.session> [QofSessionImpl::load_backend] Selected provider GnuCash Libdbi (POSTGRESQL) Backend * 05:11:33 DEBUG <qof.session> [leave QofSessionImpl::load_backend()] * 05:11:33 DEBUG <gnc.backend.dbi> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/backend/dbi/gnc-backend-dbi.cpp:GncDbiBackend<Type>::session_begin()] * 05:11:34 DEBUG <gnc.backend.dbi> [GncDbiSqlConnection::begin_transaction] BEGIN * 05:11:34 DEBUG <gnc.backend.dbi> [GncDbiSqlConnection::commit_transaction] COMMIT * 05:11:34 INFO <gnc.backend.dbi> [GncDbiBackend<Type>::session_begin] logpath=/root/.local/share/gnucash/translog/postgres_127.0.0.1_gnc_gnucash * 05:11:34 DEBUG <gnc.backend.dbi> [leave GncDbiBackend<Type>::session_begin()] * 05:11:34 INFO <qof.session> [QofSessionImpl::begin] Done running session_begin on backend * 05:11:34 DEBUG <qof.session> [leave QofSessionImpl::begin()] sess=0x560cd45e6e90 book-id=postgres://gnc:gnc@127.0.0.1:5432/gnucash * 05:11:34 ERROR <gnc.engine> xaccMallocTransaction: assertion 'book' failed * 05:11:34 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::end()] sess=0x560cd45e6e90 uri=postgres://gnc:gnc@127.0.0.1:5432/gnucash * 05:11:34 DEBUG <qof.session> [leave QofSessionImpl::end()] sess=0x560cd45e6e90 uri= * 05:11:34 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::~QofSessionImpl()] sess=0x560cd45e6e90 uri= qof session error message: (-731984408). func: qof_session_destroy * 05:11:34 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::end()] sess=0x560cd45e6e90 uri= * 05:11:34 DEBUG <qof.session> [leave QofSessionImpl::end()] sess=0x560cd45e6e90 uri= * 05:11:34 DEBUG <qof.session> [leave QofSessionImpl::~QofSessionImpl()] sess=0x560cd45e6e90
* 05:46:44 DEBUG <qof.query> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofquery.cpp:qof_query_init()] * 05:46:44 DEBUG <qof.query> [leave qof_query_init()] Completed initialization of QofQuery * 05:46:44 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] GNC_DBD_DIR not set: using libdbi built-in default * 05:46:44 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] 1 DBD drivers found * 05:46:44 INFO <gnc.backend.dbi> [gnc_module_init_backend_dbi] Driver: pgsql * 05:46:44 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::begin()] sess=0x559f61a46e90 mode=3, URI=postgres://gnc:gnc@127.0.0.1:5432/gnucash * 05:46:44 DEBUG <qof.session> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/engine/qofsession.cpp:QofSessionImpl::load_backend()] list=3 * 05:46:44 INFO <qof.session> [QofSessionImpl::load_backend] Selected provider GnuCash Libdbi (POSTGRESQL) Backend * 05:46:44 DEBUG <qof.session> [leave QofSessionImpl::load_backend()] * 05:46:44 DEBUG <gnc.backend.dbi> [enter /build/gnucash-7VuxMi/gnucash-4.2/libgnucash/backend/dbi/gnc-backend-dbi.cpp:GncDbiBackend<Type>::session_begin()] * 05:46:45 DEBUG <gnc.backend.dbi> [GncDbiSqlConnection::begin_transaction] BEGIN * 05:46:45 DEBUG <gnc.backend.dbi> [GncDbiSqlConnection::commit_transaction] COMMIT * 05:46:45 INFO <gnc.backend.dbi> [GncDbiBackend<Type>::session_begin] logpath=/root/.local/share/gnucash/translog/postgres_127.0.0.1_gnc_gnucash * 05:46:45 DEBUG <gnc.backend.dbi> [leave GncDbiBackend<Type>::session_begin()] * 05:46:45 INFO <qof.session> [QofSessionImpl::begin] Done running session_begin on backend * 05:46:45 DEBUG <qof.session> [leave QofSessionImpl::begin()] sess=0x559f61a46e90 book-id=postgres://gnc:gnc@127.0.0.1:5432/gnucash * 05:46:45 ERROR <gnc.engine> Account* xaccMallocAccount(QofBook*): assertion 'book' failed * 05:46:45 ERROR <GLib-GObject> g_type_instance_get_private: assertion 'instance != NULL && instance->g_class != NULL' failed * 05:46:45 ERROR <gnc.engine> void xaccAccountBeginEdit(Account*): assertion 'acc' failed
_______________________________________________ gnucash-devel mailing list gnucash-devel@gnucash.org https://lists.gnucash.org/mailman/listinfo/gnucash-devel