Branch data Line data Source code
1 : : /***********************************************************************\
2 : : * gnc-sql-object-backend.hpp: Encapsulate per-class table schema. *
3 : : * *
4 : : * Copyright 2016 John Ralls <jralls@ceridwen.us> *
5 : : * *
6 : : * This program is free software; you can redistribute it and/or *
7 : : * modify it under the terms of the GNU General Public License as *
8 : : * published by the Free Software Foundation; either version 2 of *
9 : : * the License, or (at your option) any later version. *
10 : : * *
11 : : * This program is distributed in the hope that it will be useful, *
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : : * GNU General Public License for more details. *
15 : : * *
16 : : * You should have received a copy of the GNU General Public License *
17 : : * along with this program; if not, contact: *
18 : : * *
19 : : * Free Software Foundation Voice: +1-617-542-5942 *
20 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : : \***********************************************************************/
23 : :
24 : : #include <config.h>
25 : : #include "gnc-sql-object-backend.hpp"
26 : : #include "gnc-sql-backend.hpp"
27 : : #include "gnc-sql-column-table-entry.hpp"
28 : : #include "gnc-slots-sql.h"
29 : :
30 : : static QofLogModule log_module = G_LOG_DOMAIN;
31 : :
32 : : bool
33 : 14 : GncSqlObjectBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
34 : : {
35 : : const GncGUID* guid;
36 : : gboolean is_infant;
37 : : E_DB_OPERATION op;
38 : : gboolean is_ok;
39 : :
40 : 14 : is_infant = qof_instance_get_infant (inst);
41 : 14 : if (qof_instance_get_destroying (inst))
42 : : {
43 : 0 : op = OP_DB_DELETE;
44 : : }
45 : 14 : else if (sql_be->pristine() || is_infant)
46 : : {
47 : 10 : op = OP_DB_INSERT;
48 : : }
49 : : else
50 : : {
51 : 4 : op = OP_DB_UPDATE;
52 : : }
53 : 14 : is_ok = sql_be->do_db_operation(op, m_table_name.c_str(),
54 : : m_type_name.c_str(), inst, m_col_table);
55 : :
56 : 14 : if (is_ok)
57 : : {
58 : : // Now, commit any slots
59 : 14 : guid = qof_instance_get_guid (inst);
60 : 14 : if (!qof_instance_get_destroying (inst))
61 : : {
62 : 14 : is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
63 : : }
64 : : else
65 : : {
66 : 0 : is_ok = gnc_sql_slots_delete (sql_be, guid);
67 : : }
68 : : }
69 : :
70 : 14 : return is_ok;
71 : : }
72 : :
73 : : void
74 : 70 : GncSqlObjectBackend::create_tables (GncSqlBackend* sql_be)
75 : : {
76 : 70 : g_return_if_fail (sql_be != nullptr);
77 : 70 : int version = sql_be->get_table_version (m_table_name);
78 : 70 : if (version == 0) //No tables, otherwise version will sql_be >= 1.
79 : : {
80 : 35 : sql_be->create_table(m_table_name, m_col_table);
81 : 35 : sql_be->set_table_version(m_table_name, m_version);
82 : : }
83 : 35 : else if (version != m_version)
84 : 0 : PERR("Version mismatch in table %s, expecting %d but backend is %d."
85 : : "Table creation aborted.", m_table_name.c_str(), m_version, version);
86 : : }
87 : :
88 : : bool
89 : 45 : GncSqlObjectBackend::instance_in_db(const GncSqlBackend* sql_be,
90 : : QofInstance* inst) const noexcept
91 : : {
92 : 45 : return sql_be->object_in_db(m_table_name.c_str(), m_type_name.c_str(),
93 : 45 : inst, m_col_table);
94 : : }
|