-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlitevariant.h
378 lines (335 loc) · 6.92 KB
/
sqlitevariant.h
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
#ifndef SQLITEVARIANT_H_
#define SQLITEVARIANT_H_
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include "sqlite/sqlite3.h"
class SQLiteVariant
{
public:
typedef enum {VARNONE = 0, VARINT, VARREAL, VARBLOB, VARTEXT} StoredType;
private:
StoredType m_storedtype;
size_t m_datalen;
size_t m_reservedlen;
union VARUNION
{
char* as_blob;
unsigned long long as_uint64;
long long as_int64;
double as_double;
unsigned int as_uint;
int as_int;
} m_data;
void FreeBlobHeapMemIfAllocated()
{
if((VARBLOB == m_storedtype || VARTEXT == m_storedtype) && m_data.as_blob)
{
free(m_data.as_blob);
}
}
public:
void ForceSetType(StoredType vartype)
{
//Only to be used as a placeholder in case the user tries to
//create a table before providing values to deduce types
m_storedtype = vartype;
}
bool HasType()
{
return VARNONE != m_storedtype;
}
const StoredType GetType() const
{
return m_storedtype;
}
const std::string GetTypeAsString()
{
static const char* typestrs[] = {"NULL", "INT", "REAL", "BLOB", "TEXT"};
return std::string(typestrs[static_cast<int>(m_storedtype)]);
}
size_t GetDataLength() const
{
return m_datalen;
}
void ClearValue()
{
//Don't clear the reserved len or release any heap memory
m_datalen = 0;
switch(m_storedtype)
{
case VARTEXT:
case VARBLOB:
memset(m_data.as_blob, 0, m_reservedlen);
break;
default:
memset(&m_data, 0, sizeof(union VARUNION));
break;
}
}
SQLiteVariant()
{
m_storedtype = VARNONE;
m_datalen = 0;
m_reservedlen = 0;
memset(&m_data, 0, sizeof(union VARUNION));
}
SQLiteVariant(const SQLiteVariant& other)
{
m_datalen = other.m_datalen;
m_storedtype = other.m_storedtype;
m_reservedlen = other.m_reservedlen;
if(VARTEXT == m_storedtype || VARBLOB == m_storedtype)
{
m_data.as_blob = (char*) malloc(m_reservedlen);
memcpy(m_data.as_blob, other.m_data.as_blob, m_reservedlen);
}
else
{
m_data = other.m_data;
}
}
SQLiteVariant(SQLiteVariant&& other)
{
m_datalen = std::move(other.m_datalen);
m_reservedlen = std::move(other.m_reservedlen);
m_storedtype = std::move(other.m_storedtype);
m_data.as_blob = std::move(other.m_data.as_blob);
other.m_data.as_blob = 0;
other.m_storedtype = VARNONE;
}
SQLiteVariant& operator=(const SQLiteVariant& other)
{
if(VARTEXT == other.m_storedtype || VARBLOB == other.m_storedtype)
{
if(!m_data.as_blob)
{
m_data.as_blob = (char*) malloc(other.m_reservedlen);
}
else if(m_reservedlen < other.m_reservedlen)
{
m_data.as_blob = (char*) realloc(m_data.as_blob, other.m_reservedlen);
}
m_datalen = other.m_datalen;
m_reservedlen = other.m_reservedlen;
m_storedtype = other.m_storedtype;
memcpy(m_data.as_blob, other.m_data.as_blob, m_reservedlen);
}
else
{
m_datalen = other.m_datalen;
m_reservedlen = other.m_reservedlen;
m_storedtype = other.m_storedtype;
m_data = other.m_data;
}
return *this;
}
~SQLiteVariant()
{
if((VARBLOB == m_storedtype || VARTEXT == m_storedtype) && m_data.as_blob)
{
free(m_data.as_blob);
}
}
void SetValue(int v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_int = v;
m_datalen = sizeof(int);
m_storedtype = VARINT;
}
void SetValue(long long v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_int64 = v;
m_datalen = sizeof(long long);
m_storedtype = VARINT;
}
void SetValue(unsigned int v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_uint = v;
m_datalen = sizeof(unsigned int);
m_storedtype = VARINT;
}
void SetValue(unsigned long long v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_uint64 = v;
m_datalen = sizeof(unsigned long long);
m_storedtype = VARINT;
}
void SetValue(float v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_double = static_cast<double>(v);
m_datalen = sizeof(float);
m_storedtype = VARREAL;
}
void SetValue(double v)
{
FreeBlobHeapMemIfAllocated();
m_data.as_double = v;
m_datalen = sizeof(double);
m_storedtype = VARREAL;
}
void SetValue(const std::string& v)
{
if(VARBLOB == m_storedtype || VARTEXT == m_storedtype)
{
if(m_data.as_blob)
{
if((v.length() + 1) > m_reservedlen)
{
m_reservedlen = (v.length() + 1) << 1;
m_data.as_blob = (char*) realloc(m_data.as_blob, m_reservedlen);
}
}
}
m_datalen = v.length() + 1;
if(!m_data.as_blob)
{
m_data.as_blob = (char*) malloc(m_datalen);
if(!m_data.as_blob)
{
printf("FAILED TO ALLOCATE MEMORY FOR VARIANT BLOB!\n");
}
m_reservedlen = m_datalen;
}
memset(m_data.as_blob, 0, m_reservedlen);
strncpy(m_data.as_blob, v.c_str(), m_datalen);
m_storedtype = VARTEXT;
}
void SetValue(const char* data, size_t len)
{
if(VARBLOB == m_storedtype || VARTEXT == m_storedtype)
{
if(m_data.as_blob)
{
if(len > m_reservedlen)
{
m_reservedlen = std::max((len << 1), (size_t) 4);
m_data.as_blob = (char*) realloc(m_data.as_blob, m_reservedlen);
}
}
}
m_datalen = std::max(len, (size_t) 4);
if(!m_data.as_blob)
{
m_data.as_blob = (char*) malloc(m_datalen);
m_reservedlen = m_datalen;
}
memset(m_data.as_blob, 0, m_reservedlen);
if(data)
{
memcpy(m_data.as_blob, data, m_datalen);
}
m_storedtype = VARBLOB;
}
bool GetValue(int& v) const
{
if(VARINT == m_storedtype)// && m_datalen == sizeof(int))
{
v = m_data.as_int;
return true;
}
else
{
return false;
}
}
bool GetValue(long long& v) const
{
if(VARINT == m_storedtype)// && m_datalen == sizeof(long long))
{
v = m_data.as_int64;
return true;
}
else
{
return false;
}
}
bool GetValue(unsigned int& v) const
{
if(VARINT == m_storedtype)// && m_datalen == sizeof(unsigned int))
{
v = m_data.as_uint;
return true;
}
else
{
return false;
}
}
bool GetValue(unsigned long long& v) const
{
if(VARINT == m_storedtype)// && m_datalen == sizeof(unsigned long long))
{
v = m_data.as_uint64;
return true;
}
else
{
return false;
}
}
bool GetValue(float& v) const
{
if(VARREAL == m_storedtype)// && m_datalen == sizeof(float))
{
v = static_cast<float>(m_data.as_double);
return true;
}
else
{
return false;
}
}
bool GetValue(double& v) const
{
if(VARREAL == m_storedtype)// && m_datalen == sizeof(double))
{
v = m_data.as_double;
return true;
}
else
{
return false;
}
}
bool GetValue(std::string& v) const
{
if(VARTEXT == m_storedtype)
{
v = std::string(m_data.as_blob);
return true;
}
else
{
return false;
}
}
const char* GetValueBlobPtr() const
{
return m_data.as_blob;
}
bool GetValue(std::vector<char>& v) const
{
if(VARBLOB == m_storedtype)
{
v.resize(m_datalen);
memcpy(&v[0], m_data.as_blob, m_datalen);
return true;
}
else
{
return false;
}
}
};
std::string VariantTypeToString(SQLiteVariant::StoredType type);
int BindVariantToStatement(sqlite3_stmt* stmt, const SQLiteVariant* value, int pos);
#endif