forked from mariadb-corporation/MaxScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_insert.cc
283 lines (227 loc) · 8.15 KB
/
bulk_insert.cc
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
/*
* Copyright (c) 2016 MariaDB Corporation Ab
* Copyright (c) 2023 MariaDB plc, Finnish Branch
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2027-04-10
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
/**
* MXS-1121: MariaDB 10.2 Bulk Insert test
*
* This test is a copy of one of the examples for bulk inserts:
* https://mariadb.com/kb/en/mariadb/bulk-insert-column-wise-binding/
*/
#include <maxtest/testconnections.hh>
static int show_mysql_error(MYSQL* mysql)
{
printf("Error(%d) [%s] \"%s\"\n",
mysql_errno(mysql),
mysql_sqlstate(mysql),
mysql_error(mysql));
return 1;
}
static int show_stmt_error(MYSQL_STMT* stmt)
{
printf("Error(%d) [%s] \"%s\"\n",
mysql_stmt_errno(stmt),
mysql_stmt_sqlstate(stmt),
mysql_stmt_error(stmt));
return 1;
}
int bind_by_column(MYSQL* mysql)
{
MYSQL_STMT* stmt;
MYSQL_BIND bind[3];
/* Data for insert */
const char* surnames[] = {"Widenius", "Axmark", "N.N."};
unsigned long surnames_length[] = {8, 6, 4};
const char* forenames[] = {"Monty", "David", "will be replaced by default value"};
char forename_ind[] = {STMT_INDICATOR_NTS, STMT_INDICATOR_NTS, STMT_INDICATOR_DEFAULT};
char id_ind[] = {STMT_INDICATOR_NULL, STMT_INDICATOR_NULL, STMT_INDICATOR_NULL};
unsigned int array_size = 3;
if (mysql_query(mysql, "DROP TABLE IF EXISTS test.bulk_example1"))
{
return show_mysql_error(mysql);
}
if (mysql_query(mysql,
"CREATE TABLE test.bulk_example1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," \
"forename CHAR(30) NOT NULL DEFAULT 'unknown', surname CHAR(30))"))
{
return show_mysql_error(mysql);
}
stmt = mysql_stmt_init(mysql);
if (mysql_stmt_prepare(stmt, "INSERT INTO test.bulk_example1 VALUES (?,?,?)", -1))
{
return show_stmt_error(stmt);
}
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
/* We autogenerate id's, so all indicators are STMT_INDICATOR_NULL */
bind[0].u.indicator = id_ind;
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = forenames;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].u.indicator = forename_ind;
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = surnames;
bind[2].length = surnames_length;
/* set array size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
/* bind parameter */
mysql_stmt_bind_param(stmt, bind);
/* execute */
if (mysql_stmt_execute(stmt))
{
return show_stmt_error(stmt);
}
mysql_stmt_close(stmt);
mysql_query(mysql, "BEGIN");
/* Check that the rows were inserted */
if (mysql_query(mysql, "SELECT * FROM test.bulk_example1"))
{
return show_mysql_error(mysql);
}
int nRows = 0;
MYSQL_RES* res = mysql_store_result(mysql);
if (res)
{
nRows = mysql_num_rows(res);
mysql_free_result(res);
}
mysql_query(mysql, "COMMIT");
if (nRows != 3)
{
printf("Expected 3 rows but got %d (%s)\n", nRows, mysql_error(mysql));
return 1;
}
if (mysql_query(mysql, "DROP TABLE test.bulk_example1"))
{
return show_mysql_error(mysql);
}
return 0;
}
int bind_by_row(MYSQL* mysql)
{
MYSQL_STMT* stmt;
MYSQL_BIND bind[3];
struct st_data
{
unsigned long id;
char id_ind;
char forename[30];
char forename_ind;
char surname[30];
char surname_ind;
};
struct st_data data[] =
{
{0, STMT_INDICATOR_NULL, "Monty", STMT_INDICATOR_NTS, "Widenius", STMT_INDICATOR_NTS},
{0, STMT_INDICATOR_NULL, "David", STMT_INDICATOR_NTS, "Axmark", STMT_INDICATOR_NTS},
{0, STMT_INDICATOR_NULL, "default", STMT_INDICATOR_DEFAULT, "N.N.", STMT_INDICATOR_NTS},
};
unsigned int array_size = 3;
size_t row_size = sizeof(struct st_data);
if (mysql_query(mysql, "DROP TABLE IF EXISTS bulk_example2"))
{
show_mysql_error(mysql);
}
if (mysql_query(mysql,
"CREATE TABLE bulk_example2 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," \
"forename CHAR(30) NOT NULL DEFAULT 'unknown', surname CHAR(30))"))
{
show_mysql_error(mysql);
}
stmt = mysql_stmt_init(mysql);
if (mysql_stmt_prepare(stmt, "INSERT INTO bulk_example2 VALUES (?,?,?)", -1))
{
show_stmt_error(stmt);
}
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
/* We autogenerate id's, so all indicators are STMT_INDICATOR_NULL */
bind[0].u.indicator = &data[0].id_ind;
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = &data[0].forename;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].u.indicator = &data[0].forename_ind;
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = &data[0].surname;
bind[2].u.indicator = &data[0].surname_ind;
/* set array size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
/* set row size */
mysql_stmt_attr_set(stmt, STMT_ATTR_ROW_SIZE, &row_size);
/* bind parameter */
mysql_stmt_bind_param(stmt, bind);
/* execute */
if (mysql_stmt_execute(stmt))
{
show_stmt_error(stmt);
}
mysql_stmt_close(stmt);
mysql_query(mysql, "BEGIN");
/* Check that the rows were inserted */
if (mysql_query(mysql, "SELECT * FROM test.bulk_example2"))
{
return show_mysql_error(mysql);
}
int nRows = 0;
MYSQL_RES* res = mysql_store_result(mysql);
if (res)
{
nRows = mysql_num_rows(res);
mysql_free_result(res);
}
if (nRows != 3)
{
printf("Expected 3 rows but got %d (%s)\n", nRows, mysql_error(mysql));
return 1;
}
mysql_query(mysql, "COMMIT");
if (mysql_query(mysql, "DROP TABLE test.bulk_example2"))
{
return show_mysql_error(mysql);
}
return 0;
}
int main(int argc, char** argv)
{
TestConnections::require_repl_version("10.2");
TestConnections test(argc, argv);
test.maxscale->connect_maxscale();
test.repl->connect();
test.tprintf("Testing column-wise binding with a direct connection");
test.add_result(bind_by_column(test.repl->nodes[0]), "Bulk inserts with a direct connection should work");
test.tprintf("Testing column-wise binding with readwritesplit");
test.add_result(bind_by_column(test.maxscale->conn_rwsplit),
"Bulk inserts with readwritesplit should work");
test.tprintf("Testing column-wise binding with readconnroute");
test.add_result(bind_by_column(test.maxscale->conn_master),
"Bulk inserts with readconnroute should work");
test.tprintf("Testing row-wise binding with a direct connection");
test.add_result(bind_by_row(test.repl->nodes[0]), "Bulk inserts with a direct connection should work");
test.tprintf("Testing row-wise binding with readwritesplit");
test.add_result(bind_by_row(test.maxscale->conn_rwsplit),
"Bulk inserts with readwritesplit should work");
test.tprintf("Testing row-wise binding with readconnroute");
test.add_result(bind_by_row(test.maxscale->conn_master),
"Bulk inserts with readconnroute should work");
test.maxscale->close_maxscale_connections();
test.log_printf(
"MXS-5106: One stopped node cause the protocol to downgrade to the lowest supported version.");
test.repl->block_node(3);
test.maxscale->restart();
test.maxscale->wait_for_monitor();
test.maxscale->connect_maxscale();
test.expect(bind_by_column(test.maxscale->conn_rwsplit) == 0,
"Bulk inserts with readwritesplit should still work");
test.expect(bind_by_column(test.maxscale->conn_master) == 0,
"Bulk inserts with readconnroute should still work");
test.repl->unblock_node(3);
test.maxscale->close_maxscale_connections();
return test.global_result;
}