-
Notifications
You must be signed in to change notification settings - Fork 247
/
bulk1.c
238 lines (188 loc) · 6.09 KB
/
bulk1.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
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
/*
Copyright 2011 Kristian Nielsen and Monty Program Ab.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this. If not, see <http://www.gnu.org/licenses/>.
*/
#include "my_test.h"
#include "ma_common.h"
#define TEST_ARRAY_SIZE 1024
char *rand_str(size_t length) {
const char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *dest= (char *)malloc(length+1);
char *p= dest;
while (length-- > 0) {
*dest++ = charset[rand() % sizeof(charset)];
}
*dest = '\0';
return p;
}
static int bulk1(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
const char *stmt_str= "INSERT INTO bulk1 VALUES (?,?)";
unsigned int array_size= TEST_ARRAY_SIZE;
int rc;
unsigned int i;
char **buffer;
unsigned long *lengths;
unsigned int *vals;
MYSQL_BIND bind[2];
MYSQL_RES *res;
MYSQL_ROW row;
unsigned int intval;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk1");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk1 (a int, b VARCHAR(255))");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, stmt_str, strlen(stmt_str));
check_stmt_rc(rc, stmt);
/* allocate memory */
buffer= calloc(TEST_ARRAY_SIZE, sizeof(char *));
lengths= (unsigned long *)calloc(sizeof(long), TEST_ARRAY_SIZE);
vals= (unsigned int *)calloc(sizeof(int), TEST_ARRAY_SIZE);
for (i=0; i < TEST_ARRAY_SIZE; i++)
{
buffer[i]= rand_str(254);
lengths[i]= -1;
vals[i]= i;
}
memset(bind, 0, sizeof(MYSQL_BIND) * 2);
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (int *)&vals[0];
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (void *)buffer;
bind[1].length= (unsigned long *)lengths;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
for (i=0; i < 10; i++)
{
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
diag("Affected rows: %lld", mysql_stmt_affected_rows(stmt));
}
for (i=0; i < array_size; i++)
free(buffer[i]);
free(buffer);
free(lengths);
free(vals);
rc= mysql_stmt_close(stmt);
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "SELECT COUNT(*) FROM bulk1");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
intval= atoi(row[0]);
mysql_free_result(res);
FAIL_IF(intval != array_size * 10, "Expected 10240 rows");
rc= mysql_query(mysql, "SELECT MAX(a) FROM bulk1");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
intval= atoi(row[0]);
mysql_free_result(res);
FAIL_IF(intval != array_size - 1, "Expected max value 1024");
/*
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk1");
check_mysql_rc(rc, mysql);
*/
return OK;
}
static int bulk2(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
int rc;
MYSQL_BIND bind[2];
unsigned int i;
unsigned int array_size=1024;
char indicator[1024];
long lval[1024];
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk2");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk2 (a int default 4, b default 2)");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, "INSERT INTO bulk2 VALUES (?,1)", -1);
check_stmt_rc(rc, stmt);
memset(bind, 0, 2 * sizeof(MYSQL_BIND));
for (i=0; i < array_size; i++)
{
indicator[i]= STMT_INDICATOR_DEFAULT;
lval[i]= i;
}
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].u.indicator= indicator;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &lval;
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
return OK;
}
static int bulk3(MYSQL *mysql)
{
struct st_bulk3 {
char char_value[20];
uchar indicator;
int int_value;
};
struct st_bulk3 val[]= {{"Row 1", STMT_INDICATOR_NTS, 1},
{"Row 2", STMT_INDICATOR_NTS, 2},
{"Row 3", STMT_INDICATOR_NTS, 3}};
int rc;
MYSQL_BIND bind[2];
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
size_t row_size= sizeof(struct st_bulk3);
int array_size= 3;
ulong length= -1;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk3");
check_mysql_rc(rc,mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk3 (name varchar(20), row int)");
check_mysql_rc(rc,mysql);
rc= mysql_stmt_prepare(stmt, "INSERT INTO bulk3 VALUES (?,?)", -1);
check_stmt_rc(rc, stmt);
memset(bind, 0, sizeof(MYSQL_BIND)*2);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].buffer= &val[0].char_value;
bind[0].length= &length;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &val[0].int_value;
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
return OK;
}
struct my_tests_st my_tests[] = {
{"bulk1", bulk1, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk2", bulk2, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"bulk3", bulk3, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{NULL, NULL, 0, 0, NULL, NULL}
};
int main(int argc, char **argv)
{
if (argc > 1)
get_options(argc, argv);
get_envvars();
run_tests(my_tests);
return(exit_status());
}