realm-js/results.cpp

59 lines
1.7 KiB
C++
Raw Normal View History

2015-08-14 15:18:49 +00:00
////////////////////////////////////////////////////////////////////////////
2015-08-13 16:28:53 +00:00
//
2015-08-14 15:18:49 +00:00
// Copyright 2015 Realm Inc.
2015-08-13 16:28:53 +00:00
//
2015-08-14 15:18:49 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2015-08-13 16:28:53 +00:00
//
2015-08-14 15:18:49 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
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::range_error(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." +
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
}