-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite-json-info.c
91 lines (75 loc) · 2.04 KB
/
sqlite-json-info.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
#include <stdlib.h>
#include <sqlite3.h>
#include "sqlite-json-info.h"
#include "internal-debug.h"
sqlite_json_info_t* sqlite_json_info_new(sqlite3_stmt* stmt, int compress) {
sqlite_json_info_t *sji = calloc(1, sizeof(sqlite_json_info_t));
int i = 0;
if(!sji) return NULL;
if(compress) {
sji->zs.zalloc = (alloc_func) 0;
sji->zs.zfree = (free_func) 0;
sji->zs.opaque = (voidpf) 0;
if(Z_OK != deflateInit2(&sji->zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS+16, 8, Z_DEFAULT_STRATEGY)) {
free(sji);
return NULL;
}
}
sji->stmt = stmt;
sji->ncol = sqlite3_column_count(stmt);
if(sji->ncol > 0) {
sji->colnames = calloc(sji->ncol, sizeof(char *));
sji->coltypes = calloc(sji->ncol, sizeof(int));
}
sji->initialized = 0;
sji->rstart = 1;
sji->compress = compress;
sji->eos = EOS_FALSE;
if(sji->ncol > 0) {
if(NULL == sji->coltypes || NULL == sji->colnames) {
if(sji->colnames) {
free(sji->colnames);
}
free(sji);
return NULL;
}
}
if(compress) {
sji->gzbuf = calloc(GZ_CACHE_SIZE, sizeof(char));
}
return sji;
}
void sqlite_json_info_init(sqlite_json_info_t *sji) {
int i = 0;
for(; i < sji->ncol; i++) {
sji->colnames[i] = (char *)sqlite3_column_name(sji->stmt, i);
//DD("colnames[%d] = %s, type = %d\n", i, sji->colnames[i], sqlite3_column_type(sji->stmt, i));
sji->coltypes[i] = sqlite3_column_type(sji->stmt, i);
}
sji->initialized = 1;
}
int sqlite_json_info_initialized(sqlite_json_info_t *sji) {
return sji->initialized;
}
void sqlite_json_info_set_eos(sqlite_json_info_t *sji, int eos) {
sji->eos = eos;
}
void sqlite_json_info_set_rstart(sqlite_json_info_t *sji, int rstart) {
sji->rstart = rstart;
}
void sqlite_json_info_free(void *data) {
sqlite_json_info_t *sji = (sqlite_json_info_t *)data;
if(sji->gzbuf) {
free(sji->gzbuf);
}
if(sji->stmt) {
sqlite3_finalize(sji->stmt);
}
if(sji->colnames) {
free(sji->colnames);
}
if(sji->coltypes) {
free(sji->coltypes);
}
free(sji);
}