/* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is also distributed with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have included with MySQL. 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, version 2.0, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "sql/dd/collection.h" #include #include // std::unique_ptr #include "my_dbug.h" #include "sql/dd/impl/object_key.h" // Needed for destructor #include "sql/dd/impl/raw/raw_record.h" #include "sql/dd/impl/raw/raw_record_set.h" // dd::Raw_record_set #include "sql/dd/impl/raw/raw_table.h" // dd::Raw_table #include "sql/dd/impl/types/abstract_table_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/check_constraint_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/column_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/column_type_element_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/foreign_key_element_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/foreign_key_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/index_element_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/index_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/parameter_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/parameter_type_element_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/partition_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/partition_index_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/partition_value_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/routine_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/table_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/tablespace_file_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/tablespace_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/trigger_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/view_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/view_routine_impl.h" // IWYU pragma: keep #include "sql/dd/impl/types/view_table_impl.h" // IWYU pragma: keep #include "template_utils.h" namespace dd { template T &Collection::Collection_iterator::operator*() { // Dereferencing an invalid iterator is prohibited. DBUG_ASSERT(m_current != m_array->end()); // Need a non-tmp placeholder of correct type since reference is returned. m_current_obj = *m_current; return m_current_obj; } template const typename Collection::abstract_type * &Collection::Collection_const_iterator::operator*() { // Dereferencing an invalid iterator is prohibited. DBUG_ASSERT(m_current != m_array->end()); // Need a non-tmp placeholder of correct type since reference is returned. m_current_obj = *m_current; return m_current_obj; } template void Collection::clear_all_items() { delete_container_pointers(m_items); delete_container_pointers(m_removed_items); } template void Collection::clear_removed_items() { delete_container_pointers(m_removed_items); } // Doxygen gets confused by this function. /** @cond */ template void Collection::remove(typename Collection::impl_type *item) { if (std::find(m_items.begin(), m_items.end(), item) != m_items.end()) { m_removed_items.push_back(item); // Remove items using the "erase-remove" idiom. m_items.erase(std::remove(m_items.begin(), m_items.end(), item), m_items.end()); renumerate_items(); } } template typename Collection::iterator Collection::find(const impl_type *item) { // Find the element and prepare iterator pointing to found element. typename Collection::iterator iterator( &m_items, std::find(m_items.begin(), m_items.end(), item)); return iterator; } /** @endcond */ template template bool Collection::restore_items(Parent_item *parent, Open_dictionary_tables_ctx *otx, Raw_table *table, Object_key *key, Compare comp) { DBUG_TRACE; // NOTE: if this assert is firing, that means the table was not registered // for that transaction. Use Open_dictionary_tables_ctx::register_tables(). DBUG_ASSERT(table); DBUG_ASSERT(empty()); std::unique_ptr key_holder(key); std::unique_ptr rs; if (table->open_record_set(key, rs)) return true; // Process records. Raw_record *r = rs->current_record(); while (r) { Collection::impl_type *item = Collection::impl_type::restore_item(parent); item->set_ordinal_position(static_cast(m_items.size() + 1)); m_items.push_back(item); if (item->restore_attributes(*r) || rs->next(r)) { clear_all_items(); return true; } } /* It is necessary to complete the table scan above reading the parent DD object first before reading child DD object. If both the parent and DD child objects are stored in single DD table (E.g., we stored parent and sub partitions both in single mysql.partitions DD table.), we cannot keep two table scans context using single table handler. We might need two handler instances to achieve the same. Support multiple table handler for same DD table is not quite easy when we look at the current DD framework design. The other solution is to complete the scan for parent DD object entries first and then read child DD object entries. This seem to serve the purpose. There seem to be no side effect of we splitting this loop into two. */ rs.reset(); for (auto item : m_items) { if (item->restore_children(otx) || item->validate()) { clear_all_items(); return true; } } // The record fetched from DB may not be ordered based on ordinal position, // since some elements store their ordinal position persistently. // So we need to sort the elements in m_item based on ordinal position. std::sort(m_items.begin(), m_items.end(), comp); return false; } template static bool item_compare(const T *a, const T *b) { return a->ordinal_position() < b->ordinal_position(); } template template bool Collection::restore_items(Parent_item *parent, Open_dictionary_tables_ctx *otx, Raw_table *table, Object_key *key) { return restore_items(parent, otx, table, key, item_compare::impl_type>); } template bool Collection::store_items(Open_dictionary_tables_ctx *otx) { DBUG_TRACE; if (empty()) return false; // Drop items from m_removed_items. for (auto *removed : m_removed_items) { if (removed->validate() || removed->drop(otx)) return true; } delete_container_pointers(m_removed_items); // Add new items and update existing if needed. for (Collection::impl_type *item : m_items) { if (item->validate() || item->store(otx)) return true; } return false; } template bool Collection::drop_items(Open_dictionary_tables_ctx *otx, Raw_table *table, Object_key *key) const { DBUG_TRACE; // Make sure key gets deleted std::unique_ptr key_holder(key); if (empty()) return false; // Drop items for (const Collection::impl_type *item : m_items) { if (item->drop_children(otx)) return true; } std::unique_ptr rs; if (table->open_record_set(key, rs)) return true; // Process records. Raw_record *r = rs->current_record(); while (r) { // Drop the item record from DD table if (r->drop()) return true; if (rs->next(r)) return true; } return false; } template const typename Collection::abstract_type *Collection::at(size_t n) const { DBUG_ASSERT(n < size()); return m_items[n]; } template template void Collection::deep_copy(const Collection &src, Parent_item *parent) { m_items.reserve(src.m_items.size()); for (const Collection::impl_type *item : src.m_items) m_items.push_back(Collection::impl_type::clone(*item, parent)); } /////////////////////////////////////////////////////////////////////////// // The explicit instantiation of the template members below // is not handled well by doxygen, so we enclose this in a // cond/endcon block. Documenting these does not add much // value anyway, if the member definitions were in a header // file, the compiler would do the instantiation for us. /** @cond */ template Column *&Collection::Collection_iterator::operator*(); template Column_type_element * &Collection::Collection_iterator::operator*(); template Foreign_key *&Collection::Collection_iterator::operator *(); template Foreign_key_element * &Collection::Collection_iterator::operator*(); template Index *&Collection::Collection_iterator::operator*(); template Index_element * &Collection::Collection_iterator::operator*(); template Parameter_type_element * &Collection::Collection_iterator::operator*(); template Partition *&Collection::Collection_iterator::operator*(); template dd::Partition_index * &Collection::Collection_iterator::operator*(); template Tablespace_file * &Collection::Collection_iterator::operator*(); template Trigger *&Collection::Collection_iterator::operator*(); template Check_constraint * &Collection::Collection_iterator::operator*(); template const Column * &Collection::Collection_const_iterator::operator*(); template const Column_type_element * &Collection::Collection_const_iterator::operator*(); template const Foreign_key * &Collection::Collection_const_iterator::operator*(); template const Foreign_key_element * &Collection::Collection_const_iterator::operator*(); template const Index *&Collection::Collection_const_iterator::operator *(); template const Index_element * &Collection::Collection_const_iterator::operator*(); template const Parameter * &Collection::Collection_const_iterator::operator*(); template const Parameter_type_element *&Collection< Parameter_type_element *>::Collection_const_iterator::operator*(); template const Partition * &Collection::Collection_const_iterator::operator*(); template const Partition_index * &Collection::Collection_const_iterator::operator*(); template const Partition_value * &Collection::Collection_const_iterator::operator*(); template const Tablespace_file * &Collection::Collection_const_iterator::operator*(); template const View_routine * &Collection::Collection_const_iterator::operator*(); template const View_table * &Collection::Collection_const_iterator::operator*(); template const Trigger * &Collection::Collection_const_iterator::operator*(); template const Check_constraint * &Collection::Collection_const_iterator::operator*(); template bool Collection::restore_items( Abstract_table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Column_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items< Table_impl, Foreign_key_order_comparator>(Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Foreign_key_order_comparator); template bool Collection::restore_items< Foreign_key_impl>(Foreign_key_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Index_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Routine_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Parameter_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Partition_order_comparator); template bool Collection::restore_items( Partition_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items< Partition_impl, Partition_order_comparator>(Partition_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Partition_order_comparator); template bool Collection::restore_items< Partition_impl, Partition_index_order_comparator>( Partition_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Partition_index_order_comparator); template bool Collection::restore_items( Partition_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items< Partition_impl, Partition_value_order_comparator>( Partition_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Partition_value_order_comparator); template bool Collection::restore_items( Tablespace_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( View_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( View_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Trigger_order_comparator); template bool Collection::restore_items( Table_impl *, Open_dictionary_tables_ctx *, Raw_table *, Object_key *, Check_constraint_order_comparator); template bool Collection::store_items(Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items(Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::store_items(Open_dictionary_tables_ctx *); template bool Collection::store_items( Open_dictionary_tables_ctx *); template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items(Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template bool Collection::drop_items( Open_dictionary_tables_ctx *, Raw_table *, Object_key *) const; template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_all_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::clear_removed_items(); template void Collection::remove(Column_impl *); template void Collection::remove( Column_type_element_impl *); template void Collection::remove(Foreign_key_impl *); template void Collection::remove(Index_impl *); template void Collection::remove(Index_element_impl *); template void Collection::remove( Foreign_key_element_impl *); template void Collection::remove(Parameter_impl *); template void Collection::remove( Parameter_type_element_impl *); template void Collection::remove(Partition_impl *); template void Collection::remove(Partition_index_impl *); template void Collection::remove(Partition_value_impl *); template void Collection::remove(Tablespace_file_impl *); template void Collection::remove(View_routine_impl *); template void Collection::remove(View_table_impl *); template void Collection::remove(Trigger_impl *); template void Collection::remove(Check_constraint_impl *); template Collection::iterator Collection::find( const Column_impl *); template Collection::iterator Collection::find(const Column_type_element_impl *); template Collection::iterator Collection::find( const Foreign_key_impl *); template Collection::iterator Collection::find( const Index_impl *); template Collection::iterator Collection::find(const Index_element_impl *); template Collection::iterator Collection::find(const Foreign_key_element_impl *); template Collection::iterator Collection::find( const Parameter_impl *); template Collection::iterator Collection::find(const Parameter_type_element_impl *); template Collection::iterator Collection::find( const Partition_impl *); template Collection::iterator Collection::find(const Partition_index_impl *); template Collection::iterator Collection::find(const Partition_value_impl *); template Collection::iterator Collection::find(const Tablespace_file_impl *); template Collection::iterator Collection::find( const View_routine_impl *); template Collection::iterator Collection::find( const View_table_impl *); template Collection::iterator Collection::find( const Trigger_impl *); template Collection::iterator Collection::find(const Check_constraint_impl *); template const Collection::abstract_type *Collection::at( size_t n) const; template const Collection::abstract_type * Collection::at(size_t n) const; template const Collection::abstract_type * Collection::at(size_t n) const; template const Collection::abstract_type *Collection::at( size_t n) const; template const Collection::abstract_type * Collection::at(size_t n) const; template const Collection::abstract_type * Collection::at(size_t n) const; template const Collection::abstract_type * Collection::at(size_t n) const; template void Collection::deep_copy( Collection const &, Abstract_table_impl *); template void Collection::deep_copy( Collection const &, Column_impl *); template void Collection::deep_copy( Collection const &, Table_impl *); template void Collection::deep_copy( Collection const &, Foreign_key_impl *); template void Collection::deep_copy( Collection const &, Table_impl *); template void Collection::deep_copy( Collection const &, Index_impl *); template void Collection::deep_copy( Collection const &, Routine_impl *); template void Collection::deep_copy( Collection const &, Parameter_impl *); template void Collection::deep_copy( Collection const &, Table_impl *); template void Collection::deep_copy( Collection const &, Partition_impl *); template void Collection::deep_copy( Collection const &, Partition_impl *); template void Collection::deep_copy( Collection const &, Partition_impl *); template void Collection::deep_copy( Collection const &, Tablespace_impl *); template void Collection::deep_copy( Collection const &, View_impl *); template void Collection::deep_copy( Collection const &, View_impl *); template void Collection::deep_copy( Collection const &, Table_impl *); template void Collection::deep_copy( Collection const &, Table_impl *); /** @endcond */ } // namespace dd