This repository was archived by the owner on Feb 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathbackend_odbc.c
More file actions
369 lines (319 loc) · 12.5 KB
/
Copy pathbackend_odbc.c
File metadata and controls
369 lines (319 loc) · 12.5 KB
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
/**
* @file
*
* @brief Source file for the ODBC backend plugin
*
* This file contains the functions that are called by the Elektra core when it uses plugins
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "./backend_odbc.h"
#include "./backend_odbc_general.h"
#include "./backend_odbc_get.h"
#include "./backend_odbc_set.h"
#include <kdbassert.h>
#include <kdbdiff.h>
#include <kdberrors.h>
#include <kdblogger.h>
int ELEKTRA_PLUGIN_FUNCTION (open) (Plugin * plugin ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int ELEKTRA_PLUGIN_FUNCTION (init) (Plugin * plugin, KeySet * ksDefinition, Key * parentKey)
{
if (!ksDefinition)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey, "Got NULL for the 'ksDefinition' argument for the mountpoint at %s",
keyName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
else if (!plugin)
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey, "Got NULL for the 'plugin' argument for the mountpoint at %s",
keyName (parentKey));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
struct odbcSharedData * sharedData = elektraCalloc (sizeof (struct odbcSharedData));
if (!sharedData)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
/* Check if the mountpoint definition CAN be valid */
struct dataSourceConfig * dsConfig = fillDsStructFromDefinitionKs (ksDefinition, parentKey);
if (!dsConfig)
{
/* The fillDsStructFromDefinitionKs () function should've set the error on the parentKey */
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
sharedData->dsConfig = dsConfig;
elektraPluginSetData (plugin, sharedData);
/* init as read-write backend */
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
static int setOdbcStorageUnitId (const struct dataSourceConfig * dsConfig, Key * parentKey)
{
ssize_t ret = keySetString (parentKey, dsConfigToString (dsConfig));
ELEKTRA_ASSERT (ret != 0,
"keySetString returned 0. This looks like a bug! Please report the issue at https://issues.libelektra.org");
if (ret == 1 || ret == -1)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
else
{
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
}
int ELEKTRA_PLUGIN_FUNCTION (get) (Plugin * plugin, KeySet * ksReturned, Key * parentKey)
{
if (elektraStrCmp (keyName (parentKey), "system:/elektra/modules/backend_odbc") == 0)
{
KeySet * contract = ksNew (
30,
keyNew ("system:/elektra/modules/backend_odbc", KEY_VALUE, "backend_odbc plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports", KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/open", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (open), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/init", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (init), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/get", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (get), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/set", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (set), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/commit", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (commit), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/error", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (error), KEY_END),
keyNew ("system:/elektra/modules/backend_odbc/exports/close", KEY_FUNC, ELEKTRA_PLUGIN_FUNCTION (close), KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/backend_odbc/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (ksReturned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
/* Gets filled by the init-function of this plugin (see above) and is used in later phases (esp. storage-phase) */
struct odbcSharedData * sharedData = elektraPluginGetData (plugin);
if (!sharedData || !(sharedData->dsConfig))
{
ELEKTRA_SET_INTERNAL_ERROR (
parentKey,
"Internal plugin data for the ODBC backend was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_GET_PHASE_RESOLVER:
/* There is no standardized way in ODBC to get the time of the last modification --> always query the data
However, feel free to implement checks for specific DBMSs. */
return setOdbcStorageUnitId (sharedData->dsConfig, parentKey);
case ELEKTRA_KDB_GET_PHASE_CACHECHECK:
/* TODO: implement cache */
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_GET_PHASE_PRE_STORAGE:
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_GET_PHASE_STORAGE: {
KeySet * ksFromDs = getKeysFromDataSource (sharedData, false, parentKey);
if (!ksFromDs)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
else
{
ksAppend (ksReturned, ksFromDs);
ksDel (ksFromDs);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
}
case ELEKTRA_KDB_GET_PHASE_POST_STORAGE:
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
default:
ELEKTRA_SET_INTERNAL_ERRORF (parentKey,
"An unknown get phase (not resolver, cachecheck, prestorage, storage or "
"poststorage) was encountered.\nThe encountered phase has an integer representation of '%d'\n"
"This looks like a bug! Please report this issues at https://issues.libelektra.org",
phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
int ELEKTRA_PLUGIN_FUNCTION (set) (Plugin * plugin, KeySet * ks, Key * parentKey)
{
/* Gets filled by the init-function of this plugin (see above) and is used in later phases (esp. storage-phase) */
struct odbcSharedData * sharedData = elektraPluginGetData (plugin);
if (!sharedData || !(sharedData->dsConfig))
{
ELEKTRA_SET_INTERNAL_ERROR (
parentKey,
"Internal plugin data for the ODBC backend was NULL. Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_RESOLVER: {
/* Check whether the data was changed since the last get operation */
int ret = setOdbcStorageUnitId (sharedData->dsConfig, parentKey);
if (ret == ELEKTRA_PLUGIN_STATUS_ERROR)
{
ksAppendKey (elektraPluginGetGlobalKeySet (plugin),
keyNew ("system:/elektra/kdb/backend/failedphase", KEY_BINARY, KEY_SIZE, sizeof (ElektraKdbPhase),
KEY_VALUE, &phase, KEY_END));
}
return ret;
}
case ELEKTRA_KDB_SET_PHASE_PRE_STORAGE:
/* This phase is currently not used (can be used for validation) */
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_SET_PHASE_STORAGE: {
/* Write the actual data to the ODBC data source */
long ret = storeKeysInDataSource (sharedData, ks, parentKey);
if (ret < 0)
{
ksAppendKey (elektraPluginGetGlobalKeySet (plugin),
keyNew ("system:/elektra/kdb/backend/failedphase", KEY_BINARY, KEY_SIZE, sizeof (ElektraKdbPhase),
KEY_VALUE, &phase, KEY_END));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
else if (ret == 0)
{
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
else
{
ELEKTRA_LOG ("The storage phase affected %ld rows.\n", ret);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
}
case ELEKTRA_KDB_SET_PHASE_POST_STORAGE:
/* Not used here (mainly intended for logging and symmetry to the get-phases) */
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
default:
ELEKTRA_SET_INTERNAL_ERRORF (parentKey,
"An unknown set phase (not resolver, prestorage, storage or poststorage) was "
"encountered.\nThe encountered phase has an integer representation of '%d'\n"
"This looks like a bug! Please report this issues at https://issues.libelektra.org",
phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
int ELEKTRA_PLUGIN_FUNCTION (commit) (Plugin * plugin, KeySet * ks ELEKTRA_UNUSED, Key * parentKey)
{
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_PRE_COMMIT:
/* Not used by the ODBC backend plugin */
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_SET_PHASE_COMMIT: {
ELEKTRA_LOG_DEBUG ("in commit phase\n");
struct odbcSharedData * sharedData = elektraPluginGetData (plugin);
if (!sharedData || !(sharedData->connection) || !(sharedData->environment))
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Could not commit transaction! Internal plugin data for the ODBC backend was NULL. "
"Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (endTransaction (sharedData->connection, true, parentKey))
{
ELEKTRA_LOG_DEBUG ("Commit succeeded\n");
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
else
{
ELEKTRA_LOG_DEBUG ("Commit failed!\n");
/* Connection handle was freed by endTransaction() */
sharedData->connection = NULL;
ksAppendKey (elektraPluginGetGlobalKeySet (plugin),
keyNew ("system:/elektra/kdb/backend/failedphase", KEY_BINARY, KEY_SIZE, sizeof (ElektraKdbPhase),
KEY_VALUE, &phase, KEY_END));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
case ELEKTRA_KDB_SET_PHASE_POST_COMMIT:
return freeSharedHandles (plugin, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (parentKey,
"An unknown commit phase (not precommit, commit or postcommit) was encountered.\n"
"The encountered phase has an integer representation of '%d'\n"
"This looks like a bug! Please report this issues at https://issues.libelektra.org",
phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
int ELEKTRA_PLUGIN_FUNCTION (error) (Plugin * plugin, KeySet * ks ELEKTRA_UNUSED, Key * parentKey)
{
ElektraKdbPhase phase = elektraPluginGetPhase (plugin);
switch (phase)
{
case ELEKTRA_KDB_SET_PHASE_PRE_ROLLBACK:
/* Phase is used by the ODBC backend plugin */
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
case ELEKTRA_KDB_SET_PHASE_ROLLBACK: {
struct odbcSharedData * sharedData = elektraPluginGetData (plugin);
if (!sharedData || !(sharedData->connection) || !(sharedData->environment))
{
ELEKTRA_SET_INTERNAL_ERROR (parentKey,
"Could not rollback transaction! Internal plugin data for the ODBC backend was NULL. "
"Please report this bug at https://issues.libelektra.org.");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
KeySet * globalKs = elektraPluginGetGlobalKeySet (plugin);
Key * keyFailedPhase = ksLookupByName (globalKs, "system:/elektra/kdb/backend/failedphase", KDB_O_POP);
if (!keyFailedPhase)
{
ELEKTRA_ADD_INTERNAL_WARNING (parentKey,
"The key 'system:/elektra/kdb/backend/failedphase' which should store the name of "
"the failed phase, was not found in global KeySet.");
}
#ifdef HAVE_LOGGER
else
{
ELEKTRA_LOG_NOTICE (
"The phase '%s' failed and lead to the execution of the rollback-procedure during kdbSet().\n"
"The changes will not be stored on in the data source!",
elektraPluginPhaseName (*((ElektraKdbPhase *) keyValue (keyFailedPhase))));
keyDel (keyFailedPhase);
}
#endif
bool ret = endTransaction (sharedData->connection, false, parentKey);
if (ret)
{
ELEKTRA_LOG_DEBUG ("ROLLBACK succeeded!\n");
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
else
{
sharedData->connection = NULL;
ELEKTRA_LOG_DEBUG ("ROLLBACK failed!\n");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
case ELEKTRA_KDB_SET_PHASE_POST_ROLLBACK:
return freeSharedHandles (plugin, parentKey);
default:
ELEKTRA_SET_INTERNAL_ERRORF (parentKey,
"An unknown rollback phase (not prerollback, rollback or postrollback) was"
"encountered.\nThe encountered phase has an integer representation of '%d'\n"
"This looks like a bug! Please report this issues at https://issues.libelektra.org",
phase);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
int ELEKTRA_PLUGIN_FUNCTION (close) (Plugin * plugin ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
struct odbcSharedData * sharedData = elektraPluginGetData (plugin);
clearOdbcSharedData (sharedData, true, true);
elektraFree (sharedData);
elektraPluginSetData (plugin, NULL);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("backend_odbc",
ELEKTRA_PLUGIN_OPEN, &ELEKTRA_PLUGIN_FUNCTION (open),
ELEKTRA_PLUGIN_INIT, &ELEKTRA_PLUGIN_FUNCTION (init),
ELEKTRA_PLUGIN_GET, &ELEKTRA_PLUGIN_FUNCTION (get),
ELEKTRA_PLUGIN_SET, &ELEKTRA_PLUGIN_FUNCTION (set),
ELEKTRA_PLUGIN_COMMIT, &ELEKTRA_PLUGIN_FUNCTION (commit),
ELEKTRA_PLUGIN_ERROR, &ELEKTRA_PLUGIN_FUNCTION (error),
ELEKTRA_PLUGIN_CLOSE, &ELEKTRA_PLUGIN_FUNCTION (close),
ELEKTRA_PLUGIN_END);
// clang-format on
}