-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkots_mysql_helper.c
153 lines (127 loc) · 3.91 KB
/
kots_mysql_helper.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
#include "kots_mysql_helper.h"
//creates an iterator to more easily manage results and row values
MYSQL_ITERATOR *mysql_iterator_create(MYSQL *mysql)
{
MYSQL_ITERATOR *iterator = malloc(sizeof(MYSQL_ITERATOR));
if (!iterator)
return NULL;
iterator->mysql = mysql;
iterator->result = NULL;
mysql_iterator_nextresult(iterator);
return iterator;
}
//frees the memory used by the iterator
void mysql_iterator_free(MYSQL_ITERATOR *iterator)
{
//if the iterator has a result stored free it
if (iterator->result)
mysql_iterator_clearresults(iterator);
//free memory used by iterator
iterator->mysql = NULL;
free(iterator);
iterator = NULL;
}
void mysql_iterator_clearresults(MYSQL_ITERATOR *iterator)
{
if (iterator->result)
mysql_free_result(iterator->result);
while (mysql_more_results(iterator->mysql))
{
mysql_next_result(iterator->mysql);
iterator->result = mysql_store_result(iterator->mysql);
if (iterator->result)
mysql_free_result(iterator->result);
}
iterator->result = NULL;
iterator->currentrow = NULL;
iterator->fields = NULL;
iterator->numfields = 0;
iterator->numrows = 0;
}
//get the next result and return whether or not there are more
int mysql_iterator_nextresult(MYSQL_ITERATOR *iterator)
{
if (iterator->result)
{
mysql_free_result(iterator->result);
iterator->result = NULL;
}
mysql_next_result(iterator->mysql);
iterator->result = mysql_store_result(iterator->mysql);
//if a result was obtained get the new field and row info
if (iterator->result)
{
iterator->numfields = mysql_field_count(iterator->mysql);
iterator->fields = mysql_fetch_fields(iterator->result);
iterator->currentrow = mysql_fetch_row(iterator->result);
iterator->numrows = (unsigned long)mysql_num_rows(iterator->result);
}
else
{
iterator->currentrow = NULL;
iterator->fields = NULL;
iterator->numfields = 0;
iterator->numrows = 0;
}
return mysql_more_results(iterator->mysql);
}
//get the next row and return whether or not there are more
int mysql_iterator_nextrow(MYSQL_ITERATOR *iterator)
{
if (!iterator->result)
return 0;
iterator->currentrow = mysql_fetch_row(iterator->result);
if (iterator->currentrow == NULL)
return 0;
else
return 1;
}
//determines if the current result set has the specified column
int mysql_iterator_hascolumn(MYSQL_ITERATOR *iterator, const char *name)
{
unsigned int i;
if (iterator->currentrow)
for (i = 0; i < iterator->numfields; i++)
if (strcmp(iterator->fields[i].name, name) == 0)
return 1;
return 0;
}
//gets a pointer for the specified column name (NULL if not found)
void *mysql_iterator_getvalue(MYSQL_ITERATOR *iterator, const char *name)
{
unsigned int i;
if (iterator->currentrow)
for (i = 0; i < iterator->numfields; i++)
if (strcmp(iterator->fields[i].name, name) == 0)
return iterator->currentrow[i];
return NULL;
}
//gets a character pointer for the specified column
void *mysql_iterator_getvalue_col(MYSQL_ITERATOR *iterator, const int col)
{
return (iterator->currentrow ? iterator->currentrow[col] : NULL);
}
//gets an integer value for the specified column name (0 if not found or null)
int mysql_iterator_getint(MYSQL_ITERATOR *iterator, const char *name)
{
char *value = mysql_iterator_getvalue(iterator, name);
return (value ? atoi(value) : 0);
}
//gets an integer value for the specified column
int mysql_iterator_getint_col(MYSQL_ITERATOR *iterator, const int col)
{
char *value = mysql_iterator_getvalue_col(iterator, col);
return (value ? atoi(value) : 0);
}
//gets an char value for the specified column name ('\0' if not found or null)
char mysql_iterator_getchar(MYSQL_ITERATOR *iterator, const char *name)
{
char *value = mysql_iterator_getvalue(iterator, name);
return (value ? *value : '\0');
}
//gets an char value for the specified column
char mysql_iterator_getchar_col(MYSQL_ITERATOR *iterator, const int col)
{
char *value = mysql_iterator_getvalue_col(iterator, col);
return (value ? *value : '\0');
}