1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-07 02:42:49 +03:00

Fix for CONC-168: string conversion of timestamps is broken

When converting datetime with microseconds to string (binary protocol) number of decimal
places was ignored. Thanks to Patrick Huesmann for providing a fix.
This commit is contained in:
Georg Richter
2016-03-24 07:12:54 +01:00
parent 68caac848a
commit b6d3af1bfc
3 changed files with 60 additions and 10 deletions

View File

@@ -847,21 +847,27 @@ void ps_fetch_datetime(MYSQL_BIND *r_param, const MYSQL_FIELD * field,
break;
case MYSQL_TYPE_TIME:
length= sprintf(dtbuffer, "%s%02u:%02u:%02u", (tm.neg ? "-" : ""), tm.hour, tm.minute, tm.second);
if (tm.second_part)
if (field->decimals)
{
char helper[16];
sprintf(helper, ".%%0%du", field->decimals);
length+= sprintf(dtbuffer + length, helper, tm.second_part);
char ms[8];
sprintf(ms, ".%06u", tm.second_part);
if (field->decimals < 6)
ms[field->decimals + 1]= 0;
length+= strlen(ms);
strcat(dtbuffer, ms);
}
break;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
length= sprintf(dtbuffer, "%04u-%02u-%02u %02u:%02u:%02u", tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
if (tm.second_part)
if (field->decimals)
{
char helper[16];
sprintf(helper, ".%%0%du", field->decimals);
length+= sprintf(dtbuffer + length, helper, tm.second_part);
char ms[8];
sprintf(ms, ".%06u", tm.second_part);
if (field->decimals < 6)
ms[field->decimals + 1]= 0;
length+= strlen(ms);
strcat(dtbuffer, ms);
}
break;
default:

View File

@@ -231,8 +231,6 @@ int mthd_stmt_read_all_rows(MYSQL_STMT *stmt)
}
}
}
current->length= packet_len;
result->rows++;
} else /* end of stream */

View File

@@ -4053,7 +4053,53 @@ static int test_conc155(MYSQL *mysql)
return OK;
}
static int test_conc168(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
MYSQL_BIND bind;
char buffer[100];
int rc;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE conc168(a datetime(3))");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO conc168 VALUES ('2016-03-09 07:51:49.000'),('2016-03-09 07:51:49.001'),('2016-03-09 07:51:49.010')");
check_mysql_rc(rc, mysql);
memset(&bind, 0, sizeof(MYSQL_BIND));
bind.buffer= buffer;
bind.buffer_type= MYSQL_TYPE_STRING;
bind.buffer_length= 100;
rc= mysql_stmt_prepare(stmt, "SELECT a FROM conc168", strlen("SELECT a FROM conc168"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_bind_result(stmt, &bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_fetch(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.000"), "expected: 2016-03-09 07:51:49.000");
rc= mysql_stmt_fetch(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.001"), "expected: 2016-03-09 07:51:49.001");
rc= mysql_stmt_fetch(stmt);
check_stmt_rc(rc, stmt);
FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.010"), "expected: 2016-03-09 07:51:49.010");
mysql_stmt_close(stmt);
return OK;
}
struct my_tests_st my_tests[] = {
{"test_conc168", test_conc168, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_conc155", test_conc155, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_conc154", test_conc154, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
{"test_conc141", test_conc141, TEST_CONNECTION_NEW, 0, NULL , NULL},