首页 > 代码库 > OTL简单封装和使用说明

OTL简单封装和使用说明

#ifndef OTLV4_HANDLE_H
#define OTLV4_HANDLE_H
// =================================================================================
// ORACLE, ODBC and DB2/CLI Template Library, Version 4.0.262,
// Copyright (C) 1996-2012, Sergei Kuchin (skuchin@gmail.com)
// modified by carea @ 2014-10-29 Version 1.0
// =================================================================================

/************************************************************************/
/*                                                                      */  
/*            邦定变量类型            |    C++ 程序使用的对应类型            */
/*            USED IN SQL select    |    USED IN C++ TYPE               */
/*            int                   |     long long                     */
/*            timestamp             |     otl_datetime                  */
/*            varchar               |     std::string                   */
/*            bigint                |     long long                     */
/*                                                                      */
/************************************************************************/

//数据类型应该支持的输入输出操作
//otl_stream& operator>>(otl_stream& in ,T& t)
//otl_stream& operator<<(otl_stream& out ,const T& t)

/*********************************************************************************/
/*                                                                               */
/*                            常用SQL语句写法                                      */
/*otl_handle.select("select f2 from test_tab",arr);                              */
/*otl_handle.insert("insert into test_tab1(f2)values(:f2<char[30]> )",arr);      */
/*                                                                               */
/*说明:数据库表中如果有属性名为:IP、PORT、INTERVAL这种字段,赶紧改名!!(与关键字冲突)   */
/*                                                                               */
/*********************************************************************************/

#ifdef OTLV4_HANDLE_WINDOWS
    #define OTL_ODBC_MSSQL_2008
    #define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
    #define OTL_STL//using std::string
    #define OTL_ANSI_CPP  // Turn on ANSI C++ typecasts
    #define OTL_BIGINT  long long
    #define OTL_STR_TO_BIGINT(str,n) {  n=_atoi64(str); }
    #define OTL_BIGINT_TO_STR(n,str) {  _i64toa(n,str,10); }
#endif

#ifdef OTLV4_HANDLE_LINUX
    #define OTL_ODBC_UNIX 
    #define OTL_ODBC_MYSQL 
    #define OTL_STL//using std::string
    #define OTL_ANSI_CPP  // Turn on ANSI C++ typecasts
    #define OTL_BIGINT  long long
    #define OTL_STR_TO_BIGINT(str,n) {  n=atoll(str); }
    #define OTL_BIGINT_TO_STR(n,str) {  sprintf(str,"%lld", n);}
#endif

#include "otlv4.h"// include the OTL 4.0 header file

#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

#ifndef SCP_EXPORT_H
#include "SCP_Export.h"
#endif

class SCP_Export Otlv4_Handle
{
public:
    Otlv4_Handle();
    bool logon(const string& db_info,string& error);
    bool reconnect_to_db_once(void);
    ~Otlv4_Handle(void);
    template<typename T>
    int insert(const string& sql,const vector<T>& data);
    template<typename T>
    int select(const string& sql,vector<T>& data,string& error);
private:
    otl_connect db; // connect object
    string db_infor_;
};

template<typename T>
int Otlv4_Handle::insert(const string& sql,const vector<T>& data)
{
    if (!db.connected)
    {
        return -4;
    }
    db.auto_commit_off();
    otl_stream out;
    try
    {
        out.open(1, //buffer size should be == 1 always on INSERT.
            sql.c_str(),
            db); // connectobject
    }
    catch(otl_exception& p)
    {
        out.flush();
        out.close();
        return -1;
    }

    //submit
    try
    {
        for (size_t i = 0 ; i < data.size(); i++)
        {
            out<<data[i];    
        }
        //testb
        out.flush();
        db.commit();
        //teste
    }
    catch (otl_exception& p)
    {
        out.flush();
        out.close();
        return -2;
    }
    catch(...)
    {
        out.flush();
        out.close();
        return -3;
    }
    return 0;
}

template<typename T>
int Otlv4_Handle::select(const string& sql,vector<T>& data,string& error)
{
    //使用邦定变量类型
    //sql = "select* from "+talbe;
    if (!db.connected)
    {
        return -4;
    }

    db.auto_commit_off();
    otl_stream in;
    try
    {
        in.open(100, //buffer size
            sql.c_str(),
            // SELECTstatement
            db // connectobject
            );
    }
    catch(otl_exception&p)
    {
        char temp[3000];
        _snprintf(temp, sizeof(temp) - 1, 
            "otl err msg : %s \notl sql : %s\notl variable that caused the error : \n",
            p.msg,
            p.stm_text,
            p.var_info);
        error.append(string(temp,sizeof(temp)));
        return -1;
    }

    try
    {
        // copy all rows to be fetched into the vector
        copy(otl_input_iterator<T,ptrdiff_t>(in),
            otl_input_iterator<T,ptrdiff_t>(),
            back_inserter(data));    
    }
    catch(otl_exception&p)
    { 
        char temp[3000];
        _snprintf(temp, sizeof(temp) - 1, 
            "otl err msg : %s \notl sql : %s\notl variable that caused the error : \n",
            p.msg,
            p.stm_text,
            p.var_info);
        error.append(string(temp,sizeof(temp)));
        return -2;
    }
    return 0;
}

#endif



OTL简单封装和使用说明