mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Replace heap-allocate GncSqlColumnInfo GList with on-stack std::vector.
Faster, more concise, can't leak.
This commit is contained in:
parent
72ac25d755
commit
be1a5f56d6
@ -62,13 +62,14 @@ typedef enum
|
||||
GNC_DBI_FAIL_TEST
|
||||
} GncDbiTestResult;
|
||||
|
||||
typedef gchar* (*CREATE_TABLE_DDL_FN) (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const GList* col_info_list);
|
||||
typedef GSList* (*GET_TABLE_LIST_FN) (dbi_conn conn, const gchar* dbname);
|
||||
typedef void (*APPEND_COLUMN_DEF_FN) (GString* ddl, GncSqlColumnInfo* info);
|
||||
typedef GSList* (*GET_INDEX_LIST_FN) (dbi_conn conn);
|
||||
typedef void (*DROP_INDEX_FN) (dbi_conn conn, const gchar* index);
|
||||
typedef gchar* (*CREATE_TABLE_DDL_FN) (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec);
|
||||
typedef GSList* (*GET_TABLE_LIST_FN) (dbi_conn conn, const gchar* dbname);
|
||||
typedef void (*APPEND_COLUMN_DEF_FN) (GString* ddl,
|
||||
const GncSqlColumnInfo& info);
|
||||
typedef GSList* (*GET_INDEX_LIST_FN) (dbi_conn conn);
|
||||
typedef void (*DROP_INDEX_FN) (dbi_conn conn, const gchar* index);
|
||||
typedef struct
|
||||
{
|
||||
CREATE_TABLE_DDL_FN create_table_ddl;
|
||||
|
@ -156,15 +156,14 @@ static gchar lock_table[] = "gnclock";
|
||||
#define SQLITE3_URI_PREFIX (SQLITE3_URI_TYPE "://")
|
||||
#define PGSQL_DEFAULT_PORT 5432
|
||||
|
||||
static gchar* conn_create_table_ddl_sqlite3 (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const GList* col_info_list);
|
||||
static gchar* conn_create_table_ddl_sqlite3 (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec);
|
||||
static GSList* conn_get_table_list (dbi_conn conn, const gchar* dbname);
|
||||
static GSList* conn_get_table_list_sqlite3 (dbi_conn conn,
|
||||
const gchar* dbname);
|
||||
static void append_sqlite3_col_def (GString* ddl, GncSqlColumnInfo* info);
|
||||
static GSList* conn_get_index_list_sqlite3 (dbi_conn conn);
|
||||
static void conn_drop_index_sqlite3 (dbi_conn conn, const gchar* index);
|
||||
static GSList* conn_get_table_list_sqlite3 (dbi_conn conn, const gchar* dbname);
|
||||
static void append_sqlite3_col_def (GString* ddl, const GncSqlColumnInfo& info);
|
||||
static GSList *conn_get_index_list_sqlite3 (dbi_conn conn);
|
||||
static void conn_drop_index_sqlite3 (dbi_conn conn, const gchar *index);
|
||||
static provider_functions_t provider_sqlite3 =
|
||||
{
|
||||
conn_create_table_ddl_sqlite3,
|
||||
@ -175,12 +174,12 @@ static provider_functions_t provider_sqlite3 =
|
||||
};
|
||||
#define SQLITE3_TIMESPEC_STR_FORMAT "%04d%02d%02d%02d%02d%02d"
|
||||
|
||||
static gchar* conn_create_table_ddl_mysql (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const GList* col_info_list);
|
||||
static void append_mysql_col_def (GString* ddl, GncSqlColumnInfo* info);
|
||||
static GSList* conn_get_index_list_mysql (dbi_conn conn);
|
||||
static void conn_drop_index_mysql (dbi_conn conn, const gchar* index);
|
||||
static gchar* conn_create_table_ddl_mysql (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec);
|
||||
static void append_mysql_col_def (GString* ddl, const GncSqlColumnInfo& info);
|
||||
static GSList *conn_get_index_list_mysql (dbi_conn conn);
|
||||
static void conn_drop_index_mysql (dbi_conn conn, const gchar *index);
|
||||
static provider_functions_t provider_mysql =
|
||||
{
|
||||
conn_create_table_ddl_mysql,
|
||||
@ -191,13 +190,13 @@ static provider_functions_t provider_mysql =
|
||||
};
|
||||
#define MYSQL_TIMESPEC_STR_FORMAT "%04d%02d%02d%02d%02d%02d"
|
||||
|
||||
static gchar* conn_create_table_ddl_pgsql (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const GList* col_info_list);
|
||||
static gchar* conn_create_table_ddl_pgsql (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec );
|
||||
static GSList* conn_get_table_list_pgsql (dbi_conn conn, const gchar* dbname);
|
||||
static void append_pgsql_col_def (GString* ddl, GncSqlColumnInfo* info);
|
||||
static GSList* conn_get_index_list_pgsql (dbi_conn conn);
|
||||
static void conn_drop_index_pgsql (dbi_conn conn, const gchar* index);
|
||||
static void append_pgsql_col_def (GString* ddl, const GncSqlColumnInfo& info);
|
||||
static GSList *conn_get_index_list_pgsql (dbi_conn conn);
|
||||
static void conn_drop_index_pgsql (dbi_conn conn, const gchar *index);
|
||||
|
||||
static provider_functions_t provider_pgsql =
|
||||
{
|
||||
@ -209,19 +208,19 @@ static provider_functions_t provider_pgsql =
|
||||
};
|
||||
#define PGSQL_TIMESPEC_STR_FORMAT "%04d%02d%02d %02d%02d%02d"
|
||||
|
||||
static gboolean gnc_dbi_lock_database (QofBackend* qbe, gboolean ignore_lock);
|
||||
static void gnc_dbi_unlock (QofBackend* qbe);
|
||||
static gboolean gnc_dbi_lock_database (QofBackend *qbe, gboolean ignore_lock);
|
||||
static void gnc_dbi_unlock (QofBackend *qbe);
|
||||
static gboolean save_may_clobber_data (QofBackend* qbe);
|
||||
|
||||
static gchar* create_index_ddl (GncSqlConnection* conn,
|
||||
const gchar* index_name,
|
||||
const gchar* table_name,
|
||||
const GncSqlColumnTableEntry* col_table);
|
||||
static gchar* add_columns_ddl (GncSqlConnection* conn,
|
||||
static gchar* create_index_ddl (GncSqlConnection* conn,
|
||||
const gchar* index_name,
|
||||
const gchar* table_name,
|
||||
GList* col_info_list);
|
||||
const GncSqlColumnTableEntry* col_table);
|
||||
static gchar* add_columns_ddl (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec);
|
||||
static GncSqlConnection* create_dbi_connection (provider_functions_t* provider,
|
||||
QofBackend* qbe, dbi_conn conn);
|
||||
QofBackend* qbe, dbi_conn conn);
|
||||
static GncDbiTestResult conn_test_dbi_library (dbi_conn conn);
|
||||
#define GNC_DBI_PROVIDER_SQLITE (&provider_sqlite3)
|
||||
#define GNC_DBI_PROVIDER_MYSQL (&provider_mysql)
|
||||
@ -2690,80 +2689,73 @@ create_index_ddl (GncSqlConnection* conn,
|
||||
return g_string_free (ddl, FALSE);
|
||||
}
|
||||
|
||||
static gchar*
|
||||
add_columns_ddl (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
GList* col_info_list)
|
||||
static gchar*
|
||||
add_columns_ddl(GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GString* ddl;
|
||||
const GList* list_node;
|
||||
guint col_num;
|
||||
GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
|
||||
|
||||
g_return_val_if_fail (conn != NULL, NULL);
|
||||
g_return_val_if_fail (table_name != NULL, NULL);
|
||||
g_return_val_if_fail (col_info_list != NULL, NULL);
|
||||
|
||||
ddl = g_string_new ("");
|
||||
g_string_printf (ddl, "ALTER TABLE %s ", table_name);
|
||||
for (list_node = col_info_list, col_num = 0; list_node != NULL;
|
||||
list_node = list_node->next, col_num++)
|
||||
for (auto const& info : info_vec)
|
||||
{
|
||||
GncSqlColumnInfo* info = (GncSqlColumnInfo*) (list_node->data);
|
||||
|
||||
if (col_num != 0)
|
||||
if (info != *info_vec.begin())
|
||||
{
|
||||
(void)g_string_append (ddl, ", ");
|
||||
}
|
||||
g_string_append (ddl, "ADD COLUMN ");
|
||||
dbi_conn->provider->append_col_def (ddl, info);
|
||||
delete info;
|
||||
}
|
||||
|
||||
return g_string_free (ddl, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
append_sqlite3_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
append_sqlite3_col_def(GString* ddl, const GncSqlColumnInfo& info)
|
||||
{
|
||||
const char* type_name = nullptr;
|
||||
|
||||
if (info->m_type == BCT_INT)
|
||||
if (info.m_type == BCT_INT)
|
||||
{
|
||||
type_name = "integer";
|
||||
}
|
||||
else if (info->m_type == BCT_INT64)
|
||||
else if (info.m_type == BCT_INT64)
|
||||
{
|
||||
type_name = "bigint";
|
||||
}
|
||||
else if (info->m_type == BCT_DOUBLE)
|
||||
else if (info.m_type == BCT_DOUBLE)
|
||||
{
|
||||
type_name = "float8";
|
||||
}
|
||||
else if (info->m_type == BCT_STRING || info->m_type == BCT_DATE
|
||||
|| info->m_type == BCT_DATETIME)
|
||||
else if (info.m_type == BCT_STRING || info.m_type == BCT_DATE
|
||||
|| info.m_type == BCT_DATETIME)
|
||||
{
|
||||
type_name = "text";
|
||||
}
|
||||
else
|
||||
{
|
||||
PERR ("Unknown column type: %d\n", info->m_type);
|
||||
PERR ("Unknown column type: %d\n", info.m_type);
|
||||
type_name = "";
|
||||
}
|
||||
g_string_append_printf (ddl, "%s %s", info->m_name.c_str(), type_name);
|
||||
if (info->m_size != 0)
|
||||
g_string_append_printf (ddl, "%s %s", info.m_name.c_str(), type_name);
|
||||
if (info.m_size != 0)
|
||||
{
|
||||
(void)g_string_append_printf (ddl, "(%d)", info->m_size);
|
||||
(void)g_string_append_printf (ddl, "(%d)", info.m_size);
|
||||
}
|
||||
if (info->m_primary_key)
|
||||
if (info.m_primary_key)
|
||||
{
|
||||
(void)g_string_append (ddl, " PRIMARY KEY");
|
||||
}
|
||||
if (info->m_autoinc)
|
||||
if (info.m_autoinc)
|
||||
{
|
||||
(void)g_string_append (ddl, " AUTOINCREMENT");
|
||||
}
|
||||
if (info->m_not_null)
|
||||
if (info.m_not_null)
|
||||
{
|
||||
(void)g_string_append (ddl, " NOT NULL");
|
||||
}
|
||||
@ -2772,29 +2764,23 @@ append_sqlite3_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
static gchar*
|
||||
conn_create_table_ddl_sqlite3 (GncSqlConnection* conn,
|
||||
const gchar* table_name,
|
||||
const GList* col_info_list)
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GString* ddl;
|
||||
const GList* list_node;
|
||||
guint col_num;
|
||||
unsigned int col_num = 0;
|
||||
|
||||
g_return_val_if_fail (conn != NULL, NULL);
|
||||
g_return_val_if_fail (table_name != NULL, NULL);
|
||||
g_return_val_if_fail (col_info_list != NULL, NULL);
|
||||
|
||||
ddl = g_string_new ("");
|
||||
g_string_printf (ddl, "CREATE TABLE %s (", table_name);
|
||||
for (list_node = col_info_list, col_num = 0; list_node != NULL;
|
||||
list_node = list_node->next, col_num++)
|
||||
for (auto const& info : info_vec)
|
||||
{
|
||||
GncSqlColumnInfo* info = (GncSqlColumnInfo*) (list_node->data);
|
||||
|
||||
if (col_num != 0)
|
||||
if (col_num++ != 0)
|
||||
{
|
||||
(void)g_string_append (ddl, ", ");
|
||||
}
|
||||
append_sqlite3_col_def (ddl, info);
|
||||
delete info;
|
||||
}
|
||||
(void)g_string_append (ddl, ")");
|
||||
|
||||
@ -2802,59 +2788,57 @@ conn_create_table_ddl_sqlite3 (GncSqlConnection* conn,
|
||||
}
|
||||
|
||||
static void
|
||||
append_mysql_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
append_mysql_col_def (GString* ddl, const GncSqlColumnInfo& info)
|
||||
{
|
||||
const char* type_name = nullptr;
|
||||
|
||||
if (info->m_type == BCT_INT)
|
||||
if (info.m_type == BCT_INT)
|
||||
{
|
||||
type_name = "integer";
|
||||
}
|
||||
else if (info->m_type == BCT_INT64)
|
||||
else if (info.m_type == BCT_INT64)
|
||||
{
|
||||
type_name = "bigint";
|
||||
}
|
||||
else if (info->m_type == BCT_DOUBLE)
|
||||
else if (info.m_type == BCT_DOUBLE)
|
||||
{
|
||||
type_name = "double";
|
||||
}
|
||||
else if (info->m_type == BCT_STRING)
|
||||
else if (info.m_type == BCT_STRING)
|
||||
{
|
||||
type_name = "varchar";
|
||||
}
|
||||
else if (info->m_type == BCT_DATE)
|
||||
else if (info.m_type == BCT_DATE)
|
||||
{
|
||||
info->m_size = 0;
|
||||
type_name = "date";
|
||||
}
|
||||
else if (info->m_type == BCT_DATETIME)
|
||||
else if (info.m_type == BCT_DATETIME)
|
||||
{
|
||||
info->m_size = 0;
|
||||
type_name = "TIMESTAMP NULL DEFAULT 0";
|
||||
}
|
||||
else
|
||||
{
|
||||
PERR ("Unknown column type: %d\n", info->m_type);
|
||||
PERR ("Unknown column type: %d\n", info.m_type);
|
||||
type_name = "";
|
||||
}
|
||||
g_string_append_printf (ddl, "%s %s", info->m_name.c_str(), type_name);
|
||||
if (info->m_size != 0)
|
||||
g_string_append_printf (ddl, "%s %s", info.m_name.c_str(), type_name);
|
||||
if (info.m_size != 0 && info.m_type == BCT_STRING)
|
||||
{
|
||||
g_string_append_printf (ddl, "(%d)", info->m_size);
|
||||
g_string_append_printf (ddl, "(%d)", info.m_size);
|
||||
}
|
||||
if (info->m_unicode)
|
||||
if (info.m_unicode)
|
||||
{
|
||||
(void)g_string_append (ddl, " CHARACTER SET utf8");
|
||||
}
|
||||
if (info->m_primary_key)
|
||||
if (info.m_primary_key)
|
||||
{
|
||||
(void)g_string_append (ddl, " PRIMARY KEY");
|
||||
}
|
||||
if (info->m_autoinc)
|
||||
if (info.m_autoinc)
|
||||
{
|
||||
(void)g_string_append (ddl, " AUTO_INCREMENT");
|
||||
}
|
||||
if (info->m_not_null)
|
||||
if (info.m_not_null)
|
||||
{
|
||||
(void)g_string_append (ddl, " NOT NULL");
|
||||
}
|
||||
@ -2862,29 +2846,23 @@ append_mysql_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
|
||||
static gchar*
|
||||
conn_create_table_ddl_mysql (GncSqlConnection* conn, const gchar* table_name,
|
||||
const GList* col_info_list)
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GString* ddl;
|
||||
const GList* list_node;
|
||||
guint col_num;
|
||||
unsigned int col_num = 0;
|
||||
|
||||
g_return_val_if_fail (conn != NULL, NULL);
|
||||
g_return_val_if_fail (table_name != NULL, NULL);
|
||||
g_return_val_if_fail (col_info_list != NULL, NULL);
|
||||
|
||||
ddl = g_string_new ("");
|
||||
g_string_printf (ddl, "CREATE TABLE %s (", table_name);
|
||||
for (list_node = col_info_list, col_num = 0; list_node != NULL;
|
||||
list_node = list_node->next, col_num++)
|
||||
for (auto const& info : info_vec)
|
||||
{
|
||||
GncSqlColumnInfo* info = (GncSqlColumnInfo*) (list_node->data);
|
||||
|
||||
if (col_num != 0)
|
||||
if (col_num++ != 0)
|
||||
{
|
||||
(void)g_string_append (ddl, ", ");
|
||||
}
|
||||
append_mysql_col_def (ddl, info);
|
||||
delete info;
|
||||
}
|
||||
(void)g_string_append (ddl, ")");
|
||||
|
||||
@ -2892,13 +2870,13 @@ conn_create_table_ddl_mysql (GncSqlConnection* conn, const gchar* table_name,
|
||||
}
|
||||
|
||||
static void
|
||||
append_pgsql_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
append_pgsql_col_def (GString* ddl, const GncSqlColumnInfo& info)
|
||||
{
|
||||
const char* type_name = nullptr;
|
||||
|
||||
if (info->m_type == BCT_INT)
|
||||
if (info.m_type == BCT_INT)
|
||||
{
|
||||
if (info->m_autoinc)
|
||||
if (info.m_autoinc)
|
||||
{
|
||||
type_name = "serial";
|
||||
}
|
||||
@ -2907,44 +2885,42 @@ append_pgsql_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
type_name = "integer";
|
||||
}
|
||||
}
|
||||
else if (info->m_type == BCT_INT64)
|
||||
else if (info.m_type == BCT_INT64)
|
||||
{
|
||||
type_name = "int8";
|
||||
}
|
||||
else if (info->m_type == BCT_DOUBLE)
|
||||
else if (info.m_type == BCT_DOUBLE)
|
||||
|
||||
{
|
||||
type_name = "double precision";
|
||||
}
|
||||
else if (info->m_type == BCT_STRING)
|
||||
else if (info.m_type == BCT_STRING)
|
||||
{
|
||||
type_name = "varchar";
|
||||
}
|
||||
else if (info->m_type == BCT_DATE)
|
||||
else if (info.m_type == BCT_DATE)
|
||||
{
|
||||
info->m_size = 0;
|
||||
type_name = "date";
|
||||
}
|
||||
else if (info->m_type == BCT_DATETIME)
|
||||
else if (info.m_type == BCT_DATETIME)
|
||||
{
|
||||
info->m_size = 0;
|
||||
type_name = "timestamp without time zone";
|
||||
}
|
||||
else
|
||||
{
|
||||
PERR ("Unknown column type: %d\n", info->m_type);
|
||||
PERR ("Unknown column type: %d\n", info.m_type);
|
||||
type_name = "";
|
||||
}
|
||||
g_string_append_printf (ddl, "%s %s", info->m_name.c_str(), type_name);
|
||||
if (info->m_size != 0)
|
||||
g_string_append_printf (ddl, "%s %s", info.m_name.c_str(), type_name);
|
||||
if (info.m_size != 0 && info.m_type == BCT_STRING)
|
||||
{
|
||||
g_string_append_printf (ddl, "(%d)", info->m_size);
|
||||
g_string_append_printf (ddl, "(%d)", info.m_size);
|
||||
}
|
||||
if (info->m_primary_key)
|
||||
if (info.m_primary_key)
|
||||
{
|
||||
(void)g_string_append (ddl, " PRIMARY KEY");
|
||||
}
|
||||
if (info->m_not_null)
|
||||
if (info.m_not_null)
|
||||
{
|
||||
(void)g_string_append (ddl, " NOT NULL");
|
||||
}
|
||||
@ -2952,43 +2928,32 @@ append_pgsql_col_def (GString* ddl, GncSqlColumnInfo* info)
|
||||
|
||||
static gchar*
|
||||
conn_create_table_ddl_pgsql (GncSqlConnection* conn, const gchar* table_name,
|
||||
const GList* col_info_list)
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GString* ddl;
|
||||
const GList* list_node;
|
||||
guint col_num;
|
||||
gboolean is_unicode = FALSE;
|
||||
unsigned int col_num = 0;
|
||||
|
||||
g_return_val_if_fail (conn != NULL, NULL);
|
||||
g_return_val_if_fail (table_name != NULL, NULL);
|
||||
g_return_val_if_fail (col_info_list != NULL, NULL);
|
||||
|
||||
ddl = g_string_new ("");
|
||||
g_string_printf (ddl, "CREATE TABLE %s (", table_name);
|
||||
for (list_node = col_info_list, col_num = 0; list_node != NULL;
|
||||
list_node = list_node->next, col_num++)
|
||||
for (auto const& info : info_vec)
|
||||
{
|
||||
GncSqlColumnInfo* info = (GncSqlColumnInfo*) (list_node->data);
|
||||
|
||||
if (col_num != 0)
|
||||
if (col_num++ != 0)
|
||||
{
|
||||
(void)g_string_append (ddl, ", ");
|
||||
}
|
||||
append_pgsql_col_def (ddl, info);
|
||||
is_unicode = is_unicode || info->m_unicode;
|
||||
delete info;
|
||||
}
|
||||
(void)g_string_append (ddl, ")");
|
||||
if (is_unicode)
|
||||
{
|
||||
}
|
||||
|
||||
return g_string_free (ddl, FALSE);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
conn_create_table (GncSqlConnection* conn, const gchar* table_name,
|
||||
GList* col_info_list)
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
|
||||
gchar* ddl;
|
||||
@ -2996,12 +2961,8 @@ conn_create_table (GncSqlConnection* conn, const gchar* table_name,
|
||||
|
||||
g_return_val_if_fail (conn != NULL, FALSE);
|
||||
g_return_val_if_fail (table_name != NULL, FALSE);
|
||||
g_return_val_if_fail (col_info_list != NULL, FALSE);
|
||||
|
||||
|
||||
ddl = dbi_conn->provider->create_table_ddl (conn, table_name,
|
||||
col_info_list);
|
||||
g_list_free (col_info_list);
|
||||
ddl = dbi_conn->provider->create_table_ddl(conn, table_name, info_vec);
|
||||
if (ddl != NULL)
|
||||
{
|
||||
gint status;
|
||||
@ -3061,8 +3022,8 @@ conn_create_index (GncSqlConnection* conn, const gchar* index_name,
|
||||
}
|
||||
|
||||
static gboolean
|
||||
conn_add_columns_to_table (GncSqlConnection* conn, const gchar* table_name,
|
||||
GList* col_info_list)
|
||||
conn_add_columns_to_table(GncSqlConnection* conn, const char* table_name,
|
||||
const ColVec& info_vec)
|
||||
{
|
||||
GncDbiSqlConnection* dbi_conn = (GncDbiSqlConnection*)conn;
|
||||
gchar* ddl;
|
||||
@ -3070,26 +3031,19 @@ conn_add_columns_to_table (GncSqlConnection* conn, const gchar* table_name,
|
||||
|
||||
g_return_val_if_fail (conn != NULL, FALSE);
|
||||
g_return_val_if_fail (table_name != NULL, FALSE);
|
||||
g_return_val_if_fail (col_info_list != NULL, FALSE);
|
||||
|
||||
ddl = add_columns_ddl (conn, table_name, col_info_list);
|
||||
if (ddl != NULL)
|
||||
{
|
||||
gint status;
|
||||
|
||||
DEBUG ("SQL: %s\n", ddl);
|
||||
result = dbi_conn_query (dbi_conn->conn, ddl);
|
||||
g_free (ddl);
|
||||
status = dbi_result_free (result);
|
||||
if (status < 0)
|
||||
{
|
||||
PERR ("Error in dbi_result_free() result\n");
|
||||
qof_backend_set_error (dbi_conn->qbe, ERR_BACKEND_SERVER_ERR);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ddl = add_columns_ddl(conn, table_name, info_vec);
|
||||
if (ddl == NULL)
|
||||
return FALSE;
|
||||
|
||||
DEBUG ("SQL: %s\n", ddl);
|
||||
result = dbi_conn_query (dbi_conn->conn, ddl);
|
||||
g_free (ddl);
|
||||
int status = dbi_result_free (result);
|
||||
if (status < 0)
|
||||
{
|
||||
PERR( "Error in dbi_result_free() result\n" );
|
||||
qof_backend_set_error( dbi_conn->qbe, ERR_BACKEND_SERVER_ERR );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
|
@ -128,9 +128,9 @@ load_address (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_address_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_address_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
GncSqlColumnInfo* info;
|
||||
gchar* buf;
|
||||
@ -138,17 +138,15 @@ add_address_col_info_to_list (const GncSqlBackend* be,
|
||||
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
for (subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++)
|
||||
{
|
||||
buf = g_strdup_printf ("%s_%s", table_row->col_name, subtable_row->col_name);
|
||||
auto info = new GncSqlColumnInfo(buf, BCT_STRING, subtable_row->size,
|
||||
true, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info(buf, BCT_STRING, subtable_row->size, true, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1236,18 +1236,15 @@ load_string (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_string_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_string_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_STRING,
|
||||
table_row->size, TRUE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_STRING, table_row->size, TRUE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1346,17 +1343,15 @@ load_int (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_int_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_int_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_INT, 0, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_INT, 0, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1449,16 +1444,15 @@ load_boolean (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_boolean_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_boolean_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_INT, 0, FALSE};
|
||||
*pList = g_list_append (*pList, static_cast<void*>(info));
|
||||
GncSqlColumnInfo info{table_row, BCT_INT, 0, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1543,16 +1537,15 @@ load_int64 (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_int64_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_int64_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_INT64, 0, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_INT64, 0, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1654,17 +1647,15 @@ load_double (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_double_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_double_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_DOUBLE, 0, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_DOUBLE, 0, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1758,18 +1749,15 @@ load_guid (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_guid_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_guid_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_STRING,
|
||||
GUID_ENCODING_LENGTH, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_STRING, GUID_ENCODING_LENGTH, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -1868,11 +1856,11 @@ gnc_sql_add_gvalue_objectref_guid_to_slist (const GncSqlBackend* be,
|
||||
}
|
||||
|
||||
void
|
||||
gnc_sql_add_objectref_guid_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
gnc_sql_add_objectref_guid_col_info_to_list( const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& info_vec)
|
||||
{
|
||||
add_guid_col_info_to_list (be, table_row, pList);
|
||||
add_guid_col_info_to_list(be, table_row, info_vec);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- */
|
||||
@ -1976,18 +1964,15 @@ load_timespec (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_timespec_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_timespec_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_DATETIME,
|
||||
TIMESPEC_COL_SIZE, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_DATETIME, TIMESPEC_COL_SIZE, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -2136,15 +2121,13 @@ load_date (const GncSqlBackend* be, GncSqlRow* row,
|
||||
static void
|
||||
add_date_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
ColVec& vec)
|
||||
{
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
auto info = new GncSqlColumnInfo{table_row, BCT_DATE, DATE_COL_SIZE, FALSE};
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
GncSqlColumnInfo info{table_row, BCT_DATE, DATE_COL_SIZE, FALSE};
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -2267,26 +2250,24 @@ load_numeric (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_numeric_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_numeric_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
gchar* buf;
|
||||
const GncSqlColumnTableEntry* subtable_row;
|
||||
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
for (subtable_row = numeric_col_table; subtable_row->col_name != NULL;
|
||||
subtable_row++)
|
||||
{
|
||||
buf = g_strdup_printf ("%s_%s", table_row->col_name, subtable_row->col_name);
|
||||
auto info = new GncSqlColumnInfo(buf, BCT_INT64, 0, false, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
buf = g_strdup_printf("%s_%s", table_row->col_name, subtable_row->col_name);
|
||||
GncSqlColumnInfo info(buf, BCT_INT64, 0, false, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
vec.emplace_back(std::move(info));
|
||||
}
|
||||
}
|
||||
|
||||
@ -3098,7 +3079,7 @@ static gboolean
|
||||
do_create_table (const GncSqlBackend* be, const gchar* table_name,
|
||||
const GncSqlColumnTableEntry* col_table)
|
||||
{
|
||||
GList* col_info_list = NULL;
|
||||
ColVec info_vec;
|
||||
gboolean ok = FALSE;
|
||||
|
||||
g_return_val_if_fail (be != NULL, FALSE);
|
||||
@ -3111,10 +3092,9 @@ do_create_table (const GncSqlBackend* be, const gchar* table_name,
|
||||
|
||||
pHandler = get_handler (col_table);
|
||||
g_assert (pHandler != NULL);
|
||||
pHandler->add_col_info_to_list_fn (be, col_table, &col_info_list);
|
||||
pHandler->add_col_info_to_list_fn (be, col_table, info_vec);
|
||||
}
|
||||
g_assert (col_info_list != NULL);
|
||||
ok = gnc_sql_connection_create_table (be->conn, table_name, col_info_list);
|
||||
ok = gnc_sql_connection_create_table (be->conn, table_name, info_vec);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@ -3219,7 +3199,7 @@ gboolean gnc_sql_add_columns_to_table (GncSqlBackend* be,
|
||||
const gchar* table_name,
|
||||
const GncSqlColumnTableEntry* new_col_table)
|
||||
{
|
||||
GList* col_info_list = NULL;
|
||||
ColVec info_vec;
|
||||
gboolean ok = FALSE;
|
||||
|
||||
g_return_val_if_fail (be != NULL, FALSE);
|
||||
@ -3232,11 +3212,9 @@ gboolean gnc_sql_add_columns_to_table (GncSqlBackend* be,
|
||||
|
||||
pHandler = get_handler (new_col_table);
|
||||
g_assert (pHandler != NULL);
|
||||
pHandler->add_col_info_to_list_fn (be, new_col_table, &col_info_list);
|
||||
pHandler->add_col_info_to_list_fn (be, new_col_table, info_vec);
|
||||
}
|
||||
g_assert (col_info_list != NULL);
|
||||
ok = gnc_sql_connection_add_columns_to_table (be->conn, table_name,
|
||||
col_info_list);
|
||||
ok = gnc_sql_connection_add_columns_to_table(be->conn, table_name, info_vec);
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
@ -171,25 +171,16 @@ struct GncSqlStatement
|
||||
struct GncSqlConnection
|
||||
{
|
||||
void (*dispose) (GncSqlConnection*);
|
||||
GncSqlResult* (*executeSelectStatement) (GncSqlConnection*,
|
||||
GncSqlStatement*); /**< Returns NULL if error */
|
||||
gint (*executeNonSelectStatement) (GncSqlConnection*,
|
||||
GncSqlStatement*); /**< Returns -1 if error */
|
||||
GncSqlResult* (*executeSelectStatement) (GncSqlConnection*, GncSqlStatement*); /**< Returns NULL if error */
|
||||
gint (*executeNonSelectStatement) (GncSqlConnection*, GncSqlStatement*); /**< Returns -1 if error */
|
||||
GncSqlStatement* (*createStatementFromSql) (GncSqlConnection*, const gchar*);
|
||||
gboolean (*doesTableExist) (GncSqlConnection*,
|
||||
const gchar*); /**< Returns true if successful */
|
||||
gboolean (*beginTransaction) (
|
||||
GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*rollbackTransaction) (
|
||||
GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*commitTransaction) (
|
||||
GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*createTable) (GncSqlConnection*, const gchar*,
|
||||
GList*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*createIndex) (GncSqlConnection*, const gchar*, const gchar*,
|
||||
const GncSqlColumnTableEntry*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*addColumnsToTable) (GncSqlConnection*, const gchar* table,
|
||||
GList*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*doesTableExist) (GncSqlConnection*, const gchar*); /**< Returns true if successful */
|
||||
gboolean (*beginTransaction) (GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*rollbackTransaction) (GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*commitTransaction) (GncSqlConnection*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*createTable) (GncSqlConnection*, const gchar*, const ColVec&); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*createIndex) (GncSqlConnection*, const gchar*, const gchar*, const GncSqlColumnTableEntry*); /**< Returns TRUE if successful, FALSE if error */
|
||||
gboolean (*addColumnsToTable) (GncSqlConnection*, const gchar* table, const ColVec&); /**< Returns TRUE if successful, FALSE if error */
|
||||
gchar* (*quoteString) (const GncSqlConnection*, gchar*);
|
||||
};
|
||||
#define gnc_sql_connection_dispose(CONN) (CONN)->dispose(CONN)
|
||||
@ -391,6 +382,18 @@ struct GncSqlColumnInfo
|
||||
bool m_not_null; /**< Column forbids NULL values */
|
||||
};
|
||||
|
||||
inline bool operator==(const GncSqlColumnInfo& l,
|
||||
const GncSqlColumnInfo& r)
|
||||
{
|
||||
return l.m_name == r.m_name && l.m_type == r.m_type;
|
||||
}
|
||||
|
||||
inline bool operator!=(const GncSqlColumnInfo& l,
|
||||
const GncSqlColumnInfo& r)
|
||||
{
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
typedef enum
|
||||
{
|
||||
OP_DB_INSERT,
|
||||
@ -403,14 +406,13 @@ typedef void (*GNC_SQL_LOAD_FN) (const GncSqlBackend* be,
|
||||
QofSetterFunc setter, gpointer pObject,
|
||||
const GncSqlColumnTableEntry* table);
|
||||
typedef void (*GNC_SQL_ADD_COL_INFO_TO_LIST_FN) (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList);
|
||||
typedef void (*GNC_SQL_ADD_COLNAME_TO_LIST_FN) (const GncSqlColumnTableEntry*
|
||||
table_row, GList** pList);
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec);
|
||||
typedef void (*GNC_SQL_ADD_COLNAME_TO_LIST_FN) (const GncSqlColumnTableEntry* table_row, GList** pList);
|
||||
typedef void (*GNC_SQL_ADD_GVALUE_TO_SLIST_FN) (const GncSqlBackend* be,
|
||||
QofIdTypeConst obj_name,
|
||||
const gpointer pObject,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GSList** pList);
|
||||
|
||||
/**
|
||||
@ -673,7 +675,7 @@ void gnc_sql_add_gvalue_objectref_guid_to_slist (const GncSqlBackend* be,
|
||||
*/
|
||||
void gnc_sql_add_objectref_guid_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList);
|
||||
ColVec& vec);
|
||||
|
||||
/**
|
||||
* Appends the ascii strings for a list of GUIDs to the end of an SQL string.
|
||||
|
@ -169,30 +169,27 @@ load_owner (const GncSqlBackend* be, GncSqlRow* row,
|
||||
}
|
||||
|
||||
static void
|
||||
add_owner_col_info_to_list (const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
GList** pList)
|
||||
add_owner_col_info_to_list(const GncSqlBackend* be,
|
||||
const GncSqlColumnTableEntry* table_row,
|
||||
ColVec& vec)
|
||||
{
|
||||
gchar* buf;
|
||||
|
||||
g_return_if_fail (be != NULL);
|
||||
g_return_if_fail (table_row != NULL);
|
||||
g_return_if_fail (pList != NULL);
|
||||
|
||||
buf = g_strdup_printf ("%s_type", table_row->col_name);
|
||||
auto info = new GncSqlColumnInfo(buf, BCT_INT, 0, false, false,
|
||||
GncSqlColumnInfo info(buf, BCT_INT, 0, false, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
vec.emplace_back(std::move(info));
|
||||
|
||||
buf = g_strdup_printf ("%s_guid", table_row->col_name);
|
||||
info = new GncSqlColumnInfo(buf, BCT_STRING, GUID_ENCODING_LENGTH,
|
||||
GncSqlColumnInfo info2(buf, BCT_STRING, GUID_ENCODING_LENGTH,
|
||||
false, false,
|
||||
table_row->flags & COL_PKEY,
|
||||
table_row->flags & COL_NNUL);
|
||||
|
||||
*pList = g_list_append (*pList, info);
|
||||
vec.emplace_back(std::move(info2));
|
||||
}
|
||||
|
||||
static void
|
||||
|
Loading…
Reference in New Issue
Block a user