Commit 0aa9195a authored by konstantin@mysql.com's avatar konstantin@mysql.com

A fix and test case for bug#6058 "Prepared statements return '0000-00-00'

(date) as empty  string": preserve time type (date, time, or datetime) for
zero dates, times, and datetimes.
parent 33fb5ab6
...@@ -3221,12 +3221,12 @@ static void read_binary_time(MYSQL_TIME *tm, uchar **pos) ...@@ -3221,12 +3221,12 @@ static void read_binary_time(MYSQL_TIME *tm, uchar **pos)
tm->second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0; tm->second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0;
tm->year= tm->month= 0; tm->year= tm->month= 0;
tm->time_type= MYSQL_TIMESTAMP_TIME;
*pos+= length; *pos+= length;
} }
else else
set_zero_time(tm); set_zero_time(tm);
tm->time_type= MYSQL_TIMESTAMP_TIME;
} }
static void read_binary_datetime(MYSQL_TIME *tm, uchar **pos) static void read_binary_datetime(MYSQL_TIME *tm, uchar **pos)
...@@ -3251,12 +3251,12 @@ static void read_binary_datetime(MYSQL_TIME *tm, uchar **pos) ...@@ -3251,12 +3251,12 @@ static void read_binary_datetime(MYSQL_TIME *tm, uchar **pos)
else else
tm->hour= tm->minute= tm->second= 0; tm->hour= tm->minute= tm->second= 0;
tm->second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0; tm->second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0;
tm->time_type= MYSQL_TIMESTAMP_DATETIME;
*pos+= length; *pos+= length;
} }
else else
set_zero_time(tm); set_zero_time(tm);
tm->time_type= MYSQL_TIMESTAMP_DATETIME;
} }
static void read_binary_date(MYSQL_TIME *tm, uchar **pos) static void read_binary_date(MYSQL_TIME *tm, uchar **pos)
...@@ -3273,12 +3273,12 @@ static void read_binary_date(MYSQL_TIME *tm, uchar **pos) ...@@ -3273,12 +3273,12 @@ static void read_binary_date(MYSQL_TIME *tm, uchar **pos)
tm->hour= tm->minute= tm->second= 0; tm->hour= tm->minute= tm->second= 0;
tm->second_part= 0; tm->second_part= 0;
tm->neg= 0; tm->neg= 0;
tm->time_type= MYSQL_TIMESTAMP_DATE;
*pos+= length; *pos+= length;
} }
else else
set_zero_time(tm); set_zero_time(tm);
tm->time_type= MYSQL_TIMESTAMP_DATE;
} }
......
...@@ -3439,7 +3439,7 @@ static void test_fetch_date() ...@@ -3439,7 +3439,7 @@ static void test_fetch_date()
MYSQL_STMT *stmt; MYSQL_STMT *stmt;
uint i; uint i;
int rc, year; int rc, year;
char date[25], time[25], ts[25], ts_4[15], ts_6[20], dt[20]; char date[25], time[25], ts[25], ts_4[25], ts_6[20], dt[20];
ulong d_length, t_length, ts_length, ts4_length, ts6_length, ulong d_length, t_length, ts_length, ts4_length, ts6_length,
dt_length, y_length; dt_length, y_length;
MYSQL_BIND bind[8]; MYSQL_BIND bind[8];
...@@ -3549,8 +3549,8 @@ static void test_fetch_date() ...@@ -3549,8 +3549,8 @@ static void test_fetch_date()
DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0); DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
DIE_UNLESS(dt_length == 19); DIE_UNLESS(dt_length == 19);
DIE_UNLESS(ts_4[0] == '\0'); DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
DIE_UNLESS(ts4_length == 0); DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0); DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
DIE_UNLESS(ts6_length == 19); DIE_UNLESS(ts6_length == 19);
...@@ -10548,7 +10548,7 @@ static void test_bug6049() ...@@ -10548,7 +10548,7 @@ static void test_bug6049()
MYSQL_RES *res; MYSQL_RES *res;
MYSQL_ROW row; MYSQL_ROW row;
const char *stmt_text; const char *stmt_text;
char *buffer[30]; char buffer[30];
ulong length; ulong length;
int rc; int rc;
...@@ -10577,6 +10577,52 @@ static void test_bug6049() ...@@ -10577,6 +10577,52 @@ static void test_bug6049()
rc= mysql_stmt_fetch(stmt); rc= mysql_stmt_fetch(stmt);
DIE_UNLESS(rc == 0); DIE_UNLESS(rc == 0);
printf("Result from query: %s\n", row[0]);
printf("Result from prepared statement: %s\n", (char*) buffer);
DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
mysql_free_result(res);
mysql_stmt_close(stmt);
}
static void test_bug6058()
{
MYSQL_STMT *stmt;
MYSQL_BIND bind[1];
MYSQL_RES *res;
MYSQL_ROW row;
const char *stmt_text;
char buffer[30];
ulong length;
int rc;
myheader("test_bug6058");
stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
myquery(rc);
res= mysql_store_result(mysql);
row= mysql_fetch_row(res);
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(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = &buffer;
bind[0].buffer_length = sizeof(buffer);
bind[0].length = &length;
mysql_stmt_bind_result(stmt, bind);
rc= mysql_stmt_fetch(stmt);
DIE_UNLESS(rc == 0);
printf("Result from query: %s\n", row[0]); printf("Result from query: %s\n", row[0]);
printf("Result from prepared statement: %s\n", buffer); printf("Result from prepared statement: %s\n", buffer);
...@@ -10587,6 +10633,7 @@ static void test_bug6049() ...@@ -10587,6 +10633,7 @@ static void test_bug6049()
} }
/* /*
Read and parse arguments and MySQL options from my.cnf Read and parse arguments and MySQL options from my.cnf
*/ */
...@@ -10898,6 +10945,7 @@ int main(int argc, char **argv) ...@@ -10898,6 +10945,7 @@ int main(int argc, char **argv)
test_bug5315(); /* check that mysql_change_user closes all test_bug5315(); /* check that mysql_change_user closes all
prepared statements */ prepared statements */
test_bug6049(); /* check support for negative TIME values */ test_bug6049(); /* check support for negative TIME values */
test_bug6058(); /* check support for 0000-00-00 dates */
/* /*
XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH. DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment