Commit f944b3ce by Maarten L. Hekkelman

changed find1 logic for only row_handles, now returns empty row_handle instead…

changed find1 logic for only row_handles, now returns empty row_handle instead of throwing when not found
various condition fixes
parent 2557f418
...@@ -236,13 +236,7 @@ class category ...@@ -236,13 +236,7 @@ class category
{ {
auto h = find(pos, std::forward<condition>(cond)); auto h = find(pos, std::forward<condition>(cond));
if (h.empty()) return h.size() != 1 ? row_handle{} : *h.begin();
throw std::runtime_error("No hits found");
if (h.size() != 1)
throw std::runtime_error("Hit not unique");
return *h.begin();
} }
const row_handle find1(condition &&cond) const const row_handle find1(condition &&cond) const
...@@ -254,13 +248,7 @@ class category ...@@ -254,13 +248,7 @@ class category
{ {
auto h = find(pos, std::forward<condition>(cond)); auto h = find(pos, std::forward<condition>(cond));
if (h.empty()) return h.size() != 1 ? row_handle{} : *h.begin();
throw std::runtime_error("No hits found");
if (h.size() != 1)
throw std::runtime_error("Hit not unique");
return *h.begin();
} }
template <typename T> template <typename T>
...@@ -346,6 +334,11 @@ class category ...@@ -346,6 +334,11 @@ class category
// } // }
iterator erase(iterator pos); iterator erase(iterator pos);
void erase(row_handle rh)
{
erase(iterator(*this, rh.m_row));
}
size_t erase(condition &&cond); size_t erase(condition &&cond);
size_t erase(condition &&cond, std::function<void(row_handle)> &&visit); size_t erase(condition &&cond, std::function<void(row_handle)> &&visit);
...@@ -461,6 +454,11 @@ class category ...@@ -461,6 +454,11 @@ class category
return result; return result;
} }
bool has_column(std::string_view name) const
{
return get_column_ix(name) < m_columns.size();
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
void reorder_by_index(); void reorder_by_index();
...@@ -486,7 +484,7 @@ class category ...@@ -486,7 +484,7 @@ class category
void update_value(row *row, size_t column, std::string_view value, bool updateLinked, bool validate = true); void update_value(row *row, size_t column, std::string_view value, bool updateLinked, bool validate = true);
private: private:
void erase_orphans(condition &&cond); void erase_orphans(condition &&cond, category &parent);
using allocator_type = std::allocator<void>; using allocator_type = std::allocator<void>;
......
...@@ -533,7 +533,7 @@ condition operator>(const key &key, const T &v) ...@@ -533,7 +533,7 @@ condition operator>(const key &key, const T &v)
s << " > " << v; s << " > " << v;
return condition(new detail::key_compare_condition_impl( return condition(new detail::key_compare_condition_impl(
key.m_item_tag, [tag = key.m_item_tag, v](const category &c, row_handle r, bool icase) key.m_item_tag, [tag = key.m_item_tag, v](row_handle r, bool icase)
{ return r[tag].template compare<T>(v, icase) > 0; }, { return r[tag].template compare<T>(v, icase) > 0; },
s.str())); s.str()));
} }
...@@ -545,7 +545,7 @@ condition operator>=(const key &key, const T &v) ...@@ -545,7 +545,7 @@ condition operator>=(const key &key, const T &v)
s << " >= " << v; s << " >= " << v;
return condition(new detail::key_compare_condition_impl( return condition(new detail::key_compare_condition_impl(
key.m_item_tag, [tag = key.m_item_tag, v](const category &c, row_handle r, bool icase) key.m_item_tag, [tag = key.m_item_tag, v](row_handle r, bool icase)
{ return r[tag].template compare<T>(v, icase) >= 0; }, { return r[tag].template compare<T>(v, icase) >= 0; },
s.str())); s.str()));
} }
...@@ -557,7 +557,7 @@ condition operator<(const key &key, const T &v) ...@@ -557,7 +557,7 @@ condition operator<(const key &key, const T &v)
s << " < " << v; s << " < " << v;
return condition(new detail::key_compare_condition_impl( return condition(new detail::key_compare_condition_impl(
key.m_item_tag, [tag = key.m_item_tag, v](const category &c, row_handle r, bool icase) key.m_item_tag, [tag = key.m_item_tag, v](row_handle r, bool icase)
{ return r[tag].template compare<T>(v, icase) < 0; }, { return r[tag].template compare<T>(v, icase) < 0; },
s.str())); s.str()));
} }
...@@ -569,7 +569,7 @@ condition operator<=(const key &key, const T &v) ...@@ -569,7 +569,7 @@ condition operator<=(const key &key, const T &v)
s << " <= " << v; s << " <= " << v;
return condition(new detail::key_compare_condition_impl( return condition(new detail::key_compare_condition_impl(
key.m_item_tag, [tag = key.m_item_tag, v](const category &c, row_handle r, bool icase) key.m_item_tag, [tag = key.m_item_tag, v](row_handle r, bool icase)
{ return r[tag].template compare<T>(v, icase) <= 0; }, { return r[tag].template compare<T>(v, icase) <= 0; },
s.str())); s.str()));
} }
...@@ -584,6 +584,11 @@ inline condition operator==(const key &key, const empty_type &) ...@@ -584,6 +584,11 @@ inline condition operator==(const key &key, const empty_type &)
return condition(new detail::key_is_empty_condition_impl(key.m_item_tag)); return condition(new detail::key_is_empty_condition_impl(key.m_item_tag));
} }
inline condition operator !(condition &&rhs)
{
return condition(new detail::not_condition_impl(std::move(rhs)));
}
struct any_type struct any_type
{ {
}; };
......
...@@ -56,6 +56,11 @@ class datablock : public std::list<category> ...@@ -56,6 +56,11 @@ class datablock : public std::list<category>
const std::string &name() const { return m_name; } const std::string &name() const { return m_name; }
void set_name(std::string_view name)
{
m_name = name;
}
void set_validator(const validator *v); void set_validator(const validator *v);
const validator *get_validator() const; const validator *get_validator() const;
......
...@@ -160,6 +160,11 @@ class row_handle ...@@ -160,6 +160,11 @@ class row_handle
return *m_category; return *m_category;
} }
bool empty() const
{
return m_category == nullptr or m_row == nullptr;
}
explicit operator bool() const explicit operator bool() const
{ {
return m_category != nullptr and m_row != nullptr; return m_category != nullptr and m_row != nullptr;
...@@ -185,17 +190,6 @@ class row_handle ...@@ -185,17 +190,6 @@ class row_handle
return item_handle(get_column_ix(column_name), const_cast<row_handle &>(*this)); return item_handle(get_column_ix(column_name), const_cast<row_handle &>(*this));
} }
// template <typename... Ts, size_t N>
// std::tuple<Ts...> get(char const *const (&columns)[N]) const
// {
// static_assert(sizeof...(Ts) == N, "Number of columns should be equal to number of types to return");
// std::array<size_t, N> cix;
// for (size_t i = 0; i < N; ++i)
// cix[i] = get_column_ix(columns[i]);
// return detail::get_row_result<Ts...>(*this, std::move(cix));
// }
template <typename... C> template <typename... C>
auto get(C... columns) const auto get(C... columns) const
{ {
......
...@@ -1032,7 +1032,7 @@ category::iterator category::erase(iterator pos) ...@@ -1032,7 +1032,7 @@ category::iterator category::erase(iterator pos)
if (m_validator != nullptr) if (m_validator != nullptr)
{ {
for (auto &&[childCat, link] : m_child_links) for (auto &&[childCat, link] : m_child_links)
childCat->erase_orphans(get_children_condition(rh, *childCat)); childCat->erase_orphans(get_children_condition(rh, *childCat), *this);
} }
delete_row(r); delete_row(r);
...@@ -1108,7 +1108,7 @@ void category::clear() ...@@ -1108,7 +1108,7 @@ void category::clear()
m_index = nullptr; m_index = nullptr;
} }
void category::erase_orphans(condition &&cond) void category::erase_orphans(condition &&cond, category &parent)
{ {
std::vector<row *> remove; std::vector<row *> remove;
...@@ -1116,15 +1116,18 @@ void category::erase_orphans(condition &&cond) ...@@ -1116,15 +1116,18 @@ void category::erase_orphans(condition &&cond)
for (auto r : *this) for (auto r : *this)
{ {
if (cond(r) and not has_parents(r)) if (not cond(r))
{ continue;
if (VERBOSE > 1)
std::cerr << "Removing orphaned record: " << std::endl if (parent.exists(get_parents_condition(r, parent)))
<< r << std::endl continue;
<< std::endl;
remove.push_back(r); if (VERBOSE > 1)
} std::cerr << "Removing orphaned record: " << std::endl
<< r << std::endl
<< std::endl;
remove.emplace_back(r.m_row);
} }
for (auto r : remove) for (auto r : remove)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment