diff --git a/include/mysql.h b/include/mysql.h index cb7b4629ec0..58c314207c1 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -145,7 +145,8 @@ enum mysql_option MYSQL_OPT_PROTOCOL, MYSQL_SHARED_MEMORY_BASE_NAME, MYSQL_OPT_READ_TIMEOUT, MYSQL_OPT_WRITE_TIMEOUT, MYSQL_OPT_USE_RESULT, MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION, - MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH + MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH, + MYSQL_REPORT_DATA_TRUNCATION }; struct st_mysql_options { @@ -186,6 +187,8 @@ struct st_mysql_options { char *client_ip; /* Refuse client connecting to server if it uses old (pre-4.1.1) protocol */ my_bool secure_auth; + /* 0 - never report, 1 - always report (default) */ + my_bool report_data_truncation; /* function pointers for local infile support */ int (*local_infile_init)(void **, const char *, void *); diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c index 7d078a179ed..ce8a5aaaaab 100644 --- a/libmysql/libmysql.c +++ b/libmysql/libmysql.c @@ -4342,7 +4342,6 @@ my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) MYSQL_FIELD *field; ulong bind_count= stmt->field_count; uint param_count= 0; - uchar report_data_truncation= 0; DBUG_ENTER("mysql_stmt_bind_result"); DBUG_PRINT("enter",("field_count: %d", bind_count)); @@ -4380,8 +4379,6 @@ my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) if (!param->error) param->error= ¶m->error_value; - else - report_data_truncation= REPORT_DATA_TRUNCATION; param->param_number= param_count++; param->offset= 0; @@ -4395,7 +4392,10 @@ my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) DBUG_RETURN(1); } } - stmt->bind_result_done= BIND_RESULT_DONE | report_data_truncation; + stmt->bind_result_done= BIND_RESULT_DONE; + if (stmt->mysql->options.report_data_truncation) + stmt->bind_result_done|= REPORT_DATA_TRUNCATION; + DBUG_RETURN(0); } diff --git a/sql-common/client.c b/sql-common/client.c index fd65ed01462..1a9bc4221a0 100644 --- a/sql-common/client.c +++ b/sql-common/client.c @@ -874,6 +874,7 @@ static const char *default_options[]= "replication-probe", "enable-reads-from-master", "repl-parse-query", "ssl-cipher", "max-allowed-packet", "protocol", "shared-memory-base-name", "multi-results", "multi-queries", "secure-auth", + "report-data-truncation", NullS }; @@ -1084,6 +1085,9 @@ void mysql_read_default_options(struct st_mysql_options *options, case 32: /* secure-auth */ options->secure_auth= TRUE; break; + case 33: /* report-data-truncation */ + options->report_data_truncation= opt_arg ? test(atoi(opt_arg)) : 1; + break; default: DBUG_PRINT("warning",("unknown option: %s",option[0])); } @@ -1427,6 +1431,7 @@ mysql_init(MYSQL *mysql) #endif mysql->options.methods_to_use= MYSQL_OPT_GUESS_CONNECTION; + mysql->options.report_data_truncation= TRUE; /* default */ return mysql; } @@ -2666,6 +2671,9 @@ mysql_options(MYSQL *mysql,enum mysql_option option, const char *arg) case MYSQL_SECURE_AUTH: mysql->options.secure_auth= *(my_bool *) arg; break; + case MYSQL_REPORT_DATA_TRUNCATION: + mysql->options.report_data_truncation= test(*(my_bool *) arg); + break; default: DBUG_RETURN(1); } diff --git a/tests/client_test.c b/tests/client_test.c index f524415eb1c..f2faf656ea6 100644 --- a/tests/client_test.c +++ b/tests/client_test.c @@ -12302,6 +12302,56 @@ static void test_truncation() myquery(rc); } +static void test_truncation_option() +{ + MYSQL_STMT *stmt; + const char *stmt_text; + int rc; + uint8 buf; + my_bool option= 0; + my_bool error; + MYSQL_BIND bind; + + myheader("test_truncation_option"); + + /* Prepare the test table */ + stmt_text= "select -1"; + + stmt= mysql_stmt_init(mysql); + rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text)); + check_execute(stmt, rc); + rc= mysql_stmt_execute(stmt); + check_execute(stmt, rc); + + bzero(&bind, sizeof(MYSQL_BIND)); + + bind.buffer= (void*) &buf; + bind.buffer_type= MYSQL_TYPE_TINY; + bind.is_unsigned= TRUE; + bind.error= &error; + + rc= mysql_stmt_bind_result(stmt, &bind); + check_execute(stmt, rc); + rc= mysql_stmt_fetch(stmt); + DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED); + DIE_UNLESS(error); + rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option); + myquery(rc); + /* need to rebind for the new setting to take effect */ + rc= mysql_stmt_bind_result(stmt, &bind); + check_execute(stmt, rc); + rc= mysql_stmt_execute(stmt); + check_execute(stmt, rc); + rc= mysql_stmt_fetch(stmt); + check_execute(stmt, rc); + /* The only change is rc - error pointers are still filled in */ + DIE_UNLESS(error == 1); + /* restore back the defaults */ + option= 1; + mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option); + + mysql_stmt_close(stmt); +} /* Read and parse arguments and MySQL options from my.cnf @@ -12517,6 +12567,7 @@ static struct my_tests_st my_tests[]= { { "test_basic_cursors", test_basic_cursors }, { "test_cursors_with_union", test_cursors_with_union }, { "test_truncation", test_truncation }, + { "test_truncation_option", test_truncation_option }, { 0, 0 } };