QofSession, QofBackend: Change book_id to uri.

It's more descriptive and less likely to be confused with the book:id value
in XML files that is the book's GUID.
Also changed the QofSessionImpl::begin new_uri parameter from std::string to
const char*. There's no point in allocating a string just to call
new_uri.c_str() all over the place.
This commit is contained in:
John Ralls
2020-06-02 11:48:33 -07:00
parent 65e2639de4
commit a320035f42
9 changed files with 89 additions and 90 deletions

View File

@@ -1499,7 +1499,7 @@ gnc_main_window_generate_title (GncMainWindow *window)
QofBook *book;
gboolean immutable;
gchar *filename = NULL;
const gchar *book_id = NULL;
const gchar *uri = NULL;
const gchar *dirty = "";
const gchar *readonly_text = NULL;
gchar *readonly;
@@ -1507,7 +1507,7 @@ gnc_main_window_generate_title (GncMainWindow *window)
if (gnc_current_session_exist())
{
book_id = qof_session_get_url (gnc_get_current_session ());
uri = qof_session_get_url (gnc_get_current_session ());
book = gnc_get_current_book();
if (qof_book_session_not_saved (book))
dirty = "*";
@@ -1522,15 +1522,15 @@ gnc_main_window_generate_title (GncMainWindow *window)
? g_strdup_printf(" %s", readonly_text)
: g_strdup("");
if (!book_id || g_strcmp0 (book_id, "") == 0)
if (!uri || g_strcmp0 (uri, "") == 0)
filename = g_strdup(_("Unsaved Book"));
else
{
if (gnc_uri_targets_local_fs (book_id))
if (gnc_uri_targets_local_fs (uri))
{
/* The filename is a true file.
* The Gnome HIG 2.0 recommends only the file name (no path) be used. (p15) */
gchar *path = gnc_uri_get_path ( book_id );
gchar *path = gnc_uri_get_path ( uri );
filename = g_path_get_basename ( path );
g_free ( path );
}
@@ -1538,7 +1538,7 @@ gnc_main_window_generate_title (GncMainWindow *window)
{
/* The filename is composed of database connection parameters.
* For this we will show access_method://username@database[:port] */
filename = gnc_uri_normalize_uri (book_id, FALSE);
filename = gnc_uri_normalize_uri (uri, FALSE);
}
}
@@ -1654,22 +1654,22 @@ static gboolean statusbar_notification_off(gpointer user_data_unused)
static gchar *generate_statusbar_lastmodified_message()
{
gchar *message = NULL;
const gchar *book_id = NULL;
const gchar *uri = NULL;
if (gnc_current_session_exist())
{
book_id = qof_session_get_url (gnc_get_current_session ());
uri = qof_session_get_url (gnc_get_current_session ());
}
if (!(book_id && strlen (book_id)))
if (!(uri && strlen (uri)))
return NULL;
else
{
if (gnc_uri_targets_local_fs (book_id))
if (gnc_uri_targets_local_fs (uri))
{
#ifdef HAVE_SYS_STAT_H
/* The filename is a true file. */
gchar *filepath = gnc_uri_get_path ( book_id );
gchar *filepath = gnc_uri_get_path ( uri );
gchar *filename = g_path_get_basename ( filepath );
{
// Access the mtime information through stat(2)

View File

@@ -376,7 +376,7 @@ error_handler<DbType::DBI_SQLITE> (dbi_conn conn, void* user_data)
template <> void
GncDbiBackend<DbType::DBI_SQLITE>::session_begin(QofSession* session,
const char* book_id,
const char* new_uri,
bool ignore_lock,
bool create, bool force)
{
@@ -384,12 +384,12 @@ GncDbiBackend<DbType::DBI_SQLITE>::session_begin(QofSession* session,
PairVec options;
g_return_if_fail (session != nullptr);
g_return_if_fail (book_id != nullptr);
g_return_if_fail (new_uri != nullptr);
ENTER (" ");
/* Remove uri type if present */
auto path = gnc_uri_get_path (book_id);
auto path = gnc_uri_get_path (new_uri);
std::string filepath{path};
g_free(path);
GFileTest ftest = static_cast<decltype (ftest)> (
@@ -441,7 +441,7 @@ GncDbiBackend<DbType::DBI_SQLITE>::session_begin(QofSession* session,
if (result < 0)
{
dbi_conn_close(conn);
PERR ("Unable to connect to %s: %d\n", book_id, result);
PERR ("Unable to connect to %s: %d\n", new_uri, result);
set_error (ERR_BACKEND_BAD_URL);
LEAVE("Error");
return;
@@ -611,21 +611,21 @@ adjust_sql_options (dbi_conn connection)
template <DbType Type> void
GncDbiBackend<Type>::session_begin (QofSession* session, const char* book_id,
GncDbiBackend<Type>::session_begin (QofSession* session, const char* new_uri,
bool ignore_lock, bool create, bool force)
{
GncDbiTestResult dbi_test_result = GNC_DBI_PASS;
PairVec options;
g_return_if_fail (session != nullptr);
g_return_if_fail (book_id != nullptr);
g_return_if_fail (new_uri != nullptr);
ENTER (" ");
/* Split the book-id
* Format is protocol://username:password@hostname:port/dbname
where username, password and port are optional) */
UriStrings uri(book_id);
UriStrings uri(new_uri);
if (Type == DbType::DBI_PGSQL)
{

View File

@@ -106,11 +106,11 @@ GncXmlBackend::check_path (const char* fullpath, bool create)
}
void
GncXmlBackend::session_begin(QofSession* session, const char* book_id,
GncXmlBackend::session_begin(QofSession* session, const char* new_uri,
bool ignore_lock, bool create, bool force)
{
/* Make sure the directory is there */
m_fullpath = gnc_uri_get_path (book_id);
m_fullpath = gnc_uri_get_path (new_uri);
if (m_fullpath.empty())
{

View File

@@ -35,7 +35,7 @@ public:
GncXmlBackend(const GncXmlBackend&&) = delete;
GncXmlBackend operator=(const GncXmlBackend&&) = delete;
~GncXmlBackend() = default;
void session_begin(QofSession* session, const char* book_id,
void session_begin(QofSession* session, const char* new_uri,
bool ignore_lock, bool create, bool force) override;
void session_end() override;
void load(QofBook* book, QofBackendLoadType loadType) override;

View File

@@ -185,7 +185,7 @@ public:
/**
* Open the file or connect to the server.
* @param session The QofSession that will control the backend.
* @param book_id The book's string identifier.
* @param new_uri The location of the data store that the backend will use.
* @param ignore_lock indicates whether the single-user lock on the backend
* should be cleared. The typical GUI sequence leading to this is:
* (1) GUI attempts to open the backend by calling this routine with
@@ -203,7 +203,7 @@ public:
* @param force works with create to force creating a new database even if
* one already exists at the same URI.
*/
virtual void session_begin(QofSession *session, const char* book_id,
virtual void session_begin(QofSession *session, const char* new_uri,
bool ignore_lock, bool create, bool force) = 0;
virtual void session_end() = 0;
/**

View File

@@ -121,7 +121,7 @@ qof_backend_get_registered_access_method_list(void)
QofSessionImpl::QofSessionImpl (QofBook* book) noexcept
: m_backend {},
m_book {book},
m_book_id {},
m_uri {},
m_saving {false},
m_last_err {},
m_error_message {}
@@ -130,7 +130,7 @@ QofSessionImpl::QofSessionImpl (QofBook* book) noexcept
QofSessionImpl::~QofSessionImpl () noexcept
{
ENTER ("sess=%p book_id=%s", this, m_book_id.c_str ());
ENTER ("sess=%p uri=%s", this, m_uri.c_str ());
end ();
destroy_backend ();
qof_book_set_backend (m_book, nullptr);
@@ -182,10 +182,10 @@ QofSessionImpl::load_backend (std::string access_method) noexcept
PINFO (" Selected provider %s", prov->provider_name);
// Only do a type check when trying to open an existing file
// When saving over an existing file the contents of the original file don't matter
if (!m_creating && !prov->type_check (m_book_id.c_str ()))
if (!m_creating && !prov->type_check (m_uri.c_str ()))
{
PINFO("Provider, %s, reported not being usable for book, %s.",
prov->provider_name, m_book_id.c_str ());
prov->provider_name, m_uri.c_str ());
continue;
}
m_backend = prov->create_backend();
@@ -203,8 +203,8 @@ QofSessionImpl::load (QofPercentageFunc percentage_func) noexcept
/* We must have an empty book to load into or bad things will happen. */
g_return_if_fail(m_book && qof_book_empty(m_book));
if (!m_book_id.size ()) return;
ENTER ("sess=%p book_id=%s", this, m_book_id.c_str ());
if (!m_uri.size ()) return;
ENTER ("sess=%p uri=%s", this, m_uri.c_str ());
/* At this point, we should are supposed to have a valid book
* id and a lock on the file. */
@@ -246,18 +246,18 @@ QofSessionImpl::load (QofPercentageFunc percentage_func) noexcept
return;
}
LEAVE ("sess = %p, book_id=%s", this, m_book_id.c_str ());
LEAVE ("sess = %p, uri=%s", this, m_uri.c_str ());
}
void
QofSessionImpl::begin (std::string new_book_id, bool ignore_lock,
QofSessionImpl::begin (const char* new_uri, bool ignore_lock,
bool create, bool force) noexcept
{
ENTER (" sess=%p ignore_lock=%d, book-id=%s",
this, ignore_lock, new_book_id.c_str ());
this, ignore_lock, new_uri);
clear_error ();
/* Check to see if this session is already open */
if (m_book_id.size ())
if (m_uri.size ())
{
if (ERR_BACKEND_NO_ERR != get_error ())
push_error (ERR_BACKEND_LOCKED, {});
@@ -266,20 +266,20 @@ QofSessionImpl::begin (std::string new_book_id, bool ignore_lock,
}
/* seriously invalid */
if (!new_book_id.size ())
if (!new_uri)
{
if (ERR_BACKEND_NO_ERR != get_error ())
push_error (ERR_BACKEND_BAD_URL, {});
LEAVE("push error missing new_book_id");
LEAVE("push error missing new_uri");
return;
}
char * scheme {g_uri_parse_scheme (new_book_id.c_str ())};
char * scheme {g_uri_parse_scheme (new_uri)};
char * filename {nullptr};
if (g_strcmp0 (scheme, "file") == 0)
filename = g_filename_from_uri (new_book_id.c_str (), nullptr, nullptr);
filename = g_filename_from_uri (new_uri, nullptr, nullptr);
else if (!scheme)
filename = g_strdup (new_book_id.c_str ());
filename = g_strdup (new_uri);
if (filename && g_file_test (filename, G_FILE_TEST_IS_DIR))
{
@@ -293,7 +293,7 @@ QofSessionImpl::begin (std::string new_book_id, bool ignore_lock,
/* destroy the old backend */
destroy_backend ();
/* Store the session URL */
m_book_id = new_book_id;
m_uri = new_uri;
m_creating = create;
if (filename)
load_backend ("file");
@@ -305,23 +305,23 @@ QofSessionImpl::begin (std::string new_book_id, bool ignore_lock,
/* No backend was found. That's bad. */
if (m_backend == nullptr)
{
m_book_id = {};
m_uri = {};
if (ERR_BACKEND_NO_ERR == get_error ())
push_error (ERR_BACKEND_BAD_URL, {});
LEAVE (" BAD: no backend: sess=%p book-id=%s",
this, new_book_id.c_str ());
this, new_uri);
return;
}
/* If there's a begin method, call that. */
m_backend->session_begin(this, m_book_id.c_str(),
m_backend->session_begin(this, m_uri.c_str(),
ignore_lock, create, force);
PINFO ("Done running session_begin on backend");
QofBackendError const err {m_backend->get_error()};
auto msg (m_backend->get_message());
if (err != ERR_BACKEND_NO_ERR)
{
m_book_id = {};
m_uri = {};
push_error (err, msg);
LEAVE (" backend error %d %s", err, msg.empty() ? "(null)" : msg.c_str());
return;
@@ -331,19 +331,19 @@ QofSessionImpl::begin (std::string new_book_id, bool ignore_lock,
PWARN("%s", msg.c_str());
}
LEAVE (" sess=%p book-id=%s", this, new_book_id.c_str ());
LEAVE (" sess=%p book-id=%s", this, new_uri);
}
void
QofSessionImpl::end () noexcept
{
ENTER ("sess=%p book_id=%s", this, m_book_id.c_str ());
ENTER ("sess=%p uri=%s", this, m_uri.c_str ());
auto backend = qof_book_get_backend (m_book);
if (backend != nullptr)
backend->session_end();
clear_error ();
m_book_id.clear();
LEAVE ("sess=%p book_id=%s", this, m_book_id.c_str ());
m_uri.clear();
LEAVE ("sess=%p uri=%s", this, m_uri.c_str ());
}
/* error handling functions --------------------------------*/
@@ -424,9 +424,9 @@ QofSessionImpl::get_file_path () const noexcept
}
std::string const &
QofSessionImpl::get_book_id () const noexcept
QofSessionImpl::get_uri () const noexcept
{
return m_book_id;
return m_uri;
}
bool
@@ -443,7 +443,7 @@ QofSessionImpl::save (QofPercentageFunc percentage_func) noexcept
if (!qof_book_session_not_saved (m_book)) //Clean book, nothing to do.
return;
m_saving = true;
ENTER ("sess=%p book_id=%s", this, m_book_id.c_str ());
ENTER ("sess=%p uri=%s", this, m_uri.c_str ());
/* If there is a backend, the book is dirty, and the backend is reachable
* (i.e. we can communicate with it), then synchronize with the backend. If
@@ -490,7 +490,7 @@ QofSessionImpl::safe_save (QofPercentageFunc percentage_func) noexcept
auto msg = m_backend->get_message();
if (err != ERR_BACKEND_NO_ERR)
{
m_book_id = nullptr;
m_uri = "";
push_error (err, msg);
}
}
@@ -540,8 +540,8 @@ QofSessionImpl::export_session (QofSessionImpl & real_session,
QofPercentageFunc percentage_func) noexcept
{
auto real_book = real_session.get_book ();
ENTER ("tmp_session=%p real_session=%p book=%p book_id=%s",
this, &real_session, real_book, m_book_id.c_str ());
ENTER ("tmp_session=%p real_session=%p book=%p uri=%s",
this, &real_session, real_book, m_uri.c_str ());
/* There must be a backend or else. (It should always be the file
* backend too.)
@@ -599,7 +599,7 @@ const char *
qof_session_get_url (const QofSession *session)
{
if (!session) return NULL;
return session->get_book_id ().c_str ();
return session->get_uri ().c_str ();
}
QofBackend *
@@ -610,11 +610,11 @@ qof_session_get_backend (const QofSession *session)
}
void
qof_session_begin (QofSession *session, const char * book_id,
qof_session_begin (QofSession *session, const char * new_uri,
gboolean ignore_lock, gboolean create, gboolean force)
{
if (!session) return;
session->begin((book_id ? book_id : ""), ignore_lock, create, force);
session->begin((new_uri ? new_uri : ""), ignore_lock, create, force);
}
void
@@ -706,18 +706,18 @@ qof_session_destroy_backend (QofSession * session)
session->destroy_backend ();
}
void qof_session_set_book_id (QofSession * session, char const * book_id)
void qof_session_set_uri (QofSession * session, char const * uri)
{
if (!book_id)
session->m_book_id = "";
if (!uri)
session->m_uri = "";
else
session->m_book_id = book_id;
session->m_uri = uri;
}
void (*p_qof_session_load_backend) (QofSession *, const char * access_method);
void (*p_qof_session_clear_error) (QofSession *);
void (*p_qof_session_destroy_backend) (QofSession *);
void (*p_qof_session_set_book_id) (QofSession *, char const * book_id);
void (*p_qof_session_set_uri) (QofSession *, char const * uri);
void
init_static_qofsession_pointers (void)
@@ -725,7 +725,7 @@ init_static_qofsession_pointers (void)
p_qof_session_load_backend = &qof_session_load_backend;
p_qof_session_clear_error = &qof_session_clear_error;
p_qof_session_destroy_backend = &qof_session_destroy_backend;
p_qof_session_set_book_id = &qof_session_set_book_id;
p_qof_session_set_uri = &qof_session_set_uri;
}
QofBackendError

View File

@@ -152,7 +152,7 @@ void qof_session_swap_data (QofSession *session_1, QofSession *session_2);
* If an error occurs, it will be pushed onto the session error
* stack, and that is where it should be examined.
*/
void qof_session_begin (QofSession *session, const char * book_id,
void qof_session_begin (QofSession *session, const char * new_uri,
gboolean ignore_lock, gboolean create,
gboolean force);

View File

@@ -41,7 +41,7 @@ struct QofSessionImpl
~QofSessionImpl () noexcept;
/** Begin this session. */
void begin (std::string book_id, bool ignore_lock, bool create, bool force) noexcept;
void begin (const char* new_uri, bool ignore_lock, bool create, bool force) noexcept;
/** Swap books with another session */
void swap_books (QofSessionImpl &) noexcept;
@@ -62,7 +62,7 @@ struct QofSessionImpl
* We return by reference so that a pointer to the data of the string lives
* long enough to make it back to C code.
*/
std::string const & get_book_id () const noexcept;
std::string const & get_uri () const noexcept;
/**
* Returns and clears the local cached error. If there is no local error, we check
* for an error in the backend.
@@ -92,10 +92,10 @@ private:
/* A book holds pointers to the various types of datasets.
* A session has exactly one book. */
QofBook * m_book;
/* The requested book id, in the form or a URI, such as
* file:/some/where, or sql:server.host.com:555
/* The requested URI, such as
* file://some/where, or sql:server.host.com:555
*/
std::string m_book_id;
std::string m_uri;
bool m_saving;
bool m_creating;
@@ -113,8 +113,8 @@ private:
/* These functions support the old testing infrastructure and should
* be removed when they are no longer necessary.*/
friend void qof_session_load_backend (QofSession *, const char *);
friend char const * qof_session_get_book_id (QofSession *);
friend void qof_session_set_book_id (QofSession *, char const *);
friend char const * qof_session_get_uri (QofSession *);
friend void qof_session_set_uri (QofSession *, char const *);
};
typedef struct qof_instance_copy_data

View File

@@ -41,7 +41,6 @@ extern "C" void test_suite_qofsession ( void );
extern void (*p_qof_session_load_backend) (QofSession *, const char * access_method);
extern void (*p_qof_session_clear_error) (QofSession *);
extern void (*p_qof_session_destroy_backend) (QofSession *);
extern void (*p_qof_session_set_book_id) (QofSession *, const char * book_id);
void init_static_qofsession_pointers (void);
@@ -134,10 +133,10 @@ struct QofMockLoadBackendProvider : public QofBackendProvider
};
bool
QofMockLoadBackendProvider::type_check (const char* book_id)
QofMockLoadBackendProvider::type_check (const char* uri)
{
g_assert (book_id);
g_assert_cmpstr (book_id, ==, "my book");
g_assert (uri);
g_assert_cmpstr (uri, ==, "my book");
load_backend_struct.check_data_type_called = true;
return load_backend_struct.data_compatible;
}
@@ -184,7 +183,7 @@ test_qof_session_load_backend (Fixture *fixture, gconstpointer pData)
qof_backend_register_provider (std::move(prov));
load_backend_struct.data_compatible = FALSE;
load_backend_struct.check_data_type_called = FALSE;
p_qof_session_set_book_id (fixture->session, "my book");
p_qof_session_set_uri (fixture->session, "my book");
p_qof_session_load_backend (fixture->session, "file");
g_assert (load_backend_struct.check_data_type_called);
g_assert_cmpint (qof_session_get_error (fixture->session), == , ERR_BACKEND_NO_HANDLER);
@@ -243,7 +242,7 @@ test_qof_session_load (Fixture *fixture, gconstpointer pData)
QofBook *newbook = NULL;
/* init */
p_qof_session_set_book_id (fixture->session, "my book");
p_qof_session_set_uri (fixture->session, "my book");
be = g_new0 (QofBackend, 1);
g_assert (be);
qof_book_set_backend (qof_session_get_book (fixture->session), be);
@@ -280,22 +279,22 @@ static struct
{
QofBackend *be;
QofSession *session;
const char *book_id;
const char *uri;
gboolean backend_new_called;
gboolean session_begin_called;
gboolean produce_error;
} session_begin_struct;
static void
mock_session_begin (QofBackend *be, QofSession *session, const char *book_id,
mock_session_begin (QofBackend *be, QofSession *session, const char *uri,
gboolean ignore_lock, gboolean create, gboolean force)
{
g_assert (be);
g_assert (be == session_begin_struct.be);
g_assert (session);
g_assert (session == session_begin_struct.session);
g_assert (book_id);
g_assert_cmpstr (book_id, == , session_begin_struct.book_id);
g_assert (uri);
g_assert_cmpstr (uri, == , session_begin_struct.uri);
g_assert (ignore_lock);
g_assert (!create);
g_assert (force);
@@ -315,9 +314,9 @@ struct QofMockSessBackendProvider : public QofBackendProvider
};
bool
QofMockSessBackendProvider::type_check (const char* book_id)
QofMockSessBackendProvider::type_check (const char* uri)
{
g_assert (book_id);
g_assert (uri);
return true;
}
@@ -350,14 +349,14 @@ test_qof_session_begin (Fixture *fixture, gconstpointer pData)
g_assert_cmpint (get_providers().size(), == , 0);
/* run tests */
g_test_message ("Test when book_id is set backend is not changed");
g_test_message ("Test when uri is set backend is not changed");
qof_book_set_backend (qof_session_get_book (fixture->session), be);
p_qof_session_set_book_id (fixture->session, "my book");
p_qof_session_set_uri (fixture->session, "my book");
qof_session_begin (fixture->session, "my book", ignore_lock, create, force);
g_assert (qof_book_get_backend (qof_session_get_book (fixture->session)) == be);
g_test_message ("Test when session book_id is not set and book_id passed is null backend is not changed");
p_qof_session_set_book_id (fixture->session, NULL);
g_test_message ("Test when session uri is not set and uri passed is null backend is not changed");
p_qof_session_set_uri (fixture->session, NULL);
qof_session_begin (fixture->session, NULL, ignore_lock, create, force);
g_assert (qof_book_get_backend (qof_session_get_book (fixture->session)) == be);
@@ -379,7 +378,7 @@ test_qof_session_begin (Fixture *fixture, gconstpointer pData)
g_test_message ("Test with valid backend returned and session begin set; error is produced");
session_begin_struct.session = fixture->session;
session_begin_struct.book_id = "postgres://localhost:8080";
session_begin_struct.uri = "postgres://localhost:8080";
session_begin_struct.backend_new_called = FALSE;
session_begin_struct.session_begin_called = FALSE;
session_begin_struct.produce_error = TRUE;
@@ -417,7 +416,7 @@ static struct
QofBackend *be;
QofBook *book;
QofSession *session;
const char *book_id;
const char *uri;
gboolean sync_called;
gboolean backend_new_called;
gboolean session_begin_called;
@@ -545,14 +544,14 @@ test_qof_session_end (Fixture *fixture, gconstpointer pData)
{
QofBackend *be = NULL;
g_test_message ("Test backend is closed, errors cleared and book_id removed");
g_test_message ("Test backend is closed, errors cleared and uri removed");
be = g_new0 (QofBackend, 1);
g_assert (be);
be->session_end = mock_session_end;
be->last_err = ERR_BACKEND_DATA_CORRUPT;
be->error_msg = g_strdup("push any error");
qof_book_set_backend (qof_session_get_book (fixture->session), be);
p_qof_session_set_book_id (fixture->session, "my book");
p_qof_session_set_uri (fixture->session, "my book");
session_end_struct.called = FALSE;
session_end_struct.be = be;
qof_session_end (fixture->session);