25#include <drizzled/internal/my_pthread.h>
26#include <drizzled/internal/thread_var.h>
28#include <drizzled/sql_select.h>
29#include <drizzled/error.h>
30#include <drizzled/gettext.h>
31#include <drizzled/nested_join.h>
32#include <drizzled/sql_base.h>
34#include <drizzled/item/cmpfunc.h>
35#include <drizzled/replication_services.h>
36#include <drizzled/check_stack_overrun.h>
37#include <drizzled/lock.h>
38#include <drizzled/plugin/listen.h>
40#include <drizzled/field/epoch.h>
41#include <drizzled/field/null.h>
43#include <drizzled/charset.h>
44#include <drizzled/pthread_globals.h>
45#include <drizzled/internal/iocache.h>
46#include <drizzled/drizzled.h>
47#include <drizzled/plugin/authorization.h>
48#include <drizzled/table/temporary.h>
49#include <drizzled/table/placeholder.h>
50#include <drizzled/table/unused.h>
51#include <drizzled/plugin/storage_engine.h>
52#include <drizzled/session.h>
53#include <drizzled/item/subselect.h>
54#include <drizzled/sql_lex.h>
55#include <drizzled/catalog/local.h>
56#include <drizzled/open_tables_state.h>
57#include <drizzled/table/cache.h>
63extern bool volatile shutdown_in_progress;
65void table_cache_free()
69 table::getUnused().clear();
70 table::getCache().clear();
91void close_handle_and_leave_table_as_lock(
Table *table)
93 assert(table->db_stat);
94 assert(table->getShare()->getType() == message::Table::STANDARD);
101 identifier::Table identifier(table->getShare()->getTableIdentifier().getCatalog(), table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
102 TableShare *share=
new TableShare(identifier.getType(), identifier, identifier.getKey().vector(),
static_cast<uint32_t
>(table->getShare()->getCacheKeySize()));
104 table->cursor->close();
106 table::instance::release(table->getMutableShare());
107 table->setShare(share);
116void Table::intern_close_table()
127void Table::free_io_cache()
131 sort.io_cache->close_cached_file();
132 safe_delete(sort.io_cache);
152bool Session::close_cached_tables(
TableList *tables,
bool wait_for_refresh,
bool wait_for_placeholders)
155 Session *session=
this;
158 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
162 table::getUnused().clear();
167 for (TableList *table= tables; table; table= table->next_local)
169 if (table::Cache::removeTable(*session, identifier::Table(catalog().identifier(), table->getSchemaName(), table->getTableName()), RTFC_OWNED_BY_Session_FLAG))
175 wait_for_refresh=
false;
178 if (wait_for_refresh)
184 session->mysys_var->current_mutex= &table::Cache::mutex();
185 session->mysys_var->current_cond= &COND_refresh;
186 session->set_proc_info(
"Flushing tables");
188 session->close_old_data_files();
192 while (found && ! session->getKilled())
195 for (table::CacheMap::const_iterator iter= table::getCache().begin();
196 iter != table::getCache().end();
199 Table *table= iter->second;
201 if (table->in_use == session)
217 if (table->needs_reopen_or_name_lock() && (table->db_stat ||
218 (table->open_placeholder && wait_for_placeholders)))
221 COND_refresh.wait(scopedLock);
231 result= session->reopen_tables();
234 for (Table *table= session->open_tables.open_tables_; table ; table= table->getNext())
240 if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
241 table->getMutableShare()->refreshVersion();
246 if (wait_for_refresh)
248 boost::mutex::scoped_lock scopedLock(session->mysys_var->mutex);
249 session->mysys_var->current_mutex= 0;
250 session->mysys_var->current_cond= 0;
251 session->set_proc_info(0);
266 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
267 assert(table->key_read == 0);
268 assert(not table->cursor || table->cursor->inited == Cursor::NONE);
272 if (table->needs_reopen_or_name_lock() ||
273 version != g_refresh_version || !table->db_stat)
275 table::remove_table(table);
282 assert(not table->open_placeholder);
285 table->cursor->ha_reset();
288 table::getUnused().link(table);
303 bool found_old_table=
false;
305 safe_mutex_assert_not_owner(table::Cache::mutex().native_handle());
307 boost::mutex::scoped_lock scoped_lock(table::Cache::mutex());
341 const char *table_name)
343 for (; table; table= table->*link)
345 if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD)
346 && not system_charset_info->strcasecmp(table->getSchemaName(), db_name)
347 && not system_charset_info->strcasecmp(table->getTableName(), table_name))
396 const char *d_name, *t_name, *t_alias;
411 if (table->table && table->table->getShare()->getType() != message::Table::STANDARD)
413 table= table->find_underlying_table(table->table);
420 d_name= table->getSchemaName();
421 t_name= table->getTableName();
422 t_alias= table->alias;
426 if ((! (res= find_table_in_global_list(table_list, d_name, t_name))) ||
427 ((!res->table || res->table != table->table) &&
428 (!check_alias || !(files_charset_info->strcasecmp(t_alias, res->alias))) &&
429 res->select_lex && !res->select_lex->exclude_from_table_unique_test))
436 table_list= res->next_global;
443 std::set<std::string>& set_of_names)
445 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
447 if (schema_identifier.compare(table->getShare()->getSchemaName()))
449 set_of_names.insert(table->getShare()->getTableName());
456 std::set<std::string> &set_of_names)
458 doGetTableNames(schema_identifier, set_of_names);
461void Open_tables_state::doGetTableIdentifiers(
const identifier::Schema &schema_identifier,
462 identifier::table::vector &set_of_identifiers)
464 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
466 if (schema_identifier.compare(table->getShare()->getSchemaName()))
468 set_of_identifiers.push_back(identifier::Table(table->getShare()->getTableIdentifier().getCatalog(),
469 table->getShare()->getSchemaName(),
470 table->getShare()->getTableName(),
471 table->getShare()->getPath()));
478 identifier::table::vector &set_of_identifiers)
480 doGetTableIdentifiers(schema_identifier, set_of_identifiers);
485 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
487 if (table->getShare()->getType() == message::Table::TEMPORARY)
489 if (identifier.getKey() == table->getShare()->getCacheKey())
500 message::Table &table_proto)
502 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
504 if (table->getShare()->getType() == message::Table::TEMPORARY)
506 if (identifier.getKey() == table->getShare()->getCacheKey())
508 table_proto.CopyFrom(*(table->getShare()->getTableMessage()));
522 if (identifier.getKey() == table->getShare()->getCacheKey())
558 Table* table= find_temporary_table(identifier);
563 if (table->query_id && table->query_id != session_.getQueryId())
565 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
568 close_temporary_table(table);
587 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
596 for (prev= &open_tables.open_tables_; *prev; )
600 if (list->getShare()->getCacheKey() == find_key)
603 *prev= list->getNext();
611 prev= list->getNextPtr();
641 if (table->getShare()->getType())
643 open_tables.close_temporary_table(table);
647 boost::mutex::scoped_lock scoped_lock(table::Cache::mutex());
653 (void)plugin::StorageEngine::dropTable(*
this, identifier);
669void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
672 const char *saved_proc_info;
673 mysys_var->current_mutex= &mutex;
674 mysys_var->current_cond= &cond;
675 saved_proc_info= get_proc_info();
688 boost::mutex::scoped_lock scopedLock(mutex, boost::adopt_lock_t());
691 cond.wait(scopedLock);
694 boost::mutex::scoped_lock mysys_scopedLock(mysys_var->mutex);
695 mysys_var->current_mutex= 0;
696 mysys_var->current_cond= 0;
716 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
724 message::Table::INTERNAL);
726 table::Cache::insert(table);
755 boost::mutex::scoped_lock scope_lock(table::Cache::mutex());
756 if (find_ptr(table::getCache(), key))
759 table.open_placeholder=
true;
760 table.setNext(open_tables.open_tables_);
761 open_tables.open_tables_= &table;
798Table *Session::openTable(
TableList *table_list,
bool *refresh, uint32_t flags)
801 const char *alias= table_list->alias;
804 assert(lex().is_lex_started);
823 identifier::Table identifier(catalog().identifier(),
824 table_list->getSchemaName(),
825 table_list->getTableName());
826 const identifier::Table::Key &key(identifier.getKey());
827 table::CacheRange ppp;
837 for (table= open_tables.getTemporaryTables(); table ; table=table->getNext())
839 if (table->getShare()->getCacheKey() == key)
849 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
860 if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
862 my_error(ER_TABLE_UNKNOWN, identifier);
875 if (!open_tables.open_tables_)
877 open_tables.version= g_refresh_version;
879 else if ((open_tables.version != g_refresh_version) &&
880 ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
905 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
917 ppp= table::getCache().equal_range(key);
920 for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter, table= NULL)
924 if (not table->in_use)
944 if (table->needs_reopen_or_name_lock())
946 if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
949 open_tables.version= table->getShare()->getVersion();
954 if (table->open_placeholder && table->in_use ==
this)
956 my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
986 if (table->in_use !=
this)
989 wait_for_condition(table::Cache::mutex(), COND_refresh);
990 scopedLock.release();
1010 table::getUnused().unlink(
static_cast<table::Concurrent *
>(table));
1011 table->in_use=
this;
1017 table::getUnused().cull();
1019 if (table_list->isCreate())
1021 identifier::Table lock_table_identifier(catalog().identifier(),
1022 table_list->getSchemaName(),
1023 table_list->getTableName(),
1024 message::Table::STANDARD);
1037 table->open_placeholder=
true;
1038 table->setNext(open_tables.open_tables_);
1039 open_tables.open_tables_= table;
1048 table::Concurrent *new_table=
new table::Concurrent;
1050 if (new_table->open_unireg_entry(
this, alias, identifier))
1055 (void)table::Cache::insert(new_table);
1062 table->setNext(open_tables.open_tables_);
1063 open_tables.open_tables_= table;
1065 table->reginfo.lock_type= TL_READ;
1068 assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1071 if (strcmp(table->getAlias(), alias))
1073 table->setAlias(alias);
1077 table->tablenr= open_tables.current_tablenr++;
1078 table->used_fields= 0;
1079 table->const_table= 0;
1080 table->null_row=
false;
1081 table->maybe_null=
false;
1082 table->force_index=
false;
1083 table->status=STATUS_NO_RECORD;
1084 table->insert_values.clear();
1086 assert(!table->auto_increment_field_not_null);
1087 table->auto_increment_field_not_null=
false;
1088 if (table->timestamp_field)
1090 table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1092 table->pos_in_table_list= table_list;
1093 table->clear_column_bitmaps();
1094 assert(table->key_read == 0);
1119 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1121 if (open_tables.lock)
1127 unlockTables(open_tables.lock);
1128 open_tables.lock= 0;
1136 for (
Table *table= open_tables.open_tables_; table ; table=table->getNext())
1138 if (table->getShare()->getCacheKey() == identifier.getKey())
1140 table->open_placeholder=
true;
1141 close_handle_and_leave_table_as_lock(table);
1169 Table *table,*next,**prev;
1171 Table **tables_ptr= 0;
1173 const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1174 DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1175 DRIZZLE_LOCK_IGNORE_FLUSH;
1177 if (open_tables.open_tables_ == NULL)
1180 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1188 for (table= open_tables.open_tables_; table ; table=table->getNext())
1192 tables=
new Table *[opens];
1197 prev= &open_tables.open_tables_;
1198 for (table= open_tables.open_tables_; table ; table=next)
1200 next= table->getNext();
1202 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1208 if (tables != tables_ptr)
1216 if (not lockTables(tables, (uint32_t) (tables_ptr - tables), flags))
1223 my_error(ER_LOCK_DEADLOCK, MYF(0));
1252 bool found= send_refresh;
1254 Table *table= open_tables.open_tables_;
1256 for (; table ; table=table->getNext())
1261 if (table->needs_reopen_or_name_lock())
1268 Table *ulcktbl= table;
1281 if ((ulcktbl != table) && ulcktbl->
db_stat)
1289 ulcktbl->open_placeholder=
true;
1290 close_handle_and_leave_table_as_lock(ulcktbl);
1296 table->open_placeholder=
true;
1298 close_handle_and_leave_table_as_lock(table);
1300 else if (table->open_placeholder && !morph_locks)
1313 table->open_placeholder=
false;
1348 Table *table,*next,**prev, *found= 0;
1349 prev= &session->open_tables.open_tables_;
1358 for (table= session->open_tables.open_tables_; table ; table=next)
1360 next=table->getNext();
1361 if (table->getShare()->getCacheKey() == identifier.getKey())
1363 session->removeLock(table);
1372 table->cursor->close();
1384 prev= table->getNextPtr();
1402void abort_locked_tables(
Session *session,
const drizzled::identifier::Table &identifier)
1405 for (table= session->open_tables.open_tables_; table ; table= table->getNext())
1407 if (table->getShare()->getCacheKey() == identifier.getKey())
1410 session->abortLock(table);
1446int Session::open_tables_from_list(
TableList **start, uint32_t *counter, uint32_t flags)
1448 TableList *tables= NULL;
1452 bool safe_to_ignore_table;
1454 open_tables.current_tablenr= 0;
1463 for (tables= *start; tables ;tables= tables->next_global)
1465 safe_to_ignore_table=
false;
1473 if (tables->derived)
1484 identifier::Table the_table(catalog().identifier(),
1485 tables->getSchemaName(),
1486 tables->getTableName());
1498 if (tables->table == NULL)
1499 tables->table= openTable(tables, &refresh, flags);
1501 if (tables->table == NULL)
1523 if (safe_to_ignore_table)
1529 if (tables->lock_type != TL_UNLOCK)
1531 if (tables->lock_type == TL_WRITE_DEFAULT)
1532 tables->table->reginfo.lock_type= update_lock_default;
1533 else if (tables->table->getShare()->getType() == message::Table::STANDARD)
1534 tables->table->reginfo.lock_type= tables->lock_type;
1540 if (result && tables)
1546 tables->table= NULL;
1577Table *Session::openTableLock(
TableList *table_list, thr_lock_type lock_type)
1583 open_tables.current_tablenr= 0;
1584 while (!(table= openTable(table_list, &refresh)) && refresh) ;
1588 table_list->lock_type= lock_type;
1589 table_list->table= table;
1591 assert(open_tables.lock == 0);
1592 if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
1594 if (not (open_tables.lock= lockTables(&table_list->table, 1, 0)))
1632int Session::lock_tables(
TableList *tables, uint32_t count,
bool *need_reopen)
1638 *need_reopen=
false;
1643 assert(not open_tables.lock);
1646 Table** ptr=start=
new (mem) Table*[count];
1647 for (TableList* table= tables; table; table= table->next_global)
1649 if (!table->placeholder())
1650 *(ptr++)= table->table;
1652 if (not (open_tables.lock= lockTables(start, (uint32_t) (ptr - start), DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN)))
1682 assert(identifier.isTmp());
1683 table::Temporary *new_tmp_table=
new table::Temporary(identifier.getType(), identifier,
1684 identifier.getPath().c_str(),
static_cast<uint32_t
>(identifier.getPath().length()));
1688 if (new_tmp_table->getMutableShare()->open_table_def(*
this, identifier) ||
1689 new_tmp_table->getMutableShare()->open_table_from_share(
this, identifier, identifier.getTableName().c_str(),
1690 (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE | HA_GET_INDEX), ha_open_options, *new_tmp_table))
1693 delete new_tmp_table->getMutableShare();
1694 delete new_tmp_table;
1698 new_tmp_table->reginfo.lock_type= TL_WRITE;
1703 new_tmp_table->setNext(open_tables.temporary_tables);
1704 if (new_tmp_table->getNext())
1706 new_tmp_table->getNext()->setPrev(new_tmp_table);
1708 open_tables.temporary_tables= new_tmp_table;
1709 open_tables.temporary_tables->setPrev(0);
1711 new_tmp_table->pos_in_table_list= 0;
1713 return new_tmp_table;
1730static void update_field_dependencies(
Session *session,
Field *field,
Table *table)
1732 if (session->mark_used_columns != MARK_COLUMNS_NONE)
1734 boost::dynamic_bitset<> *current_bitmap= NULL;
1741 table->covering_keys&= field->part_of_key;
1742 table->merge_keys|= field->part_of_key;
1744 if (session->mark_used_columns == MARK_COLUMNS_READ)
1746 current_bitmap= table->read_set;
1750 current_bitmap= table->write_set;
1754 if (current_bitmap->test(field->position()))
1756 if (session->mark_used_columns == MARK_COLUMNS_WRITE)
1757 session->dup_field= field;
1760 table->used_fields++;
1795 const char *name, uint32_t ,
Item **,
1798 List<Natural_join_column>::iterator field_it(table_ref->join_columns->begin());
1802 assert(table_ref->is_natural_join && table_ref->join_columns);
1803 assert(*actual_table == NULL);
1805 for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; curr_nj_col= field_it++)
1807 if (!system_charset_info->strcasecmp(curr_nj_col->name(), name))
1811 my_error(ER_NON_UNIQ_ERROR, MYF(0), name, session->where());
1814 nj_col= curr_nj_col;
1821 assert(nj_col->table_ref->table == nj_col->table_field->getTable());
1822 found_field= nj_col->table_field;
1823 update_field_dependencies(session, found_field, nj_col->table_ref->table);
1826 *actual_table= nj_col->table_ref;
1828 return(found_field);
1851find_field_in_table(
Session *session,
Table *table,
const char *name, uint32_t length,
1852 bool allow_rowid, uint32_t *cached_field_index_ptr)
1854 Field **field_ptr, *field;
1855 uint32_t cached_field_index= *cached_field_index_ptr;
1858 if (cached_field_index < table->getShare()->sizeFields()
1859 && not system_charset_info->strcasecmp(table->getField(cached_field_index)->field_name, name))
1861 field_ptr= table->getFields() + cached_field_index;
1863 else if (table->getShare()->getNamedFieldSize())
1865 field_ptr= table->getMutableShare()->getNamedField(std::string(name, length));
1872 field_ptr= (table->getFields() + table->getShare()->positionFields(field_ptr));
1877 if (!(field_ptr= table->getFields()))
1879 for (; *field_ptr; ++field_ptr)
1880 if (not system_charset_info->strcasecmp((*field_ptr)->field_name, name))
1884 if (field_ptr && *field_ptr)
1886 *cached_field_index_ptr= field_ptr - table->getFields();
1892 system_charset_info->strcasecmp(name,
"_rowid") ||
1893 table->getShare()->rowid_field_offset == 0)
1895 field= table->getField(table->getShare()->rowid_field_offset-1);
1898 update_field_dependencies(session, field, table);
1947 const char *name, uint32_t length,
1948 const char *item_name,
const char *db_name,
1949 const char *table_name,
Item **ref,
1951 uint32_t *cached_field_index_ptr,
1952 bool register_tree_change,
TableList **actual_table)
1956 assert(table_list->alias);
1977 (!table_list->getNestedJoin()) &&
1983 table_name && table_name[0] &&
1984 (table_alias_charset->strcasecmp(table_list->alias, table_name) ||
1985 (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
1986 strcmp(db_name, table_list->getSchemaName()))))
1989 *actual_table= NULL;
1991 if (!table_list->getNestedJoin())
1994 assert(table_list->table);
1995 if ((fld= find_field_in_table(session, table_list->table, name, length,
1997 cached_field_index_ptr)))
1998 *actual_table= table_list;
2009 if (table_name && table_name[0])
2011 List<TableList>::iterator it(table_list->getNestedJoin()->join_list.begin());
2013 while ((table= it++))
2015 if ((fld= find_field_in_table_ref(session, table, name, length, item_name,
2016 db_name, table_name, ref,
2018 cached_field_index_ptr,
2019 register_tree_change, actual_table)))
2030 fld= find_field_in_natural_join(session, table_list, name, length, ref,
2031 register_tree_change, actual_table);
2036 if (session->mark_used_columns != MARK_COLUMNS_NONE)
2043 Field *field_to_set= NULL;
2044 if (fld == view_ref_found)
2046 Item *it= (*ref)->real_item();
2047 if (it->type() == Item::FIELD_ITEM)
2051 if (session->mark_used_columns == MARK_COLUMNS_READ)
2052 it->walk(&Item::register_field_in_read_map, 1, (
unsigned char *) 0);
2059 Table *table= field_to_set->getTable();
2060 if (session->mark_used_columns == MARK_COLUMNS_READ)
2061 table->setReadSet(field_to_set->position());
2063 table->setWriteSet(field_to_set->position());
2107 Item **ref, find_item_error_report_type report_error,
2108 bool register_tree_change)
2111 const char *db= item->db_name;
2112 const char *table_name= item->table_name;
2113 const char *name= item->field_name;
2114 uint32_t length=(uint32_t) strlen(name);
2115 char name_buff[NAME_LEN+1];
2120 if (!table_name || !table_name[0])
2126 allow_rowid= table_name || (cur_table && !cur_table->next_local);
2128 if (item->cached_table)
2139 TableList *table_ref= item->cached_table;
2145 if (table_ref->table)
2146 found= find_field_in_table(session, table_ref->table, name, length,
2147 true, &(item->cached_field_index));
2149 found= find_field_in_table_ref(session, table_ref, name, length, item->name,
2151 true, &(item->cached_field_index),
2152 register_tree_change,
2161 Select_Lex *current_sel= session->lex().current_select;
2162 Select_Lex *last_select= table_ref->select_lex;
2167 if (current_sel != last_select)
2182 strncpy(name_buff, db,
sizeof(name_buff)-1);
2183 files_charset_info->casedn_str(name_buff);
2190 for (; cur_table != last_table ;
2191 cur_table= cur_table->next_name_resolution_table)
2193 Field *cur_field= find_field_in_table_ref(session, cur_table, name, length,
2194 item->name, db, table_name, ref,
2196 &(item->cached_field_index),
2197 register_tree_change,
2205 item->cached_table= found ? 0 : actual_table;
2207 assert(session->where());
2217 if (report_error == REPORT_ALL_ERRORS ||
2218 report_error == IGNORE_EXCEPT_NON_UNIQUE)
2219 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2220 table_name ? item->full_name() : name, session->where());
2237 if (table_name && (cur_table == first_table) &&
2238 (report_error == REPORT_ALL_ERRORS ||
2239 report_error == REPORT_EXCEPT_NON_UNIQUE))
2241 char buff[NAME_LEN*2+1];
2246 assert(strlen(db) <= NAME_LEN);
2247 assert(strlen(table_name) <= NAME_LEN);
2250 strcat(buff, table_name);
2253 my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where());
2257 if (report_error == REPORT_ALL_ERRORS ||
2258 report_error == REPORT_EXCEPT_NON_UNIQUE)
2259 my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where());
2261 found= not_found_field;
2301Item **not_found_item= (
Item**) 0x1;
2305find_item_in_list(
Session *session,
2307 find_item_error_report_type report_error,
2310 List<Item>::iterator li(items.begin());
2311 Item **found=0, **found_unaliased= 0, *item;
2312 const char *db_name=0;
2313 const char *field_name=0;
2314 const char *table_name=0;
2315 bool found_unaliased_non_uniq= 0;
2320 bool is_ref_by_name= 0;
2321 uint32_t unaliased_counter= 0;
2323 *resolution= NOT_RESOLVED;
2325 is_ref_by_name= (find->type() == Item::FIELD_ITEM ||
2326 find->type() == Item::REF_ITEM);
2329 field_name= ((
Item_ident*) find)->field_name;
2330 table_name= ((
Item_ident*) find)->table_name;
2334 for (uint32_t i= 0; (item=li++); i++)
2336 if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
2346 if (!item_field->name)
2367 if (item_field->field_name && item_field->table_name &&
2368 not system_charset_info->strcasecmp(item_field->field_name, field_name) &&
2369 not table_alias_charset->strcasecmp(item_field->table_name, table_name) &&
2370 (!db_name || (item_field->db_name && !strcmp(item_field->db_name, db_name))))
2372 if (found_unaliased)
2374 if ((*found_unaliased)->eq(item, 0))
2381 if (report_error != IGNORE_ERRORS)
2382 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2383 find->full_name(), session->where());
2386 found_unaliased= li.ref();
2387 unaliased_counter= i;
2388 *resolution= RESOLVED_IGNORING_ALIAS;
2395 int fname_cmp= system_charset_info->strcasecmp(item_field->field_name, field_name);
2396 if (not system_charset_info->strcasecmp(item_field->name, field_name))
2407 if ((*found)->eq(item, 0))
2409 if (report_error != IGNORE_ERRORS)
2410 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2411 find->full_name(), session->where());
2416 *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS : RESOLVED_WITH_NO_ALIAS;
2418 else if (!fname_cmp)
2426 if (found_unaliased)
2428 if ((*found_unaliased)->eq(item, 0))
2430 found_unaliased_non_uniq= 1;
2432 found_unaliased= li.ref();
2433 unaliased_counter= i;
2437 else if (!table_name)
2439 if (is_ref_by_name && find->name && item->name &&
2440 not system_charset_info->strcasecmp(item->name,find->name))
2444 *resolution= RESOLVED_AGAINST_ALIAS;
2447 else if (find->eq(item,0))
2451 *resolution= RESOLVED_IGNORING_ALIAS;
2458 if (found_unaliased_non_uniq)
2460 if (report_error != IGNORE_ERRORS)
2461 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2462 find->full_name(), session->where());
2465 if (found_unaliased)
2467 found= found_unaliased;
2468 *counter= unaliased_counter;
2469 *resolution= RESOLVED_BEHIND_ALIAS;
2474 if (report_error != REPORT_EXCEPT_NOT_FOUND)
2476 if (report_error == REPORT_ALL_ERRORS)
2477 my_error(ER_BAD_FIELD_ERROR, MYF(0),
2478 find->full_name(), session->where());
2482 return (
Item **) not_found_item;
2504test_if_string_in_list(
const char *find,
List<String> *str_list)
2506 List<String>::iterator str_list_it(str_list->begin());
2508 size_t find_length= strlen(find);
2509 while ((curr_str= str_list_it++))
2511 if (find_length != curr_str->length())
2513 if (not system_charset_info->strcasecmp(find, curr_str->ptr()))
2543 context->first_name_resolution_table= context->last_name_resolution_table= table_ref;
2544 item->context= context;
2581 List<String> *using_fields, uint32_t *found_using_fields)
2585 bool first_outer_loop=
true;
2590 TableList *leaf_1= (table_ref_1->getNestedJoin() &&
2591 ! table_ref_1->is_natural_join) ?
2593 TableList *leaf_2= (table_ref_2->getNestedJoin() &&
2594 ! table_ref_2->is_natural_join) ?
2597 *found_using_fields= 0;
2599 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2602 const char *field_name_1;
2604 bool is_using_column_1;
2605 if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
2607 field_name_1= nj_col_1->name();
2608 is_using_column_1= using_fields &&
2609 test_if_string_in_list(field_name_1, using_fields);
2619 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2622 const char *cur_field_name_2;
2623 if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
2625 cur_field_name_2= cur_nj_col_2->name();
2638 if (!system_charset_info->strcasecmp(field_name_1, cur_field_name_2))
2640 if (cur_nj_col_2->is_common ||
2641 (found && (!using_fields || is_using_column_1)))
2643 my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where());
2646 nj_col_2= cur_nj_col_2;
2650 if (first_outer_loop && leaf_2)
2656 leaf_2->is_join_columns_complete=
true;
2657 first_outer_loop=
false;
2667 if (nj_col_2 && (!using_fields ||is_using_column_1))
2669 Item *item_1= nj_col_1->create_item(session);
2670 Item *item_2= nj_col_2->create_item(session);
2671 Field *field_1= nj_col_1->field();
2672 Field *field_2= nj_col_2->field();
2674 if (!item_1 || !item_2)
2681 assert(item_1->type() == Item::FIELD_ITEM ||
2682 item_1->type() == Item::REF_ITEM);
2683 assert(item_2->type() == Item::FIELD_ITEM ||
2684 item_2->type() == Item::REF_ITEM);
2698 set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref);
2699 set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref);
2708 add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
2709 table_ref_1 : table_ref_2),
2712 nj_col_1->is_common= nj_col_2->is_common=
true;
2716 Table *table_1= nj_col_1->table_ref->table;
2718 table_1->setReadSet(field_1->position());
2719 table_1->covering_keys&= field_1->part_of_key;
2720 table_1->merge_keys|= field_1->part_of_key;
2724 Table *table_2= nj_col_2->table_ref->table;
2726 table_2->setReadSet(field_2->position());
2727 table_2->covering_keys&= field_2->part_of_key;
2728 table_2->merge_keys|= field_2->part_of_key;
2731 if (using_fields != NULL)
2732 ++(*found_using_fields);
2786store_natural_using_join_columns(
Session *session,
2791 uint32_t found_using_fields)
2796 assert(!natural_using_join->join_columns);
2802 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2804 nj_col_1= it_1.get_natural_column_ref();
2805 if (nj_col_1->is_common)
2807 natural_using_join->join_columns->push_back(nj_col_1);
2809 nj_col_1->is_common=
false;
2812 non_join_columns->push_back(nj_col_1);
2820 if (using_fields && found_using_fields < using_fields->size())
2822 List<String>::iterator using_fields_it(using_fields->begin());
2823 while (
String* using_field_name= using_fields_it++)
2825 const char *using_field_name_ptr= using_field_name->c_ptr();
2826 List<Natural_join_column>::iterator it(natural_using_join->join_columns->begin());
2831 if (not common_field)
2833 my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr, session->where());
2836 if (!system_charset_info->strcasecmp(common_field->name(), using_field_name_ptr))
2843 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2845 nj_col_2= it_2.get_natural_column_ref();
2846 if (!nj_col_2->is_common)
2847 non_join_columns->push_back(nj_col_2);
2851 nj_col_2->is_common=
false;
2855 if (non_join_columns->size() > 0)
2856 natural_using_join->join_columns->concat(non_join_columns);
2857 natural_using_join->is_join_columns_complete=
true;
2899 if (table_ref->getNestedJoin())
2901 List<TableList>::iterator nested_it(table_ref->getNestedJoin()->join_list.begin());
2902 TableList *same_level_left_neighbor= nested_it++;
2903 TableList *same_level_right_neighbor= NULL;
2905 TableList *real_left_neighbor, *real_right_neighbor;
2907 while (same_level_left_neighbor)
2909 TableList *cur_table_ref= same_level_left_neighbor;
2910 same_level_left_neighbor= nested_it++;
2922 if (same_level_left_neighbor &&
2923 cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
2926 assert(table_ref->getNestedJoin()->join_list.size() == 2);
2927 std::swap(same_level_left_neighbor, cur_table_ref);
2934 real_left_neighbor= (same_level_left_neighbor) ?
2935 same_level_left_neighbor : left_neighbor;
2936 real_right_neighbor= (same_level_right_neighbor) ?
2937 same_level_right_neighbor : right_neighbor;
2939 if (cur_table_ref->getNestedJoin() &&
2940 store_top_level_join_columns(session, cur_table_ref, real_left_neighbor, real_right_neighbor))
2942 same_level_right_neighbor= cur_table_ref;
2950 if (table_ref->is_natural_join)
2952 assert(table_ref->getNestedJoin() &&
2953 table_ref->getNestedJoin()->join_list.size() == 2);
2954 List<TableList>::iterator operand_it(table_ref->getNestedJoin()->join_list.begin());
2962 List<String> *using_fields= table_ref->join_using_fields;
2963 uint32_t found_using_fields;
2969 if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
2970 std::swap(table_ref_1, table_ref_2);
2971 if (mark_common_columns(session, table_ref_1, table_ref_2,
2972 using_fields, &found_using_fields))
2980 if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
2981 std::swap(table_ref_1, table_ref_2);
2982 if (store_natural_using_join_columns(session, table_ref, table_ref_1,
2983 table_ref_2, using_fields,
2984 found_using_fields))
2993 table_ref_1->natural_join= table_ref_2->natural_join= NULL;
2996 if (table_ref_2->outer_join &&
2997 !table_ref_1->on_expr && !table_ref_2->on_expr)
2998 table_ref_2->on_expr=
new Item_int((int64_t) 1,1);
3014 table_ref->next_name_resolution_table= NULL;
3044static bool setup_natural_join_row_types(
Session *session,
3048 session->setWhere(
"from clause");
3049 if (from_clause->size() == 0)
3052 List<TableList>::iterator table_ref_it(from_clause->begin());
3060 for (left_neighbor= table_ref_it++; left_neighbor ; )
3062 table_ref= left_neighbor;
3063 left_neighbor= table_ref_it++;
3064 if (store_top_level_join_columns(session, table_ref, left_neighbor, right_neighbor))
3072 right_neighbor= table_ref;
3081 assert(right_neighbor);
3082 context->first_name_resolution_table=
3083 right_neighbor->first_leaf_for_name_resolution();
3101 List<Item>::iterator it(fields.begin());
3103 session->lex().current_select->cur_pos_in_select_list= 0;
3104 while (wild_num && (item= it++))
3106 if (item->type() == Item::FIELD_ITEM &&
3108 ((
Item_field*) item)->field_name[0] ==
'*' &&
3111 uint32_t elem= fields.size();
3112 bool any_privileges= ((
Item_field *) item)->any_privileges;
3113 Item_subselect *subsel= session->lex().current_select->master_unit()->item;
3115 subsel->substype() == Item_subselect::EXISTS_SUBS)
3122 it.replace(
new Item_int(
"Not_used", (int64_t) 1,
3123 MY_INT64_NUM_DECIMAL_DIGITS));
3125 else if (insert_fields(session, ((
Item_field*) item)->context,
3139 sum_func_list->set_size(sum_func_list->size() + fields.size() - elem);
3144 session->lex().current_select->cur_pos_in_select_list++;
3146 session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
3155bool setup_fields(
Session *session,
Item **ref_pointer_array,
3156 List<Item> &fields, enum_mark_columns mark_used_columns,
3157 List<Item> *sum_func_list,
bool allow_sum_func)
3160 enum_mark_columns save_mark_used_columns= session->mark_used_columns;
3161 nesting_map save_allow_sum_func= session->lex().allow_sum_func;
3162 List<Item>::iterator it(fields.begin());
3163 bool save_is_item_list_lookup;
3165 session->mark_used_columns= mark_used_columns;
3167 session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
3169 save_is_item_list_lookup= session->lex().current_select->is_item_list_lookup;
3170 session->lex().current_select->is_item_list_lookup= 0;
3182 if (ref_pointer_array)
3184 memset(ref_pointer_array, 0,
sizeof(
Item *) * fields.size());
3187 Item **ref= ref_pointer_array;
3188 session->lex().current_select->cur_pos_in_select_list= 0;
3189 while ((item= it++))
3191 if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
3193 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3194 session->lex().allow_sum_func= save_allow_sum_func;
3195 session->mark_used_columns= save_mark_used_columns;
3200 if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
3202 item->split_sum_func(session, ref_pointer_array, *sum_func_list);
3203 session->used_tables|= item->used_tables();
3204 session->lex().current_select->cur_pos_in_select_list++;
3206 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3207 session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
3209 session->lex().allow_sum_func= save_allow_sum_func;
3210 session->mark_used_columns= save_mark_used_columns;
3211 return(test(session->is_error()));
3232 list= &table->next_leaf;
3270 assert((
select_insert && !tables->next_name_resolution_table) || !tables ||
3271 (context->table_list && context->first_name_resolution_table));
3279 make_leaves_list(leaves, tables);
3281 uint32_t tablenr= 0;
3282 for (
TableList* table_list= *leaves; table_list; table_list= table_list->
next_leaf, tablenr++)
3284 Table *table= table_list->table;
3285 table->pos_in_table_list= table_list;
3286 if (first_select_table &&
3287 table_list->top_table() == first_select_table)
3290 first_select_table= 0;
3293 table->setup_table_map(table_list, tablenr);
3294 if (table_list->process_index_hints(table))
3297 if (tablenr > MAX_TABLES)
3299 my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
3304 if (setup_natural_join_row_types(session, from_clause, context))
3334bool setup_tables_and_check_access(
Session *session,
3343 if (setup_tables(session, context, from_clause, tables, &leaves_tmp,
select_insert))
3347 *leaves= leaves_tmp;
3373 const char *table_name, List<Item>::iterator *it,
3378 char name_buff[NAME_LEN+1];
3387 strncpy(name_buff, db_name,
sizeof(name_buff)-1);
3388 files_charset_info->casedn_str(name_buff);
3399 for (
TableList *tables= (table_name ? context->table_list :
3400 context->first_name_resolution_table);
3402 tables= (table_name ? tables->next_local :
3403 tables->next_name_resolution_table)
3407 Table *table= tables->table;
3409 assert(tables->is_leaf_for_name_resolution());
3411 if ((table_name && table_alias_charset->strcasecmp(table_name, tables->alias)) ||
3412 (db_name && system_charset_info->strcasecmp(tables->getSchemaName(),db_name)))
3420 session->used_tables|= table->map;
3428 field_iterator.set(tables);
3430 for (; !field_iterator.end_of_fields(); field_iterator.next())
3432 Item *item= field_iterator.create_item(session);
3444 if ((field= field_iterator.field()))
3447 field->getTable()->setReadSet(field->position());
3450 table->covering_keys&= field->part_of_key;
3451 table->merge_keys|= field->part_of_key;
3453 if (tables->is_natural_join)
3461 if (!(nj_col= field_iterator.get_natural_column_ref()))
3463 assert(nj_col->table_field);
3464 field_table= nj_col->table_ref->table;
3467 session->used_tables|= field_table->map;
3468 field_table->covering_keys&= field->part_of_key;
3469 field_table->merge_keys|= field->part_of_key;
3470 field_table->used_fields++;
3476 session->used_tables|= item->used_tables();
3479 session->lex().current_select->cur_pos_in_select_list++;
3488 table->used_fields= table->getShare()->sizeFields();
3500 my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
3504 my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
3529int Session::setup_conds(
TableList *leaves, COND **conds)
3531 Session *session=
this;
3532 Select_Lex *select_lex= session->lex().current_select;
3533 TableList *table= NULL;
3534 void *save_session_marker= session->session_marker;
3543 bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
3544 select_lex->is_item_list_lookup= 0;
3546 session->mark_used_columns= MARK_COLUMNS_READ;
3547 select_lex->cond_count= 0;
3548 select_lex->between_count= 0;
3549 select_lex->max_equal_elems= 0;
3551 session->session_marker= (
void*)1;
3554 session->setWhere(
"where clause");
3555 if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
3556 (*conds)->check_cols(1))
3559 session->session_marker= save_session_marker;
3565 for (table= leaves; table; table= table->next_leaf)
3567 TableList *embedded;
3568 TableList *embedding= table;
3571 embedded= embedding;
3572 if (embedded->on_expr)
3575 session->session_marker= (
void*)embedded;
3576 session->setWhere(
"on clause");
3577 if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
3578 embedded->on_expr->check_cols(1))
3580 select_lex->cond_count++;
3582 embedding= embedded->getEmbedding();
3585 &embedding->getNestedJoin()->join_list.front() == embedded);
3588 session->session_marker= save_session_marker;
3590 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3591 return(test(session->is_error()));
3594 select_lex->is_item_list_lookup= save_is_item_list_lookup;
3628 List<Item>::iterator f(fields.begin());
3629 List<Item>::iterator v(values.begin());
3645 table= field->field->getTable();
3646 table->auto_increment_field_not_null=
false;
3650 while ((field=
static_cast<Item_field *
>(f++)))
3654 Field *rfield= field->field;
3655 table= rfield->getTable();
3657 if (rfield == table->next_number_field)
3658 table->auto_increment_field_not_null=
true;
3659 if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
3661 my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
3663 table->auto_increment_field_not_null=
false;
3669 return session->is_error();
3694 List<Item>::iterator v(values.begin());
3709 table= (*ptr)->getTable();
3710 table->auto_increment_field_not_null=
false;
3713 while ((field = *ptr++) && ! session->is_error())
3716 table= field->getTable();
3718 if (field == table->next_number_field)
3720 table->auto_increment_field_not_null=
true;
3723 if (value->save_in_field(field, 0) < 0)
3726 table->auto_increment_field_not_null=
false;
3732 return(session->is_error());
3736void drizzle_rm_tmp_tables()
3738 assert(drizzle_tmpdir.size());
3740 session->thread_stack= (
char*) session.get();
3741 session->storeGlobals();
3742 plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir.c_str());
3751 pthread_kill(signal_thread, SIGTERM);
3752 shutdown_in_progress=
true;
Defines the interface to the CachedDirectory class.
int drop_temporary_table(const identifier::Table &)
table::Placeholder & table_cache_insert_placeholder(const identifier::Table &)
void set_proc_info(const char *info)
void close_data_files_and_morph_locks(const identifier::Table &)
void close_old_data_files(bool morph_locks=false, bool send_refresh=false)
query_id_t getQueryId() const
void drop_open_table(Table *table, const identifier::Table &identifier)
void close_tables_for_reopen(TableList **tables)
Table * lock_table_name_if_not_cached(const identifier::Table &)
static const char *const DEFAULT_WHERE
void unlink_open_table(Table *find)
TableList * last_leaf_for_name_resolution()
bool is_join_columns_complete
TableList * first_leaf_for_name_resolution()
TableList * next_name_resolution_table
int delete_table(bool free_share=false)
static bool isAuthorized(const drizzled::identifier::User &user_ctx, const identifier::Schema &schema_identifier, bool send_error=true)
static plugin::Client * getNullClient()
static bool doesTableExist(Session &session, const drizzled::identifier::Table &identifier, bool include_temporary_tables=true)
void removeLock(Table *table)
void abortLock(Table *table)
void broadcast_refresh(void)
TODO: Rename this file - func.h is stupid.
void mark_select_range_as_dependent(Session *session, Select_Lex *last_select, Select_Lex *current_sel, Field *found_field, Item *found_item, Item_ident *resolved_item)
bool check_stack_overrun(Session *session, long margin, void *)