|  | /** | 
|  | *   Copyright 2011-2015 Quickstep Technologies LLC. | 
|  | *   Copyright 2015 Pivotal Software, Inc. | 
|  | * | 
|  | *   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. | 
|  | **/ | 
|  |  | 
|  | #ifndef QUICKSTEP_CATALOG_CATALOG_ATTRIBUTE_HPP_ | 
|  | #define QUICKSTEP_CATALOG_CATALOG_ATTRIBUTE_HPP_ | 
|  |  | 
|  | #include <string> | 
|  |  | 
|  | #include "catalog/Catalog.pb.h" | 
|  | #include "catalog/CatalogTypedefs.hpp" | 
|  | #include "utility/Macros.hpp" | 
|  |  | 
|  | namespace quickstep { | 
|  |  | 
|  | class CatalogRelationSchema; | 
|  | class Type; | 
|  |  | 
|  | /** \addtogroup Catalog | 
|  | *  @{ | 
|  | */ | 
|  |  | 
|  | /** | 
|  | * @brief An attribute in a relation. | 
|  | **/ | 
|  | class CatalogAttribute { | 
|  | public: | 
|  | /** | 
|  | * @brief Create a new attribute. | 
|  | * | 
|  | * @param parent The relation this attribute belongs to. | 
|  | * @param name This attribute's name. | 
|  | * @param type This attribute's complete data type. | 
|  | * @param id This attribute's ID (defaults to -1, which means invalid/unset). | 
|  | * @param display_name A different name to display when printing values of | 
|  | *        this attribute out. Defaults to name. | 
|  | **/ | 
|  | CatalogAttribute(CatalogRelationSchema *parent, | 
|  | const std::string &name, | 
|  | const Type &type, | 
|  | const attribute_id id = -1, | 
|  | const std::string &display_name = "") | 
|  | : parent_(parent), id_(id), name_(name), display_name_(display_name), type_(&type) { | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Reconstruct an attribute from its serialized Protocol Buffer form. | 
|  | * | 
|  | * @param proto The Protocol Buffer serialization of an attribute, | 
|  | *        previously produced by getProto(). | 
|  | **/ | 
|  | explicit CatalogAttribute(const serialization::CatalogAttribute &proto); | 
|  |  | 
|  | /** | 
|  | * @brief Get the parent relation. | 
|  | * | 
|  | * @return Parent relation. | 
|  | **/ | 
|  | const CatalogRelationSchema& getParent() const { | 
|  | return *parent_; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Get a mutable pointer to the parent relation. | 
|  | * | 
|  | * @return Parent relation. | 
|  | **/ | 
|  | CatalogRelationSchema* getParentMutable() { | 
|  | return parent_; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Get this attribute's ID. | 
|  | * | 
|  | * @return This attribute's ID. | 
|  | **/ | 
|  | attribute_id getID() const { | 
|  | return id_; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Get this attribute's name. | 
|  | * | 
|  | * @return This attribute's name. | 
|  | **/ | 
|  | const std::string& getName() const { | 
|  | return name_; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Get this attribute's display name (the name which would be printed | 
|  | *        to the screen). | 
|  | * | 
|  | * @return This attribute's display name. | 
|  | **/ | 
|  | const std::string& getDisplayName() const { | 
|  | if (display_name_.empty()) { | 
|  | return name_; | 
|  | } else { | 
|  | return display_name_; | 
|  | } | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Set this attribute's display name (the name which would be printed | 
|  | *        to the screen). | 
|  | */ | 
|  | void setDisplayName(const std::string &new_display_name) { | 
|  | display_name_ = new_display_name; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Get this attribute's type. | 
|  | * | 
|  | * @return This attribute's type. | 
|  | **/ | 
|  | const Type& getType() const { | 
|  | return *type_; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Serialize the attribute as Protocol Buffer. | 
|  | * | 
|  | * @return The Protocol Buffer representation of the attribute. | 
|  | **/ | 
|  | serialization::CatalogAttribute getProto() const; | 
|  |  | 
|  | /** | 
|  | * @brief Check whether a serialization::CatalogAttribute is fully-formed and | 
|  | *        all parts are valid. | 
|  | * | 
|  | * @param proto A serialized Protocol Buffer representation of a CatalogAttribute, | 
|  | *        originally generated by getProto(). | 
|  | * @return Whether proto is fully-formed and valid. | 
|  | **/ | 
|  | static bool ProtoIsValid(const serialization::CatalogAttribute &proto); | 
|  |  | 
|  | private: | 
|  | /** | 
|  | * @brief Set the parent CatalogRelationSchema for this attribute. Used by | 
|  | *        CatalogRelationSchema (a friend of this class) when adding a new | 
|  | *        attribute. | 
|  | * | 
|  | * @param parent The new parent for this CatalogAttribute. | 
|  | **/ | 
|  | void setParent(CatalogRelationSchema *parent) { | 
|  | parent_ = parent; | 
|  | } | 
|  |  | 
|  | /** | 
|  | * @brief Set the ID of this attribute. Used by CatalogRelationSchema (a | 
|  | *        friend of this class) when adding a new attribute. | 
|  | * | 
|  | * @param id The new ID for this CatalogAttribute. | 
|  | **/ | 
|  | void setID(const attribute_id id) { | 
|  | id_ = id; | 
|  | } | 
|  |  | 
|  | CatalogRelationSchema *parent_; | 
|  |  | 
|  | // The attribute id in CatalogRelationSchema. | 
|  | attribute_id id_; | 
|  |  | 
|  | // The internal attribute name. | 
|  | const std::string name_; | 
|  |  | 
|  | // The external attribute name used to print on screen, if non-empty. | 
|  | std::string display_name_; | 
|  |  | 
|  | // The attribute's underlying type. | 
|  | const Type *type_; | 
|  |  | 
|  | friend class CatalogRelationSchema; | 
|  |  | 
|  | DISALLOW_COPY_AND_ASSIGN(CatalogAttribute); | 
|  | }; | 
|  |  | 
|  | /** @} */ | 
|  |  | 
|  | }  // namespace quickstep | 
|  |  | 
|  | #endif  // QUICKSTEP_CATALOG_CATALOG_ATTRIBUTE_HPP_ |