Wt: Wt::Dbo::sql_value_traits< V, Enable > Class Template Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
Wt::Dbo::sql_value_traits< V, Enable > Class Template Reference
Database Objects (Wt::Dbo)

Traits class for value types. More...

`#include <Wt/Dbo/SqlTraits.h>`

## Static Public Member Functions
static const char * | type (SqlConnection *connection, int size)
| Returns the SQL type name.
static void | bind (const V &v, SqlStatement *statement, int index, int size)
| Binds a value to a statement parameter.
static bool | read (V &v, SqlStatement *statement, int column, int size)
| Reads a result from an executed query.

## Detailed Description

template<typename V, class Enable>
class Wt::Dbo::sql_value_traits< V, Enable >

Traits class for value types.

This traits class may be specialized for a custom type `V`, to add dbo support for custom types. A value type has a one-to-one mapping to a single database column.

The library has built-in support for:

- `std::string`
- `char const *` (read-only: only as a bound parameter for a query)
- `short`, `int`, `long long`
- `long`: since the size of a `long` is 64bit on UNIX/Linux 64bit systems and 32bit otherwise, it is mapped to an `integer` or a `bigint` depending on the environment.
- `float`, `double`
- enum types
- `bool`
- `std::vector<unsigned char>` (binary data)
- `boost::optional<T>`: to make the type optional (allowing an SQL `null` value)
- `std::optional<T>`: to make the type optional (allowing an SQL `null` value, C++17 only)
- `std::chrono::system_clock::time_point`: time stamp
- `std::chrono::duration<int, std::milli>`: time interval

In <Wt/Dbo/WtSqlTraits.h>, traits classes are also provided for:

- WDate
- WDateTime
- WTime
- WString
- Json::Object
- Json::Array

Example for an enum that is saved as a string rather than an int:

enum class Pet {
Cat,
Dog,
Other
};
std::string petToString(Pet p) {
switch (p) {
case Pet::Cat:
return "cat";
case Pet::Dog:
return "dog";
case Pet::Other:
return "other";
}
throw std::invalid_argument("Unknown pet type: " + std::to_string(static_cast<int>(p)));
}
Pet petFromString(const std::string &s) {
if (s == "cat")
return Pet::Cat;
else if (s == "dog")
return Pet::Dog;
else if (s == "other")
return Pet::Other;
else
throw std::invalid_argument("Unknown pet type: " + s);
}
namespace Wt {
namespace Dbo {
template<>
struct sql_value_traits<Pet>
{
static std::string type(SqlConnection *conn, int size)
{
return sql_value_traits<std::string>::type(conn, size);
}
static void bind(Pet p, SqlStatement *statement, int column, int size)
{
statement->bind(column, petToString(p));
}
static bool read(Pet &p, SqlStatement *statement, int column, int size)
{
std::string s;
bool result = statement->getResult(column, &s, size);
if (!result)
return false;
p = petFromString(s);
return true;
}
};
} // Dbo
} // Wt
Wt
The namespace for Wt.
Definition strand.hpp:29
Wt::EventType::Other
@ Other
An event which is not user- or timer-initiated.
Wt::Dbo::sql_value_traits::type
static const char * type(SqlConnection *connection, int size)
Returns the SQL type name.
Wt::Dbo::sql_value_traits::read
static bool read(V &v, SqlStatement *statement, int column, int size)
Reads a result from an executed query.
Wt::Dbo::sql_value_traits::bind
static void bind(const V &v, SqlStatement *statement, int index, int size)
Binds a value to a statement parameter.

See also
  query_result_traits

## Member Function Documentation

## ◆ bind()

template<typename V , class Enable >

static void Wt::Dbo::sql_value_traits< V, Enable >::bind | ( | const V & | v,
|  | SqlStatement * | statement,
|  | int | index,
|  | int | size )
| static

Binds a value to a statement parameter.

The value `v` must be bound to parameter with index `index` in the `statement`.

See also
  SqlStatement::bind()

## ◆ read()

template<typename V , class Enable >

static bool Wt::Dbo::sql_value_traits< V, Enable >::read | ( | V & | v,
|  | SqlStatement * | statement,
|  | int | column,
|  | int | size )
| static

Reads a result from an executed query.

The value `v` must be read from result column `column` in the `statement`.

Returns `true` if the value was not `null`. This result may be used by the boost::optional<V> specialization to support fields that may have `null` values.

See also
  SqlStatement::getResult()

## ◆ type()

template<typename V , class Enable >

static const char * Wt::Dbo::sql_value_traits< V, Enable >::type | ( | SqlConnection * | connection,
|  | int | size )
| static

Returns the SQL type name.

The `size` (for strings) is a hint and may be ignored by a back-end.

This will usually return a type ending with " not null" except for C++ types that support `null` values. For a normal c++ value type `T`, boost::optional<T> has been specialized to allow for `null` values.