📄 jy_oci.h
字号:
/*########################################################################
【文件名】: jy_oci.h
【名 称】: oci 封装类.
【版 本】: 0.20
【作 者】: 叶栋
【E-mail】: yedong113@163.com
---------------------------------------------------------
【创建时间】: 2009-1-6
【修改时间】: 2009-1-9 15:42:55
---------------------------------------------------------
【版本历史】:
[0.10] : 封装了 COciException、COciConnection、COciConnPool和COciCursor 类.
[-2009-1-9-]
[0.20] : 在COciCursor中提供了对Blob数据的读写
---------------------------------------------------------
【使用说明】:
1. 将Oracle的oci接口目录添加
########################################################################*/
#pragma once
#include <oci.h>
//引用库
#if defined(WIN32)
#pragma comment(lib, "oci.lib")
#endif
#include <vector>
#include <string>
using namespace std;
class COciException
{
public:
//构造函数
COciException(){}
//检查status状态下的错误
static bool checkErr(OCIError *pErr, sb4 status);
//获取错误码
static int errCode() { return err; }
static char* errMsg() { return (char*)msg; }
private:
static int err;
static char msg[512];
};
class COciConnection
{
protected:
//是否连接上
bool connected;
public:
//与连接相关的句柄
OCIEnv * m_envhp; //环境句柄
OCIError * m_errhp; //错误句柄
OCIServer * m_srvhp; //服务句柄
OCISvcCtx * m_svchp; //服务上下文句柄
OCISession * m_usrhp; //用户会话句柄
OCIDescribe * m_dschp; //描述句柄
OCITrans * m_txnhp; //事务句柄
public:
//构造函数
COciConnection();
//析构
~COciConnection() { Disconnect(); }
//建立与Oracle的连接
bool Connect(const char* dblink, const char* username, const char* password);
//断连
void Disconnect();
//是否连接
bool IsConnected() { return connected; }
};
//OCI 数据库连接池
class COciConnPool
{
public:
enum { MAX_CONN = 20 };
//获取实例
static COciConnPool* CreatePool();
//获取可用连接
COciConnection* GetConn();
//析构
~COciConnPool();
private:
//禁用缺省构造及拷贝构造
COciConnPool();
COciConnPool(const COciConnPool&);
COciConnPool& operator=(const COciConnPool&);
//连接池实例指针
static COciConnPool* m_pool;
//连接数组
vector<COciConnection*> m_conns;
};
class COciCursor
{
public:
//构造
COciCursor(COciConnection* connect);
~COciCursor(void);
private:
//打开
bool Open();
//检查游标是否打开
bool isOpen() { return m_isOpen; }
//关闭游标
void Close();
//获取连接指针
COciConnection* GetConn() { return m_conn; }
private:
void SetColInfo(int colNum);
ub4 GetLobSize(int colNum);
private:
//获取列数
bool GetColumnDesc();
//获取结果
bool Fetch(int rows=1);
//获取列数
int NumCols(void);
//返回记录行数
int NumRows(void);
//获取列类型
ub2 ColType (int colnumber);
//获取列名
char* ColName (int colnumber);
int GetColIdWithColName(std::string colName);
//获取列值的长度
ub2 ColSize (int colnumber);
//获取列值的小数位数
sb1 ColScale (int colnumber);
//得到SQL操作类型(SELECT、UPDATE、INSERT)
int QueryType(void);
//为游标分配空间
bool AllocateCursor(void);
char *GetFildValue(int i);
void FreeResult();
public:
int GetCollect(int index,std::string &value);
int GetCollent(std::string col_name,std::string &value);
//查询SQL 传入参数 C 风格字符串
bool Query(const char* query);
//查询SQL 传入参数 C++ 风格字符串
bool Query(string query);
bool ReadBlob(std::string col_name,char* &buffer, int &bufsize);
bool WriteBlob(std::string col_name,char* buffer,unsigned int bufsize);
//准备SQL语句 传入参数 C 风格字符串
void Prepare(const char* stmt);
//准备SQL语句 传入参数 c++风格字符串
void Prepare(string stmt);
//执行SQL、获取结果集 缺省为执行一次
bool Execute(int times=1);
//绑定输入变量
void BindByPos(int pos, void* value, int size, void* indicator, int type);
void BindByName(char* name, void* value, int size, void* indicator, int type);
//定义输出变量
void DefineByPos(int pos, void* value, int size, void* indicator, int type);
public:
COciConnection* m_conn; //连接
OCIStmt* m_stmthp; //语句句柄
OCIParam* m_colhd; //列句柄
bool m_isOpen; //是否打开游标
string m_fieldValue; //字段值
vector<void *> m_buffers; //一行的记录值缓冲
vector <sb2> m_ind; //空指示符数组
vector<OCIDefine *> m_defines; //定义句柄数组
vector<OCILobLocator *>m_vp_lob;//定义lob定位符数组
OCILobLocator* m_lobp; //lob定位符
vector<int> m_col_type; //列类型数组
vector<int> m_col_size; //列大小数组
vector<int> m_col_scale; //列的小数位数数组
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -