Commit 7204ce1a authored by Davis King's avatar Davis King

Added templated versions of get_column() and bind() that inter what

type to get/bind based on the type of variable passed in.
parent 0c23aae4
......@@ -11,6 +11,7 @@
#include <sqlite3.h>
#include "../smart_pointers.h"
#include "../serialize.h"
#include <limits>
// --------------------------------------------------------------------------------------------
......@@ -223,6 +224,31 @@ namespace dlib
return sql_string;
}
template <typename T>
typename enable_if_c<std::numeric_limits<T>::is_integer>::type get_column (
unsigned long idx,
T& item
) const
{
if (sizeof(T) <= 4)
item = get_column_as_int(idx);
else
item = get_column_as_int64(idx);
}
void get_column(unsigned long idx, std::string& item) const { item = get_column_as_text(idx); }
void get_column(unsigned long idx, float& item ) const { item = get_column_as_double(idx); }
void get_column(unsigned long idx, double& item ) const { item = get_column_as_double(idx); }
void get_column(unsigned long idx, long double& item) const { item = get_column_as_double(idx); }
template <typename T>
typename disable_if_c<std::numeric_limits<T>::is_integer>::type get_column (
unsigned long idx,
T& item
) const
{
get_column_as_object(idx, item);
}
const std::vector<char> get_column_as_blob (
unsigned long idx
......@@ -354,6 +380,32 @@ namespace dlib
return sqlite3_bind_parameter_index(stmt, name.c_str());
}
template <typename T>
typename enable_if_c<std::numeric_limits<T>::is_integer>::type bind (
unsigned long idx,
T& item
)
{
if (sizeof(T) <= 4)
bind_int(idx, item);
else
bind_int64(idx, item);
}
void bind(unsigned long idx, std::string& item) { bind_text(idx, item); }
void bind(unsigned long idx, float& item ) { bind_double(idx, item); }
void bind(unsigned long idx, double& item ) { bind_double(idx, item); }
void bind(unsigned long idx, long double& item) { bind_double(idx, item); }
template <typename T>
typename disable_if_c<std::numeric_limits<T>::is_integer>::type bind (
unsigned long idx,
T& item
)
{
bind_object(idx, item);
}
void bind_blob (
unsigned long parameter_id,
const std::vector<char>& item
......
......@@ -244,6 +244,31 @@ namespace dlib
routines.
!*/
template <
typename T
>
void get_column (
unsigned long idx,
T& item
) const;
/*!
requires
- idx < get_num_columns()
ensures
- This function automatically selects how to extract the column data based
on the type of item given. In particular:
- if (T is a 32bit or smaller built in integer type) then
- #item == get_column_as_int(idx)
- else if (T is a 64bit built in integer type) then
- #item == get_column_as_int64(idx)
- else if (T is float, double, or long double) then
- #item == get_column_as_double(idx)
- else if (T is std::string) then
- #item == get_column_as_text(idx)
- else
- invokes: get_column_as_object(idx, item)
!*/
const std::vector<char> get_column_as_blob (
unsigned long idx
) const;
......@@ -349,6 +374,31 @@ namespace dlib
- returns 0
!*/
template <
typename T
>
void bind (
unsigned long parameter_id,
T& item
) const;
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- This function automatically selects how to bind item to a statement based
on the type of item given. In particular:
- if (T is a 32bit or smaller built in integer type) then
- invokes: bind_int(parameter_id, item)
- else if (T is a 64bit built in integer type) then
- invokes: bind_int64(parameter_id, item)
- else if (T is float, double, or long double) then
- invokes: bind_double(parameter_id, item)
- else if (T is std::string) then
- invokes: bind_text(parameter_id, item)
- else
- invokes: bind_object(parameter_id, item)
!*/
void bind_blob (
unsigned long parameter_id,
const std::vector<char>& item
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment