1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#pragma once
#include "Core/Maybe.h"
namespace sql {
/**
* A description of a datatype in SQL.
*
* This type aims to be generic in the sense that it aims to represent the type in a
* representation that is usable across databases.
*
* A type as described here contains a type name (as an ID), and an optional size of the type.
*/
class QueryType {
STORM_VALUE;
public:
// Create a type representing "void".
STORM_CTOR QueryType();
// Create the type TEXT.
static QueryType STORM_FN text() { return QueryType(1); }
// Create the type INTEGER.
static QueryType STORM_FN integer() { return QueryType(2); }
// Create the type REAL.
static QueryType STORM_FN real() { return QueryType(3); }
// Parse from string. Returns "void" if the type is unknown. Throws `SQLError` on malformed
// type specification.
static QueryType STORM_FN parse(Str *from);
// Create a copy of the type with the specified size.
QueryType STORM_FN sized(Nat size) const {
return QueryType(type, size + 1);
}
// Remove the type information.
QueryType STORM_FN withoutSize() const {
return QueryType(type, 0);
}
// Get the size.
Maybe<Nat> STORM_FN size() const {
if (typeSize == 0)
return Maybe<Nat>();
else
return Maybe<Nat>(typeSize - 1);
}
// Check if the types are the same, ignoring the size.
Bool STORM_FN sameType(const QueryType &o) const {
return type == o.type;
}
// Check if we are compatible with the parameter. Compatibility compares the size, but
// allows any size of `o`, assuming our size is not specified. Also allows 'o' to have a
// larger size than us.
Bool STORM_FN compatible(const QueryType &o) const;
// Is this type empty (i.e. `void`).
Bool STORM_FN empty() const { return type == 0; }
// Is there any type (i.e. not `void`).
Bool STORM_FN any() const { return type != 0; }
// Output.
void STORM_FN toS(StrBuf *to) const;
private:
// Create a custom type.
QueryType(Nat typeId);
QueryType(Nat typeId, Nat size);
// Type id.
Nat type;
// Type size - 1. Zero means no size.
Nat typeSize;
friend class QueryStr;
friend class QueryStrBuilder;
};
}
|