add double/float supporting for query class of db module

This commit is contained in:
zsx 2015-07-21 17:16:53 +08:00
parent ec3978400d
commit 79269106c8
3 changed files with 29 additions and 6 deletions

View File

@ -61,17 +61,19 @@ public:
*
* @param name {const char*}
* @param value {float}
* @param precision {int}
* @return {query&}
*/
query& set_parameter(const char* name, float value);
query& set_parameter(const char* name, float value, int precision = 8);
/**
*
* @param name {const char*}
* @param value {double}
* @param precision {int}
* @return {query&}
*/
query& set_parameter(const char* name, double value);
query& set_parameter(const char* name, double value, int precision = 8);
/**
* 64
@ -161,6 +163,7 @@ private:
{
char type;
int dlen;
int precision;
union
{
char c;

View File

@ -26,9 +26,10 @@ int main(void)
int age = 20;
query.create_sql("update table set name=:name, age=%d"
", home=:home where nick=:name", age)
", home=:home where nick=:name and price=:price", age)
.set_parameter("name", "zsx1&xsz1")
.set_parameter("home", "»ØÁú¹Û");
.set_parameter("home", "»ØÁú¹Û")
.set_parameter("price", 1.00212, 6);
printf("sql: %s\r\n", query.to_string().c_str());
query.reset();

View File

@ -1,6 +1,7 @@
#include "acl_stdafx.hpp"
#include <assert.h>
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/db/query.hpp"
namespace acl
@ -39,6 +40,8 @@ bool query::append_key(string& buf, char* key)
return false;
}
char fmt[256];
query_param* param = it->second;
switch (param->type)
{
@ -58,6 +61,14 @@ bool query::append_key(string& buf, char* key)
buf.format_append("'%s'",
escape(param->v.S, param->dlen, buf_).c_str());
break;
case DB_PARAM_FLOAT:
safe_snprintf(fmt, sizeof(fmt), "%%.%df", param->precision);
buf.format_append(fmt, param->v.f);
break;
case DB_PARAM_DOUBLE:
safe_snprintf(fmt, sizeof(fmt), "%%.%df", param->precision);
buf.format_append(fmt, param->v.d);
break;
default:
logger_error("unknown type: %d", param->type);
break;
@ -210,7 +221,7 @@ query& query::set_parameter(const char* name, acl_int64 value)
return *this;
}
query& query::set_parameter(const char* name, float value)
query& query::set_parameter(const char* name, float value, int precision /* = 8 */)
{
string key(name);
key.lower();
@ -220,12 +231,16 @@ query& query::set_parameter(const char* name, float value)
param->type = DB_PARAM_FLOAT;
param->v.f = value;
param->dlen = sizeof(float);
if (precision >= 0)
param->precision = precision;
else
param->precision = 8;
params_[key] = param;
return *this;
}
query& query::set_parameter(const char* name, double value)
query& query::set_parameter(const char* name, double value, int precision /* = 8 */)
{
string key(name);
key.lower();
@ -235,6 +250,10 @@ query& query::set_parameter(const char* name, double value)
param->type = DB_PARAM_DOUBLE;
param->v.d = value;
param->dlen = sizeof(double);
if (precision >= 0)
param->precision = precision;
else
param->precision = 8;
params_[key] = param;
return *this;