sql_select.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:8k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. /* classes to use when handling where clause */
  17. #ifdef __GNUC__
  18. #pragma interface /* gcc class implementation */
  19. #endif
  20. #include "procedure.h"
  21. #include <myisam.h>
  22. typedef struct keyuse_t {
  23.   TABLE *table;
  24.   Item *val; /* or value if no field */
  25.   uint key,keypart;
  26.   table_map used_tables;
  27. } KEYUSE;
  28. class store_key;
  29. typedef struct st_table_ref
  30. {
  31.   bool key_err;
  32.   uint          key_parts;                // num of ...
  33.   uint          key_length;               // length of key_buff
  34.   int           key;                      // key no
  35.   byte          *key_buff;                // value to look for with key
  36.   byte          *key_buff2;               // key_buff+key_length
  37.   store_key     **key_copy;               //
  38.   Item          **items;                  // val()'s for each keypart
  39.   table_map depend_map;   // Table depends on these tables.
  40. } TABLE_REF;
  41. /*
  42. ** CACHE_FIELD and JOIN_CACHE is used on full join to cache records in outer
  43. ** table
  44. */
  45. typedef struct st_cache_field {
  46.   char *str;
  47.   uint length,blob_length;
  48.   Field_blob *blob_field;
  49.   bool strip;
  50. } CACHE_FIELD;
  51. typedef struct st_join_cache {
  52.   uchar *buff,*pos,*end;
  53.   uint records,record_nr,ptr_record,fields,length,blobs;
  54.   CACHE_FIELD *field,**blob_ptr;
  55.   SQL_SELECT *select;
  56. } JOIN_CACHE;
  57. /*
  58. ** The structs which holds the join connections and join states
  59. */
  60. enum join_type { JT_UNKNOWN,JT_SYSTEM,JT_CONST,JT_EQ_REF,JT_REF,JT_MAYBE_REF,
  61.  JT_ALL, JT_RANGE, JT_NEXT, JT_FT};
  62. class JOIN;
  63. typedef struct st_join_table {
  64.   TABLE *table;
  65.   KEYUSE *keyuse; /* pointer to first used key */
  66.   SQL_SELECT *select;
  67.   COND *select_cond;
  68.   QUICK_SELECT *quick;
  69.   Item *on_expr;
  70.   const char *info;
  71.   int (*read_first_record)(struct st_join_table *tab);
  72.   int (*next_select)(JOIN *,struct st_join_table *,bool);
  73.   READ_RECORD read_record;
  74.   double worst_seeks;
  75.   key_map const_keys; /* Keys with constant part */
  76.   key_map checked_keys; /* Keys checked in find_best */
  77.   key_map needed_reg;
  78.   ha_rows records,found_records,read_time;
  79.   table_map dependent,key_dependent;
  80.   uint keys; /* all keys with can be used */
  81.   uint use_quick,index;
  82.   uint status; // Save status for cache
  83.   uint used_fields,used_fieldlength,used_blobs;
  84.   enum join_type type;
  85.   bool cached_eq_ref_table,eq_ref_table,not_used_in_distinct;
  86.   TABLE_REF ref;
  87.   JOIN_CACHE cache;
  88. } JOIN_TAB;
  89. typedef struct st_position { /* Used in find_best */
  90.   double records_read;
  91.   JOIN_TAB *table;
  92.   KEYUSE *key;
  93. } POSITION;
  94. /* Param to create temporary tables when doing SELECT:s */
  95. class TMP_TABLE_PARAM {
  96.  public:
  97.   List<Item> copy_funcs;
  98.   Copy_field *copy_field;
  99.   byte     *group_buff;
  100.   Item_result_field **funcs;
  101.   MI_COLUMNDEF *recinfo,*start_recinfo;
  102.   KEY *keyinfo;
  103.   ha_rows end_write_records;
  104.   uint copy_field_count,field_count,sum_func_count,func_count;
  105.   uint  hidden_field_count;
  106.   uint group_parts,group_length;
  107.   uint quick_group;
  108.   bool  using_indirect_summary_function;
  109.   TMP_TABLE_PARAM() :copy_field(0), group_parts(0), group_length(0)
  110.   {}
  111.   ~TMP_TABLE_PARAM()
  112.   {
  113.     cleanup();
  114.   }
  115.   inline void cleanup(void)
  116.   {
  117.     delete [] copy_field;
  118.     copy_field=0;
  119.   }
  120. };
  121. class JOIN {
  122.  public:
  123.   JOIN_TAB *join_tab,**best_ref,**map2table;
  124.   TABLE    **table,**all_tables,*sort_by_table;
  125.   uint    tables,const_tables;
  126.   uint    send_group_parts;
  127.   bool    sort_and_group,first_record,full_join,group, no_field_update;
  128.   table_map const_table_map,outer_join;
  129.   ha_rows  send_records,found_records;
  130.   POSITION positions[MAX_TABLES+1],best_positions[MAX_TABLES+1];
  131.   double   best_read;
  132.   List<Item> *fields;
  133.   List<Item_buff> group_fields;
  134.   TABLE    *tmp_table;
  135.   THD    *thd;
  136.   Item_sum  **sum_funcs;
  137.   Procedure *procedure;
  138.   Item     *having;
  139.   uint     select_options;
  140.   select_result *result;
  141.   TMP_TABLE_PARAM tmp_table_param;
  142.   MYSQL_LOCK *lock;
  143. };
  144. typedef struct st_select_check {
  145.   uint const_ref,reg_ref;
  146. } SELECT_CHECK;
  147. extern const char *join_type_str[];
  148. void TEST_join(JOIN *join);
  149. /* Extern functions in sql_select.cc */
  150. bool store_val_in_field(Field *field,Item *val);
  151. TABLE *create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
  152. ORDER *group, bool distinct, bool save_sum_fields,
  153. bool allow_distinct_limit, uint select_options);
  154. void free_tmp_table(THD *thd, TABLE *entry);
  155. void count_field_types(TMP_TABLE_PARAM *param, List<Item> &fields,
  156.        bool reset_with_sum_func);
  157. bool setup_copy_fields(TMP_TABLE_PARAM *param,List<Item> &fields);
  158. void copy_fields(TMP_TABLE_PARAM *param);
  159. void copy_funcs(Item_result_field **func_ptr);
  160. bool create_myisam_from_heap(TABLE *table, TMP_TABLE_PARAM *param, int error,
  161.      bool ignore_last_dupp_error);
  162. /* functions from opt_sum.cc */
  163. int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds);
  164. /* class to copying an field/item to a key struct */
  165. class store_key :public Sql_alloc
  166. {
  167.  protected:
  168.   Field *to_field; // Store data here
  169.   Field *key_field; // Copy of key field
  170.   char *null_ptr;
  171.   char err;
  172.  public:
  173.   store_key(Field *field_arg, char *ptr, char *null, uint length)
  174.     :null_ptr(null),err(0)
  175.   {
  176.     if (field_arg->type() == FIELD_TYPE_BLOB)
  177.       to_field=new Field_varstring(ptr, length, (uchar*) null, 1, 
  178.    Field::NONE, field_arg->field_name,
  179.    field_arg->table, field_arg->binary());
  180.     else
  181.     {
  182.       to_field=field_arg->new_field(field_arg->table);
  183.       if (to_field)
  184. to_field->move_field(ptr, (uchar*) null, 1);
  185.     }
  186.   }
  187.   virtual ~store_key() {} /* Not actually needed */
  188.   virtual bool copy()=0;
  189.   virtual const char *name() const=0;
  190. };
  191. class store_key_field: public store_key
  192. {
  193.   Copy_field copy_field;
  194.   const char *field_name;
  195.  public:
  196.   store_key_field(Field *to_field_arg, char *ptr, char *null_ptr_arg,
  197.   uint length, Field *from_field, const char *name_arg)
  198.     :store_key(to_field_arg,ptr,
  199.        null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
  200.        : NullS,length), field_name(name_arg)
  201.   {
  202.     if (to_field)
  203.     {
  204.       copy_field.set(to_field,from_field,0);
  205.     }
  206.   }
  207.  bool copy()
  208.  {
  209.    copy_field.do_copy(&copy_field);
  210.    return err != 0;
  211.  }
  212.  const char *name() const { return field_name; }
  213. };
  214. class store_key_item :public store_key
  215. {
  216.  protected:
  217.   Item *item;
  218. public:
  219.   store_key_item(Field *to_field_arg, char *ptr, char *null_ptr_arg,
  220.  uint length, Item *item_arg)
  221.     :store_key(to_field_arg,ptr,
  222.        null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
  223.        &err : NullS, length), item(item_arg)
  224.   {}
  225.   bool copy()
  226.   {
  227.     item->save_in_field(to_field);
  228.     return err != 0;
  229.   }
  230.   const char *name() const { return "func"; }
  231. };
  232. class store_key_const_item :public store_key_item
  233. {
  234.   bool inited;
  235. public:
  236.   store_key_const_item(Field *to_field_arg, char *ptr,
  237.        char *null_ptr_arg, uint length,
  238.        Item *item_arg)
  239.     :store_key_item(to_field_arg,ptr,
  240.     null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
  241.     &err : NullS, length, item_arg), inited(0)
  242.   {
  243.   }
  244.   bool copy()
  245.   {
  246.     if (!inited)
  247.     {
  248.       inited=1;
  249.       item->save_in_field(to_field);
  250.     }
  251.     return err != 0;
  252.   }
  253.   const char *name() const { return "const"; }
  254. };
  255. bool cp_buffer_from_ref(TABLE_REF *ref);