1 | /* |
2 | */ |
3 | |
4 | #include "my_test.h" |
5 | |
6 | static int execute_direct(MYSQL *mysql) |
7 | { |
8 | int rc= 0; |
9 | long i= 0; |
10 | MYSQL_STMT *stmt; |
11 | MYSQL_BIND bind; |
12 | unsigned int param_count= 1; |
13 | MYSQL_RES *res= NULL; |
14 | |
15 | stmt= mysql_stmt_init(mysql); |
16 | |
17 | rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS t1" , -1); |
18 | check_stmt_rc(rc, stmt); |
19 | |
20 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1" , -1); |
21 | check_stmt_rc(rc, stmt); |
22 | |
23 | while (!mysql_stmt_fetch(stmt)); |
24 | |
25 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1" , -1); |
26 | check_stmt_rc(rc, stmt); |
27 | |
28 | rc= mariadb_stmt_execute_direct(stmt, "CREATE TABLE t1 (a int)" , -1); |
29 | check_stmt_rc(rc, stmt); |
30 | |
31 | memset(&bind, 0, sizeof(MYSQL_BIND)); |
32 | |
33 | bind.buffer= &i; |
34 | bind.buffer_type= MYSQL_TYPE_LONG; |
35 | bind.buffer_length= sizeof(long); |
36 | |
37 | mysql_stmt_close(stmt); |
38 | stmt= mysql_stmt_init(mysql); |
39 | mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count); |
40 | |
41 | rc= mysql_stmt_bind_param(stmt, &bind); |
42 | check_stmt_rc(rc, stmt); |
43 | rc= mariadb_stmt_execute_direct(stmt, "INSERT INTO t1 VALUES (?)" , -1); |
44 | check_stmt_rc(rc, stmt); |
45 | |
46 | for (i=1; i < 1000; i++) |
47 | { |
48 | rc= mysql_stmt_execute(stmt); |
49 | check_stmt_rc(rc, stmt); |
50 | } |
51 | rc= mysql_stmt_close(stmt); |
52 | check_mysql_rc(rc, mysql); |
53 | |
54 | rc= mysql_query(mysql, "SELECT * FROM t1" ); |
55 | check_mysql_rc(rc, mysql); |
56 | |
57 | res= mysql_store_result(mysql); |
58 | FAIL_IF(mysql_num_rows(res) != 1000, "Expected 1000 rows" ); |
59 | |
60 | mysql_free_result(res); |
61 | |
62 | rc= mysql_query(mysql, "DROP TABLE t1" ); |
63 | check_mysql_rc(rc, mysql); |
64 | |
65 | return OK; |
66 | } |
67 | |
68 | static int execute_direct_example(MYSQL *mysql) |
69 | { |
70 | MYSQL_STMT *stmt= mysql_stmt_init(mysql); |
71 | MYSQL_BIND bind[2]; |
72 | int intval= 1; |
73 | int param_count= 2; |
74 | int rc; |
75 | const char *strval= "execute_direct_example1" ; |
76 | |
77 | /* Direct execution without parameters */ |
78 | rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS execute_direct" , -1); |
79 | check_stmt_rc(rc, stmt); |
80 | rc= mariadb_stmt_execute_direct(stmt, "CREATE TABLE execute_direct (a int, b varchar(20))" , -1); |
81 | rc= mysql_stmt_close(stmt); |
82 | stmt= mysql_stmt_init(mysql); |
83 | check_stmt_rc(rc, stmt); |
84 | memset(bind, 0, sizeof(MYSQL_BIND) * 2); |
85 | bind[0].buffer_type= MYSQL_TYPE_SHORT; |
86 | bind[0].buffer= &intval; |
87 | bind[1].buffer_type= MYSQL_TYPE_STRING; |
88 | bind[1].buffer= (char *)strval; |
89 | bind[1].buffer_length= (unsigned long)strlen(strval); |
90 | |
91 | /* set number of parameters */ |
92 | rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count); |
93 | check_stmt_rc(rc, stmt); |
94 | |
95 | /* bind parameters */ |
96 | rc= mysql_stmt_bind_param(stmt, bind); |
97 | check_stmt_rc(rc, stmt); |
98 | |
99 | rc= mariadb_stmt_execute_direct(stmt, "INSERT INTO execute_direct VALUES (?,?)" , -1); |
100 | check_stmt_rc(rc, stmt); |
101 | |
102 | mysql_stmt_close(stmt); |
103 | |
104 | rc= mysql_query(mysql, "DROP TABLE execute_direct" ); |
105 | check_mysql_rc(rc, mysql); |
106 | return OK; |
107 | } |
108 | |
109 | static int conc_213(MYSQL *mysql) |
110 | { |
111 | MYSQL_BIND bind; |
112 | unsigned int param_count= 1; |
113 | long id= 1234; |
114 | MYSQL_STMT *stmt; |
115 | |
116 | stmt = mysql_stmt_init(mysql); |
117 | |
118 | memset(&bind, '\0', sizeof(bind)); |
119 | |
120 | bind.buffer_type = MYSQL_TYPE_LONG; |
121 | bind.buffer = (void *)&id; |
122 | bind.buffer_length = sizeof(long); |
123 | /* bind.is_null = &is_null; |
124 | bind.length = &length; |
125 | bind.error = &error; */ |
126 | |
127 | mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count); |
128 | check_stmt_rc(mysql_stmt_bind_param(stmt, &bind), stmt); |
129 | check_stmt_rc(mariadb_stmt_execute_direct(stmt, "SELECT ?" , -1), stmt); |
130 | check_stmt_rc(mysql_stmt_store_result(stmt), stmt); |
131 | check_stmt_rc(mysql_stmt_free_result(stmt), stmt); |
132 | |
133 | mysql_stmt_close(stmt); |
134 | |
135 | return OK; |
136 | } |
137 | |
138 | static int conc_212(MYSQL *mysql) |
139 | { |
140 | MYSQL_STMT *stmt= mysql_stmt_init(mysql); |
141 | int rc; |
142 | |
143 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1, 2" , -1); |
144 | check_stmt_rc(rc, stmt); |
145 | mysql_stmt_store_result(stmt); |
146 | mysql_stmt_free_result(stmt); |
147 | |
148 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1, 2" , -1); |
149 | check_stmt_rc(rc, stmt); |
150 | mysql_stmt_store_result(stmt); |
151 | mysql_stmt_free_result(stmt); |
152 | |
153 | rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1" ); |
154 | check_mysql_rc(rc,mysql); |
155 | |
156 | |
157 | rc= mysql_stmt_close(stmt); |
158 | |
159 | return OK; |
160 | } |
161 | |
162 | static int conc_218(MYSQL *mysql) |
163 | { |
164 | MYSQL_STMT *stmt= mysql_stmt_init(mysql); |
165 | MYSQL_BIND bind[2]; |
166 | int id=1; |
167 | my_bool is_null= 0, error= 0; |
168 | unsigned int param_count= 1; |
169 | |
170 | memset(bind, 0, 2 * sizeof(MYSQL_BIND)); |
171 | bind[0].buffer_type = MYSQL_TYPE_LONG; |
172 | bind[0].buffer = (void *)&id; |
173 | bind[0].buffer_length = 4; |
174 | bind[0].is_null = &is_null; |
175 | bind[0].error = &error; |
176 | |
177 | mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count); |
178 | check_stmt_rc(mysql_stmt_bind_param(stmt, bind), stmt); |
179 | check_stmt_rc(mariadb_stmt_execute_direct(stmt, "SELECT ?" , -1), stmt); |
180 | check_stmt_rc(mysql_stmt_store_result(stmt), stmt); |
181 | |
182 | check_stmt_rc(mysql_stmt_free_result(stmt), stmt); |
183 | |
184 | param_count= 1; |
185 | mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, ¶m_count); |
186 | check_stmt_rc(mysql_stmt_bind_param(stmt, bind), stmt); |
187 | check_stmt_rc(mariadb_stmt_execute_direct(stmt, "SELECT ?" , -1), stmt); |
188 | mysql_stmt_close(stmt); |
189 | |
190 | return OK; |
191 | } |
192 | |
193 | static int test_cursor(MYSQL *mysql) |
194 | { |
195 | int rc; |
196 | MYSQL_STMT *stmt; |
197 | unsigned long prefetch_rows= 1; |
198 | unsigned long cursor_type= CURSOR_TYPE_READ_ONLY; |
199 | |
200 | stmt= mysql_stmt_init(mysql); |
201 | rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type); |
202 | check_stmt_rc(rc, stmt); |
203 | rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS, &prefetch_rows); |
204 | check_stmt_rc(rc, stmt); |
205 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1 FROM DUAL UNION SELECT 2 FROM DUAL" , -1); |
206 | check_stmt_rc(rc, stmt); |
207 | rc= mysql_stmt_fetch(stmt); |
208 | check_stmt_rc(rc, stmt); |
209 | rc= mariadb_stmt_execute_direct(stmt, "SELECT 1 FROM DUAL UNION SELECT 2 FROM DUAL" , -1); |
210 | check_stmt_rc(rc, stmt); |
211 | mysql_stmt_close(stmt); |
212 | return OK; |
213 | } |
214 | |
215 | |
216 | struct my_tests_st my_tests[] = { |
217 | {"test_cursor" , test_cursor, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
218 | {"conc_218" , conc_218, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
219 | {"conc_212" , conc_212, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
220 | {"conc_213" , conc_213, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
221 | {"execute_direct" , execute_direct, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
222 | {"execute_direct_example" , execute_direct_example, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, |
223 | {NULL, NULL, 0, 0, NULL, NULL} |
224 | }; |
225 | |
226 | |
227 | int main(int argc, char **argv) |
228 | { |
229 | |
230 | mysql_library_init(0,0,NULL); |
231 | |
232 | if (argc > 1) |
233 | get_options(argc, argv); |
234 | |
235 | get_envvars(); |
236 | |
237 | run_tests(my_tests); |
238 | |
239 | mysql_server_end(); |
240 | return(exit_status()); |
241 | } |
242 | |