2015-06-04 00:27:21 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2015-06-11 17:33:46 +00:00
|
|
|
// Copyright 2015 Realm Inc.
|
2015-06-04 00:27:21 +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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "object_schema.hpp"
|
|
|
|
#include "object_store.hpp"
|
|
|
|
|
2015-06-10 21:21:43 +00:00
|
|
|
#include <realm/group.hpp>
|
|
|
|
#include <realm/table.hpp>
|
|
|
|
|
2015-06-04 00:27:21 +00:00
|
|
|
using namespace realm;
|
|
|
|
using namespace std;
|
|
|
|
|
2015-06-11 17:33:46 +00:00
|
|
|
ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) {
|
|
|
|
TableRef tableRef = ObjectStore::table_for_object_type(group, name);
|
|
|
|
Table *table = tableRef.get();
|
2015-06-06 01:47:19 +00:00
|
|
|
|
2015-06-04 00:27:21 +00:00
|
|
|
size_t count = table->get_column_count();
|
2015-06-10 21:21:43 +00:00
|
|
|
properties.reserve(count);
|
2015-06-04 00:27:21 +00:00
|
|
|
for (size_t col = 0; col < count; col++) {
|
|
|
|
Property property;
|
|
|
|
property.name = table->get_column_name(col).data();
|
|
|
|
property.type = (PropertyType)table->get_column_type(col);
|
|
|
|
property.is_indexed = table->has_search_index(col);
|
|
|
|
property.is_primary = false;
|
2015-06-18 21:46:37 +00:00
|
|
|
#ifdef REALM_ENABLE_NULL
|
2015-04-16 23:48:07 +00:00
|
|
|
property.is_nullable = table->is_nullable(col) || property.type == PropertyTypeObject;
|
2015-06-18 21:46:37 +00:00
|
|
|
#else
|
|
|
|
property.is_nullable = property.type == PropertyTypeObject;
|
|
|
|
#endif
|
2015-06-04 00:27:21 +00:00
|
|
|
property.table_column = col;
|
|
|
|
if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) {
|
|
|
|
// set link type for objects and arrays
|
|
|
|
realm::TableRef linkTable = table->get_link_target(col);
|
2015-06-04 01:04:21 +00:00
|
|
|
property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data());
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
2015-06-10 21:21:43 +00:00
|
|
|
properties.push_back(move(property));
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
primary_key = realm::ObjectStore::get_primary_key_for_object(group, name);
|
|
|
|
if (primary_key.length()) {
|
2015-06-10 21:21:43 +00:00
|
|
|
auto primary_key_prop = primary_key_property();
|
|
|
|
if (!primary_key_prop) {
|
2015-06-16 17:07:40 +00:00
|
|
|
throw ObjectStoreException(ObjectStoreException::Kind::ObjectSchemaMismatchedPrimaryKey, {
|
|
|
|
{ObjectStoreException::InfoKey::ObjectType, name},
|
|
|
|
{ObjectStoreException::InfoKey::PrimaryKey, ""},
|
|
|
|
{ObjectStoreException::InfoKey::OldPrimaryKey, primary_key}});
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
2015-06-10 21:21:43 +00:00
|
|
|
primary_key_prop->is_primary = true;
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:52:00 +00:00
|
|
|
Property *ObjectSchema::property_for_name(const std::string &name) {
|
2015-06-05 02:24:01 +00:00
|
|
|
for (auto& prop:properties) {
|
|
|
|
if (prop.name == name) {
|
|
|
|
return ∝
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-05 02:24:01 +00:00
|
|
|
return nullptr;
|
2015-06-04 00:27:21 +00:00
|
|
|
}
|
2015-06-06 01:47:19 +00:00
|
|
|
|