19
19
Low level functions for storing data to be send to the MySQL client.
20
20
The actual communction is handled by the net_xxx functions in net_serv.cc
22
#include <drizzled/server_includes.h>
23
#include <drizzled/drizzled_error_messages.h>
24
#include <drizzled/sql_state.h>
23
#ifdef USE_PRAGMA_IMPLEMENTATION
24
#pragma implementation // gcc: Class implementation
27
#include "mysql_priv.h"
26
30
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
27
31
/* Declared non-static only because of the embedded library. */
28
static void net_send_error_packet(THD *thd, uint32_t sql_errno, const char *err);
32
void net_send_error_packet(THD *thd, uint sql_errno, const char *err);
33
void net_send_ok(THD *, uint, uint, ha_rows, uint64_t, const char *);
34
void net_send_eof(THD *thd, uint server_status, uint total_warn_count);
29
35
static void write_eof_packet(THD *thd, NET *net,
30
uint32_t server_status, uint32_t total_warn_count);
36
uint server_status, uint total_warn_count);
32
bool Protocol::net_store_data(const unsigned char *from, size_t length)
38
bool Protocol::net_store_data(const uchar *from, size_t length)
34
40
ulong packet_length=packet->length();
171
178
buff[0]=0; // No fields
172
179
pos=net_store_length(buff+1,affected_rows);
173
180
pos=net_store_length(pos, id);
175
int2store(pos, server_status);
178
/* We can only return up to 65535 warnings in two bytes */
179
uint32_t tmp= cmin(total_warn_count, (uint)65535);
181
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
183
int2store(pos, server_status);
186
/* We can only return up to 65535 warnings in two bytes */
187
uint tmp= min(total_warn_count, 65535);
191
else if (net->return_status) // For 4.0 protocol
193
int2store(pos, server_status);
183
196
thd->main_da.can_overwrite_status= true;
185
198
if (message && message[0])
186
pos= net_store_data(pos, (unsigned char*) message, strlen(message));
187
my_net_write(net, buff, (size_t) (pos-buff));
199
pos= net_store_data(pos, (uchar*) message, strlen(message));
200
VOID(my_net_write(net, buff, (size_t) (pos-buff)));
201
VOID(net_flush(net));
190
203
thd->main_da.can_overwrite_status= false;
208
static uchar eof_buff[1]= { (uchar) 254 }; /* Marker for end of fields */
194
211
Send eof (= end of result set) to the client.
196
213
The eof packet has the following structure:
198
- 254 (DRIZZLE_PROTOCOL_NO_MORE_DATA) : Marker (1 byte)
215
- 254 : Marker (1 byte)
199
216
- warning_count : Stored in 2 bytes; New in 4.1 protocol
200
217
- status_flag : Stored in 2 bytes;
201
218
For flags like SERVER_MORE_RESULTS_EXISTS.
232
250
static void write_eof_packet(THD *thd, NET *net,
233
uint32_t server_status,
234
uint32_t total_warn_count)
252
uint total_warn_count)
236
unsigned char buff[5];
238
Don't send warn count during SP execution, as the warn_list
239
is cleared between substatements, and mysqltest gets confused
241
uint32_t tmp= cmin(total_warn_count, (uint)65535);
242
buff[0]= DRIZZLE_PROTOCOL_NO_MORE_DATA;
243
int2store(buff+1, tmp);
245
The following test should never be true, but it's better to do it
246
because if 'is_fatal_error' is set the server is not going to execute
247
other queries (see the if test in dispatch_command / COM_QUERY)
249
if (thd->is_fatal_error)
250
server_status&= ~SERVER_MORE_RESULTS_EXISTS;
251
int2store(buff + 3, server_status);
252
my_net_write(net, buff, 5);
254
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
258
Don't send warn count during SP execution, as the warn_list
259
is cleared between substatements, and mysqltest gets confused
261
uint tmp= min(total_warn_count, 65535);
263
int2store(buff+1, tmp);
265
The following test should never be true, but it's better to do it
266
because if 'is_fatal_error' is set the server is not going to execute
267
other queries (see the if test in dispatch_command / COM_QUERY)
269
if (thd->is_fatal_error)
270
server_status&= ~SERVER_MORE_RESULTS_EXISTS;
271
int2store(buff + 3, server_status);
272
VOID(my_net_write(net, buff, 5));
275
VOID(my_net_write(net, eof_buff, 1));
255
void net_send_error_packet(THD *thd, uint32_t sql_errno, const char *err)
278
void net_send_error_packet(THD *thd, uint sql_errno, const char *err)
257
280
NET *net= &thd->net;
260
buff[]: sql_errno:2 + ('#':1 + SQLSTATE_LENGTH:5) + DRIZZLE_ERRMSG_SIZE:512
283
buff[]: sql_errno:2 + ('#':1 + SQLSTATE_LENGTH:5) + MYSQL_ERRMSG_SIZE:512
262
unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
285
uchar buff[2+1+SQLSTATE_LENGTH+MYSQL_ERRMSG_SIZE], *pos;
264
287
if (net->vio == 0)
404
429
/* The following will only be used for short strings < 65K */
406
unsigned char *net_store_data(unsigned char *to, const unsigned char *from, size_t length)
431
uchar *net_store_data(uchar *to, const uchar *from, size_t length)
408
433
to=net_store_length_fast(to,length);
409
434
memcpy(to,from,length);
410
435
return to+length;
413
unsigned char *net_store_data(unsigned char *to,int32_t from)
438
uchar *net_store_data(uchar *to,int32 from)
416
uint32_t length=(uint) (int10_to_str(from,buff,10)-buff);
441
uint length=(uint) (int10_to_str(from,buff,10)-buff);
417
442
to=net_store_length_fast(to,length);
418
443
memcpy(to,buff,length);
419
444
return to+length;
422
unsigned char *net_store_data(unsigned char *to,int64_t from)
447
uchar *net_store_data(uchar *to,int64_t from)
425
uint32_t length=(uint) (int64_t10_to_str(from,buff,10)-buff);
450
uint length=(uint) (int64_t10_to_str(from,buff,10)-buff);
426
451
to=net_store_length_fast(to,length);
427
452
memcpy(to,buff,length);
428
453
return to+length;
476
501
1 Error (Note that in this case the error is not sent to the
479
bool Protocol::send_fields(List<Item> *list, uint32_t flags)
504
bool Protocol::send_fields(List<Item> *list, uint flags)
481
506
List_iterator_fast<Item> it(*list);
483
unsigned char buff[80];
484
509
String tmp((char*) buff,sizeof(buff),&my_charset_bin);
485
510
Protocol_text prot(thd);
486
511
String *local_packet= prot.storage_packet();
487
const CHARSET_INFO * const thd_charset= thd->variables.character_set_results;
512
CHARSET_INFO *thd_charset= thd->variables.character_set_results;
489
514
if (flags & SEND_NUM_ROWS)
490
515
{ // Packet with number of elements
491
unsigned char *pos= net_store_length(buff, list->elements);
516
uchar *pos= net_store_length(buff, list->elements);
492
517
(void) my_net_write(&thd->net, buff, (size_t) (pos-buff));
495
520
while ((item=it++))
498
const CHARSET_INFO * const cs= system_charset_info;
523
CHARSET_INFO *cs= system_charset_info;
499
524
Send_field field;
500
525
item->make_field(&field);
502
527
prot.prepare_for_resend();
505
if (prot.store(STRING_WITH_LEN("def"), cs, thd_charset) ||
506
prot.store(field.db_name, (uint) strlen(field.db_name),
508
prot.store(field.table_name, (uint) strlen(field.table_name),
510
prot.store(field.org_table_name, (uint) strlen(field.org_table_name),
512
prot.store(field.col_name, (uint) strlen(field.col_name),
514
prot.store(field.org_col_name, (uint) strlen(field.org_col_name),
516
local_packet->realloc(local_packet->length()+12))
519
/* Store fixed length fields */
520
pos= (char*) local_packet->ptr()+local_packet->length();
521
*pos++= 12; // Length of packed fields
522
if (item->collation.collation == &my_charset_bin || thd_charset == NULL)
529
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
525
int2store(pos, field.charsetnr);
526
int4store(pos+2, field.length);
531
if (prot.store(STRING_WITH_LEN("def"), cs, thd_charset) ||
532
prot.store(field.db_name, (uint) strlen(field.db_name),
534
prot.store(field.table_name, (uint) strlen(field.table_name),
536
prot.store(field.org_table_name, (uint) strlen(field.org_table_name),
538
prot.store(field.col_name, (uint) strlen(field.col_name),
540
prot.store(field.org_col_name, (uint) strlen(field.org_col_name),
542
local_packet->realloc(local_packet->length()+12))
544
/* Store fixed length fields */
545
pos= (char*) local_packet->ptr()+local_packet->length();
546
*pos++= 12; // Length of packed fields
547
if (item->collation.collation == &my_charset_bin || thd_charset == NULL)
550
int2store(pos, field.charsetnr);
551
int4store(pos+2, field.length);
555
/* With conversion */
557
int2store(pos, thd_charset->number);
559
For TEXT/BLOB columns, field_length describes the maximum data
560
length in bytes. There is no limit to the number of characters
561
that a TEXT column can store, as long as the data fits into
562
the designated space.
563
For the rest of textual columns, field_length is evaluated as
564
char_count * mbmaxlen, where character count is taken from the
565
definition of the column. In other words, the maximum number
566
of characters here is limited by the column definition.
568
max_char_len= field.length / item->collation.collation->mbmaxlen;
569
int4store(pos+2, max_char_len * thd_charset->mbmaxlen);
572
int2store(pos+7,field.flags);
573
pos[9]= (char) field.decimals;
574
pos[10]= 0; // For the future
575
pos[11]= 0; // For the future
530
/* With conversion */
531
uint32_t max_char_len;
532
int2store(pos, thd_charset->number);
534
For TEXT/BLOB columns, field_length describes the maximum data
535
length in bytes. There is no limit to the number of characters
536
that a TEXT column can store, as long as the data fits into
537
the designated space.
538
For the rest of textual columns, field_length is evaluated as
539
char_count * mbmaxlen, where character count is taken from the
540
definition of the column. In other words, the maximum number
541
of characters here is limited by the column definition.
543
max_char_len= field.length / item->collation.collation->mbmaxlen;
544
int4store(pos+2, max_char_len * thd_charset->mbmaxlen);
580
if (prot.store(field.table_name, (uint) strlen(field.table_name),
582
prot.store(field.col_name, (uint) strlen(field.col_name),
584
local_packet->realloc(local_packet->length()+10))
586
pos= (char*) local_packet->ptr()+local_packet->length();
589
int3store(pos+1,field.length);
593
int2store(pos+7,field.flags);
594
pos[9]= (char) field.decimals;
547
int2store(pos+7,field.flags);
548
pos[9]= (char) field.decimals;
549
pos[10]= 0; // For the future
550
pos[11]= 0; // For the future
553
597
local_packet->length((uint) (pos - local_packet->ptr()));
554
598
if (flags & SEND_DEFAULTS)
555
599
item->send(&prot, &tmp); // Send default value
665
708
tocs != &my_charset_bin)
667
710
/* Store with conversion */
668
return net_store_data((unsigned char*) from, length, fromcs, tocs);
711
return net_store_data((uchar*) from, length, fromcs, tocs);
670
713
/* Store without conversion */
671
return net_store_data((unsigned char*) from, length);
714
return net_store_data((uchar*) from, length);
675
718
bool Protocol_text::store(const char *from, size_t length,
676
const CHARSET_INFO * const fromcs,
677
const CHARSET_INFO * const tocs)
719
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
679
721
return store_string_aux(from, length, fromcs, tocs);
683
725
bool Protocol_text::store(const char *from, size_t length,
684
const CHARSET_INFO * const fromcs)
726
CHARSET_INFO *fromcs)
686
const CHARSET_INFO * const tocs= this->thd->variables.character_set_results;
728
CHARSET_INFO *tocs= this->thd->variables.character_set_results;
687
729
return store_string_aux(from, length, fromcs, tocs);
729
771
char buff[DECIMAL_MAX_STR_LENGTH];
730
772
String str(buff, sizeof(buff), &my_charset_bin);
731
773
(void) my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
732
return net_store_data((unsigned char*) str.ptr(), str.length());
774
return net_store_data((uchar*) str.ptr(), str.length());
736
bool Protocol_text::store(float from, uint32_t decimals, String *buffer)
778
bool Protocol_text::store(float from, uint32 decimals, String *buffer)
738
780
buffer->set_real((double) from, decimals, thd->charset());
739
return net_store_data((unsigned char*) buffer->ptr(), buffer->length());
781
return net_store_data((uchar*) buffer->ptr(), buffer->length());
743
bool Protocol_text::store(double from, uint32_t decimals, String *buffer)
785
bool Protocol_text::store(double from, uint32 decimals, String *buffer)
745
787
buffer->set_real(from, decimals, thd->charset());
746
return net_store_data((unsigned char*) buffer->ptr(), buffer->length());
788
return net_store_data((uchar*) buffer->ptr(), buffer->length());