MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
protocol.h
1 #ifndef PROTOCOL_INCLUDED
2 #define PROTOCOL_INCLUDED
3 
4 /* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 #include "sql_error.h"
20 #include "my_decimal.h" /* my_decimal */
21 
22 class i_string;
23 class Field;
24 class THD;
25 class Item_param;
26 typedef struct st_mysql_field MYSQL_FIELD;
27 typedef struct st_mysql_rows MYSQL_ROWS;
28 
29 class Protocol
30 {
31 protected:
32  THD *thd;
33  String *packet;
34  String *convert;
35  uint field_pos;
36 #ifndef DBUG_OFF
37  enum enum_field_types *field_types;
38 #endif
39  uint field_count;
40 #ifndef EMBEDDED_LIBRARY
41  bool net_store_data(const uchar *from, size_t length);
42 #else
43  virtual bool net_store_data(const uchar *from, size_t length);
44  char **next_field;
45  MYSQL_FIELD *next_mysql_field;
46  MEM_ROOT *alloc;
47 #endif
48  bool net_store_data(const uchar *from, size_t length,
49  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
50  bool store_string_aux(const char *from, size_t length,
51  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
52 
53  virtual bool send_ok(uint server_status, uint statement_warn_count,
54  ulonglong affected_rows, ulonglong last_insert_id,
55  const char *message);
56 
57  virtual bool send_eof(uint server_status, uint statement_warn_count);
58 
59  virtual bool send_error(uint sql_errno, const char *err_msg,
60  const char *sql_state);
61 
62 public:
63  Protocol() {}
64  Protocol(THD *thd_arg) { init(thd_arg); }
65  virtual ~Protocol() {}
66  void init(THD* thd_arg);
67 
68  enum { SEND_NUM_ROWS= 1, SEND_DEFAULTS= 2, SEND_EOF= 4 };
69  virtual bool send_result_set_metadata(List<Item> *list, uint flags);
70  bool send_result_set_row(List<Item> *row_items);
71 
72  bool store(I_List<i_string> *str_list);
73  bool store(const char *from, const CHARSET_INFO *cs);
74  String *storage_packet() { return packet; }
75  inline void free() { packet->free(); }
76  virtual bool write();
77  inline bool store(int from)
78  { return store_long((longlong) from); }
79  inline bool store(uint32 from)
80  { return store_long((longlong) from); }
81  inline bool store(longlong from)
82  { return store_longlong((longlong) from, 0); }
83  inline bool store(ulonglong from)
84  { return store_longlong((longlong) from, 1); }
85  inline bool store(String *str)
86  { return store((char*) str->ptr(), str->length(), str->charset()); }
87 
88  virtual bool prepare_for_send(uint num_columns)
89  {
90  field_count= num_columns;
91  return 0;
92  }
93  virtual bool flush();
94  virtual void end_partial_result_set(THD *thd);
95  virtual void prepare_for_resend()=0;
96 
97  virtual bool store_null()=0;
98  virtual bool store_tiny(longlong from)=0;
99  virtual bool store_short(longlong from)=0;
100  virtual bool store_long(longlong from)=0;
101  virtual bool store_longlong(longlong from, bool unsigned_flag)=0;
102  virtual bool store_decimal(const my_decimal *)=0;
103  virtual bool store(const char *from, size_t length,
104  const CHARSET_INFO *cs)=0;
105  virtual bool store(const char *from, size_t length,
106  const CHARSET_INFO *fromcs,
107  const CHARSET_INFO *tocs)=0;
108  virtual bool store(float from, uint32 decimals, String *buffer)=0;
109  virtual bool store(double from, uint32 decimals, String *buffer)=0;
110  virtual bool store(MYSQL_TIME *time, uint precision)=0;
111  virtual bool store_date(MYSQL_TIME *time)=0;
112  virtual bool store_time(MYSQL_TIME *time, uint precision)=0;
113  virtual bool store(Field *field)=0;
114 
115  virtual bool send_out_parameters(List<Item_param> *sp_params)=0;
116 #ifdef EMBEDDED_LIBRARY
117  int begin_dataset();
118  virtual void remove_last_row() {}
119 #else
120  void remove_last_row() {}
121 #endif
122  enum enum_protocol_type
123  {
124  /*
125  Before adding a new type, please make sure
126  there is enough storage for it in Query_cache_query_flags.
127  */
128  PROTOCOL_TEXT= 0, PROTOCOL_BINARY= 1, PROTOCOL_LOCAL= 2
129  };
130  virtual enum enum_protocol_type type()= 0;
131 
132  void end_statement();
133 };
134 
135 
138 class Protocol_text :public Protocol
139 {
140 public:
141  Protocol_text() {}
142  Protocol_text(THD *thd_arg) :Protocol(thd_arg) {}
143  virtual void prepare_for_resend();
144  virtual bool store_null();
145  virtual bool store_tiny(longlong from);
146  virtual bool store_short(longlong from);
147  virtual bool store_long(longlong from);
148  virtual bool store_longlong(longlong from, bool unsigned_flag);
149  virtual bool store_decimal(const my_decimal *);
150  virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs);
151  virtual bool store(const char *from, size_t length,
152  const CHARSET_INFO *fromcs,
153  const CHARSET_INFO *tocs);
154  virtual bool store(MYSQL_TIME *time, uint precision);
155  virtual bool store_date(MYSQL_TIME *time);
156  virtual bool store_time(MYSQL_TIME *time, uint precision);
157  virtual bool store(float nr, uint32 decimals, String *buffer);
158  virtual bool store(double from, uint32 decimals, String *buffer);
159  virtual bool store(Field *field);
160 
161  virtual bool send_out_parameters(List<Item_param> *sp_params);
162 #ifdef EMBEDDED_LIBRARY
163  void remove_last_row();
164 #endif
165  virtual enum enum_protocol_type type() { return PROTOCOL_TEXT; };
166 };
167 
168 
170 {
171 private:
172  uint bit_fields;
173 public:
174  Protocol_binary() {}
175  Protocol_binary(THD *thd_arg) :Protocol(thd_arg) {}
176  virtual bool prepare_for_send(uint num_columns);
177  virtual void prepare_for_resend();
178 #ifdef EMBEDDED_LIBRARY
179  virtual bool write();
180  bool net_store_data(const uchar *from, size_t length);
181 #endif
182  virtual bool store_null();
183  virtual bool store_tiny(longlong from);
184  virtual bool store_short(longlong from);
185  virtual bool store_long(longlong from);
186  virtual bool store_longlong(longlong from, bool unsigned_flag);
187  virtual bool store_decimal(const my_decimal *);
188  virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs);
189  virtual bool store(const char *from, size_t length,
190  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
191  virtual bool store(MYSQL_TIME *time, uint precision);
192  virtual bool store_date(MYSQL_TIME *time);
193  virtual bool store_time(MYSQL_TIME *time, uint precision);
194  virtual bool store(float nr, uint32 decimals, String *buffer);
195  virtual bool store(double from, uint32 decimals, String *buffer);
196  virtual bool store(Field *field);
197 
198  virtual bool send_out_parameters(List<Item_param> *sp_params);
199 
200  virtual enum enum_protocol_type type() { return PROTOCOL_BINARY; };
201 };
202 
203 void send_warning(THD *thd, uint sql_errno, const char *err=0);
204 bool net_send_error(THD *thd, uint sql_errno, const char *err,
205  const char* sqlstate);
206 uchar *net_store_data(uchar *to,const uchar *from, size_t length);
207 uchar *net_store_data(uchar *to,int32 from);
208 uchar *net_store_data(uchar *to,longlong from);
209 
210 #endif /* PROTOCOL_INCLUDED */