forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_odbc_set.c
1285 lines (1104 loc) · 41.2 KB
/
backend_odbc_set.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* @brief Functions for writing data to an ODBC data source.
* INSERT-, UPDATE and DELETE operations are supported.
*
* This file contains all functions that are especially needed for setting data on a data source.
* This includes building strings for INSERT INTO-, UPDATE- and DELETE-queries, preparing and executing queries and sending data.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "./backend_odbc_set.h"
#include "./backend_odbc_general.h"
#include "./backend_odbc_get.h"
#include <kdbassert.h>
#include <kdbdiff.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdblogger.h>
#include <string.h>
/* ODBC related includes */
#include <sqlext.h>
/* Value for SQLite, adjust for your ODBC driver/DBMS */
#define ODBC_SQL_MAX_PARAMS 32766
/**
* @internal
*
* @brief Get an SQL query for deleting metadata for ONE specific key and some metakeys
* If you want to delete ALL metakeys for one or more key-names, it's recommended to use the getDeleteQuery() function instead
*
* @param numMetaKeys The number of metakeys you want to remove (needed for number of parameters in IN(..) clause).
* @param dsConfig The data source configuration (contains mountpoint definition).
* @param quoteStr The char(s) that should be used for quoting identifiers.
* The identifiers in @dsConfig must not contain @p quoteString as a substring.
* @param[out] errorKey Used to store errors and warnings.
*
* @return The string with the query incl. parameter markers '?'. This query must be executed with a statement that has correctly bound
* parameters. Make sure to free the returned string.
* @retval NULL if an error occurred.
*/
static char * getDeleteMetaDataQuery (unsigned int numMetaKeys, const struct dataSourceConfig * dsConfig, const char * quoteStr,
Key * errorKey)
{
if (!dsConfig)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "The provided dataSourceConfig struct must not be NULL.");
return NULL;
}
if (!quoteStr || !(*quoteStr))
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL or empty strings are not supported for the quote string.");
return NULL;
}
if (numMetaKeys == 0)
{
ELEKTRA_ADD_INTERFACE_WARNING (errorKey,
"The provided number of metakeys to delete was 0. "
"Therefore, no query was created.");
return NULL;
}
/* Check if any of the used identifiers contains the quote string */
if (strstr (dsConfig->metaTableName, quoteStr))
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey, "The meta table name contained the quote string '%s', this is not allowed!",
quoteStr);
return NULL;
}
if (strstr (dsConfig->metaTableKeyColName, quoteStr))
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey,
"The specified name of the key column in the metatable contained the quote string "
"'%s', this is not allowed!",
quoteStr);
return NULL;
}
if (strstr (dsConfig->metaTableMetaKeyColName, quoteStr))
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey,
"The specified name of the metakey column in the metatable contained the quote "
"string '%s', this is not allowed!",
quoteStr);
return NULL;
}
/* Example: DELETE FROM metaTable WHERE keyName=myKey AND metaKeyName IN (mk1, mk2, mk3) */
size_t queryStrLen = strlen ("DELETE FROM ") + strlen (quoteStr) + strlen (dsConfig->metaTableName) + strlen (quoteStr) +
strlen (" WHERE ") + strlen (quoteStr) + strlen (dsConfig->metaTableKeyColName) + strlen (quoteStr) +
strlen ("=? AND ") + strlen (quoteStr) + strlen (dsConfig->metaTableMetaKeyColName) + strlen (quoteStr) +
strlen (" IN ()");
/* We need one comma less then number of metakeys to delete, so we already have to char for the \0 */
queryStrLen += strlen ("?,") * numMetaKeys;
char * queryStr = elektraMalloc (queryStrLen * sizeof (char));
if (!queryStr)
{
return NULL;
}
char * strEnd = stpcpy (queryStr, "DELETE FROM ");
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, dsConfig->metaTableName);
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, " WHERE ");
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, dsConfig->metaTableKeyColName);
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, "=? AND ");
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, dsConfig->metaTableMetaKeyColName);
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, " IN (");
for (unsigned int u = 0; u < numMetaKeys; u++)
{
if (u < (numMetaKeys - 1))
{
strEnd = stpcpy (strEnd, "?,");
}
else
{
/* last iteration */
stpcpy (strEnd, "?)");
}
}
return queryStr;
}
/**
* @internal
*
* @brief Get an SQL query for deleting one or multiple keys from the key-table or ALL metadata for one or multiple keys from the metatable.
*
* @param numKeys The number of keys which you want to delete.
* @param tableName The name of the table (key-table or metatable) from which you want to delete rows.
* @param keyColName The name of the column that stores the key-name.
* @param quoteStr The char(s) that should be used for quoting identifiers.
* The identifiers in @dsConfig must not contain @p quoteString as a substring.
* @param[out] errorKey Used to store errors and warnings.
*
* @return The string with the query incl. parameter markers '?'. This query must be executed with a statement
* that has correctly bound parameters.
* Make sure to free the returned string.
*/
static char * getDeleteQuery (unsigned int numKeys, const char * tableName, const char * keyColName, const char * quoteStr, Key * errorKey)
{
if (numKeys == 0 || !tableName || !(*tableName) || !keyColName || !(*keyColName))
{
return NULL;
}
if (!quoteStr || !(*quoteStr))
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL or empty strings are not supported for the quote string.");
return NULL;
}
/* Check if any of the used identifiers contains the quote string */
if (strstr (tableName, quoteStr))
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey, "The table name contained the quote string '%s', this is not allowed!", quoteStr);
return NULL;
}
if (strstr (keyColName, quoteStr))
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey,
"The specified name of the key column in the table contained the quote string "
"'%s', this is not allowed!",
quoteStr);
return NULL;
}
/* Example: DELETE from myTable WHERE keyName IN (key1, key2, key3) */
size_t queryStrLen = strlen ("DELETE FROM ") + strlen (quoteStr) + strlen (tableName) + strlen (quoteStr) + strlen (" WHERE ") +
strlen (quoteStr) + strlen (keyColName) + strlen (quoteStr) + strlen (" IN ()");
/* One comma less than number of keys in the keyset is required, so we've already counted the char for \0 */
queryStrLen += strlen ("?,") * numKeys;
char * queryStr = elektraMalloc (queryStrLen * sizeof (char));
if (!queryStr)
{
return NULL;
}
char * strEnd = stpcpy (queryStr, "DELETE FROM ");
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, tableName);
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, " WHERE ");
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, keyColName);
strEnd = stpcpy (strEnd, quoteStr);
strEnd = stpcpy (strEnd, " IN (");
for (unsigned int u = 0; u < numKeys; u++)
{
if (u < (numKeys - 1))
{
strEnd = stpcpy (strEnd, "?,");
}
else
{
/* last iteration */
stpcpy (strEnd, "?)");
}
}
return queryStr;
}
/**
* @internal
*
* @brief Get an SQL query for updating an existing row (specified by key-name) with a new value.
*
* @param dsConfig The data source configuration (contains mountpoint definition).
* @param forMetaTable Should a query for updating rows in the key-table or in the metatable be created?
* @param quoteStr The char(s) that should be used for quoting identifiers.
* The identifiers in @dsConfig must not contain @p quoteString as a substring.
* @param[out] errorKey Used to store errors and warnings.
*
* @return The string with the query incl. parameter markers '?'. This query must be executed with a correctly prepared statement.
* Make sure to free the returned string.
*/
static char * getUpdateQuery (const struct dataSourceConfig * dsConfig, bool forMetaTable, const char * quoteStr, Key * errorKey)
{
if (!quoteStr || !(*quoteStr))
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "NULL or empty strings are not supported for the quote string.");
return NULL;
}
/* Check if any identifier contains the quote string */
if (checkIdentifiersForSubString (dsConfig, quoteStr, errorKey))
{
/* A concrete error message should have been set to errorKey */
return NULL;
}
if (forMetaTable)
{
return elektraFormat ("UPDATE %s%s%s SET %s%s%s=? WHERE %s%s%s=? AND %s%s%s=?", quoteStr, dsConfig->metaTableName, quoteStr,
quoteStr, dsConfig->metaTableMetaValColName, quoteStr, quoteStr, dsConfig->metaTableKeyColName,
quoteStr, quoteStr, dsConfig->metaTableMetaKeyColName, quoteStr);
}
else
{
return elektraFormat ("UPDATE %s%s%s SET %s%s%s=? WHERE %s%s%s=?", quoteStr, dsConfig->tableName, quoteStr, quoteStr,
dsConfig->valColName, quoteStr, quoteStr, dsConfig->keyColName, quoteStr);
}
}
/**
* @internal
*
* @brief Get an SQL query for inserting a new row into the key-table or metatable.
*
* @param dsConfig The data source configuration (contains mountpoint definition).
* @param forMetaTable Should a query for inserting rows in the key-table or in the metatable be created?
* @param quoteStr The char(s) that should be used for quoting identifiers.
* The identifiers in @dsConfig must not contain @p quoteString as a substring.
* @param[out] errorKey Used to store errors and warnings.
*
* @return The string with the query incl. parameter markers '?'. This query must be executed with a statement that has correctly bound
* parameters. Make sure to free the returned string.
*/
static char * getInsertQuery (const struct dataSourceConfig * dsConfig, bool forMetaTable, const char * quoteStr, Key * errorKey)
{
/* Check if any identifier contains the quote string */
if (checkIdentifiersForSubString (dsConfig, quoteStr, errorKey))
{
/* A concrete error message should have been set to errorKey */
return NULL;
}
if (forMetaTable)
{
return elektraFormat ("INSERT INTO %s%s%s (%s%s%s, %s%s%s, %s%s%s) VALUES (?,?,?)", quoteStr, dsConfig->metaTableName,
quoteStr, quoteStr, dsConfig->metaTableKeyColName, quoteStr, quoteStr,
dsConfig->metaTableMetaKeyColName, quoteStr, quoteStr, dsConfig->metaTableMetaValColName, quoteStr);
}
else
{
/* Set value first to have the same parameter order as for the UPDATE query */
return elektraFormat ("INSERT INTO %s%s%s (%s%s%s, %s%s%s) VALUES (?,?)", quoteStr, dsConfig->tableName, quoteStr, quoteStr,
dsConfig->valColName, quoteStr, quoteStr, dsConfig->keyColName, quoteStr);
}
}
/**
* @internal
*
* @brief Like bindStringsToStatement, just with a va_list argument instead of vararg ('...')
*
* @see bindStringsToStatement
*/
static bool bindStringsToStatementV (SQLHSTMT sqlStmt, ssize_t startPos, Key * errorKey, unsigned int numParams, va_list argList)
{
if (!sqlStmt)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "The given statement handle must be allocated!");
return false;
}
if (numParams == 0)
{
ELEKTRA_SET_INTERFACE_ERROR (errorKey, "The number of parameters to bind must be >0!");
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
if (startPos < 1)
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey,
"The first parameter has the position 1, so the argument 'startPos' must be >= 1, "
"but you provided %zd",
startPos);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
if (numParams > USHRT_MAX)
{
ELEKTRA_SET_INTERFACE_ERRORF (errorKey,
"The maximum number of parameter values to bind is %d, but you specified a number of %u",
USHRT_MAX, numParams);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
for (unsigned short u = 0; u < (SQLUSMALLINT) numParams; u++)
{
SQLRETURN ret;
const char * curStr = va_arg (argList, const char *);
if (!curStr)
{
ELEKTRA_ADD_INTERFACE_WARNING (errorKey, "Binding NULL data to an SQL statement!");
ret = SQLBindParameter (sqlStmt, u + startPos, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, NULL, 0,
(SQLLEN *) SQL_NULL_DATA);
}
else
{
ret = SQLBindParameter (sqlStmt, u + startPos, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen (curStr), 0,
(SQLCHAR *) curStr, (SQLLEN) strlen (curStr) + 1, NULL);
}
ELEKTRA_LOG ("Bound value '%s' to statement\n", curStr);
if (!SQL_SUCCEEDED (ret))
{
ELEKTRA_SET_ODBC_ERROR (SQL_HANDLE_STMT, sqlStmt, errorKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
else if (ret == SQL_SUCCESS_WITH_INFO)
{
ELEKTRA_ADD_ODBC_WARNING (SQL_HANDLE_STMT, sqlStmt, errorKey);
}
}
return true;
}
/**
* @internal
*
* @brief Bind a specified number of strings to a given SQL statement.
*
* The number of bound statements must equal the number of parameter markers ('?') in the query to execute.
* You can combine binding strings and key-names from keys in a KeySet.
* Just use the @p startPos argument to specify for which parameter(s) you want to bind the values.
*
* @param sqlStmt The statement for which the values should be bound.
* @param startPos The position of the parameter marker ('?') for which the first given value is going to be bound.
* @param[out] errorKey Used to store errors and warnings.
* @param numParams The number of strings you want to bind.
* @param ... The strings the should be bound.
* Make sure that you only pass allocated char buffers that are not freed until the statement for which the values are bound is not
* executed any more or other values are bound to the statement.
*
* @retval 'false' if an error occurred
* @retval 'true' otherwise
*
* @see bindStringsToStatementV
* @see bindKeyNamesToStatement
*/
static bool bindStringsToStatement (SQLHSTMT sqlStmt, ssize_t startPos, Key * errorKey, unsigned int numParams, ...)
{
va_list argList;
va_start (argList, numParams);
bool ret = bindStringsToStatementV (sqlStmt, startPos, errorKey, numParams, argList);
va_end (argList);
return ret;
}
/**
* @internal
*
* @brief Bind all key-names of the Keys in a KeySet to a statement.
*
* Make sure to bind exactly the number of parameters that match the number of the placeholder which the query-string you want to execute
* what the given statement handle has.
*
* @param sqlStmt The statement for which the values should be bound.
* @param keysToBind The KeySet that contains the Keys for which you want to bind the key-names.
* @param startPosStatement The position of the parameter marker ('?') for which the first given value is going to be bound.
* The first parameter has the value '1', so a startPos <1 is an invalid value.
* @param startPosKeySet The position of the first key in @p keyToBind that gets bound
* The index of the first key has the value '0'.
* @param endPosKeySet The position of the last key in @p keyToBind that gets bound
* The index of the last key has the value ksGetSize(keysToBind)-1.
* @param useRelativeKeyNames Should the relative names of the key-names be bound? This is the keyname without the beginning-part.
* The key-name of @parentKey is used to determine the relative name.
* Example: key-name = "user:/software/odbc", key-name of parent key = "user:/software" --> string "odbc" gets bound.
* If set to 'false', the base-names of the Keys are used (esp. useful for processing rows in the metatable).
*
* @param[in,out] parentKey The key-name of this key is needed to determine the relative key-names of the Keys in the KeySet.
* Used to store errors and warnings.
*
* @retval 'false' if an error occurred
* @retval 'true' otherwise
*
* @see elektraKeyGetRelativeName
* @see keyBaseName
*/
static bool bindKeyNamesToStatement (SQLHSTMT sqlStmt, const KeySet * keysToBind, ssize_t startPosStatement, ssize_t startPosKeySet,
ssize_t endPosKeySet, bool useRelativeKeyNames, Key * parentKey)
{
if (!sqlStmt)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "The provided statement handle must not be NULL.");
return false;
}
if (startPosStatement < 1)
{
ELEKTRA_SET_INTERFACE_ERRORF (parentKey,
"The first parameter has the position 1, so the argument 'startPosStatement' must be >= 1, "
"but you provided %zd",
startPosStatement);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
if (ksGetSize (keysToBind) < 1)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "The KeySet with the keys to bind must not be NULL or empty.");
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
if ((endPosKeySet - startPosKeySet) >= USHRT_MAX)
{
ELEKTRA_SET_INTERFACE_ERRORF (parentKey,
"The maximum number of parameter values to bind is %d, but you specified a number of %zd",
USHRT_MAX, ksGetSize (keysToBind));
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
if (ksGetSize (keysToBind) <= endPosKeySet)
{
ELEKTRA_SET_INTERFACE_ERRORF (parentKey,
"The given end position (%zd) is beyond the index of the last element (%zd) in the KeySet.",
endPosKeySet, ksGetSize (keysToBind) - 1);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
for (elektraCursor it = startPosKeySet; it <= endPosKeySet; it++)
{
const char * curKeyName = NULL;
if (useRelativeKeyNames)
{
curKeyName = elektraKeyGetRelativeName (ksAtCursor (keysToBind, it), parentKey);
}
else
{
/* use basename for metakeys to remove "meta:/" */
curKeyName = keyBaseName (ksAtCursor (keysToBind, it));
}
SQLRETURN ret = SQLBindParameter (sqlStmt, (it + startPosStatement) - startPosKeySet, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
strlen (curKeyName), 0, (SQLCHAR *) curKeyName, (SQLLEN) strlen (curKeyName) + 1, NULL);
ELEKTRA_LOG ("Bound value '%s' to statement\n", curKeyName);
if (!SQL_SUCCEEDED (ret))
{
ELEKTRA_SET_ODBC_ERROR (SQL_HANDLE_STMT, sqlStmt, parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return false;
}
else if (ret == SQL_SUCCESS_WITH_INFO)
{
ELEKTRA_ADD_ODBC_WARNING (SQL_HANDLE_STMT, sqlStmt, parentKey);
}
}
return true;
}
/**
* @internal
*
* @brief Remove rows from a table in the data source.
*
* @param sqlStmt The statement on which the DELETE query should be executed.
* @param ksRemovedKeys A KeySet with the Keys that should be removed (only the key-name is considered, the key-value is for determining
* which rows should be deleted.
* @param tableName The name of the table from which the rows should be removed.
* @param keyColName The name of the column that contains the name of the Keys.
* @param quoteStr The char(s) that should be used for quoting identifiers.
* @param[in,out] parentKey The key-name of this key is needed to determine the relative key-names of the Keys in the KeySet.
* Used to store errors and warnings.
*
* @return The number of the deleted rows.
* @retval -1 if an error occurred.
* @retval -2 it the number of deleted rows could not be retrieved (e.g. because the used ODBC driver doesn't support this feature).
*/
static SQLLEN removeRows (SQLHSTMT sqlStmt, const KeySet * ksRemovedKeys, const char * tableName, const char * keyColName,
const char * quoteStr, Key * parentKey)
{
if (!sqlStmt)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "The provided statement handle must not be NULL.");
return -1;
}
ssize_t numRemovedKeys = ksGetSize (ksRemovedKeys);
char * queryDelete = NULL;
SQLLEN sumAffectedRows = 0;
ssize_t endPos = -1;
for (ssize_t startPos = 0; numRemovedKeys > 0; startPos = endPos + 1)
{
if (numRemovedKeys > ODBC_SQL_MAX_PARAMS)
{
if (!queryDelete)
{
queryDelete = getDeleteQuery (ODBC_SQL_MAX_PARAMS, tableName, keyColName, quoteStr, parentKey);
}
endPos += ODBC_SQL_MAX_PARAMS;
numRemovedKeys -= ODBC_SQL_MAX_PARAMS;
}
else
{
if (queryDelete)
{
elektraFree (queryDelete);
}
SQLFreeStmt (sqlStmt, SQL_RESET_PARAMS);
queryDelete = getDeleteQuery (numRemovedKeys, tableName, keyColName, quoteStr, parentKey);
/* Last iteration */
endPos += numRemovedKeys;
numRemovedKeys = 0;
}
if (!queryDelete)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return -1;
}
if (!bindKeyNamesToStatement (sqlStmt, ksRemovedKeys, 1, startPos, endPos, true, parentKey))
{
/* sqlStmt was already freed by bindRelativeKeyNamesToStatement() */
/* bindRelativeKeyNamesToStatement() should've set an error in the parentKey */
return -1;
}
/* Now our statement handle is ready to use */
/* Execute the delete query (not persisted until transaction is committed!) */
SQLLEN affectedRows = executeQuery (sqlStmt, queryDelete, parentKey);
if (affectedRows == -1)
{
/* error, statement handle got freed by executeQuery() */
return -1;
}
else if (affectedRows > 0)
{
sumAffectedRows += affectedRows;
}
else
{
sumAffectedRows = -2;
}
}
elektraFree (queryDelete);
return sumAffectedRows;
}
/**
* @internal
*
* @brief This function is used to process the metadata associated with an modified Key.
*
* A Key is also considered as modified if only its metadata has changed, but not its value.
* This function may execute INSERT-, UPDATE- and/or DELETE-queries that affect the metatable.
* Which queries are executed depends on the changes of the metadata for the given Key.
*
* @param sqlStmt The statement on which the queries should be executed.
* If the given statement already contains bound parameters, they are overwritten by this function.
* @param modifiedKey The key for which the associated metadata should be processed.
* @param dsConfig The data source configuration (contains mountpoint definition).
* @param diffSet The ElektraDiff that was created by comparing the data to store with the data that is currently in the data source.
* @param quoteStr The char(s) that should be used for quoting identifiers.
* @param[in,out] parentKey The key-name of this key is needed to determine the relative key-name of the modified Key.
* Used to store errors and warnings.
*
* @return The number of the deleted rows.
* @retval -1 if an error occurred.
* @retval -2 it the number of deleted rows could not be retrieved (e.g. because the used ODBC driver doesn't support this feature).
*/
static SQLLEN processMetaDataForModifiedKey (SQLHSTMT sqlStmt, Key * modifiedKey, const struct dataSourceConfig * dsConfig,
const ElektraDiff * diffSet, const char * quoteStr, Key * parentKey)
{
/* For the UPDATE operation, we've to consider added, modified and deleted metadata
* --> this implies we may need INSERT, UPDATE and DELETE queries for the metadata */
KeySet * ksMetaRemoved = elektraDiffGetRemovedMetaKeys (diffSet, modifiedKey);
SQLLEN curAffectedRows;
SQLLEN sumAffectedRows = 0;
if (ksGetSize (ksMetaRemoved) > 0)
{
/* DELETE FROM metaTable where keyname=? and metaKeyName IN (?,?,?,...) */
/* At first, we've to bind the key-name */
if (!bindStringsToStatement (sqlStmt, 1, parentKey, 1, elektraKeyGetRelativeName (modifiedKey, parentKey)))
{
/* sqlStmt was already freed by the binding functions */
ksDel (ksMetaRemoved);
return -1;
}
char * queryMetaDelete = NULL;
ssize_t numMetaRemoved = ksGetSize (ksMetaRemoved);
ssize_t endPos = -1;
for (ssize_t startPos = 0; numMetaRemoved > 0; startPos = endPos + 1)
{
/* Get the SQL DELETE query with the correct number of parameters for this iteration */
if (numMetaRemoved > ODBC_SQL_MAX_PARAMS)
{
if (!queryMetaDelete)
{
queryMetaDelete = getDeleteMetaDataQuery (ODBC_SQL_MAX_PARAMS, dsConfig, quoteStr, parentKey);
}
endPos += ODBC_SQL_MAX_PARAMS;
numMetaRemoved -= ODBC_SQL_MAX_PARAMS;
}
else
{
if (queryMetaDelete)
{
elektraFree (queryMetaDelete);
}
queryMetaDelete = getDeleteMetaDataQuery (numMetaRemoved, dsConfig, quoteStr, parentKey);
endPos += numMetaRemoved;
/* We are in the last iteration */
numMetaRemoved = 0;
}
if (!queryMetaDelete)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
ksDel (ksMetaRemoved);
return -1;
}
/* Now, we bind the names of the metakeys */
if (!bindKeyNamesToStatement (sqlStmt, ksMetaRemoved, 2, startPos, endPos, false, parentKey))
{
/* sqlStmt was already freed by the binding functions */
ksDel (ksMetaRemoved);
elektraFree (queryMetaDelete);
return -1;
}
startPos = endPos + 1;
curAffectedRows = executeQuery (sqlStmt, queryMetaDelete, parentKey);
if (curAffectedRows == -1)
{
/* sqlStmt was already freed by executeQuery() */
ksDel (ksMetaRemoved);
return -1;
}
else if (curAffectedRows > 0)
{
sumAffectedRows += curAffectedRows;
}
}
elektraFree (queryMetaDelete);
}
ksDel (ksMetaRemoved);
KeySet * ksMetaAdded = elektraDiffGetAddedMetaKeys (diffSet, modifiedKey);
if (ksGetSize (ksMetaAdded) > 0)
{
/* INSERT INTO metaTable (keyName, metaKeyName, metaValName) VALUES (?,?,?) */
char * queryMetaInsert = getInsertQuery (dsConfig, true, quoteStr, parentKey);
if (!queryMetaInsert)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
ksDel (ksMetaAdded);
return -1;
}
for (elektraCursor itMeta = 0; itMeta < ksGetSize (ksMetaAdded); itMeta++)
{
Key * curMetaKey = ksAtCursor (ksMetaAdded, itMeta);
if (!bindStringsToStatement (sqlStmt, 1, parentKey, 3, elektraKeyGetRelativeName (modifiedKey, parentKey),
keyBaseName (curMetaKey), keyString (curMetaKey)))
{
/* sqlStmt was already freed by the bindStringsToStatement() */
ksDel (ksMetaAdded);
elektraFree (queryMetaInsert);
return -1;
}
curAffectedRows = executeQuery (sqlStmt, queryMetaInsert, parentKey);
if (curAffectedRows == -1)
{
/* sqlStmt was already freed by executeQuery() */
ksDel (ksMetaAdded);
elektraFree (queryMetaInsert);
return -1;
}
else if (curAffectedRows > 0)
{
sumAffectedRows += curAffectedRows;
}
}
elektraFree (queryMetaInsert);
}
ksDel (ksMetaAdded);
KeySet * ksMetaModified = elektraDiffGetModifiedMetaKeys (diffSet, modifiedKey);
if (ksGetSize (ksMetaModified) > 0)
{
/* UPDATE metaTable SET metaval=? where keyName=? and metaKeyName=? */
char * queryMetaUpdate = getUpdateQuery (dsConfig, true, quoteStr, parentKey);
if (!queryMetaUpdate)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
ksDel (ksMetaModified);
return -1;
}
KeySet * ksNewMeta = keyMeta (modifiedKey);
for (elektraCursor itMeta = 0; itMeta < ksGetSize (ksMetaModified); itMeta++)
{
Key * curMetaKey = ksAtCursor (ksMetaModified, itMeta);
/* The curMetaKey contains the old metavalue --> we have to get the metakey from the modified key */
Key * newMetaKey = ksLookup (ksNewMeta, curMetaKey, KDB_O_NONE);
if (!bindStringsToStatement (sqlStmt, 1, parentKey, 3, keyString (newMetaKey),
elektraKeyGetRelativeName (modifiedKey, parentKey), keyBaseName (newMetaKey)))
{
/* sqlStmt was already freed by the bindStringsToStatement() */
ksDel (ksMetaModified);
elektraFree (queryMetaUpdate);
return -1;
}
curAffectedRows = executeQuery (sqlStmt, queryMetaUpdate, parentKey);
if (curAffectedRows == -1)
{
/* sqlStmt was already freed by executeQuery() */
ksDel (ksMetaModified);
elektraFree (queryMetaUpdate);
return -1;
}
else if (curAffectedRows > 0)
{
sumAffectedRows += curAffectedRows;
}
}
elektraFree (queryMetaUpdate);
}
ksDel (ksMetaModified);
return sumAffectedRows;
}
/**
* @internal
*
* @brief This function is used for inserting or updating rows in the key-table and metatable based on the given KeySet and ElektraDiff
*
* @param sqlStmt The statement on which the queries should be executed.
* If the given statement already contains bound parameters, they are overwritten by this function.
* @param ks The KeySet that contains the Keys that should be inserted or updated.
* The Keys inside @ks must contain the new-values which you want to store in the ODBC data source.
* @param dsConfig The data source configuration (contains mountpoint definition).
* @param update 'true' if existing rows should be updated, 'false' if new rows should be inserted.
* @param diffSet The ElektraDiff that was created by comparing the data to store with the data that is currently in the data source.
@p diffSet is only needed for UPDATE operation, for INSERT it is ignored.
* @param quoteStr The char(s) that should be used for quoting identifiers.
* @param[in,out] parentKey The key-name of this key is needed to determine the relative key-name of the Keys in @ks.
* Used to store errors and warnings.
*
* @return The number of the deleted rows.
* @retval -1 if an error occurred.
* @retval -2 it the number of deleted rows could not be retrieved (e.g. because the used ODBC driver doesn't support this feature).
*/
static SQLLEN insertOrUpdateRows (SQLHSTMT sqlStmt, const KeySet * ks, const struct dataSourceConfig * dsConfig, bool update,
const ElektraDiff * diffSet, const char * quoteStr, Key * parentKey)
{
if (ksGetSize (ks) < 1)
{
/* no rows to insert */
return 0;
}
if (!sqlStmt)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "The provided statement handle must not be NULL.");
return -1;
}
if (!dsConfig)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "The provided dataSourceConfig struct must not be NULL.");
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return -1;
}
if (update && !diffSet)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "For the UPDATE operation, the diffSet must not be NULL.");
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return -1;
}
char * query;
if (update)
{
query = getUpdateQuery (dsConfig, false, quoteStr, parentKey);
}
else
{
query = getInsertQuery (dsConfig, false, quoteStr, parentKey);
}
if (!query)
{
ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey);
SQLFreeHandle (SQL_HANDLE_STMT, sqlStmt);
return -1;
}
SQLLEN curAffectedRows;
/* The value -2 indicates that the number of affected rows could not be retrieved from the data source */
SQLLEN sumAffectedRows = 0;
for (elektraCursor it = 0; it < ksGetSize (ks); it++)
{
Key * curKey = ksAtCursor (ks, it);
/* For the UPDATE operation, we have to check if the value was changed, it is possible that only metadata was changed */
if (!update || elektraDiffKeyValueChanged (diffSet, curKey))
{
/* UPDATE table set val=? where key=?
* --> we have to bind the value first and then the key-name for which the value should be set
* The INSERT query was designed to have the value before the key-name, so that the same parameter order as in the
* UPDATE query can be used */
if (!bindStringsToStatement (sqlStmt, 1, parentKey, 2, keyString (curKey),
elektraKeyGetRelativeName (curKey, parentKey)))
{
/* sqlStmt was already freed by bindStringsToStatement() */
/* bindStringsToStatement() should've set an error in the parentKey */
elektraFree (query);
return -1;
}
/* Now our statement handle is ready to use for executing the query */
curAffectedRows = executeQuery (sqlStmt, query, parentKey);
if (curAffectedRows == -1)
{
/* sqlStmt was already freed by executeQuery() */
elektraFree (query);
return -1;
}
else if (curAffectedRows > 0)
{
sumAffectedRows += curAffectedRows;
}
}
/* Now process the metadata, this KeySet MUST NOT be deleted */
KeySet * curMetaKs = keyMeta (curKey);
if (update)
{
curAffectedRows = processMetaDataForModifiedKey (sqlStmt, curKey, dsConfig, diffSet, quoteStr, parentKey);
if (curAffectedRows == -1)
{
elektraFree (query);
}
else if (curAffectedRows > 0)
{
sumAffectedRows += curAffectedRows;
}
}
else if (ksGetSize (curMetaKs) > 0)
{
char * queryMetaInsert = getInsertQuery (dsConfig, true, quoteStr, parentKey);
for (elektraCursor itMeta = 0; itMeta < ksGetSize (curMetaKs); itMeta++)
{
Key * curMetaKey = ksAtCursor (curMetaKs, itMeta);
/* INSERT metadata (3 parameters)
INSERT INTO metaTableName (keyColName, metaKeyColName, valColName) VALUES (?,?,?) */
if (!bindStringsToStatement (sqlStmt, 1, parentKey, 3, elektraKeyGetRelativeName (curKey, parentKey),
keyBaseName (curMetaKey), keyString (curMetaKey)))
{
elektraFree (query);
elektraFree (queryMetaInsert);
return -1;
}
/* Now insert the keys */
curAffectedRows = executeQuery (sqlStmt, queryMetaInsert, parentKey);
if (curAffectedRows == -1)
{
/* sqlStmt was already freed by executeQuery() */
elektraFree (query);
elektraFree (queryMetaInsert);
return -1;