11 #ifndef PQXX_H_STRINGCONV
12 #define PQXX_H_STRINGCONV
14 #include "pqxx/compiler-public.hxx"
57 const std::string &type);
68 #define PQXX_DECLARE_TYPE_NAME(TYPE) \
69 template<> struct type_name<TYPE> \
70 { static constexpr const char *value = #TYPE; }
89 #undef PQXX_DECLARE_TYPE_NAME
92 {
static constexpr
const char *value =
"char[]"; };
104 static constexpr
const char *
name() noexcept
106 static constexpr
bool has_null() noexcept {
return false; }
109 static void from_string(
const char Str[], TYPE &Obj);
116 #define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(TYPE) \
117 template<> struct PQXX_LIBEXPORT string_traits<TYPE> : \
118 internal::builtin_traits<TYPE> {};
120 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
bool)
122 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
short)
123 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
unsigned short)
124 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
int)
125 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
unsigned int)
126 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
long)
127 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
unsigned long)
128 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
long long)
129 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
unsigned long long)
131 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
float)
132 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
double)
133 PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(
long double)
135 #undef PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
148 template<
typename ENUM>
154 static constexpr
bool has_null() noexcept {
return false; }
155 [[noreturn]]
static ENUM
null()
161 underlying_traits::from_string(Str, tmp);
182 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
184 struct string_traits<ENUM> : pqxx::enum_traits<ENUM> \
186 static constexpr const char *name() noexcept { return #ENUM; } \
187 [[noreturn]] static ENUM null() \
188 { internal::throw_null_conversion(name()); } \
195 static constexpr
const char *
name() noexcept {
return "const char *"; }
196 static constexpr
bool has_null() noexcept {
return true; }
197 static bool is_null(
const char *t) {
return t ==
nullptr; }
198 static const char *
null() {
return nullptr; }
199 static void from_string(
const char Str[],
const char *&Obj) { Obj = Str; }
200 static std::string
to_string(
const char *Obj) {
return Obj; }
206 static constexpr
const char *
name() noexcept {
return "char *"; }
207 static constexpr
bool has_null() noexcept {
return true; }
208 static bool is_null(
const char *t) {
return t ==
nullptr; }
209 static const char *
null() {
return nullptr; }
220 static constexpr
const char *
name() noexcept {
return "char[]"; }
221 static constexpr
bool has_null() noexcept {
return true; }
222 static bool is_null(
const char t[]) {
return t ==
nullptr; }
223 static const char *
null() {
return nullptr; }
224 static std::string
to_string(
const char Obj[]) {
return Obj; }
229 static constexpr
const char *
name() noexcept {
return "string"; }
230 static constexpr
bool has_null() noexcept {
return false; }
231 static bool is_null(
const std::string &) {
return false; }
232 [[noreturn]]
static std::string
null()
234 static void from_string(
const char Str[], std::string &Obj) { Obj=Str; }
235 static std::string
to_string(
const std::string &Obj) {
return Obj; }
240 static constexpr
const char *
name() noexcept {
return "const string"; }
241 static constexpr
bool has_null() noexcept {
return false; }
242 static bool is_null(
const std::string &) {
return false; }
243 [[noreturn]]
static const std::string
null()
245 static const std::string
to_string(
const std::string &Obj) {
return Obj; }
250 static constexpr
const char *
name() noexcept {
return "stringstream"; }
251 static constexpr
bool has_null() noexcept {
return false; }
252 static bool is_null(
const std::stringstream &) {
return false; }
253 [[noreturn]]
static std::stringstream
null()
256 { Obj.clear(); Obj << Str; }
257 static std::string
to_string(
const std::stringstream &Obj)
258 {
return Obj.str(); }
280 if (Str ==
nullptr)
throw std::runtime_error{
"Attempt to read null string."};
292 template<
typename T>
inline void from_string(
const char Str[], T &Obj,
size_t)
298 inline void from_string<std::string>(
303 if (Str ==
nullptr)
throw std::runtime_error{
"Attempt to read null string."};
304 Obj.assign(Str, len);
315 template<>
inline void
325 {
return static_cast<char>(i+
'0'); }
334 template<
typename T>
inline std::string
to_string(
const T &Obj)
int encoding_id() const
Get the connection's encoding, as a PostgreSQL-defined code.
Definition: connection_base.cxx:1417
static constexpr const char * name() noexcept
Definition: strconv.hxx:195
static constexpr bool has_null() noexcept
Definition: strconv.hxx:251
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:240
result exec(const std::string &Query, const std::string &Desc=std::string{})
Execute query.
Definition: transaction_base.cxx:249
Private namespace for libpqxx's internal use; do not access.
Definition: connection_base.hxx:43
static void from_string(const char Str[], std::stringstream &Obj)
Definition: strconv.hxx:255
static constexpr bool has_null() noexcept
Definition: strconv.hxx:154
accesspolicy
Cursor access-pattern policy.
Definition: cursor.hxx:50
static bool is_null(const char *t)
Definition: strconv.hxx:197
@ forward_only
Cursor can move forward only.
Definition: cursor.hxx:53
static std::string to_string(TYPE Obj)
static constexpr bool has_null() noexcept
Definition: strconv.hxx:230
Result set containing data returned by a query or command.
Definition: result.hxx:69
void from_string(const field &F, T &Obj)
Convert a field's string contents to another type.
Definition: field.hxx:365
static constexpr bool has_null() noexcept
Definition: strconv.hxx:207
static bool is_null(const std::stringstream &)
Definition: strconv.hxx:252
static difference_type all() noexcept
Special value: read until end.
Definition: cursor.cxx:27
static bool is_null(const char *t)
Definition: strconv.hxx:208
Give a human-readable name for a type, at compile time.
Definition: strconv.hxx:67
static std::string to_string(const char *Obj)
Definition: strconv.hxx:200
static std::string to_string(char *Obj)
Definition: strconv.hxx:214
encoding_group enc_group(int libpq_enc_id)
Definition: encodings.cxx:637
static bool is_null(const std::string &)
Definition: strconv.hxx:231
static const std::string to_string(const std::string &Obj)
Definition: strconv.hxx:245
static bool is_null(const char t[])
Definition: strconv.hxx:222
std::string quote_name(const std::string &identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:225
updatepolicy
Cursor update policy.
Definition: cursor.hxx:62
static constexpr const char * name() noexcept
Definition: strconv.hxx:240
Internal error in libpqxx library.
Definition: except.hxx:207
typename std::underlying_type< ENUM >::type underlying_type
Definition: strconv.hxx:151
static constexpr bool has_null() noexcept
Definition: strconv.hxx:106
constexpr char number_to_digit(int i) noexcept
Definition: strconv.hxx:324
static constexpr bool has_null() noexcept
Definition: strconv.hxx:221
static std::string to_string(ENUM Obj)
Definition: strconv.hxx:165
static void from_string(const char Str[], std::string &Obj)
Definition: strconv.hxx:234
static std::string to_string(const std::stringstream &Obj)
Definition: strconv.hxx:257
static void from_string(const char Str[], TYPE &Obj)
static void from_string(const char Str[], ENUM &Obj)
Definition: strconv.hxx:158
connection_base & conn() const
Connection this transaction is running in.
Definition: transaction_base.hxx:492
static void from_string(const char Str[], const char *&Obj)
Definition: strconv.hxx:199
static difference_type backward_all() noexcept
Special value: read backwards from current position back to origin.
Definition: cursor.cxx:35
static constexpr const char * name() noexcept
Definition: strconv.hxx:250
static bool is_null(const std::string &)
Definition: strconv.hxx:242
Helper: string traits implementation for built-in types.
Definition: strconv.hxx:102
static constexpr bool has_null() noexcept
Definition: strconv.hxx:241
static std::string to_string(const char Obj[])
Definition: strconv.hxx:224
static constexpr const char * name() noexcept
Definition: strconv.hxx:206
result_difference_type difference_type
Definition: cursor.hxx:44
static constexpr const char * name() noexcept
Definition: strconv.hxx:229
static constexpr const char * name() noexcept
Definition: strconv.hxx:104
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
static constexpr const char * name() noexcept
Definition: strconv.hxx:220
Common definitions for cursor types.
Definition: cursor.hxx:40
@ owned
Destroy SQL cursor when cursor object is closed at end of transaction.
Definition: cursor.hxx:92
static bool is_null(TYPE)
Definition: strconv.hxx:107
constexpr int digit_to_number(char c) noexcept
Compute numeric value of given textual digit (assuming that it is a digit)
Definition: strconv.hxx:323
ownershippolicy
Cursor destruction policy.
Definition: cursor.hxx:89
@ update
Cursor can be used to update data as well as read it.
Definition: cursor.hxx:67
@ loose
Leave SQL cursor in existence after close of object and transaction.
Definition: cursor.hxx:94
static std::string to_string(const std::string &Obj)
Definition: strconv.hxx:235
Traits class for use in string conversions.
Definition: strconv.hxx:51
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:136
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:218
PQXX_DECLARE_TYPE_NAME(bool)
Helper class for defining enum conversions.
Definition: strconv.hxx:149
static constexpr bool has_null() noexcept
Definition: strconv.hxx:196
void throw_null_conversion(const std::string &type)
Throw exception for attempt to convert null to given type.
Definition: strconv.cxx:58