| 1 | /* |
| 2 | Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
| 3 | |
| 4 | The MySQL Connector/C is licensed under the terms of the GPLv2 |
| 5 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most |
| 6 | MySQL Connectors. There are special exceptions to the terms and |
| 7 | conditions of the GPLv2 as it is applied to this software, see the |
| 8 | FLOSS License Exception |
| 9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published |
| 13 | by the Free Software Foundation; version 2 of the License. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but |
| 16 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 17 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License along |
| 21 | with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | #include "my_test.h" |
| 25 | |
| 26 | #ifdef ENABLE_IF_IN_USE |
| 27 | static int enable_general_log(MYSQL *mysql, int truncate) |
| 28 | { |
| 29 | int rc; |
| 30 | |
| 31 | rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log" ); |
| 32 | check_mysql_rc(rc, mysql); |
| 33 | |
| 34 | rc= mysql_query(mysql, "set @@global.general_log=on" ); |
| 35 | check_mysql_rc(rc, mysql); |
| 36 | |
| 37 | if (truncate) |
| 38 | { |
| 39 | rc= mysql_query(mysql, "truncate mysql.general_log" ); |
| 40 | check_mysql_rc(rc, mysql); |
| 41 | } |
| 42 | |
| 43 | return OK; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | static int restore_general_log(MYSQL *mysql) |
| 48 | { |
| 49 | int rc; |
| 50 | rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log" ); |
| 51 | check_mysql_rc(rc, mysql); |
| 52 | |
| 53 | return OK; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | /* Test update/binary logs */ |
| 58 | |
| 59 | static int test_logs(MYSQL *mysql) |
| 60 | { |
| 61 | MYSQL_STMT *stmt; |
| 62 | MYSQL_BIND my_bind[2]; |
| 63 | char data[255]; |
| 64 | size_t length; |
| 65 | int rc; |
| 66 | short id; |
| 67 | |
| 68 | rc= mysql_query(mysql, "SET session sql_mode=''" ); |
| 69 | check_mysql_rc(rc, mysql); |
| 70 | |
| 71 | rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs" ); |
| 72 | check_mysql_rc(rc, mysql); |
| 73 | |
| 74 | rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))" ); |
| 75 | check_mysql_rc(rc, mysql); |
| 76 | |
| 77 | strcpy((char *)data, "INSERT INTO test_logs VALUES(?, ?)" ); |
| 78 | stmt= mysql_stmt_init(mysql); |
| 79 | FAIL_IF(!stmt, mysql_error(mysql)); |
| 80 | |
| 81 | rc= mysql_stmt_prepare(stmt, SL(data)); |
| 82 | check_stmt_rc(rc, stmt); |
| 83 | |
| 84 | memset(my_bind, '\0', sizeof(my_bind)); |
| 85 | |
| 86 | my_bind[0].buffer_type= MYSQL_TYPE_SHORT; |
| 87 | my_bind[0].buffer= (void *)&id; |
| 88 | |
| 89 | my_bind[1].buffer_type= MYSQL_TYPE_STRING; |
| 90 | my_bind[1].buffer= (void *)&data; |
| 91 | my_bind[1].buffer_length= 255; |
| 92 | my_bind[1].length= (unsigned long *)&length; |
| 93 | |
| 94 | id= 9876; |
| 95 | strcpy((char *)data, "MySQL - Open Source Database" ); |
| 96 | length= strlen(data); |
| 97 | |
| 98 | rc= mysql_stmt_bind_param(stmt, my_bind); |
| 99 | check_stmt_rc(rc, stmt); |
| 100 | |
| 101 | rc= mysql_stmt_execute(stmt); |
| 102 | check_stmt_rc(rc, stmt); |
| 103 | |
| 104 | strcpy((char *)data, "'" ); |
| 105 | length= 1; |
| 106 | |
| 107 | rc= mysql_stmt_execute(stmt); |
| 108 | check_stmt_rc(rc, stmt); |
| 109 | |
| 110 | strcpy((char *)data, "\"" ); |
| 111 | length= 1; |
| 112 | |
| 113 | rc= mysql_stmt_execute(stmt); |
| 114 | check_stmt_rc(rc, stmt); |
| 115 | |
| 116 | strcpy((char *)data, "my\'sql\'" ); |
| 117 | length= strlen(data); |
| 118 | rc= mysql_stmt_execute(stmt); |
| 119 | check_stmt_rc(rc, stmt); |
| 120 | |
| 121 | strcpy((char *)data, "my\"sql\"" ); |
| 122 | length= strlen(data); |
| 123 | rc= mysql_stmt_execute(stmt); |
| 124 | check_stmt_rc(rc, stmt); |
| 125 | |
| 126 | mysql_stmt_close(stmt); |
| 127 | |
| 128 | strcpy((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')" ); |
| 129 | stmt= mysql_stmt_init(mysql); |
| 130 | FAIL_IF(!stmt, mysql_error(mysql)); |
| 131 | |
| 132 | rc= mysql_stmt_prepare(stmt, SL(data)); |
| 133 | check_stmt_rc(rc, stmt); |
| 134 | |
| 135 | rc= mysql_stmt_execute(stmt); |
| 136 | check_stmt_rc(rc, stmt); |
| 137 | |
| 138 | rc= mysql_stmt_execute(stmt); |
| 139 | check_stmt_rc(rc, stmt); |
| 140 | |
| 141 | mysql_stmt_close(stmt); |
| 142 | |
| 143 | strcpy((char *)data, "SELECT * FROM test_logs WHERE id=?" ); |
| 144 | stmt= mysql_stmt_init(mysql); |
| 145 | FAIL_IF(!stmt, mysql_error(mysql)); |
| 146 | |
| 147 | rc= mysql_stmt_prepare(stmt, SL(data)); |
| 148 | check_stmt_rc(rc, stmt); |
| 149 | |
| 150 | rc= mysql_stmt_bind_param(stmt, my_bind); |
| 151 | check_stmt_rc(rc, stmt); |
| 152 | |
| 153 | rc= mysql_stmt_execute(stmt); |
| 154 | check_stmt_rc(rc, stmt); |
| 155 | |
| 156 | my_bind[1].buffer_length= 255; |
| 157 | rc= mysql_stmt_bind_result(stmt, my_bind); |
| 158 | check_stmt_rc(rc, stmt); |
| 159 | |
| 160 | rc= mysql_stmt_fetch(stmt); |
| 161 | check_stmt_rc(rc, stmt); |
| 162 | |
| 163 | FAIL_UNLESS(id == 9876, "id != 9876" ); |
| 164 | FAIL_UNLESS(length == 19 || length == 20, "Invalid Length" ); /* Due to VARCHAR(20) */ |
| 165 | FAIL_UNLESS(strncmp(data, "MySQL - Open Source" , 19) == 0, "data != 'MySQL - Open Source'" ); |
| 166 | |
| 167 | rc= mysql_stmt_fetch(stmt); |
| 168 | check_stmt_rc(rc, stmt); |
| 169 | |
| 170 | FAIL_UNLESS(length == 1, "length != 1" ); |
| 171 | FAIL_UNLESS(strcmp(data, "'" ) == 0, "data != '''" ); |
| 172 | |
| 173 | rc= mysql_stmt_fetch(stmt); |
| 174 | check_stmt_rc(rc, stmt); |
| 175 | |
| 176 | FAIL_UNLESS(length == 1, "length != 1" ); |
| 177 | FAIL_UNLESS(strcmp(data, "\"" ) == 0, "data != '\"'" ); |
| 178 | |
| 179 | rc= mysql_stmt_fetch(stmt); |
| 180 | check_stmt_rc(rc, stmt); |
| 181 | |
| 182 | FAIL_UNLESS(length == 7, "length != 7" ); |
| 183 | FAIL_UNLESS(strcmp(data, "my\'sql\'" ) == 0, "data != my'sql'" ); |
| 184 | |
| 185 | rc= mysql_stmt_fetch(stmt); |
| 186 | check_stmt_rc(rc, stmt); |
| 187 | |
| 188 | FAIL_UNLESS(length == 7, "length != 7" ); |
| 189 | |
| 190 | rc= mysql_stmt_fetch(stmt); |
| 191 | FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA" ); |
| 192 | |
| 193 | mysql_stmt_close(stmt); |
| 194 | |
| 195 | rc= mysql_query(mysql, "DROP TABLE test_logs" ); |
| 196 | check_mysql_rc(rc, mysql); |
| 197 | |
| 198 | return OK; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | struct my_tests_st my_tests[] = { |
| 203 | {"test_logs" , test_logs, TEST_CONNECTION_DEFAULT, 0, NULL , NULL}, |
| 204 | {NULL, NULL, 0, 0, NULL, NULL} |
| 205 | }; |
| 206 | |
| 207 | int main(int argc, char **argv) |
| 208 | { |
| 209 | if (argc > 1) |
| 210 | get_options(argc, argv); |
| 211 | |
| 212 | get_envvars(); |
| 213 | |
| 214 | run_tests(my_tests); |
| 215 | |
| 216 | return(exit_status()); |
| 217 | } |
| 218 | |