mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 10:57:34 +08:00
add double/float supporting for query class of db module
This commit is contained in:
parent
ec3978400d
commit
79269106c8
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user