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

203 lines
5.5 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/string.hpp"
#include "../stdlib/noncopyable.hpp"
2014-11-19 00:25:21 +08:00
#include <map>
#if !defined(ACL_DB_DISABLE)
2014-11-19 00:25:21 +08:00
namespace acl
{
/**
* SQL sql 使
* java hibernate SQL
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API query : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
query();
~query();
/**
* sql printf
* @param sql_fmt {const char*} sql
* select * from xxx where name = :name and len >= %d
* :name, set_parameter , len
* @return {query&}
*/
query& create_sql(const char* sql_fmt, ...) ACL_CPP_PRINTF(2, 3);
/**
* sql
* @param sql {const char*} sql
2014-11-19 00:25:21 +08:00
* select * from xxx where name = :name and len >= :len
* :name, :len set_parameter
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& create(const char* sql);
2014-11-19 00:25:21 +08:00
/**
*
* @param name {const char*}
* @param value {const char*}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_parameter(const char* name, const char *value);
/**
*
* @param name {const char*}
* @param value {char}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_parameter(const char* name, char value);
/**
* 16
* @param name {const char*}
* @param value {short}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_parameter(const char* name, short value);
/**
* 32
* @param name {const char*}
* @param value {int}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_parameter(const char* name, int value);
/**
*
* @param name {const char*}
* @param value {float}
* @param precision {int}
* @return {query&}
*/
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, int precision = 8);
2014-11-19 00:25:21 +08:00
/**
* 64
* @param name {const char*}
* @param value {long long int}
2014-11-19 00:25:21 +08:00
* @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
/**
* (time_t)
* @param name {const char*}
* @param value {time_t}
* @param fmt {const char*}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_date(const char* name, time_t value,
const char* fmt = "%Y-%m-%d %H:%M:%S");
/**
*
* @param name {const char*}
* @param fmt {const char*}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_format(const char* name, const char* fmt, ...)
ACL_CPP_PRINTF(3, 4);
/**
*
* @param name {const char*}
* @param fmt {const char*}
* @param ap {va_list}
2014-11-19 00:25:21 +08:00
* @return {query&}
*/
query& set_vformat(const char* name, const char* fmt, va_list ap);
/**
* sql
2014-11-19 00:25:21 +08:00
* @return {const string&}
*/
const string& to_string();
/**
* SQL 使
* SQL
2014-11-19 00:25:21 +08:00
*/
void reset();
/**
* sql SQL
* @param in {const char*}
* @param len {size_t} in
* @param out {string&}
* @return {const string&} ( out )
2014-11-19 00:25:21 +08:00
*/
static const string& escape(const char* in, size_t len, string& out);
/**
* DateTime (YYYY-MM-DD HH:MM:SS)
* @param t {time_t}
* @param out {string&}
* @param fmt {const char*} _WIN32
* _WIN32 API "%Y-%m-%d %H:%M:%S"
* @return {const char*} NULL
2014-11-19 00:25:21 +08:00
*/
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_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;
int precision;
2014-11-19 00:25:21 +08:00
union
{
char c;
short s;
int n;
long long int l;
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
#endif // !defined(ACL_DB_DISABLE)