susumu.yata
null+****@clear*****
Mon Sep 1 12:54:05 JST 2014
susumu.yata 2014-09-01 12:54:05 +0900 (Mon, 01 Sep 2014) New Revision: 29a7561b344abd7f4b60b4b52798b12be00a1bb0 https://github.com/groonga/grnxx/commit/29a7561b344abd7f4b60b4b52798b12be00a1bb0 Message: Add an option "ref_table_name" to ColumnOptions. Modified files: include/grnxx/types.hpp lib/grnxx/column.cpp lib/grnxx/column_impl.hpp lib/grnxx/types.cpp Modified: include/grnxx/types.hpp (+3 -0) =================================================================== --- include/grnxx/types.hpp 2014-08-28 15:51:56 +0900 (a014394) +++ include/grnxx/types.hpp 2014-09-01 12:54:05 +0900 (f3af08f) @@ -508,6 +508,9 @@ struct TableOptions { }; struct ColumnOptions { + // The referenced (parent) table. + String ref_table_name; + ColumnOptions(); }; Modified: lib/grnxx/column.cpp (+76 -0) =================================================================== --- lib/grnxx/column.cpp 2014-08-28 15:51:56 +0900 (a2a1d09) +++ lib/grnxx/column.cpp 2014-09-01 12:54:05 +0900 (3c66975) @@ -3,6 +3,7 @@ #include "grnxx/column_impl.hpp" #include "grnxx/cursor.hpp" #include "grnxx/datum.hpp" +#include "grnxx/db.hpp" #include "grnxx/error.hpp" #include "grnxx/table.hpp" @@ -122,6 +123,15 @@ bool Column::initialize_base(Error *error, return false; } data_type_ = data_type; + if (data_type == INT_DATA) { + if (options.ref_table_name.size() != 0) { + auto ref_table = table->db()->find_table(error, options.ref_table_name); + if (!ref_table) { + return false; + } + ref_table_ = ref_table; + } + } return true; } @@ -210,6 +220,72 @@ void ColumnImpl<T>::unset(Int row_id) { template <typename T> ColumnImpl<T>::ColumnImpl() : Column(), values_() {} +// -- ColumnImpl<Int> -- + +bool ColumnImpl<Int>::set(Error *error, Int row_id, const Datum &datum) { + if (datum.type() != INT_DATA) { + GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type"); + return false; + } + if (!table_->test_row(error, row_id)) { + return false; + } + if (ref_table_) { + if (!ref_table_->test_row(error, datum.force_int())) { + return false; + } + } + // Note that a Bool object does not have its own address. + values_.set(row_id, datum.force_int()); + return true; +} + +bool ColumnImpl<Int>::get(Error *error, Int row_id, Datum *datum) const { + if (!table_->test_row(error, row_id)) { + return false; + } + *datum = values_[row_id]; + return true; +} + +unique_ptr<ColumnImpl<Int>> ColumnImpl<Int>::create( + Error *error, + Table *table, + String name, + const ColumnOptions &options) { + unique_ptr<ColumnImpl> column(new (nothrow) ColumnImpl); + if (!column) { + GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); + return nullptr; + } + if (!column->initialize_base(error, table, name, INT_DATA, options)) { + return nullptr; + } + if (!column->values_.resize(error, table->max_row_id() + 1, + TypeTraits<Int>::default_value())) { + return nullptr; + } + return column; +} + +ColumnImpl<Int>::~ColumnImpl() {} + +bool ColumnImpl<Int>::set_default_value(Error *error, Int row_id) { + if (row_id >= values_.size()) { + if (!values_.resize(error, row_id + 1, TypeTraits<Int>::default_value())) { + return false; + } + } + values_.set(row_id, TypeTraits<Int>::default_value()); + return true; +} + +void ColumnImpl<Int>::unset(Int row_id) { + values_.set(row_id, TypeTraits<Int>::default_value()); +} + +ColumnImpl<Int>::ColumnImpl() : Column(), values_() {} + // -- ColumnImpl<Text> -- bool ColumnImpl<Text>::set(Error *error, Int row_id, const Datum &datum) { Modified: lib/grnxx/column_impl.hpp (+38 -0) =================================================================== --- lib/grnxx/column_impl.hpp 2014-08-28 15:51:56 +0900 (55f71e5) +++ lib/grnxx/column_impl.hpp 2014-09-01 12:54:05 +0900 (05ab25a) @@ -45,6 +45,44 @@ class ColumnImpl : public Column { }; template <> +class ColumnImpl<Int> : public Column { + public: + // -- Public API -- + + bool set(Error *error, Int row_id, const Datum &datum); + bool get(Error *error, Int row_id, Datum *datum) const; + + // -- Internal API -- + + // Create a new column. + // + // Returns a pointer to the column on success. + // On failure, returns nullptr and stores error information into "*error" if + // "error" != nullptr. + static unique_ptr<ColumnImpl> create(Error *error, + Table *table, + String name, + const ColumnOptions &options); + + ~ColumnImpl(); + + bool set_default_value(Error *error, Int row_id); + void unset(Int row_id); + + // Return a value identified by "row_id". + // + // Assumes that "row_id" is valid. Otherwise, the result is undefined. + Int get(Int row_id) const { + return values_[row_id]; + } + + protected: + Array<Int> values_; + + ColumnImpl(); +}; + +template <> class ColumnImpl<Text> : public Column { public: // -- Public API -- Modified: lib/grnxx/types.cpp (+1 -1) =================================================================== --- lib/grnxx/types.cpp 2014-08-28 15:51:56 +0900 (64b8a2e) +++ lib/grnxx/types.cpp 2014-09-01 12:54:05 +0900 (117d755) @@ -44,7 +44,7 @@ DBOptions::DBOptions() {} TableOptions::TableOptions() {} -ColumnOptions::ColumnOptions() {} +ColumnOptions::ColumnOptions() : ref_table_name("") {} IndexOptions::IndexOptions() {} -------------- next part -------------- HTML����������������������������... Descargar