summaryrefslogtreecommitdiff
path: root/sql/time.cc
diff options
context:
space:
mode:
authorunknown <konstantin@mysql.com>2004-05-25 02:03:49 +0400
committerunknown <konstantin@mysql.com>2004-05-25 02:03:49 +0400
commit093d62922b326cec1a05bd8baba2d9ed96137488 (patch)
tree39de3845fc8340f9a27f30803b9104e416c7ab62 /sql/time.cc
parent88da3ae5f9e69a3bf6ced379908d01da5b3bfbb0 (diff)
downloadmariadb-git-093d62922b326cec1a05bd8baba2d9ed96137488.tar.gz
Support for character set conversion in binary protocol: another go
after Monty's review. - Item_param was rewritten. - it turns out that we can't convert string data to character set of connection on the fly, because they first should be written to the binary log. To support efficient conversion we need to rewrite prepared statements binlogging code first. include/my_global.h: Macro swap(a, b, c) was renamed to resolve name conflict with String::swap() method. include/my_sys.h: Added declaration of escape_string_for_mysql() include/mysql_com.h: Removed and moved back: a macro which is visible to libmysql user but has sence only in prepared statement protocol implementation. isam/_search.c: swap -> swap_variables isam/test2.c: swap -> swap_variables libmysql/libmysql.c: - sub_escape_string moved to mysys/charset.c to be visible in sql/ - few cleanups myisam/mi_test2.c: swap -> swap_variables mysys/charset.c: sub_escape_string was moved from libmysql.c to be able to use it in sql/ code. mysys/my_chsize.c: rename: swap -> swap_variables mysys/my_compress.c: swap -> swap_variables mysys/my_handler.c: swap -> swap_variables sql/field.cc: Field::store_time refactored to use TIME_to_string function from time.cc sql/item.cc: New implementation of Item_param class: added support for character sets conversion. sql/item.h: Item_param: - 'state' member introduced instead of many boolean variables. - put ltime, int_value and real_value into union to save space. - remove unimplemented members - set_value renamed to set_str sql/item_timefunc.cc: Refactored to use functions from time.cc sql/lock.cc: rename: swap -> swap_variables sql/mysql_priv.h: - added declarations for TIME_to_ulonglong_*, TIME_to_string functions - const specifiers for make_date, make_time, make_datetime arguments sql/opt_range.cc: rename: swap -> swap_variables sql/protocol.cc: - added character set conversion support to binary protocol. - Protocol::convert changed to point at shared buffer in THD. This lets us use one convert buffer for binary and simple protocol. The same buffer is used for client->server conversions in prepared statements code. - string conversion code refactored to Protocol::store_string_aux function. - few more comments sql/protocol.h: - Protocol::convert now points at THD::convert_buffer: we want to share one buffer between all protocol implementations. sql/sql_class.cc: - implementation of THD::convert_string using THD::convert_buffer (conversion of strings allocated in the system heap). sql/sql_class.h: - THD::convert_buffer is shared between THD and network Protocols and used for character set conversion of strings. - new function to convert String object from one charset to another using THD::convert_buffer sql/sql_insert.cc: A little fix in a comment. sql/sql_parse.cc: Shrink convert buffer in the end of each statement. sql/sql_prepare.cc: Many changes: - static specifier for set_param_* family of functions. - FIELD_TYPE -> MYSQL_TYPE - added set_param_binary as handler for BLOB types. - added character set support - added support for param typecode in mysql_stmt_get_longdata (mysql_stmt_send_long_data handler) - changes in Item_param deployed - few cleanups sql/sql_select.cc: rename: swap -> swap_variables sql/sql_string.cc: - String::append rewritten to support character set conversion for single-byte encodings. - added String::swap method to efficiently exchange two string objects. sql/sql_string.h: Declraration for String::swap(). sql/time.cc: - function TIME_to_string to convert TIME to String in default MySQL format - family of functions TIME_to_ulonglong_* tests/client_test.c: Test for support for character set conversions in prepared statements (binary and text data).
Diffstat (limited to 'sql/time.cc')
-rw-r--r--sql/time.cc122
1 files changed, 119 insertions, 3 deletions
diff --git a/sql/time.cc b/sql/time.cc
index db05d606292..6d15fa184a1 100644
--- a/sql/time.cc
+++ b/sql/time.cc
@@ -1255,9 +1255,15 @@ const char *get_date_time_format_str(KNOWN_DATE_TIME_FORMAT *format,
MySQL doesn't support comparing of date/time/datetime strings that
are not in arbutary order as dates are compared as strings in some
context)
+ This functions don't check that given TIME structure members are
+ in valid range. If they are not, return value won't reflect any
+ valid date either. Additionally, make_time doesn't take into
+ account time->day member: it's assumed that days have been converted
+ to hours already.
****************************************************************************/
-void make_time(DATE_TIME_FORMAT *format, TIME *l_time, String *str)
+void make_time(const DATE_TIME_FORMAT *format __attribute__((unused)),
+ const TIME *l_time, String *str)
{
long length= my_sprintf((char*) str->ptr(),
((char*) str->ptr(),
@@ -1271,7 +1277,8 @@ void make_time(DATE_TIME_FORMAT *format, TIME *l_time, String *str)
}
-void make_date(DATE_TIME_FORMAT *format, TIME *l_time, String *str)
+void make_date(const DATE_TIME_FORMAT *format __attribute__((unused)),
+ const TIME *l_time, String *str)
{
long length= my_sprintf((char*) str->ptr(),
((char*) str->ptr(),
@@ -1284,7 +1291,8 @@ void make_date(DATE_TIME_FORMAT *format, TIME *l_time, String *str)
}
-void make_datetime(DATE_TIME_FORMAT *format, TIME *l_time, String *str)
+void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)),
+ const TIME *l_time, String *str)
{
long length= my_sprintf((char*) str->ptr(),
((char*) str->ptr(),
@@ -1330,3 +1338,111 @@ void make_truncated_value_warning(THD *thd, const char *str_val,
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
ER_TRUNCATED_WRONG_VALUE, warn_buff);
}
+
+
+/* Convert time value to integer in YYYYMMDDHHMMSS format */
+
+ulonglong TIME_to_ulonglong_datetime(const TIME *time)
+{
+ return ((ulonglong) (time->year * 10000UL +
+ time->month * 100UL +
+ time->day) * ULL(1000000) +
+ (ulonglong) (time->hour * 10000UL +
+ time->minute * 100UL +
+ time->second));
+}
+
+
+/* Convert TIME value to integer in YYYYMMDD format */
+
+ulonglong TIME_to_ulonglong_date(const TIME *time)
+{
+ return (ulonglong) (time->year * 10000UL + time->month * 100UL + time->day);
+}
+
+
+/*
+ Convert TIME value to integer in HHMMSS format.
+ This function doesn't take into account time->day member:
+ it's assumed that days have been converted to hours already.
+*/
+
+ulonglong TIME_to_ulonglong_time(const TIME *time)
+{
+ return (ulonglong) (time->hour * 10000UL +
+ time->minute * 100UL +
+ time->second);
+}
+
+
+/*
+ Convert struct TIME (date and time split into year/month/day/hour/...
+ to a number in format YYYYMMDDHHMMSS (DATETIME),
+ YYYYMMDD (DATE) or HHMMSS (TIME).
+
+ SYNOPSIS
+ TIME_to_ulonglong()
+
+ DESCRIPTION
+ The function is used when we need to convert value of time item
+ to a number if it's used in numeric context, i. e.:
+ SELECT NOW()+1, CURDATE()+0, CURTIMIE()+0;
+ SELECT ?+1;
+
+ NOTE
+ This function doesn't check that given TIME structure members are
+ in valid range. If they are not, return value won't reflect any
+ valid date either.
+*/
+
+ulonglong TIME_to_ulonglong(const TIME *time)
+{
+ switch (time->time_type) {
+ case TIMESTAMP_DATETIME:
+ return TIME_to_ulonglong_datetime(time);
+ case TIMESTAMP_DATE:
+ return TIME_to_ulonglong_date(time);
+ case TIMESTAMP_TIME:
+ return TIME_to_ulonglong_time(time);
+ case TIMESTAMP_NONE:
+ case TIMESTAMP_DATETIME_ERROR:
+ return ULL(0);
+ default:
+ DBUG_ASSERT(0);
+ }
+ return 0;
+}
+
+
+/*
+ Convert struct DATE/TIME/DATETIME value to string using built-in
+ MySQL time conversion formats.
+
+ SYNOPSIS
+ TIME_to_string()
+
+ NOTE
+ The string must have at least MAX_DATE_REP_LENGTH bytes reserved.
+*/
+
+void TIME_to_string(const TIME *time, String *str)
+{
+ switch (time->time_type) {
+ case TIMESTAMP_DATETIME:
+ make_datetime((DATE_TIME_FORMAT*) 0, time, str);
+ break;
+ case TIMESTAMP_DATE:
+ make_date((DATE_TIME_FORMAT*) 0, time, str);
+ break;
+ case TIMESTAMP_TIME:
+ make_time((DATE_TIME_FORMAT*) 0, time, str);
+ break;
+ case TIMESTAMP_NONE:
+ case TIMESTAMP_DATETIME_ERROR:
+ str->length(0);
+ str->set_charset(&my_charset_bin);
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+}