summaryrefslogtreecommitdiff
path: root/sql/item_subselect.h
blob: 92839eb0e5f820e15c63618f991dcf311887e5c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Copyright (C) 2000 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; either version 2 of the License, or
   (at your option) any later version.

   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 */

/* subselect Item */

#ifdef __GNUC__
#pragma interface			/* gcc class implementation */
#endif

class st_select_lex;
class st_select_lex_unit;
class JOIN;
class select_subselect;
class subselect_engine;

/* base class for subselects */

class Item_subselect :public Item
{
  my_bool engine_owner; /* Is this item owner of engine */
  my_bool value_assigned; /* value already assigned to subselect */
protected:
  /* engine that perform execution of subselect (single select or union) */
  subselect_engine *engine; 
  /* allowed number of columns (1 for single value subqueries) */
  uint max_columns;

public:
  Item_subselect(THD *thd, st_select_lex *select_lex,
		 select_subselect* result);
  Item_subselect(Item_subselect *item)
  {
    null_value= item->null_value;
    decimals= item->decimals;
    max_columns= item->max_columns;
    engine= item->engine;
    engine_owner= 0;
    name= item->name;
  }
  ~Item_subselect();
  virtual void assign_null() 
  {
    null_value= 1;
  }
  bool assigned() { return value_assigned; }
  void assigned(bool a) { value_assigned= a; }
  enum Type type() const;
  bool is_null() { return null_value; }
  void make_field (Send_field *);
  bool fix_fields(THD *thd, TABLE_LIST *tables, Item **ref);
  virtual void fix_length_and_dec();
  table_map used_tables() const;

  friend class select_subselect;
};


/* single value subselect */

class Item_singleval_subselect :public Item_subselect
{
protected:
  longlong int_value; /* here stored integer value of this item */
  double real_value; /* here stored real value of this item */
  enum Item_result res_type; /* type of results */

public:
  Item_singleval_subselect(THD *thd, st_select_lex *select_lex);
  Item_singleval_subselect(Item_singleval_subselect *item):
    Item_subselect(item)
  {
    int_value= item->int_value;
    real_value= item->real_value;
    max_length= item->max_length;
    decimals= item->decimals;
    res_type= item->res_type;
  }
  virtual void assign_null() 
  {
    null_value= 1;
    int_value= 0;
    real_value= 0;
    max_length= 4;
    res_type= STRING_RESULT;
  }
  double val ();
  longlong val_int ();
  String *val_str (String *);
  Item *new_item() { return new Item_singleval_subselect(this); }
  enum Item_result result_type() const { return res_type; }
  void fix_length_and_dec();
  friend class select_singleval_subselect;
};

/* exists subselect */

class Item_exists_subselect :public Item_subselect
{
protected:
  longlong value; /* value of this item (boolean: exists/not-exists) */

public:
  Item_exists_subselect(THD *thd, st_select_lex *select_lex);
  Item_exists_subselect(Item_exists_subselect *item):
    Item_subselect(item)
  {
    value= item->value;
  }
  virtual void assign_null() 
  {
    value= 0;
  }

  Item *new_item() { return new Item_exists_subselect(this); }
  enum Item_result result_type() const { return INT_RESULT;}
  longlong val_int();
  double val();
  String *val_str(String*);
  void fix_length_and_dec();
  friend class select_exists_subselect;
};

class subselect_engine
{
protected:
  select_subselect *result; /* results storage class */
  THD *thd; /* pointer to current THD */
  Item_subselect *item; /* item, that use this engine */
  enum Item_result res_type; /* type of results */
public:
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
  static void operator delete(void *ptr, size_t size) {}

  subselect_engine(THD *thd, Item_subselect *si, select_subselect *res) 
  {
    result= res;
    item= si;
    this->thd= thd;
    res_type= STRING_RESULT;
  }

  virtual int prepare()= 0;
  virtual void fix_length_and_dec()= 0;
  virtual int exec()= 0;
  virtual uint cols()= 0; /* return number of columnss in select */
  virtual bool depended()= 0; /* depended from outer select */
  enum Item_result type() { return res_type; }
};

class subselect_single_select_engine: public subselect_engine
{
  my_bool executed; /* simple subselect is executed */
  my_bool optimized; /* simple subselect is optimized */
  st_select_lex *select_lex; /* corresponding select_lex */
  JOIN * join; /* corresponding JOIN structure */
public:
  subselect_single_select_engine(THD *thd, st_select_lex *select,
				 select_subselect *result,
				 Item_subselect *item);
  virtual int prepare();
  virtual void fix_length_and_dec();
  virtual int exec();
  virtual uint cols();
  virtual bool depended();
};

class subselect_union_engine: public subselect_engine
{
  st_select_lex_unit *unit;  /* corresponding unit structure */
public:
  subselect_union_engine(THD *thd,
			 st_select_lex_unit *u,
			 select_subselect *result,
			 Item_subselect *item);
  virtual int prepare();
  virtual void fix_length_and_dec();
  virtual int exec();
  virtual uint cols();
  virtual bool depended();
};