-
Notifications
You must be signed in to change notification settings - Fork 760
/
gnc-dbiproviderimpl.hpp
371 lines (347 loc) · 11.1 KB
/
gnc-dbiproviderimpl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/************************************************************************
* gnc-dbiproviderimpl.hpp: Encapsulate differences among Dbi backends. *
* *
* Copyright 2016 John Ralls <jralls@ceridwen.us> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\***********************************************************************/
#ifndef __GNC_DBISQLPROVIDERIMPL_HPP__
#define __GNC_DBISQLPROVIDERIMPL_HPP__
#include <guid.hpp>
extern "C"
{
#include <config.h>
}
#include "gnc-backend-dbi.hpp"
#include "gnc-dbiprovider.hpp"
template <DbType T>
class GncDbiProviderImpl : public GncDbiProvider
{
public:
StrVec get_table_list(dbi_conn conn, const std::string& dbname);
void append_col_def(std::string& ddl, const GncSqlColumnInfo& info);
StrVec get_index_list (dbi_conn conn);
void drop_index(dbi_conn conn, const std::string& index);
};
template <DbType T> GncDbiProviderPtr
make_dbi_provider()
{
return GncDbiProviderPtr(new GncDbiProviderImpl<T>);
}
template<> void
GncDbiProviderImpl<DbType::DBI_SQLITE>::append_col_def(std::string& ddl,
const GncSqlColumnInfo& info)
{
const char* type_name = nullptr;
if (info.m_type == BCT_INT)
{
type_name = "integer";
}
else if (info.m_type == BCT_INT64)
{
type_name = "bigint";
}
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)
{
type_name = "text";
}
else
{
PERR ("Unknown column type: %d\n", info.m_type);
type_name = "";
}
ddl += (info.m_name + " " + type_name);
if (info.m_size != 0)
{
ddl += "(" + std::to_string(info.m_size) + ")";
}
if (info.m_primary_key)
{
ddl += " PRIMARY KEY";
}
if (info.m_autoinc)
{
ddl += " AUTOINCREMENT";
}
if (info.m_not_null)
{
ddl += " NOT NULL";
}
}
template<> void
GncDbiProviderImpl<DbType::DBI_MYSQL>::append_col_def (std::string& ddl,
const GncSqlColumnInfo& info)
{
const char* type_name = nullptr;
if (info.m_type == BCT_INT)
{
type_name = "integer";
}
else if (info.m_type == BCT_INT64)
{
type_name = "bigint";
}
else if (info.m_type == BCT_DOUBLE)
{
type_name = "double";
}
else if (info.m_type == BCT_STRING)
{
type_name = "varchar";
}
else if (info.m_type == BCT_DATE)
{
type_name = "date";
}
else if (info.m_type == BCT_DATETIME)
{
type_name = "TIMESTAMP NULL DEFAULT 0";
}
else
{
PERR ("Unknown column type: %d\n", info.m_type);
type_name = "";
}
ddl += info.m_name + " " + type_name;
if (info.m_size != 0 && info.m_type == BCT_STRING)
{
ddl += "(" + std::to_string(info.m_size) + ")";
}
if (info.m_unicode)
{
ddl += " CHARACTER SET utf8";
}
if (info.m_primary_key)
{
ddl += " PRIMARY KEY";
}
if (info.m_autoinc)
{
ddl += " AUTO_INCREMENT";
}
if (info.m_not_null)
{
ddl += " NOT NULL";
}
}
template<> void
GncDbiProviderImpl<DbType::DBI_PGSQL>::append_col_def (std::string& ddl,
const GncSqlColumnInfo& info)
{
const char* type_name = nullptr;
if (info.m_type == BCT_INT)
{
if (info.m_autoinc)
{
type_name = "serial";
}
else
{
type_name = "integer";
}
}
else if (info.m_type == BCT_INT64)
{
type_name = "int8";
}
else if (info.m_type == BCT_DOUBLE)
{
type_name = "double precision";
}
else if (info.m_type == BCT_STRING)
{
type_name = "varchar";
}
else if (info.m_type == BCT_DATE)
{
type_name = "date";
}
else if (info.m_type == BCT_DATETIME)
{
type_name = "timestamp without time zone";
}
else
{
PERR ("Unknown column type: %d\n", info.m_type);
type_name = "";
}
ddl += info.m_name + " " + type_name;
if (info.m_size != 0 && info.m_type == BCT_STRING)
{
ddl += "(" + std::to_string(info.m_size) + ")";
}
if (info.m_primary_key)
{
ddl += " PRIMARY KEY";
}
if (info.m_not_null)
{
ddl += " NOT NULL";
}
}
static StrVec
conn_get_table_list (dbi_conn conn, const std::string& dbname)
{
StrVec retval;
auto tables = dbi_conn_get_table_list (conn, dbname.c_str(), nullptr);
while (dbi_result_next_row (tables) != 0)
{
std::string table_name {dbi_result_get_string_idx (tables, 1)};
retval.push_back(table_name);
}
dbi_result_free (tables);
return retval;
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_SQLITE>::get_table_list (dbi_conn conn,
const std::string& dbname)
{
/* Return the list, but remove the tables that sqlite3 adds for
* its own use. */
auto list = conn_get_table_list (conn, dbname);
auto end = std::remove(list.begin(), list.end(), "sqlite_sequence");
list.erase(end, list.end());
return list;
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_MYSQL>::get_table_list (dbi_conn conn,
const std::string& dbname)
{
return conn_get_table_list (conn, dbname);
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_PGSQL>::get_table_list (dbi_conn conn,
const std::string& dbname)
{
auto list = conn_get_table_list (conn, dbname);
auto end = std::remove_if (list.begin(), list.end(),
[](std::string& table_name){
return table_name == "sql_features" ||
table_name == "sql_implementation_info" ||
table_name == "sql_languages" ||
table_name == "sql_packages" ||
table_name == "sql_parts" ||
table_name == "sql_sizing" ||
table_name == "sql_sizing_profiles";
});
list.erase(end, list.end());
return list;
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_SQLITE>::get_index_list (dbi_conn conn)
{
StrVec retval;
const char* errmsg;
dbi_result result = dbi_conn_query (conn,
"SELECT name FROM sqlite_master WHERE type = 'index' AND name NOT LIKE 'sqlite_autoindex%'");
if (dbi_conn_error (conn, &errmsg) != DBI_ERROR_NONE)
{
PWARN ("Index Table Retrieval Error: %s\n", errmsg);
return retval;
}
while (dbi_result_next_row (result) != 0)
{
std::string index_name {dbi_result_get_string_idx (result, 1)};
retval.push_back(index_name);
}
dbi_result_free (result);
return retval;
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_MYSQL>::get_index_list (dbi_conn conn)
{
StrVec retval;
const char* errmsg;
auto dbname = dbi_conn_get_option (conn, "dbname");
auto table_list = dbi_conn_get_table_list (conn, dbname, nullptr);
if (dbi_conn_error (conn, &errmsg) != DBI_ERROR_NONE)
{
PWARN ("Table Retrieval Error: %s\n", errmsg);
return retval;
}
while (dbi_result_next_row (table_list) != 0)
{
auto table_name = dbi_result_get_string_idx (table_list, 1);
auto result = dbi_conn_queryf (conn,
"SHOW INDEXES IN %s WHERE Key_name != 'PRIMARY'",
table_name);
if (dbi_conn_error (conn, &errmsg) != DBI_ERROR_NONE)
{
PWARN ("Index Table Retrieval Error: %s on table %s\n",
errmsg, table_name);
continue;
}
while (dbi_result_next_row (result) != 0)
{
std::string index_name {dbi_result_get_string_idx (result, 3)};
retval.push_back(index_name + " " + table_name);
}
dbi_result_free (result);
}
return retval;
}
template<> StrVec
GncDbiProviderImpl<DbType::DBI_PGSQL>::get_index_list (dbi_conn conn)
{
StrVec retval;
const char* errmsg;
PINFO ("Retrieving postgres index list\n");
auto result = dbi_conn_query (conn,
"SELECT relname FROM pg_class AS a INNER JOIN pg_index AS b ON (b.indexrelid = a.oid) INNER JOIN pg_namespace AS c ON (a.relnamespace = c.oid) WHERE reltype = '0' AND indisprimary = 'f' AND nspname = 'public'");
if (dbi_conn_error (conn, &errmsg) != DBI_ERROR_NONE)
{
PWARN("Index Table Retrieval Error: %s\n", errmsg);
return retval;
}
while (dbi_result_next_row (result) != 0)
{
std::string index_name {dbi_result_get_string_idx (result, 1)};
retval.push_back(index_name);
}
dbi_result_free (result);
return retval;
}
template <DbType P> void
GncDbiProviderImpl<P>::drop_index(dbi_conn conn, const std::string& index)
{
dbi_result result = dbi_conn_queryf (conn, "DROP INDEX %s", index.c_str());
if (result)
dbi_result_free (result);
}
template<> void
GncDbiProviderImpl<DbType::DBI_MYSQL>::drop_index (dbi_conn conn, const std::string& index)
{
auto sep = index.find(' ', 0);
if (index.find(' ', sep + 1) != std::string::npos)
{
PWARN("Drop index error: invalid MySQL index format (<index> <table>): %s",
index.c_str());
return;
}
auto result = dbi_conn_queryf (conn, "DROP INDEX %s ON %s",
index.substr(0, sep).c_str(),
index.substr(sep + 1).c_str());
if (result)
dbi_result_free (result);
}
#endif //__GNC_DBISQLPROVIDERIMPL_HPP__