mirror of
https://github.com/MariaDB/server.git
synced 2025-01-20 22:12:30 +01:00
dca006dfd2
Cache". WL#1569 "Prepared Statements: implement support of Query Cache". Prepared SELECTs did not look up in the query cache, and their results were not stored in the query cache. This made them slower than non-prepared SELECTs in some cases. The fix is to re-use the expanded query (the prepared query where "?" placeholders are replaced by their values, at execution time) for searching/storing in the query cache. It works fine for statements prepared via mysql_stmt_prepare(), which are the most commonly used and were the scope of this bugfix and WL. It works less fine for statements prepared via the SQL command PREPARE...FROM, which are still not using the query cache if they have at least one parameter (because then the expanded query contains names of user variables, and user variables don't work with the query cache, even in non-prepared queries). Note that results from prepared SELECTs, which are in the binary protocol, and results from normal SELECTs, which are in the text protocol, ignore each other in the query cache, because a result in the binary protocol should never be served to a SELECT expecting the text protocol and vice-versa. Note, after this patch, bug 25843 starts applying to query cache ("changing default database between PREPARE and EXECUTE of statement breaks binlog"), we need to fix it. mysql-test/include/have_query_cache.inc: Now prepared statements work with the query cache, so don't disable prep stmts by default when doing a query cache test. All tests which include this file will now be really tested against prepared statements (in particular, query_cache.test). mysql-test/r/query_cache.result: result update mysql-test/t/grant_cache.test: Cannot enable this test in ps-protocol, because in normal protocol, a SELECT failing due to insufficient privileges increments Qcache_not_cached, while in ps-protocol, no. In detail: in normal protocol, the "access denied" errors on SELECT are issued at (stack trace): mysql_parse/mysql_execute_command/execute_sqlcom_select/handle_select/ mysql_select/JOIN::prepare/setup_wild/insert_fields/ check_grant_all_columns/my_error/my_message_sql, which then calls push_warning/query_cache_abort: at this moment, query_cache_store_query() has been called, so query exists in cache, so thd->net.query_cache_query!=NULL, so query_cache_abort() removes the query from cache, which causes a query_cache.refused++ (thus, a Qcache_not_cached++). While in ps-protocol, the error is issued at prepare time; for this mysql_test_select() is called, not execute_sqlcom_select() (and that also leads to JOIN::prepare/etc). Thus, as query_cache_store_query() has not been called, thd->net.query_cache_query==NULL, so query_cache_abort() does nothing: Qcache_not_cached is not incremented. As this test prints Qcache_not_cached after SELECT failures, we cannot enable this test in ps-protocol. mysql-test/t/ndb_cache_multi2.test: The principle of this test is: two mysqlds connected to one cluster, both using their query cache. Queries are cached in server1 ("select a!=3 from t1", "select * from t1"), table t1 is modified in server2, we want to see that this invalidates the query cache of server1. Invalidation with NDB works like this: when a query is found in the query cache, NDB is asked if the tables have changed. In this test, ha_ndbcluster calls NDB every millisecond to collect change information about tables. Due to this millisecond delay, there is need for a loop ("while...") in this test, which waits until a query1 ("select a!=3 from t1") is invalidated (which is equivalent to it returning up-to-date results), and then expects query2 ("select * from t1") to have been invalidated (see up-to-date results). But when enabling --ps-protocol in this test, the logic breaks, because query1 is still done via mysql_real_query() (see mysqltest.c: eval_expr() always uses mysql_real_query()). So, query1 returning up-to-date results is not a sign of it being invalidated in the cache, because it was NOT in the cache ("select a!=3 from t1" on line 39 was done with prep stmts, while `select a!=3 from t1` is not, thus the second does not see the first in the cache). Thus, we may run query2 when cache still has not been invalidated. The solution is to make the initial "select a!=3 from t1" run as a normal query, this repairs the broken logic. But note, "select * from t1" is still using prepared statements which was the goal of this test with --ps-protocol. mysql-test/t/query_cache.test: now that prepared statements work with the query cache, we check that results in binary protocol (prepared statements) and in text protocol (normal queries) don't mix in the query cache even though the text of the statement/query are identical. sql/mysql_priv.h: In class Query_cache_flags, we add a bit to say if the result is in binary or text format (because, a result in binary format should never be served to a query expecting text format, and vice- versa). A macro to emphasize that we read the size of the query cache without mutex ("maybe" word). A macro which gives a first indication of if a query is cache-able (first indication - it does not consider the query cache's state). sql/protocol.cc: indentation. sql/protocol.h: Children classes of Protocol report their type (currently, text or binary). Query cache needs to know that. sql/sql_cache.cc: When we store a result in the query cache, we need to remember if it's in binary or text format. And when we search for a result in the query cache, we need to select only those results which are in the format which the current statement expects (binary or text). sql/sql_prepare.cc: Enabling use of the query cache by prepared statements. 1) Prep stmts are of two types: a) prepared via the mysql_stmt_prepare() API call b) prepared via the SQL PREPARE...FROM statement. 2) We already, when executing a prepared statement, sometimes built an "expanded" statement. For a), "?" placeholders were replaced by their values. For b), by names of the user variables containing the values. We did that only when we needed to write the query to logs. We now use this expanded query also for storing/searching in the query cache. Assume a query "SELECT * FROM T WHERE c=?", and the parameter is 10. For a), the expanded query is "SELECT * FROM T WHERE c=10", we look for "SELECT * FROM T WHERE c=10" in the query cache, and store that query's result in the query cache. For b), the expanded query is "SELECT * FROM T WHERE c=@somevar", and user variables don't work with the query cache (even inside non- prepared queries), so we don't enable query caching for SQL PREPARE'd statements if they have at least one parameter (see "if (stmt->param_count > 0)" in the patch). 3) If query cache is enabled and this is a SELECT, we build the expanded query (as an optimisation, we don't want to build this expanded query if the query cache is disabled or this is not a SELECT). As the decision of building the expanded query or not is taken at prepare time (setup_set_params()), if query cache is disabled at prepare time, we won't build the expanded query at all next executions, thus shouldn't use this query for query cacheing. To ensure that we don't, we set safe_to_cache_query to FALSE. Note that we read the size of the query cache without mutex, which is ok: if we see it 0 but that cache has just been enlarged, no big deal, just our statement will not use the query cache; if we see it >0 but that cache has just been made destroyed, we'll build the expanded query at all executions, but query_cache_store_query() and query_cache_send_result_to_client() will read the size with a mutex and so properly decide to cache or not cache. 4) Some functions in this file were named "withlog", others "with_log", now using "with_log" for all. tests/mysql_client_test.c: Testing of how prepared statements enter and hit the query cache. test_ps_query_cache() is inspired from test_ps_conj_select(). It creates data, a prepared SELECT statement, executes it once, then a second time with the same parameter value, to see that cache is hit, then a 3rd time with another parameter value to see that cache is not hit. Then, same from another connection, expecting hits. Then, tests border cases (enables query cache at prepare and disables at execute and vice-versa). It checks all results of SELECTs, cache hits and misses. mysql-test/r/query_cache_sql_prepare.result: result of new test: we see hits when there is no parameter, no hit when there is a parameter. mysql-test/t/query_cache_sql_prepare.test: new test to see if SQL PREPARE'd statements enter/hit the query cache: - if having at least one parameter, they should not - if having zero parameters, they should.
1240 lines
33 KiB
C++
1240 lines
33 KiB
C++
/* Copyright (C) 2000-2003 MySQL AB
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
/*
|
|
Low level functions for storing data to be send to the MySQL client
|
|
The actual communction is handled by the net_xxx functions in net_serv.cc
|
|
*/
|
|
|
|
#ifdef USE_PRAGMA_IMPLEMENTATION
|
|
#pragma implementation // gcc: Class implementation
|
|
#endif
|
|
|
|
#include "mysql_priv.h"
|
|
#include "sp_rcontext.h"
|
|
#include <stdarg.h>
|
|
|
|
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
|
|
void net_send_error_packet(THD *thd, uint sql_errno, const char *err);
|
|
#ifndef EMBEDDED_LIBRARY
|
|
static void write_eof_packet(THD *thd, NET *net);
|
|
#endif
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
bool Protocol::net_store_data(const char *from, uint length)
|
|
#else
|
|
bool Protocol_binary::net_store_data(const char *from, uint length)
|
|
#endif
|
|
{
|
|
ulong packet_length=packet->length();
|
|
/*
|
|
The +9 comes from that strings of length longer than 16M require
|
|
9 bytes to be stored (see net_store_length).
|
|
*/
|
|
if (packet_length+9+length > packet->alloced_length() &&
|
|
packet->realloc(packet_length+9+length))
|
|
return 1;
|
|
char *to=(char*) net_store_length((char*) packet->ptr()+packet_length,
|
|
length);
|
|
memcpy(to,from,length);
|
|
packet->length((uint) (to+length-packet->ptr()));
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
Send a error string to client
|
|
|
|
Design note:
|
|
|
|
net_printf_error and net_send_error are low-level functions
|
|
that shall be used only when a new connection is being
|
|
established or at server startup.
|
|
For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
|
|
critical that every error that can be intercepted is issued in one
|
|
place only, my_message_sql.
|
|
*/
|
|
void net_send_error(THD *thd, uint sql_errno, const char *err)
|
|
{
|
|
NET *net= &thd->net;
|
|
bool generate_warning= thd->killed != THD::KILL_CONNECTION;
|
|
DBUG_ENTER("net_send_error");
|
|
DBUG_PRINT("enter",("sql_errno: %d err: %s", sql_errno,
|
|
err ? err : net->last_error[0] ?
|
|
net->last_error : "NULL"));
|
|
|
|
DBUG_ASSERT(!thd->spcont);
|
|
|
|
if (net && net->no_send_error)
|
|
{
|
|
thd->clear_error();
|
|
thd->is_fatal_error= 0; // Error message is given
|
|
DBUG_PRINT("info", ("sending error messages prohibited"));
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
thd->query_error= 1; // needed to catch query errors during replication
|
|
if (!err)
|
|
{
|
|
if (sql_errno)
|
|
err=ER(sql_errno);
|
|
else
|
|
{
|
|
if ((err=net->last_error)[0])
|
|
{
|
|
sql_errno=net->last_errno;
|
|
generate_warning= 0; // This warning has already been given
|
|
}
|
|
else
|
|
{
|
|
sql_errno=ER_UNKNOWN_ERROR;
|
|
err=ER(sql_errno); /* purecov: inspected */
|
|
}
|
|
}
|
|
}
|
|
|
|
if (generate_warning)
|
|
{
|
|
/* Error that we have not got with my_error() */
|
|
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, sql_errno, err);
|
|
}
|
|
|
|
net_send_error_packet(thd, sql_errno, err);
|
|
|
|
thd->is_fatal_error= 0; // Error message is given
|
|
thd->net.report_error= 0;
|
|
|
|
/* Abort multi-result sets */
|
|
thd->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
/*
|
|
Write error package and flush to client
|
|
It's a little too low level, but I don't want to use another buffer for
|
|
this
|
|
|
|
Design note:
|
|
|
|
net_printf_error and net_send_error are low-level functions
|
|
that shall be used only when a new connection is being
|
|
established or at server startup.
|
|
For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
|
|
critical that every error that can be intercepted is issued in one
|
|
place only, my_message_sql.
|
|
*/
|
|
|
|
void
|
|
net_printf_error(THD *thd, uint errcode, ...)
|
|
{
|
|
va_list args;
|
|
uint length,offset;
|
|
const char *format;
|
|
#ifndef EMBEDDED_LIBRARY
|
|
const char *text_pos;
|
|
int head_length= NET_HEADER_SIZE;
|
|
#else
|
|
char text_pos[1024];
|
|
#endif
|
|
NET *net= &thd->net;
|
|
|
|
DBUG_ENTER("net_printf_error");
|
|
DBUG_PRINT("enter",("message: %u",errcode));
|
|
|
|
DBUG_ASSERT(!thd->spcont);
|
|
|
|
if (net && net->no_send_error)
|
|
{
|
|
thd->clear_error();
|
|
thd->is_fatal_error= 0; // Error message is given
|
|
DBUG_PRINT("info", ("sending error messages prohibited"));
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
thd->query_error= 1; // needed to catch query errors during replication
|
|
#ifndef EMBEDDED_LIBRARY
|
|
query_cache_abort(net); // Safety
|
|
#endif
|
|
va_start(args,errcode);
|
|
/*
|
|
The following is needed to make net_printf_error() work with 0 argument
|
|
for errorcode and use the argument after that as the format string. This
|
|
is useful for rare errors that are not worth the hassle to put in
|
|
errmsg.sys, but at the same time, the message is not fixed text
|
|
*/
|
|
if (errcode)
|
|
format= ER(errcode);
|
|
else
|
|
{
|
|
format=va_arg(args,char*);
|
|
errcode= ER_UNKNOWN_ERROR;
|
|
}
|
|
offset= (net->return_errno ?
|
|
((thd->client_capabilities & CLIENT_PROTOCOL_41) ?
|
|
2+SQLSTATE_LENGTH+1 : 2) : 0);
|
|
#ifndef EMBEDDED_LIBRARY
|
|
text_pos=(char*) net->buff + head_length + offset + 1;
|
|
length= (uint) ((char*)net->buff_end - text_pos);
|
|
#else
|
|
length=sizeof(text_pos)-1;
|
|
#endif
|
|
length=my_vsnprintf(my_const_cast(char*) (text_pos),
|
|
min(length, sizeof(net->last_error)),
|
|
format,args);
|
|
va_end(args);
|
|
|
|
/* Replication slave relies on net->last_* to see if there was error */
|
|
net->last_errno= errcode;
|
|
strmake(net->last_error, text_pos, sizeof(net->last_error)-1);
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
if (net->vio == 0)
|
|
{
|
|
if (thd->bootstrap)
|
|
{
|
|
/*
|
|
In bootstrap it's ok to print on stderr
|
|
This may also happen when we get an error from a slave thread
|
|
*/
|
|
fprintf(stderr,"ERROR: %d %s\n",errcode,text_pos);
|
|
thd->fatal_error();
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
int3store(net->buff,length+1+offset);
|
|
net->buff[3]= (net->compress) ? 0 : (uchar) (net->pkt_nr++);
|
|
net->buff[head_length]=(uchar) 255; // Error package
|
|
if (offset)
|
|
{
|
|
uchar *pos= net->buff+head_length+1;
|
|
int2store(pos, errcode);
|
|
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
|
|
{
|
|
pos[2]= '#'; /* To make the protocol backward compatible */
|
|
memcpy(pos+3, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
|
|
}
|
|
}
|
|
VOID(net_real_write(net,(char*) net->buff,length+head_length+1+offset));
|
|
#else
|
|
net->last_errno= errcode;
|
|
strmake(net->last_error, text_pos, length);
|
|
strmake(net->sqlstate, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
|
|
#endif
|
|
if (thd->killed != THD::KILL_CONNECTION)
|
|
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, errcode,
|
|
text_pos ? text_pos : ER(errcode));
|
|
thd->is_fatal_error=0; // Error message is given
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
/*
|
|
Return ok to the client.
|
|
|
|
SYNOPSIS
|
|
send_ok()
|
|
thd Thread handler
|
|
affected_rows Number of rows changed by statement
|
|
id Auto_increment id for first row (if used)
|
|
message Message to send to the client (Used by mysql_status)
|
|
|
|
DESCRIPTION
|
|
The ok packet has the following structure
|
|
|
|
0 Marker (1 byte)
|
|
affected_rows Stored in 1-9 bytes
|
|
id Stored in 1-9 bytes
|
|
server_status Copy of thd->server_status; Can be used by client
|
|
to check if we are inside an transaction
|
|
New in 4.0 protocol
|
|
warning_count Stored in 2 bytes; New in 4.1 protocol
|
|
message Stored as packed length (1-9 bytes) + message
|
|
Is not stored if no message
|
|
|
|
If net->no_send_ok return without sending packet
|
|
*/
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
void
|
|
send_ok(THD *thd, ha_rows affected_rows, ulonglong id, const char *message)
|
|
{
|
|
NET *net= &thd->net;
|
|
char buff[MYSQL_ERRMSG_SIZE+10],*pos;
|
|
DBUG_ENTER("send_ok");
|
|
|
|
if (net->no_send_ok || !net->vio) // hack for re-parsing queries
|
|
{
|
|
DBUG_PRINT("info", ("no send ok: %s, vio present: %s",
|
|
(net->no_send_ok ? "YES" : "NO"),
|
|
(net->vio ? "YES" : "NO")));
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
buff[0]=0; // No fields
|
|
pos=net_store_length(buff+1,affected_rows);
|
|
pos=net_store_length(pos, id);
|
|
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
|
|
{
|
|
DBUG_PRINT("info",
|
|
("affected_rows: %lu id: %lu status: %u warning_count: %u",
|
|
(ulong) affected_rows,
|
|
(ulong) id,
|
|
(uint) (thd->server_status & 0xffff),
|
|
(uint) thd->total_warn_count));
|
|
int2store(pos,thd->server_status);
|
|
pos+=2;
|
|
|
|
/* We can only return up to 65535 warnings in two bytes */
|
|
uint tmp= min(thd->total_warn_count, 65535);
|
|
int2store(pos, tmp);
|
|
pos+= 2;
|
|
}
|
|
else if (net->return_status) // For 4.0 protocol
|
|
{
|
|
int2store(pos,thd->server_status);
|
|
pos+=2;
|
|
}
|
|
if (message)
|
|
pos=net_store_data((char*) pos, message, strlen(message));
|
|
VOID(my_net_write(net,buff,(uint) (pos-buff)));
|
|
VOID(net_flush(net));
|
|
/* We can't anymore send an error to the client */
|
|
thd->net.report_error= 0;
|
|
thd->net.no_send_error= 1;
|
|
DBUG_PRINT("info", ("OK sent, so no more error sending allowed"));
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
static char eof_buff[1]= { (char) 254 }; /* Marker for end of fields */
|
|
|
|
/*
|
|
Send eof (= end of result set) to the client
|
|
|
|
SYNOPSIS
|
|
send_eof()
|
|
thd Thread handler
|
|
no_flush Set to 1 if there will be more data to the client,
|
|
like in send_fields().
|
|
|
|
DESCRIPTION
|
|
The eof packet has the following structure
|
|
|
|
254 Marker (1 byte)
|
|
warning_count Stored in 2 bytes; New in 4.1 protocol
|
|
status_flag Stored in 2 bytes;
|
|
For flags like SERVER_MORE_RESULTS_EXISTS
|
|
|
|
Note that the warning count will not be sent if 'no_flush' is set as
|
|
we don't want to report the warning count until all data is sent to the
|
|
client.
|
|
*/
|
|
|
|
void
|
|
send_eof(THD *thd)
|
|
{
|
|
NET *net= &thd->net;
|
|
DBUG_ENTER("send_eof");
|
|
if (net->vio != 0 && !net->no_send_eof)
|
|
{
|
|
write_eof_packet(thd, net);
|
|
VOID(net_flush(net));
|
|
thd->net.no_send_error= 1;
|
|
DBUG_PRINT("info", ("EOF sent, so no more error sending allowed"));
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Format EOF packet according to the current protocol and
|
|
write it to the network output buffer.
|
|
*/
|
|
|
|
static void write_eof_packet(THD *thd, NET *net)
|
|
{
|
|
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
|
|
{
|
|
uchar buff[5];
|
|
/*
|
|
Don't send warn count during SP execution, as the warn_list
|
|
is cleared between substatements, and mysqltest gets confused
|
|
*/
|
|
uint tmp= (thd->spcont ? 0 : min(thd->total_warn_count, 65535));
|
|
buff[0]= 254;
|
|
int2store(buff+1, tmp);
|
|
/*
|
|
The following test should never be true, but it's better to do it
|
|
because if 'is_fatal_error' is set the server is not going to execute
|
|
other queries (see the if test in dispatch_command / COM_QUERY)
|
|
*/
|
|
if (thd->is_fatal_error)
|
|
thd->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
|
|
int2store(buff+3, thd->server_status);
|
|
VOID(my_net_write(net, (char*) buff, 5));
|
|
}
|
|
else
|
|
VOID(my_net_write(net, eof_buff, 1));
|
|
}
|
|
|
|
/*
|
|
Please client to send scrambled_password in old format.
|
|
SYNOPSYS
|
|
send_old_password_request()
|
|
thd thread handle
|
|
|
|
RETURN VALUE
|
|
0 ok
|
|
!0 error
|
|
*/
|
|
|
|
bool send_old_password_request(THD *thd)
|
|
{
|
|
NET *net= &thd->net;
|
|
return my_net_write(net, eof_buff, 1) || net_flush(net);
|
|
}
|
|
|
|
|
|
void net_send_error_packet(THD *thd, uint sql_errno, const char *err)
|
|
{
|
|
NET *net= &thd->net;
|
|
uint length;
|
|
char buff[MYSQL_ERRMSG_SIZE+2], *pos;
|
|
|
|
DBUG_ENTER("send_error_packet");
|
|
|
|
if (net->vio == 0)
|
|
{
|
|
if (thd->bootstrap)
|
|
{
|
|
/* In bootstrap it's ok to print on stderr */
|
|
fprintf(stderr,"ERROR: %d %s\n",sql_errno,err);
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
if (net->return_errno)
|
|
{ // new client code; Add errno before message
|
|
int2store(buff,sql_errno);
|
|
pos= buff+2;
|
|
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
|
|
{
|
|
/* The first # is to make the protocol backward compatible */
|
|
buff[2]= '#';
|
|
pos= strmov(buff+3, mysql_errno_to_sqlstate(sql_errno));
|
|
}
|
|
length= (uint) (strmake(pos, err, MYSQL_ERRMSG_SIZE-1) - buff);
|
|
err=buff;
|
|
}
|
|
else
|
|
{
|
|
length=(uint) strlen(err);
|
|
set_if_smaller(length,MYSQL_ERRMSG_SIZE-1);
|
|
}
|
|
VOID(net_write_command(net,(uchar) 255, "", 0, (char*) err,length));
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
#endif /* EMBEDDED_LIBRARY */
|
|
|
|
/*
|
|
Faster net_store_length when we know that length is less than 65536.
|
|
We keep a separate version for that range because it's widely used in
|
|
libmysql.
|
|
uint is used as agrument type because of MySQL type conventions:
|
|
uint for 0..65536
|
|
ulong for 0..4294967296
|
|
ulonglong for bigger numbers.
|
|
*/
|
|
|
|
static char *net_store_length_fast(char *pkg, uint length)
|
|
{
|
|
uchar *packet=(uchar*) pkg;
|
|
if (length < 251)
|
|
{
|
|
*packet=(uchar) length;
|
|
return (char*) packet+1;
|
|
}
|
|
*packet++=252;
|
|
int2store(packet,(uint) length);
|
|
return (char*) packet+2;
|
|
}
|
|
|
|
|
|
/****************************************************************************
|
|
Functions used by the protocol functions (like send_ok) to store strings
|
|
and numbers in the header result packet.
|
|
****************************************************************************/
|
|
|
|
/* The following will only be used for short strings < 65K */
|
|
|
|
char *net_store_data(char *to,const char *from, uint length)
|
|
{
|
|
to=net_store_length_fast(to,length);
|
|
memcpy(to,from,length);
|
|
return to+length;
|
|
}
|
|
|
|
char *net_store_data(char *to,int32 from)
|
|
{
|
|
char buff[20];
|
|
uint length=(uint) (int10_to_str(from,buff,10)-buff);
|
|
to=net_store_length_fast(to,length);
|
|
memcpy(to,buff,length);
|
|
return to+length;
|
|
}
|
|
|
|
char *net_store_data(char *to,longlong from)
|
|
{
|
|
char buff[22];
|
|
uint length=(uint) (longlong10_to_str(from,buff,10)-buff);
|
|
to=net_store_length_fast(to,length);
|
|
memcpy(to,buff,length);
|
|
return to+length;
|
|
}
|
|
|
|
|
|
/*****************************************************************************
|
|
Default Protocol functions
|
|
*****************************************************************************/
|
|
|
|
void Protocol::init(THD *thd_arg)
|
|
{
|
|
thd=thd_arg;
|
|
packet= &thd->packet;
|
|
convert= &thd->convert_buffer;
|
|
#ifndef DBUG_OFF
|
|
field_types= 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
bool Protocol::flush()
|
|
{
|
|
#ifndef EMBEDDED_LIBRARY
|
|
return net_flush(&thd->net);
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
Send name and type of result to client.
|
|
|
|
SYNOPSIS
|
|
send_fields()
|
|
THD Thread data object
|
|
list List of items to send to client
|
|
flag Bit mask with the following functions:
|
|
1 send number of rows
|
|
2 send default values
|
|
4 don't write eof packet
|
|
|
|
DESCRIPTION
|
|
Sum fields has table name empty and field_name.
|
|
|
|
RETURN VALUES
|
|
0 ok
|
|
1 Error (Note that in this case the error is not sent to the client)
|
|
*/
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
bool Protocol::send_fields(List<Item> *list, uint flags)
|
|
{
|
|
List_iterator_fast<Item> it(*list);
|
|
Item *item;
|
|
char buff[80];
|
|
String tmp((char*) buff,sizeof(buff),&my_charset_bin);
|
|
Protocol_text prot(thd);
|
|
String *local_packet= prot.storage_packet();
|
|
CHARSET_INFO *thd_charset= thd->variables.character_set_results;
|
|
DBUG_ENTER("send_fields");
|
|
|
|
if (flags & SEND_NUM_ROWS)
|
|
{ // Packet with number of elements
|
|
char *pos=net_store_length(buff, list->elements);
|
|
(void) my_net_write(&thd->net, buff,(uint) (pos-buff));
|
|
}
|
|
|
|
#ifndef DBUG_OFF
|
|
field_types= (enum_field_types*) thd->alloc(sizeof(field_types) *
|
|
list->elements);
|
|
uint count= 0;
|
|
#endif
|
|
|
|
while ((item=it++))
|
|
{
|
|
char *pos;
|
|
CHARSET_INFO *cs= system_charset_info;
|
|
Send_field field;
|
|
item->make_field(&field);
|
|
|
|
/* Keep things compatible for old clients */
|
|
if (field.type == MYSQL_TYPE_VARCHAR)
|
|
field.type= MYSQL_TYPE_VAR_STRING;
|
|
|
|
prot.prepare_for_resend();
|
|
|
|
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
|
|
{
|
|
if (prot.store(STRING_WITH_LEN("def"), cs, thd_charset) ||
|
|
prot.store(field.db_name, (uint) strlen(field.db_name),
|
|
cs, thd_charset) ||
|
|
prot.store(field.table_name, (uint) strlen(field.table_name),
|
|
cs, thd_charset) ||
|
|
prot.store(field.org_table_name, (uint) strlen(field.org_table_name),
|
|
cs, thd_charset) ||
|
|
prot.store(field.col_name, (uint) strlen(field.col_name),
|
|
cs, thd_charset) ||
|
|
prot.store(field.org_col_name, (uint) strlen(field.org_col_name),
|
|
cs, thd_charset) ||
|
|
local_packet->realloc(local_packet->length()+12))
|
|
goto err;
|
|
/* Store fixed length fields */
|
|
pos= (char*) local_packet->ptr()+local_packet->length();
|
|
*pos++= 12; // Length of packed fields
|
|
if (item->collation.collation == &my_charset_bin || thd_charset == NULL)
|
|
{
|
|
/* No conversion */
|
|
int2store(pos, field.charsetnr);
|
|
int4store(pos+2, field.length);
|
|
}
|
|
else
|
|
{
|
|
/* With conversion */
|
|
uint max_char_len;
|
|
int2store(pos, thd_charset->number);
|
|
/*
|
|
For TEXT/BLOB columns, field_length describes the maximum data
|
|
length in bytes. There is no limit to the number of characters
|
|
that a TEXT column can store, as long as the data fits into
|
|
the designated space.
|
|
For the rest of textual columns, field_length is evaluated as
|
|
char_count * mbmaxlen, where character count is taken from the
|
|
definition of the column. In other words, the maximum number
|
|
of characters here is limited by the column definition.
|
|
*/
|
|
max_char_len= (field.type >= (int) MYSQL_TYPE_TINY_BLOB &&
|
|
field.type <= (int) MYSQL_TYPE_BLOB) ?
|
|
field.length / item->collation.collation->mbminlen :
|
|
field.length / item->collation.collation->mbmaxlen;
|
|
int4store(pos+2, max_char_len * thd_charset->mbmaxlen);
|
|
}
|
|
pos[6]= field.type;
|
|
int2store(pos+7,field.flags);
|
|
pos[9]= (char) field.decimals;
|
|
pos[10]= 0; // For the future
|
|
pos[11]= 0; // For the future
|
|
pos+= 12;
|
|
}
|
|
else
|
|
{
|
|
if (prot.store(field.table_name, (uint) strlen(field.table_name),
|
|
cs, thd_charset) ||
|
|
prot.store(field.col_name, (uint) strlen(field.col_name),
|
|
cs, thd_charset) ||
|
|
local_packet->realloc(local_packet->length()+10))
|
|
goto err;
|
|
pos= (char*) local_packet->ptr()+local_packet->length();
|
|
|
|
#ifdef TO_BE_DELETED_IN_6
|
|
if (!(thd->client_capabilities & CLIENT_LONG_FLAG))
|
|
{
|
|
pos[0]=3;
|
|
int3store(pos+1,field.length);
|
|
pos[4]=1;
|
|
pos[5]=field.type;
|
|
pos[6]=2;
|
|
pos[7]= (char) field.flags;
|
|
pos[8]= (char) field.decimals;
|
|
pos+= 9;
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
pos[0]=3;
|
|
int3store(pos+1,field.length);
|
|
pos[4]=1;
|
|
pos[5]=field.type;
|
|
pos[6]=3;
|
|
int2store(pos+7,field.flags);
|
|
pos[9]= (char) field.decimals;
|
|
pos+= 10;
|
|
}
|
|
}
|
|
local_packet->length((uint) (pos - local_packet->ptr()));
|
|
if (flags & SEND_DEFAULTS)
|
|
item->send(&prot, &tmp); // Send default value
|
|
if (prot.write())
|
|
break; /* purecov: inspected */
|
|
#ifndef DBUG_OFF
|
|
field_types[count++]= field.type;
|
|
#endif
|
|
}
|
|
|
|
if (flags & SEND_EOF)
|
|
write_eof_packet(thd, &thd->net);
|
|
DBUG_RETURN(prepare_for_send(list));
|
|
|
|
err:
|
|
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES),
|
|
MYF(0)); /* purecov: inspected */
|
|
DBUG_RETURN(1); /* purecov: inspected */
|
|
}
|
|
|
|
|
|
bool Protocol::write()
|
|
{
|
|
DBUG_ENTER("Protocol::write");
|
|
DBUG_RETURN(my_net_write(&thd->net, packet->ptr(), packet->length()));
|
|
}
|
|
#endif /* EMBEDDED_LIBRARY */
|
|
|
|
|
|
/*
|
|
Send \0 end terminated string
|
|
|
|
SYNOPSIS
|
|
store()
|
|
from NullS or \0 terminated string
|
|
|
|
NOTES
|
|
In most cases one should use store(from, length) instead of this function
|
|
|
|
RETURN VALUES
|
|
0 ok
|
|
1 error
|
|
*/
|
|
|
|
bool Protocol::store(const char *from, CHARSET_INFO *cs)
|
|
{
|
|
if (!from)
|
|
return store_null();
|
|
uint length= strlen(from);
|
|
return store(from, length, cs);
|
|
}
|
|
|
|
|
|
/*
|
|
Send a set of strings as one long string with ',' in between
|
|
*/
|
|
|
|
bool Protocol::store(I_List<i_string>* str_list)
|
|
{
|
|
char buf[256];
|
|
String tmp(buf, sizeof(buf), &my_charset_bin);
|
|
uint32 len;
|
|
I_List_iterator<i_string> it(*str_list);
|
|
i_string* s;
|
|
|
|
tmp.length(0);
|
|
while ((s=it++))
|
|
{
|
|
tmp.append(s->ptr);
|
|
tmp.append(',');
|
|
}
|
|
if ((len= tmp.length()))
|
|
len--; // Remove last ','
|
|
return store((char*) tmp.ptr(), len, tmp.charset());
|
|
}
|
|
|
|
|
|
/****************************************************************************
|
|
Functions to handle the simple (default) protocol where everything is
|
|
This protocol is the one that is used by default between the MySQL server
|
|
and client when you are not using prepared statements.
|
|
|
|
All data are sent as 'packed-string-length' followed by 'string-data'
|
|
****************************************************************************/
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
void Protocol_text::prepare_for_resend()
|
|
{
|
|
packet->length(0);
|
|
#ifndef DBUG_OFF
|
|
field_pos= 0;
|
|
#endif
|
|
}
|
|
|
|
bool Protocol_text::store_null()
|
|
{
|
|
#ifndef DBUG_OFF
|
|
field_pos++;
|
|
#endif
|
|
char buff[1];
|
|
buff[0]= (char)251;
|
|
return packet->append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
|
|
}
|
|
#endif
|
|
|
|
|
|
/*
|
|
Auxilary function to convert string to the given character set
|
|
and store in network buffer.
|
|
*/
|
|
|
|
bool Protocol::store_string_aux(const char *from, uint length,
|
|
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
|
|
{
|
|
/* 'tocs' is set 0 when client issues SET character_set_results=NULL */
|
|
if (tocs && !my_charset_same(fromcs, tocs) &&
|
|
fromcs != &my_charset_bin &&
|
|
tocs != &my_charset_bin)
|
|
{
|
|
uint dummy_errors;
|
|
return convert->copy(from, length, fromcs, tocs, &dummy_errors) ||
|
|
net_store_data(convert->ptr(), convert->length());
|
|
}
|
|
return net_store_data(from, length);
|
|
}
|
|
|
|
|
|
bool Protocol_text::store(const char *from, uint length,
|
|
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
|
|
field_types[field_pos] == MYSQL_TYPE_BIT ||
|
|
field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL ||
|
|
(field_types[field_pos] >= MYSQL_TYPE_ENUM &&
|
|
field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
|
|
field_pos++;
|
|
#endif
|
|
return store_string_aux(from, length, fromcs, tocs);
|
|
}
|
|
|
|
|
|
bool Protocol_text::store(const char *from, uint length,
|
|
CHARSET_INFO *fromcs)
|
|
{
|
|
CHARSET_INFO *tocs= this->thd->variables.character_set_results;
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
|
|
field_types[field_pos] == MYSQL_TYPE_BIT ||
|
|
field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL ||
|
|
(field_types[field_pos] >= MYSQL_TYPE_ENUM &&
|
|
field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
|
|
field_pos++;
|
|
#endif
|
|
return store_string_aux(from, length, fromcs, tocs);
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_tiny(longlong from)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 || field_types[field_pos] == MYSQL_TYPE_TINY);
|
|
field_pos++;
|
|
#endif
|
|
char buff[20];
|
|
return net_store_data((char*) buff,
|
|
(uint) (int10_to_str((int) from,buff, -10)-buff));
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_short(longlong from)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_YEAR ||
|
|
field_types[field_pos] == MYSQL_TYPE_SHORT);
|
|
field_pos++;
|
|
#endif
|
|
char buff[20];
|
|
return net_store_data((char*) buff,
|
|
(uint) (int10_to_str((int) from,buff, -10)-buff));
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_long(longlong from)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_INT24 ||
|
|
field_types[field_pos] == MYSQL_TYPE_LONG);
|
|
field_pos++;
|
|
#endif
|
|
char buff[20];
|
|
return net_store_data((char*) buff,
|
|
(uint) (int10_to_str((long int)from,buff, (from <0)?-10:10)-buff));
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_longlong(longlong from, bool unsigned_flag)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_LONGLONG);
|
|
field_pos++;
|
|
#endif
|
|
char buff[22];
|
|
return net_store_data((char*) buff,
|
|
(uint) (longlong10_to_str(from,buff,
|
|
unsigned_flag ? 10 : -10)-
|
|
buff));
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_decimal(const my_decimal *d)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL);
|
|
field_pos++;
|
|
#endif
|
|
char buff[DECIMAL_MAX_STR_LENGTH];
|
|
String str(buff, sizeof(buff), &my_charset_bin);
|
|
(void) my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
|
|
return net_store_data(str.ptr(), str.length());
|
|
}
|
|
|
|
|
|
bool Protocol_text::store(float from, uint32 decimals, String *buffer)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_FLOAT);
|
|
field_pos++;
|
|
#endif
|
|
buffer->set_real((double) from, decimals, thd->charset());
|
|
return net_store_data((char*) buffer->ptr(), buffer->length());
|
|
}
|
|
|
|
|
|
bool Protocol_text::store(double from, uint32 decimals, String *buffer)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_DOUBLE);
|
|
field_pos++;
|
|
#endif
|
|
buffer->set_real(from, decimals, thd->charset());
|
|
return net_store_data((char*) buffer->ptr(), buffer->length());
|
|
}
|
|
|
|
|
|
bool Protocol_text::store(Field *field)
|
|
{
|
|
if (field->is_null())
|
|
return store_null();
|
|
#ifndef DBUG_OFF
|
|
field_pos++;
|
|
#endif
|
|
char buff[MAX_FIELD_WIDTH];
|
|
String str(buff,sizeof(buff), &my_charset_bin);
|
|
CHARSET_INFO *tocs= this->thd->variables.character_set_results;
|
|
#ifndef DBUG_OFF
|
|
TABLE *table= field->table;
|
|
my_bitmap_map *old_map= 0;
|
|
if (table->file)
|
|
old_map= dbug_tmp_use_all_columns(table, table->read_set);
|
|
#endif
|
|
|
|
field->val_str(&str);
|
|
#ifndef DBUG_OFF
|
|
if (old_map)
|
|
dbug_tmp_restore_column_map(table->read_set, old_map);
|
|
#endif
|
|
|
|
return store_string_aux(str.ptr(), str.length(), str.charset(), tocs);
|
|
}
|
|
|
|
|
|
/*
|
|
TODO:
|
|
Second_part format ("%06") needs to change when
|
|
we support 0-6 decimals for time.
|
|
*/
|
|
|
|
|
|
bool Protocol_text::store(TIME *tm)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_DATETIME ||
|
|
field_types[field_pos] == MYSQL_TYPE_TIMESTAMP);
|
|
field_pos++;
|
|
#endif
|
|
char buff[40];
|
|
uint length;
|
|
length= my_sprintf(buff,(buff, "%04d-%02d-%02d %02d:%02d:%02d",
|
|
(int) tm->year,
|
|
(int) tm->month,
|
|
(int) tm->day,
|
|
(int) tm->hour,
|
|
(int) tm->minute,
|
|
(int) tm->second));
|
|
if (tm->second_part)
|
|
length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
|
|
return net_store_data((char*) buff, length);
|
|
}
|
|
|
|
|
|
bool Protocol_text::store_date(TIME *tm)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_DATE);
|
|
field_pos++;
|
|
#endif
|
|
char buff[MAX_DATE_STRING_REP_LENGTH];
|
|
int length= my_date_to_str(tm, buff);
|
|
return net_store_data(buff, (uint) length);
|
|
}
|
|
|
|
|
|
/*
|
|
TODO:
|
|
Second_part format ("%06") needs to change when
|
|
we support 0-6 decimals for time.
|
|
*/
|
|
|
|
bool Protocol_text::store_time(TIME *tm)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_TIME);
|
|
field_pos++;
|
|
#endif
|
|
char buff[40];
|
|
uint length;
|
|
uint day= (tm->year || tm->month) ? 0 : tm->day;
|
|
length= my_sprintf(buff,(buff, "%s%02ld:%02d:%02d",
|
|
tm->neg ? "-" : "",
|
|
(long) day*24L+(long) tm->hour,
|
|
(int) tm->minute,
|
|
(int) tm->second));
|
|
if (tm->second_part)
|
|
length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
|
|
return net_store_data((char*) buff, length);
|
|
}
|
|
|
|
|
|
/****************************************************************************
|
|
Functions to handle the binary protocol used with prepared statements
|
|
|
|
Data format:
|
|
|
|
[ok:1] reserved ok packet
|
|
[null_field:(field_count+7+2)/8] reserved to send null data. The size is
|
|
calculated using:
|
|
bit_fields= (field_count+7+2)/8;
|
|
2 bits are reserved for identifying type
|
|
of package.
|
|
[[length]data] data field (the length applies only for
|
|
string/binary/time/timestamp fields and
|
|
rest of them are not sent as they have
|
|
the default length that client understands
|
|
based on the field type
|
|
[..]..[[length]data] data
|
|
****************************************************************************/
|
|
|
|
bool Protocol_binary::prepare_for_send(List<Item> *item_list)
|
|
{
|
|
Protocol::prepare_for_send(item_list);
|
|
bit_fields= (field_count+9)/8;
|
|
if (packet->alloc(bit_fields+1))
|
|
return 1;
|
|
/* prepare_for_resend will be called after this one */
|
|
return 0;
|
|
}
|
|
|
|
|
|
void Protocol_binary::prepare_for_resend()
|
|
{
|
|
packet->length(bit_fields+1);
|
|
bzero((char*) packet->ptr(), 1+bit_fields);
|
|
field_pos=0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store(const char *from, uint length,
|
|
CHARSET_INFO *fromcs)
|
|
{
|
|
CHARSET_INFO *tocs= thd->variables.character_set_results;
|
|
field_pos++;
|
|
return store_string_aux(from, length, fromcs, tocs);
|
|
}
|
|
|
|
bool Protocol_binary::store(const char *from,uint length,
|
|
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
|
|
{
|
|
field_pos++;
|
|
return store_string_aux(from, length, fromcs, tocs);
|
|
}
|
|
|
|
bool Protocol_binary::store_null()
|
|
{
|
|
uint offset= (field_pos+2)/8+1, bit= (1 << ((field_pos+2) & 7));
|
|
/* Room for this as it's allocated in prepare_for_send */
|
|
char *to= (char*) packet->ptr()+offset;
|
|
*to= (char) ((uchar) *to | (uchar) bit);
|
|
field_pos++;
|
|
return 0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store_tiny(longlong from)
|
|
{
|
|
char buff[1];
|
|
field_pos++;
|
|
buff[0]= (uchar) from;
|
|
return packet->append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store_short(longlong from)
|
|
{
|
|
field_pos++;
|
|
char *to= packet->prep_append(2, PACKET_BUFFER_EXTRA_ALLOC);
|
|
if (!to)
|
|
return 1;
|
|
int2store(to, (int) from);
|
|
return 0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store_long(longlong from)
|
|
{
|
|
field_pos++;
|
|
char *to= packet->prep_append(4, PACKET_BUFFER_EXTRA_ALLOC);
|
|
if (!to)
|
|
return 1;
|
|
int4store(to, from);
|
|
return 0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store_longlong(longlong from, bool unsigned_flag)
|
|
{
|
|
field_pos++;
|
|
char *to= packet->prep_append(8, PACKET_BUFFER_EXTRA_ALLOC);
|
|
if (!to)
|
|
return 1;
|
|
int8store(to, from);
|
|
return 0;
|
|
}
|
|
|
|
bool Protocol_binary::store_decimal(const my_decimal *d)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
DBUG_ASSERT(field_types == 0 ||
|
|
field_types[field_pos] == MYSQL_TYPE_NEWDECIMAL);
|
|
field_pos++;
|
|
#endif
|
|
char buff[DECIMAL_MAX_STR_LENGTH];
|
|
String str(buff, sizeof(buff), &my_charset_bin);
|
|
(void) my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
|
|
return store(str.ptr(), str.length(), str.charset());
|
|
}
|
|
|
|
bool Protocol_binary::store(float from, uint32 decimals, String *buffer)
|
|
{
|
|
field_pos++;
|
|
char *to= packet->prep_append(4, PACKET_BUFFER_EXTRA_ALLOC);
|
|
if (!to)
|
|
return 1;
|
|
float4store(to, from);
|
|
return 0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store(double from, uint32 decimals, String *buffer)
|
|
{
|
|
field_pos++;
|
|
char *to= packet->prep_append(8, PACKET_BUFFER_EXTRA_ALLOC);
|
|
if (!to)
|
|
return 1;
|
|
float8store(to, from);
|
|
return 0;
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store(Field *field)
|
|
{
|
|
/*
|
|
We should not increment field_pos here as send_binary() will call another
|
|
protocol function to do this for us
|
|
*/
|
|
if (field->is_null())
|
|
return store_null();
|
|
return field->send_binary(this);
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store(TIME *tm)
|
|
{
|
|
char buff[12],*pos;
|
|
uint length;
|
|
field_pos++;
|
|
pos= buff+1;
|
|
|
|
int2store(pos, tm->year);
|
|
pos[2]= (uchar) tm->month;
|
|
pos[3]= (uchar) tm->day;
|
|
pos[4]= (uchar) tm->hour;
|
|
pos[5]= (uchar) tm->minute;
|
|
pos[6]= (uchar) tm->second;
|
|
int4store(pos+7, tm->second_part);
|
|
if (tm->second_part)
|
|
length=11;
|
|
else if (tm->hour || tm->minute || tm->second)
|
|
length=7;
|
|
else if (tm->year || tm->month || tm->day)
|
|
length=4;
|
|
else
|
|
length=0;
|
|
buff[0]=(char) length; // Length is stored first
|
|
return packet->append(buff, length+1, PACKET_BUFFER_EXTRA_ALLOC);
|
|
}
|
|
|
|
bool Protocol_binary::store_date(TIME *tm)
|
|
{
|
|
tm->hour= tm->minute= tm->second=0;
|
|
tm->second_part= 0;
|
|
return Protocol_binary::store(tm);
|
|
}
|
|
|
|
|
|
bool Protocol_binary::store_time(TIME *tm)
|
|
{
|
|
char buff[13], *pos;
|
|
uint length;
|
|
field_pos++;
|
|
pos= buff+1;
|
|
pos[0]= tm->neg ? 1 : 0;
|
|
if (tm->hour >= 24)
|
|
{
|
|
/* Fix if we come from Item::send */
|
|
uint days= tm->hour/24;
|
|
tm->hour-= days*24;
|
|
tm->day+= days;
|
|
}
|
|
int4store(pos+1, tm->day);
|
|
pos[5]= (uchar) tm->hour;
|
|
pos[6]= (uchar) tm->minute;
|
|
pos[7]= (uchar) tm->second;
|
|
int4store(pos+8, tm->second_part);
|
|
if (tm->second_part)
|
|
length=12;
|
|
else if (tm->hour || tm->minute || tm->second || tm->day)
|
|
length=8;
|
|
else
|
|
length=0;
|
|
buff[0]=(char) length; // Length is stored first
|
|
return packet->append(buff, length+1, PACKET_BUFFER_EXTRA_ALLOC);
|
|
}
|