forked from vlcn-io/cr-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes-vtab-common.c
72 lines (64 loc) · 1.93 KB
/
changes-vtab-common.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
#include "changes-vtab-common.h"
#include "consts.h"
#include <string.h>
#include "util.h"
char *crsql_extractWhereList(
crsql_ColumnInfo *zColumnInfos,
int columnInfosLen,
const char *quoteConcatedVals)
{
char **zzParts = 0;
if (columnInfosLen == 1)
{
zzParts = sqlite3_malloc(1 * sizeof(char *));
zzParts[0] = strdup(quoteConcatedVals);
}
else
{
// zzParts will not be greater or less than columnInfosLen.
// TODO: we need something other than `crsql_split` here. `crsql_quoteConcatSplit`
zzParts = crsql_splitQuoteConcat(quoteConcatedVals, columnInfosLen);
}
if (zzParts == 0)
{
return 0;
}
for (int i = 0; i < columnInfosLen; ++i)
{
// this is safe since pks are extracted as `quote` in the prior queries
// %z will de-allocate pksArr[i] so we can re-allocate it in the assignment
// TODO: we currently invoke this in a non safe case
// where pksArr is receive from a network socket rather than the
// local db. In the apply patches path.
zzParts[i] = sqlite3_mprintf("\"%s\" = %z", zColumnInfos[i].name, zzParts[i]);
}
// join2 will free the contents of zzParts given identity is a pass-thru
char *ret = crsql_join2((char *(*)(const char *)) & crsql_identity, zzParts, columnInfosLen, " AND ");
sqlite3_free(zzParts);
return ret;
}
// parts must already be properly quoted and escaped for inclusion in a SQL statement
char *crsql_quotedValuesAsList(char **parts, int numParts)
{
int len = 0;
for (int i = 0; i < numParts; ++i)
{
len += strlen(parts[i]);
}
len += numParts - 1;
char *ret = sqlite3_malloc((len + 1) * sizeof *ret);
crsql_joinWith(ret, parts, numParts, ',');
ret[len] = '\0';
return ret;
}
char *crsql_quoteConcatedValuesAsList(
const char *quoteConcatedVals,
int len)
{
char **parts = crsql_splitQuoteConcat(quoteConcatedVals, len);
if (parts == 0)
{
return 0;
}
return crsql_quotedValuesAsList(parts, len);
}