acl/lib_acl_cpp/include/acl_cpp/db/query.hpp

166 lines
3.9 KiB
C++
Raw Normal View History

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);
/**
* <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&}
*/
#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>
* @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"
* @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,
DB_PARAM_STR
} db_param_type;
struct query_param
{
char type;
int dlen;
union
{
char c;
short s;
int n;
long long int l;
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