-
Notifications
You must be signed in to change notification settings - Fork 2
/
resultset.c
144 lines (118 loc) · 2.57 KB
/
resultset.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
/**
* resultset.c
*
* ResultSet module
*/
#include <stdlib.h>
#include <stdio.h>
#include <json/json.h>
#include "stringlist.h"
#include "resultset.h"
/* create empty ResultSet */
ResultSet* createResultSet(StringItemList* sil) {
ResultSet* rs = MALLOC(sizeof(ResultSet));
if (rs == NULL)
return NULL;
rs->column_num = sil->item_num;
rs->columns = sil;
rs->rows = 0;
rs->first = NULL;
rs->current= NULL;
rs->last = NULL;
return rs;
}
RowData* createRowData(StringItemList* sil) {
RowData* rd = MALLOC(sizeof(RowData));
if (rd == NULL)
return NULL;
rd->data = sil;
rd->next = NULL;
return rd;
};
int addRowData(ResultSet* rs, RowData* rd) {
if (rs->first == NULL) {
rs->first = rd;
rs->last = rd;
} else {
rs->last->next = rd;
rs->last = rd;
}
rs->rows ++;
return rs->rows;
}
void initResultSet(ResultSet* rs) {
rs->current = NULL;
}
RowData* getFirstResultSet(ResultSet* rs) {
rs->current = rs->first;
return rs->current;
}
RowData* getNextResultSet(ResultSet* rs) {
if (rs->current == NULL) {
rs->current = rs->first;
} else {
if (rs->current == NULL) {
return NULL;
} else {
rs->current = rs->current->next;
}
}
return rs->current;
}
char* getValueRowData(RowData* rd, int index) {
return getValueStringItemList(rd->data, index);
}
void freeRowData(RowData* rd) {
freeStringItemList(rd->data);
FREE(rd);
}
void freeResultSet(ResultSet* rs) {
RowData* cur;
RowData* next;
freeStringItemList(rs->columns);
for (cur = rs->first; cur != NULL; cur = next) {
next = cur->next;
freeRowData(cur);
}
FREE(rs);
}
void printResultSet(ResultSet* rs) {
RowData* rd;
printf("rs->column_num=%d\n",rs->column_num);
printStringItemList(rs->columns);
printf("rs->rows_num=%d\n",rs->rows);
for (rd = rs->first; rd != NULL; rd = rd->next) {
printStringItemList(rd->data);
}
}
#ifdef UNIT_TEST_RESULTSET
int main(int argc, char** argv) {
StringItemList* sil;
StringItem* si;
ResultSet* rs;
int rows, cols;
char tmp_data[128];
sil = createStringItemList();
for (cols = 1; cols <= 3; cols++) {
sprintf(tmp_data, "col_%02d", cols);
si = createStringItem(tmp_data);
addStringItem(sil, si);
}
rs = createResultSet(sil);
for (rows = 1; rows <= 10; rows++) {
StringItemList* sil;
StringItem* si;
RowData* rd;
sil = createStringItemList();
for (cols = 1; cols <= rs->column_num; cols++) {
sprintf(tmp_data, "data_%02d_%02d", rows, cols);
si = createStringItem(tmp_data);
addStringItem(sil, si);
}
rd = createRowData(sil);
addRowData(rs, rd);
}
printResultSet(rs);
freeResultSet(rs);
}
#endif