00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <drizzled/field_iterator.h>
00022 #include <drizzled/table_list.h>
00023 #include <drizzled/session.h>
00024 #include <drizzled/sql_lex.h>
00025 #include <drizzled/table.h>
00026
00027 namespace drizzled {
00028
00029 const char *Field_iterator_table::name() const
00030 {
00031 return (*ptr)->field_name;
00032 }
00033
00034
00035 void Field_iterator_table::set(TableList *table)
00036 {
00037 ptr= table->table->getFields();
00038 }
00039
00040
00041 void Field_iterator_table::set_table(Table *table)
00042 {
00043 ptr= table->getFields();
00044 }
00045
00046
00047 Item *Field_iterator_table::create_item(Session *session)
00048 {
00049 return new Item_field(session, &session->lex().current_select->context, *ptr);
00050 }
00051
00052
00053 void Field_iterator_natural_join::set(TableList *table_ref)
00054 {
00055 assert(table_ref->join_columns);
00056 column_ref_it= table_ref->join_columns->begin();
00057 cur_column_ref= column_ref_it++;
00058 }
00059
00060
00061 void Field_iterator_natural_join::next()
00062 {
00063 cur_column_ref= column_ref_it++;
00064 assert(!cur_column_ref || ! cur_column_ref->table_field ||
00065 cur_column_ref->table_ref->table ==
00066 cur_column_ref->table_field->getTable());
00067 }
00068
00069
00070 void Field_iterator_table_ref::set_field_iterator()
00071 {
00072
00073
00074
00075
00076
00077
00078
00079 if (table_ref->is_join_columns_complete)
00080 {
00081 field_it= &natural_join_it;
00082 }
00083
00084 else
00085 {
00086 assert(table_ref->table);
00087 field_it= &table_field_it;
00088 }
00089 field_it->set(table_ref);
00090 return;
00091 }
00092
00093
00094 void Field_iterator_table_ref::set(TableList *table)
00095 {
00096 assert(table);
00097 first_leaf= table->first_leaf_for_name_resolution();
00098 last_leaf= table->last_leaf_for_name_resolution();
00099 assert(first_leaf && last_leaf);
00100 table_ref= first_leaf;
00101 set_field_iterator();
00102 }
00103
00104
00105 void Field_iterator_table_ref::next()
00106 {
00107
00108 field_it->next();
00109
00110
00111
00112
00113 if (field_it->end_of_fields() && table_ref != last_leaf)
00114 {
00115 table_ref= table_ref->next_name_resolution_table;
00116 assert(table_ref);
00117 set_field_iterator();
00118 }
00119 }
00120
00121
00122 const char *Field_iterator_table_ref::table_name()
00123 {
00124 if (table_ref->is_natural_join)
00125 return natural_join_it.column_ref()->table_name();
00126
00127 assert(!strcmp(table_ref->getTableName(),
00128 table_ref->table->getShare()->getTableName()));
00129 return table_ref->getTableName();
00130 }
00131
00132
00133 const char *Field_iterator_table_ref::db_name()
00134 {
00135 if (table_ref->is_natural_join)
00136 return natural_join_it.column_ref()->db_name();
00137
00138
00139
00140
00141
00142 assert(!strcmp(table_ref->getSchemaName(), table_ref->table->getShare()->getSchemaName()));
00143 return table_ref->getSchemaName();
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 Natural_join_column *
00186 Field_iterator_table_ref::get_or_create_column_ref(TableList *parent_table_ref)
00187 {
00188 Natural_join_column *nj_col;
00189 bool is_created= true;
00190 uint32_t field_count=0;
00191 TableList *add_table_ref= parent_table_ref ?
00192 parent_table_ref : table_ref;
00193
00194 if (field_it == &table_field_it)
00195 {
00196
00197 Field *tmp_field= table_field_it.field();
00198 nj_col= new Natural_join_column(tmp_field, table_ref);
00199 field_count= table_ref->table->getShare()->sizeFields();
00200 }
00201 else
00202 {
00203
00204
00205
00206
00207
00208 assert(table_ref->is_join_columns_complete);
00209 is_created= false;
00210 nj_col= natural_join_it.column_ref();
00211 assert(nj_col);
00212 }
00213 assert(!nj_col->table_field ||
00214 nj_col->table_ref->table == nj_col->table_field->getTable());
00215
00216
00217
00218
00219
00220
00221 if (is_created)
00222 {
00223
00224 assert(!add_table_ref->is_join_columns_complete);
00225 if (!add_table_ref->join_columns)
00226 {
00227
00228 add_table_ref->join_columns= new List<Natural_join_column>;
00229 add_table_ref->is_join_columns_complete= false;
00230 }
00231 add_table_ref->join_columns->push_back(nj_col);
00232
00233
00234
00235
00236
00237
00238
00239 if (!parent_table_ref &&
00240 add_table_ref->join_columns->size() == field_count)
00241 add_table_ref->is_join_columns_complete= true;
00242 }
00243
00244 return nj_col;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 Natural_join_column *
00265 Field_iterator_table_ref::get_natural_column_ref()
00266 {
00267 assert(field_it == &natural_join_it);
00268
00269
00270
00271
00272
00273 Natural_join_column* nj_col= natural_join_it.column_ref();
00274 assert(nj_col && (!nj_col->table_field || nj_col->table_ref->table == nj_col->table_field->getTable()));
00275 return nj_col;
00276 }
00277
00278 }