My Project
protocol.h
00001 #ifndef PROTOCOL_INCLUDED
00002 #define PROTOCOL_INCLUDED
00003 
00004 /* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; version 2 of the License.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00018 
00019 #include "sql_error.h"
00020 #include "my_decimal.h"                         /* my_decimal */
00021 
00022 class i_string;
00023 class Field;
00024 class THD;
00025 class Item_param;
00026 typedef struct st_mysql_field MYSQL_FIELD;
00027 typedef struct st_mysql_rows MYSQL_ROWS;
00028 
00029 class Protocol
00030 {
00031 protected:
00032   THD    *thd;
00033   String *packet;
00034   String *convert;
00035   uint field_pos;
00036 #ifndef DBUG_OFF
00037   enum enum_field_types *field_types;
00038 #endif
00039   uint field_count;
00040 #ifndef EMBEDDED_LIBRARY
00041   bool net_store_data(const uchar *from, size_t length);
00042 #else
00043   virtual bool net_store_data(const uchar *from, size_t length);
00044   char **next_field;
00045   MYSQL_FIELD *next_mysql_field;
00046   MEM_ROOT *alloc;
00047 #endif
00048   bool net_store_data(const uchar *from, size_t length,
00049                       const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
00050   bool store_string_aux(const char *from, size_t length,
00051                         const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
00052 
00053   virtual bool send_ok(uint server_status, uint statement_warn_count,
00054                        ulonglong affected_rows, ulonglong last_insert_id,
00055                        const char *message);
00056 
00057   virtual bool send_eof(uint server_status, uint statement_warn_count);
00058 
00059   virtual bool send_error(uint sql_errno, const char *err_msg,
00060                           const char *sql_state);
00061 
00062 public:
00063   Protocol() {}
00064   Protocol(THD *thd_arg) { init(thd_arg); }
00065   virtual ~Protocol() {}
00066   void init(THD* thd_arg);
00067 
00068   enum { SEND_NUM_ROWS= 1, SEND_DEFAULTS= 2, SEND_EOF= 4 };
00069   virtual bool send_result_set_metadata(List<Item> *list, uint flags);
00070   bool send_result_set_row(List<Item> *row_items);
00071 
00072   bool store(I_List<i_string> *str_list);
00073   bool store(const char *from, const CHARSET_INFO *cs);
00074   String *storage_packet() { return packet; }
00075   inline void free() { packet->free(); }
00076   virtual bool write();
00077   inline  bool store(int from)
00078   { return store_long((longlong) from); }
00079   inline  bool store(uint32 from)
00080   { return store_long((longlong) from); }
00081   inline  bool store(longlong from)
00082   { return store_longlong((longlong) from, 0); }
00083   inline  bool store(ulonglong from)
00084   { return store_longlong((longlong) from, 1); }
00085   inline bool store(String *str)
00086   { return store((char*) str->ptr(), str->length(), str->charset()); }
00087 
00088   virtual bool prepare_for_send(uint num_columns)
00089   {
00090     field_count= num_columns;
00091     return 0;
00092   }
00093   virtual bool flush();
00094   virtual void end_partial_result_set(THD *thd);
00095   virtual void prepare_for_resend()=0;
00096 
00097   virtual bool store_null()=0;
00098   virtual bool store_tiny(longlong from)=0;
00099   virtual bool store_short(longlong from)=0;
00100   virtual bool store_long(longlong from)=0;
00101   virtual bool store_longlong(longlong from, bool unsigned_flag)=0;
00102   virtual bool store_decimal(const my_decimal *)=0;
00103   virtual bool store(const char *from, size_t length,
00104                      const CHARSET_INFO *cs)=0;
00105   virtual bool store(const char *from, size_t length, 
00106                      const CHARSET_INFO *fromcs,
00107                      const CHARSET_INFO *tocs)=0;
00108   virtual bool store(float from, uint32 decimals, String *buffer)=0;
00109   virtual bool store(double from, uint32 decimals, String *buffer)=0;
00110   virtual bool store(MYSQL_TIME *time, uint precision)=0;
00111   virtual bool store_date(MYSQL_TIME *time)=0;
00112   virtual bool store_time(MYSQL_TIME *time, uint precision)=0;
00113   virtual bool store(Field *field)=0;
00114 
00115   virtual bool send_out_parameters(List<Item_param> *sp_params)=0;
00116 #ifdef EMBEDDED_LIBRARY
00117   int begin_dataset();
00118   virtual void remove_last_row() {}
00119 #else
00120   void remove_last_row() {}
00121 #endif
00122   enum enum_protocol_type
00123   {
00124     /*
00125       Before adding a new type, please make sure
00126       there is enough storage for it in Query_cache_query_flags.
00127     */
00128     PROTOCOL_TEXT= 0, PROTOCOL_BINARY= 1, PROTOCOL_LOCAL= 2
00129   };
00130   virtual enum enum_protocol_type type()= 0;
00131 
00132   void end_statement();
00133 };
00134 
00135 
00138 class Protocol_text :public Protocol
00139 {
00140 public:
00141   Protocol_text() {}
00142   Protocol_text(THD *thd_arg) :Protocol(thd_arg) {}
00143   virtual void prepare_for_resend();
00144   virtual bool store_null();
00145   virtual bool store_tiny(longlong from);
00146   virtual bool store_short(longlong from);
00147   virtual bool store_long(longlong from);
00148   virtual bool store_longlong(longlong from, bool unsigned_flag);
00149   virtual bool store_decimal(const my_decimal *);
00150   virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs);
00151   virtual bool store(const char *from, size_t length,
00152                      const CHARSET_INFO *fromcs,
00153                      const CHARSET_INFO *tocs);
00154   virtual bool store(MYSQL_TIME *time, uint precision);
00155   virtual bool store_date(MYSQL_TIME *time);
00156   virtual bool store_time(MYSQL_TIME *time, uint precision);
00157   virtual bool store(float nr, uint32 decimals, String *buffer);
00158   virtual bool store(double from, uint32 decimals, String *buffer);
00159   virtual bool store(Field *field);
00160 
00161   virtual bool send_out_parameters(List<Item_param> *sp_params);
00162 #ifdef EMBEDDED_LIBRARY
00163   void remove_last_row();
00164 #endif
00165   virtual enum enum_protocol_type type() { return PROTOCOL_TEXT; };
00166 };
00167 
00168 
00169 class Protocol_binary :public Protocol
00170 {
00171 private:
00172   uint bit_fields;
00173 public:
00174   Protocol_binary() {}
00175   Protocol_binary(THD *thd_arg) :Protocol(thd_arg) {}
00176   virtual bool prepare_for_send(uint num_columns);
00177   virtual void prepare_for_resend();
00178 #ifdef EMBEDDED_LIBRARY
00179   virtual bool write();
00180   bool net_store_data(const uchar *from, size_t length);
00181 #endif
00182   virtual bool store_null();
00183   virtual bool store_tiny(longlong from);
00184   virtual bool store_short(longlong from);
00185   virtual bool store_long(longlong from);
00186   virtual bool store_longlong(longlong from, bool unsigned_flag);
00187   virtual bool store_decimal(const my_decimal *);
00188   virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs);
00189   virtual bool store(const char *from, size_t length,
00190                      const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
00191   virtual bool store(MYSQL_TIME *time, uint precision);
00192   virtual bool store_date(MYSQL_TIME *time);
00193   virtual bool store_time(MYSQL_TIME *time, uint precision);
00194   virtual bool store(float nr, uint32 decimals, String *buffer);
00195   virtual bool store(double from, uint32 decimals, String *buffer);
00196   virtual bool store(Field *field);
00197 
00198   virtual bool send_out_parameters(List<Item_param> *sp_params);
00199 
00200   virtual enum enum_protocol_type type() { return PROTOCOL_BINARY; };
00201 };
00202 
00203 void send_warning(THD *thd, uint sql_errno, const char *err=0);
00204 bool net_send_error(THD *thd, uint sql_errno, const char *err,
00205                     const char* sqlstate);
00206 uchar *net_store_data(uchar *to,const uchar *from, size_t length);
00207 uchar *net_store_data(uchar *to,int32 from);
00208 uchar *net_store_data(uchar *to,longlong from);
00209 
00210 #endif /* PROTOCOL_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines