realm-js/results.cpp

45 lines
1.1 KiB
C++
Raw Normal View History

/* Copyright 2015 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
2015-08-13 16:28:53 +00:00
#include "results.hpp"
#import <stdexcept>
using namespace realm;
2015-09-03 22:46:31 +00:00
Results::Results(SharedRealm &r, ObjectSchema &o, Query q, SortOrder s) :
2015-08-13 16:28:53 +00:00
realm(r), object_schema(o), backing_query(q), table_view(backing_query.find_all())
{
2015-09-03 22:46:31 +00:00
setSort(std::move(s));
2015-08-13 16:28:53 +00:00
}
2015-09-03 22:46:31 +00:00
size_t Results::size()
{
2015-08-13 16:28:53 +00:00
verify_attached();
return table_view.size();
}
2015-09-03 22:46:31 +00:00
void Results::setSort(SortOrder s)
{
sort_order = std::make_unique<SortOrder>(std::move(s));
table_view.sort(sort_order->columnIndices, sort_order->ascending);
}
Row Results::get(std::size_t row_ndx)
{
2015-08-13 16:28:53 +00:00
verify_attached();
if (row_ndx >= table_view.size()) {
throw std::out_of_range(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." +
2015-08-13 16:28:53 +00:00
std::to_string(table_view.size()) + ".");
}
return table_view.get(row_ndx);
}
2015-09-03 22:46:31 +00:00
void Results::verify_attached()
{
2015-08-13 16:28:53 +00:00
if (!table_view.is_attached()) {
throw std::runtime_error("Tableview is not attached");
}
table_view.sync_if_needed();
2015-08-14 17:47:56 +00:00
}