Skip to content

Commit 7926910

Browse files
committed
add double/float supporting for query class of db module
1 parent ec39784 commit 7926910

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

lib_acl_cpp/include/acl_cpp/db/query.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,19 @@ class ACL_CPP_API query
6161
* 设置单精度浮点类型的变量值
6262
* @param name {const char*} 变量名
6363
* @param value {float} 单精度浮点类型
64+
* @param precision {int} 尾数的精度值
6465
* @return {query&}
6566
*/
66-
query& set_parameter(const char* name, float value);
67+
query& set_parameter(const char* name, float value, int precision = 8);
6768

6869
/**
6970
* 设置双精度浮点类型的变量值
7071
* @param name {const char*} 变量名
7172
* @param value {double} 双精度浮点类型
73+
* @param precision {int} 尾数的精度值
7274
* @return {query&}
7375
*/
74-
query& set_parameter(const char* name, double value);
76+
query& set_parameter(const char* name, double value, int precision = 8);
7577

7678
/**
7779
* 设置 64 位短整类型的变量值
@@ -161,6 +163,7 @@ class ACL_CPP_API query
161163
{
162164
char type;
163165
int dlen;
166+
int precision;
164167
union
165168
{
166169
char c;

lib_acl_cpp/samples/db/query/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ int main(void)
2626

2727
int age = 20;
2828
query.create_sql("update table set name=:name, age=%d"
29-
", home=:home where nick=:name", age)
29+
", home=:home where nick=:name and price=:price", age)
3030
.set_parameter("name", "zsx1&xsz1")
31-
.set_parameter("home", "回龙观");
31+
.set_parameter("home", "回龙观")
32+
.set_parameter("price", 1.00212, 6);
3233
printf("sql: %s\r\n", query.to_string().c_str());
3334

3435
query.reset();

lib_acl_cpp/src/db/query.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "acl_stdafx.hpp"
22
#include <assert.h>
33
#include "acl_cpp/stdlib/log.hpp"
4+
#include "acl_cpp/stdlib/snprintf.hpp"
45
#include "acl_cpp/db/query.hpp"
56

67
namespace acl
@@ -39,6 +40,8 @@ bool query::append_key(string& buf, char* key)
3940
return false;
4041
}
4142

43+
char fmt[256];
44+
4245
query_param* param = it->second;
4346
switch (param->type)
4447
{
@@ -58,6 +61,14 @@ bool query::append_key(string& buf, char* key)
5861
buf.format_append("'%s'",
5962
escape(param->v.S, param->dlen, buf_).c_str());
6063
break;
64+
case DB_PARAM_FLOAT:
65+
safe_snprintf(fmt, sizeof(fmt), "%%.%df", param->precision);
66+
buf.format_append(fmt, param->v.f);
67+
break;
68+
case DB_PARAM_DOUBLE:
69+
safe_snprintf(fmt, sizeof(fmt), "%%.%df", param->precision);
70+
buf.format_append(fmt, param->v.d);
71+
break;
6172
default:
6273
logger_error("unknown type: %d", param->type);
6374
break;
@@ -210,7 +221,7 @@ query& query::set_parameter(const char* name, acl_int64 value)
210221
return *this;
211222
}
212223

213-
query& query::set_parameter(const char* name, float value)
224+
query& query::set_parameter(const char* name, float value, int precision /* = 8 */)
214225
{
215226
string key(name);
216227
key.lower();
@@ -220,12 +231,16 @@ query& query::set_parameter(const char* name, float value)
220231
param->type = DB_PARAM_FLOAT;
221232
param->v.f = value;
222233
param->dlen = sizeof(float);
234+
if (precision >= 0)
235+
param->precision = precision;
236+
else
237+
param->precision = 8;
223238

224239
params_[key] = param;
225240
return *this;
226241
}
227242

228-
query& query::set_parameter(const char* name, double value)
243+
query& query::set_parameter(const char* name, double value, int precision /* = 8 */)
229244
{
230245
string key(name);
231246
key.lower();
@@ -235,6 +250,10 @@ query& query::set_parameter(const char* name, double value)
235250
param->type = DB_PARAM_DOUBLE;
236251
param->v.d = value;
237252
param->dlen = sizeof(double);
253+
if (precision >= 0)
254+
param->precision = precision;
255+
else
256+
param->precision = 8;
238257

239258
params_[key] = param;
240259
return *this;

0 commit comments

Comments
 (0)