2014-11-19 00:25:21 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include "acl_cpp/acl_cpp_define.hpp"
|
|
|
|
|
#include "acl_cpp/stdlib/string.hpp"
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SQL <EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD> sql <EFBFBD>е<EFBFBD>һЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>壬ʹ<EFBFBD>÷<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* java hibernate <EFBFBD><EFBFBD> SQL <EFBFBD><EFBFBD><EFBFBD>乹<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|
|
|
|
*/
|
|
|
|
|
class ACL_CPP_API query
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
query();
|
|
|
|
|
~query();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> sql <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param sql_fmt {const char*} sql <EFBFBD><EFBFBD><EFBFBD>䣬<EFBFBD><EFBFBD>ʽ<EFBFBD>磺
|
|
|
|
|
* select * from xxx where name = :name and len >= :len
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> :name, :len <EFBFBD><EFBFBD><EFBFBD><EFBFBD> set_parameter <EFBFBD>е<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& create_sql(const char* sql_fmt, ...) ACL_CPP_PRINTF(2, 3);;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_parameter(const char* name, const char *value);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {char} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_parameter(const char* name, char value);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 16 λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {short} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_parameter(const char* name, short value);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 32 λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_parameter(const char* name, int value);
|
|
|
|
|
|
2015-07-21 15:26:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {float} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-07-21 17:16:53 +08:00
|
|
|
|
* @param precision {int} β<EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
2015-07-21 15:26:53 +08:00
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
2015-07-21 17:16:53 +08:00
|
|
|
|
query& set_parameter(const char* name, float value, int precision = 8);
|
2015-07-21 15:26:53 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><EFBFBD><EFBFBD>ȸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {double} ˫<EFBFBD><EFBFBD><EFBFBD>ȸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-07-21 17:16:53 +08:00
|
|
|
|
* @param precision {int} β<EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
2015-07-21 15:26:53 +08:00
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
2015-07-21 17:16:53 +08:00
|
|
|
|
query& set_parameter(const char* name, double value, int precision = 8);
|
2015-07-21 15:26:53 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 64 λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {long long int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
2015-06-29 17:33:11 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
query& set_parameter(const char* name, __int64 value);
|
|
|
|
|
#else
|
|
|
|
|
query& set_parameter(const char* name, long long int value);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(time_t)<EFBFBD><EFBFBD><EFBFBD>͵ı<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param value {time_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param fmt {const char*} <EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD>ʽ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_date(const char* name, time_t value,
|
|
|
|
|
const char* fmt = "%Y-%m-%d %H:%M:%S");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>Ա<EFBFBD><EFBFBD>η<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param fmt {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ʽ
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_format(const char* name, const char* fmt, ...)
|
|
|
|
|
ACL_CPP_PRINTF(3, 4);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>Ա<EFBFBD><EFBFBD>η<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param name {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param fmt {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ʽ
|
|
|
|
|
* @param ap {va_list} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>б<EFBFBD>
|
|
|
|
|
* @return {query&}
|
|
|
|
|
*/
|
|
|
|
|
query& set_vformat(const char* name, const char* fmt, va_list ap);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>Բ<EFBFBD>ѯ sql <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {const string&}
|
|
|
|
|
*/
|
|
|
|
|
const string& to_string();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ղ<EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>εĻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SQL <EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮ǰ<EFBFBD><EFBFBD> SQL <EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD>״̬
|
|
|
|
|
*/
|
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD> sql <EFBFBD>е<EFBFBD>һЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>崦<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD>ֹ SQL ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param in {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
* @param len {size_t} in <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param out {string&} <EFBFBD>洢ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {const string&} ת<EFBFBD>崦<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD> out <EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|
|
|
|
*/
|
|
|
|
|
static const string& escape(const char* in, size_t len, string& out);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD> DateTime <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>(YYYY-MM-DD HH:MM:SS)
|
|
|
|
|
* @param t {time_t} ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param out {string&} <EFBFBD>洢ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-06-29 17:33:11 +08:00
|
|
|
|
* @param fmt {const char*} <EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> _WIN32 <EFBFBD>±<EFBFBD><EFBFBD>뱣֤<EFBFBD>ø<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>ԣ<EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> _WIN32 API <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>磺"%Y-%m-%d %H:%M:%S"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* @return {const char*} ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾת<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
static const char* to_date(time_t t, string& out,
|
|
|
|
|
const char* fmt = "%Y-%m-%d %H:%M:%S");
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
DB_PARAM_CHAR,
|
|
|
|
|
DB_PARAM_SHORT,
|
|
|
|
|
DB_PARAM_INT32,
|
|
|
|
|
DB_PARAM_INT64,
|
2015-07-21 15:26:53 +08:00
|
|
|
|
DB_PARAM_FLOAT,
|
|
|
|
|
DB_PARAM_DOUBLE,
|
2014-11-19 00:25:21 +08:00
|
|
|
|
DB_PARAM_STR
|
|
|
|
|
} db_param_type;
|
|
|
|
|
|
|
|
|
|
struct query_param
|
|
|
|
|
{
|
|
|
|
|
char type;
|
|
|
|
|
int dlen;
|
2015-07-21 17:16:53 +08:00
|
|
|
|
int precision;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
short s;
|
|
|
|
|
int n;
|
|
|
|
|
long long int l;
|
2015-07-21 15:26:53 +08:00
|
|
|
|
double d;
|
|
|
|
|
float f;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
char S[1];
|
|
|
|
|
} v;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::map<string, query_param*> params_;
|
|
|
|
|
string* sql_buf_;
|
|
|
|
|
string sql_;
|
|
|
|
|
string buf_;
|
|
|
|
|
|
|
|
|
|
void del_param(const string& key);
|
|
|
|
|
bool append_key(string& buf, char* key);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|