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
00030 const char *Field_iterator_table::name()
00031 {
00032 return (*ptr)->field_name;
00033 }
00034
00035
00036 void Field_iterator_table::set(TableList *table)
00037 {
00038 ptr= table->table->getFields();
00039 }
00040
00041
00042 void Field_iterator_table::set_table(Table *table)
00043 {
00044 ptr= table->getFields();
00045 }
00046
00047
00048 Item *Field_iterator_table::create_item(Session *session)
00049 {
00050 return new Item_field(session, &session->lex().current_select->context, *ptr);
00051 }
00052
00053
00054 void Field_iterator_natural_join::set(TableList *table_ref)
00055 {
00056 assert(table_ref->join_columns);
00057 column_ref_it= table_ref->join_columns->begin();
00058 cur_column_ref= column_ref_it++;
00059 }
00060
00061
00062 void Field_iterator_natural_join::next()
00063 {
00064 cur_column_ref= column_ref_it++;
00065 assert(!cur_column_ref || ! cur_column_ref->table_field ||
00066 cur_column_ref->table_ref->table ==
00067 cur_column_ref->table_field->getTable());
00068 }
00069
00070
00071 void Field_iterator_table_ref::set_field_iterator()
00072 {
00073
00074
00075
00076
00077
00078
00079
00080 if (table_ref->is_join_columns_complete)
00081 {
00082 field_it= &natural_join_it;
00083 }
00084
00085 else
00086 {
00087 assert(table_ref->table);
00088 field_it= &table_field_it;
00089 }
00090 field_it->set(table_ref);
00091 return;
00092 }
00093
00094
00095 void Field_iterator_table_ref::set(TableList *table)
00096 {
00097 assert(table);
00098 first_leaf= table->first_leaf_for_name_resolution();
00099 last_leaf= table->last_leaf_for_name_resolution();
00100 assert(first_leaf && last_leaf);
00101 table_ref= first_leaf;
00102 set_field_iterator();
00103 }
00104
00105
00106 void Field_iterator_table_ref::next()
00107 {
00108
00109 field_it->next();
00110
00111
00112
00113
00114 if (field_it->end_of_fields() && table_ref != last_leaf)
00115 {
00116 table_ref= table_ref->next_name_resolution_table;
00117 assert(table_ref);
00118 set_field_iterator();
00119 }
00120 }
00121
00122
00123 const char *Field_iterator_table_ref::table_name()
00124 {
00125 if (table_ref->is_natural_join)
00126 return natural_join_it.column_ref()->table_name();
00127
00128 assert(!strcmp(table_ref->getTableName(),
00129 table_ref->table->getShare()->getTableName()));
00130 return table_ref->getTableName();
00131 }
00132
00133
00134 const char *Field_iterator_table_ref::db_name()
00135 {
00136 if (table_ref->is_natural_join)
00137 return natural_join_it.column_ref()->db_name();
00138
00139
00140
00141
00142
00143 assert(!strcmp(table_ref->getSchemaName(), table_ref->table->getShare()->getSchemaName()));
00144 return table_ref->getSchemaName();
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
00186 Natural_join_column *
00187 Field_iterator_table_ref::get_or_create_column_ref(TableList *parent_table_ref)
00188 {
00189 Natural_join_column *nj_col;
00190 bool is_created= true;
00191 uint32_t field_count=0;
00192 TableList *add_table_ref= parent_table_ref ?
00193 parent_table_ref : table_ref;
00194
00195 if (field_it == &table_field_it)
00196 {
00197
00198 Field *tmp_field= table_field_it.field();
00199 nj_col= new Natural_join_column(tmp_field, table_ref);
00200 field_count= table_ref->table->getShare()->sizeFields();
00201 }
00202 else
00203 {
00204
00205
00206
00207
00208
00209 assert(table_ref->is_join_columns_complete);
00210 is_created= false;
00211 nj_col= natural_join_it.column_ref();
00212 assert(nj_col);
00213 }
00214 assert(!nj_col->table_field ||
00215 nj_col->table_ref->table == nj_col->table_field->getTable());
00216
00217
00218
00219
00220
00221
00222 if (is_created)
00223 {
00224
00225 assert(!add_table_ref->is_join_columns_complete);
00226 if (!add_table_ref->join_columns)
00227 {
00228
00229 if (!(add_table_ref->join_columns= new List<Natural_join_column>))
00230 return NULL;
00231 add_table_ref->is_join_columns_complete= false;
00232 }
00233 add_table_ref->join_columns->push_back(nj_col);
00234
00235
00236
00237
00238
00239
00240
00241 if (!parent_table_ref &&
00242 add_table_ref->join_columns->size() == field_count)
00243 add_table_ref->is_join_columns_complete= true;
00244 }
00245
00246 return nj_col;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 Natural_join_column *
00267 Field_iterator_table_ref::get_natural_column_ref()
00268 {
00269 Natural_join_column *nj_col;
00270
00271 assert(field_it == &natural_join_it);
00272
00273
00274
00275
00276
00277 nj_col= natural_join_it.column_ref();
00278 assert(nj_col &&
00279 (!nj_col->table_field ||
00280 nj_col->table_ref->table == nj_col->table_field->getTable()));
00281 return nj_col;
00282 }
00283
00284 }