Hyper API for C++  0.0.15530
Hyper client library for C++ applications
SqlType.hpp
Go to the documentation of this file.
1 
5 #ifndef TABLEAU_HYPER_SQLTYPE_HPP
6 #define TABLEAU_HYPER_SQLTYPE_HPP
7 
8 #include <hyperapi/impl/infra.hpp>
9 #include <string>
10 #include <hyperapi/hyperapi.h>
11 
12 namespace hyperapi {
13 
15 static CONSTEXPR_OPTIONAL optional<internal::AnyType> null = {};
16 
18 enum class TypeTag : int {
19  Unsupported = HYPER_UNSUPPORTED,
20  Bool = HYPER_BOOL,
21  BigInt = HYPER_BIG_INT,
22  SmallInt = HYPER_SMALL_INT,
23  Int = HYPER_INT,
24  Numeric = HYPER_NUMERIC,
25  Double = HYPER_DOUBLE,
26  Oid = HYPER_OID,
27  Bytes = HYPER_BYTE_A,
28  Text = HYPER_TEXT,
29  Varchar = HYPER_VARCHAR,
30  Char = HYPER_CHAR,
31  Json = HYPER_JSON,
32  Date = HYPER_DATE,
33  Interval = HYPER_INTERVAL,
34  Time = HYPER_TIME,
35  Timestamp = HYPER_TIMESTAMP,
36  TimestampTZ = HYPER_TIMESTAMP_TZ,
37  Geography = HYPER_GEOGRAPHY
38 };
39 
43 class SqlType final {
44  public:
46  SqlType(TypeTag tag, uint32_t oid, hyper_type_modifier_t modifier = HYPER_UNUSED_MODIFIER) noexcept
47  : tag(tag), internalOid(oid), modifier(modifier) {
48  }
49 
53  TypeTag getTag() const noexcept { return tag; }
54 
60  uint32_t getInternalOid() const noexcept { return internalOid; }
61 
67  uint32_t getInternalTypeModifier() const noexcept { return modifier; }
68 
72  uint32_t getPrecision() const noexcept;
73 
77  uint32_t getScale() const noexcept;
78 
82  uint32_t getMaxLength() const noexcept;
83 
89  std::string toString() const;
90 
95  static SqlType boolean() noexcept { return {TypeTag::Bool, HYPER_OID_BOOL}; }
100  static SqlType bigInt() noexcept { return {TypeTag::BigInt, HYPER_OID_BIG_INT}; }
105  static SqlType smallInt() noexcept { return {TypeTag::SmallInt, HYPER_OID_SMALL_INT}; }
110  static SqlType integer() noexcept { return {TypeTag::Int, HYPER_OID_INT}; }
117  static SqlType numeric(uint16_t precision, uint16_t scale) noexcept { return {TypeTag::Numeric, HYPER_OID_NUMERIC, hyper_encode_numeric_modifier(precision, scale)}; }
122  static SqlType doublePrecision() noexcept { return {TypeTag::Double, HYPER_OID_DOUBLE}; }
127  static SqlType oid() noexcept { return {TypeTag::Oid, HYPER_OID_OID}; }
132  static SqlType bytes() noexcept { return {TypeTag::Bytes, HYPER_OID_BYTE_A}; }
137  static SqlType text() noexcept { return {TypeTag::Text, HYPER_OID_TEXT}; }
143  static SqlType varchar(uint32_t maxLength) noexcept { return {TypeTag::Varchar, HYPER_OID_VARCHAR, hyper_encode_string_modifier(maxLength)}; }
149  static SqlType character(uint32_t maxLength) noexcept { return {TypeTag::Char, static_cast<uint32_t>((maxLength == 1) ? HYPER_OID_CHAR1 : HYPER_OID_CHAR), hyper_encode_string_modifier(maxLength)}; }
154  static SqlType json() noexcept { return {TypeTag::Json, HYPER_OID_JSON}; }
159  static SqlType date() noexcept { return {TypeTag::Date, HYPER_OID_DATE}; }
164  static SqlType interval() noexcept { return {TypeTag::Interval, HYPER_OID_INTERVAL}; }
169  static SqlType time() noexcept { return {TypeTag::Time, HYPER_OID_TIME}; }
174  static SqlType timestamp() noexcept { return {TypeTag::Timestamp, HYPER_OID_TIMESTAMP}; }
179  static SqlType timestampTZ() noexcept { return {TypeTag::TimestampTZ, HYPER_OID_TIMESTAMP_TZ}; }
184  static SqlType geography() noexcept { return {TypeTag::Geography, HYPER_OID_GEOGRAPHY}; }
185 
189  friend bool operator==(const SqlType& a, const SqlType& b) noexcept;
193  friend bool operator>(const SqlType& a, const SqlType& b) noexcept;
197  friend bool operator!=(const SqlType& a, const SqlType& b) noexcept;
201  friend bool operator<(const SqlType& a, const SqlType& b) noexcept;
205  friend bool operator<=(const SqlType& a, const SqlType& b) noexcept;
209  friend bool operator>=(const SqlType& a, const SqlType& b) noexcept;
213  friend std::ostream& operator<<(std::ostream& os, const SqlType& obj);
214 
215  private:
217  TypeTag tag;
219  uint32_t internalOid;
221  hyper_type_modifier_t modifier;
222 
223  friend class internal::HyperTableDefinition;
224  friend struct std::hash<hyperapi::SqlType>;
225 };
226 }
227 
228 namespace std {
229 
231 template <>
232 struct hash<hyperapi::SqlType> {
234  size_t operator()(const hyperapi::SqlType& type) const noexcept { return (static_cast<size_t>(type.tag) << 32) | type.modifier; }
235 };
236 }
237 
238 #include <hyperapi/impl/SqlType.impl.hpp>
239 
240 #endif
hyperapi::SqlType::varchar
static SqlType varchar(uint32_t maxLength) noexcept
Returns the VARCHAR SQL type.
Definition: SqlType.hpp:143
hyperapi::SqlType::bytes
static SqlType bytes() noexcept
Returns the BYTEA SQL type.
Definition: SqlType.hpp:132
hyperapi::SqlType
A Hyper SQL type.
Definition: SqlType.hpp:43
hyperapi::SqlType::operator!=
friend bool operator!=(const SqlType &a, const SqlType &b) noexcept
Not equal operator.
hyperapi::SqlType::operator>
friend bool operator>(const SqlType &a, const SqlType &b) noexcept
Greater operator.
hyperapi::SqlType::SqlType
SqlType(TypeTag tag, uint32_t oid, hyper_type_modifier_t modifier=HYPER_UNUSED_MODIFIER) noexcept
Constructor.
Definition: SqlType.hpp:46
hyperapi::SqlType::character
static SqlType character(uint32_t maxLength) noexcept
Returns the CHARACTER SQL type.
Definition: SqlType.hpp:149
hyperapi::SqlType::smallInt
static SqlType smallInt() noexcept
Returns the SMALL INTEGER SQL type.
Definition: SqlType.hpp:105
hyperapi::TypeTag
TypeTag
A type tag.
Definition: SqlType.hpp:18
hyperapi::SqlType::getMaxLength
uint32_t getMaxLength() const noexcept
Returns the maximum length parameter of the type if the type supports it, HYPER_UNUSED_MODIFIER other...
hyperapi::SqlType::getScale
uint32_t getScale() const noexcept
Returns the scale parameter of the type if the type supports it, HYPER_UNUSED_MODIFIER otherwise.
hyperapi::SqlType::getTag
TypeTag getTag() const noexcept
Returns the type tag.
Definition: SqlType.hpp:53
hyperapi::SqlType::timestamp
static SqlType timestamp() noexcept
Returns the TIMESTAMP SQL type.
Definition: SqlType.hpp:174
hyperapi::SqlType::toString
std::string toString() const
Returns a string representation for debugging.
hyperapi::SqlType::date
static SqlType date() noexcept
Returns the DATE SQL type.
Definition: SqlType.hpp:159
hyperapi::SqlType::timestampTZ
static SqlType timestampTZ() noexcept
Returns the TIMESTAMPTZ SQL type.
Definition: SqlType.hpp:179
hyperapi::Interval
An interval data value.
Definition: Interval.hpp:18
hyperapi::Time
A time data value.
Definition: Time.hpp:18
hyperapi::SqlType::operator==
friend bool operator==(const SqlType &a, const SqlType &b) noexcept
Equality operator.
hyperapi::SqlType::time
static SqlType time() noexcept
Returns the TIME SQL type.
Definition: SqlType.hpp:169
hyperapi::Timestamp
A timestamp data value.
Definition: Timestamp.hpp:16
hyperapi::SqlType::operator<<
friend std::ostream & operator<<(std::ostream &os, const SqlType &obj)
Stream output operator.
hyperapi::SqlType::getPrecision
uint32_t getPrecision() const noexcept
Returns the maximum precision parameter of the type if the type supports it, HYPER_UNUSED_MODIFIER ot...
hyperapi::SqlType::bigInt
static SqlType bigInt() noexcept
Returns the BIG INTEGER SQL type.
Definition: SqlType.hpp:100
hyperapi::Numeric
A fixed-point numeric data value with scale fraction digits and precision digits overall.
Definition: Numeric.hpp:43
hyperapi
The primary namespace of the Hyper API for C++.
Definition: ByteSpan.hpp:15
hyperapi::SqlType::text
static SqlType text() noexcept
Returns the TEXT SQL type.
Definition: SqlType.hpp:137
hyperapi::SqlType::getInternalOid
uint32_t getInternalOid() const noexcept
Returns the internal oid.
Definition: SqlType.hpp:60
hyperapi::SqlType::getInternalTypeModifier
uint32_t getInternalTypeModifier() const noexcept
Returns the internal type modifier.
Definition: SqlType.hpp:67
hyperapi::SqlType::integer
static SqlType integer() noexcept
Returns the INTEGER SQL type.
Definition: SqlType.hpp:110
hyperapi::SqlType::operator<
friend bool operator<(const SqlType &a, const SqlType &b) noexcept
Smaller operator.
hyperapi::SqlType::oid
static SqlType oid() noexcept
Returns the OID SQL type.
Definition: SqlType.hpp:127
hyperapi::SqlType::operator>=
friend bool operator>=(const SqlType &a, const SqlType &b) noexcept
Greater or equal operator.
hyperapi::SqlType::numeric
static SqlType numeric(uint16_t precision, uint16_t scale) noexcept
Returns the NUMERIC SQL type.
Definition: SqlType.hpp:117
hyperapi::SqlType::doublePrecision
static SqlType doublePrecision() noexcept
Returns the DOUBLE PRECISION SQL type.
Definition: SqlType.hpp:122
hyperapi::SqlType::json
static SqlType json() noexcept
Returns the JSON SQL type.
Definition: SqlType.hpp:154
hyperapi::SqlType::interval
static SqlType interval() noexcept
Returns the INTERVAL SQL type.
Definition: SqlType.hpp:164
hyperapi::SqlType::geography
static SqlType geography() noexcept
Returns the GEOGRAPHY SQL type.
Definition: SqlType.hpp:184
std::hash< hyperapi::SqlType >::operator()
size_t operator()(const hyperapi::SqlType &type) const noexcept
Calculates the hash value of the given SQL type.
Definition: SqlType.hpp:234
hyperapi::Date
A date data value.
Definition: Date.hpp:18
hyperapi::SqlType::operator<=
friend bool operator<=(const SqlType &a, const SqlType &b) noexcept
Smaller or equal operator.