//===----------------------------------------------------------------------===// // DuckDB // // duckdb/parser/constraint.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/common/common.hpp" #include "duckdb/common/vector.hpp" #include "duckdb/common/exception.hpp" namespace duckdb { class Serializer; class Deserializer; class FieldWriter; class FieldReader; //===--------------------------------------------------------------------===// // Constraint Types //===--------------------------------------------------------------------===// enum class ConstraintType : uint8_t { INVALID = 0, // invalid constraint type NOT_NULL = 1, // NOT NULL constraint CHECK = 2, // CHECK constraint UNIQUE = 3, // UNIQUE constraint FOREIGN_KEY = 4, // FOREIGN KEY constraint }; enum class ForeignKeyType : uint8_t { FK_TYPE_PRIMARY_KEY_TABLE = 0, // main table FK_TYPE_FOREIGN_KEY_TABLE = 1, // referencing table FK_TYPE_SELF_REFERENCE_TABLE = 2 // self refrencing table }; struct ForeignKeyInfo { ForeignKeyType type; string schema; //! if type is FK_TYPE_FOREIGN_KEY_TABLE, means main key table, if type is FK_TYPE_PRIMARY_KEY_TABLE, means foreign //! key table string table; //! The set of main key table's column's index vector pk_keys; //! The set of foreign key table's column's index vector fk_keys; }; //! Constraint is the base class of any type of table constraint. class Constraint { public: DUCKDB_API explicit Constraint(ConstraintType type); DUCKDB_API virtual ~Constraint(); ConstraintType type; public: DUCKDB_API virtual string ToString() const = 0; DUCKDB_API void Print() const; DUCKDB_API virtual unique_ptr Copy() const = 0; //! Serializes a Constraint to a stand-alone binary blob DUCKDB_API void Serialize(Serializer &serializer) const; //! Serializes a Constraint to a stand-alone binary blob DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0; //! Deserializes a blob back into a Constraint DUCKDB_API static unique_ptr Deserialize(Deserializer &source); public: template TARGET &Cast() { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } return reinterpret_cast(*this); } template const TARGET &Cast() const { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } return reinterpret_cast(*this); } }; } // namespace duckdb