C/C++19_postgresql.h,_postgresql.cpp

Posted 码农编程录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C/C++19_postgresql.h,_postgresql.cpp相关的知识,希望对你有一定的参考价值。


1._postgresql.h

#ifndef _POSTGRESQL_H
#define _POSTGRESQL_H
// C/C++库常用头文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>

// postgresql接口库的头文件
#include <libpq-fe.h>

// 定义SQL语句中,输入和输出字段个数的最大值,256是很大的了,可以根据实际情况调整
#define MAXPARAMS  256

// 输入或输出参数的最大长度
#define MAXFIELDLENGTH 2000   

// OCI执行的结果
struct CDA_DEF

  int      rc;       // 执行结果
  unsigned long rpc; // 执行SQL语句后,影响记录的行数
  char     message[2048];
;

/* 数据库连接类 */
class connection

public:
  PGconn *m_conn;

  // 连接状态,0-未连接,1-已连接
  int m_state;

  // 数据库种类,固定取值为postgresql
  char m_dbtype[21];

  // 自动提交标志,0-关闭自动提交;1-开启自动提交
  int m_autocommitopt;

  // 用于存放connection操作数据库的错误或最后一次执行SQL语句的结果
  CDA_DEF m_cda;

  connection();

 ~connection();

  // 连接数据库,0-关闭自动提交;1-开启自动提交
  // connstr为连接数据库的参数,如下:
  // "host=127.0.0.1 user=postgres password=oracle dbname=fsqx port=5432"
  int connecttodb(char *connstr,char *charset,unsigned int autocommitopt=0);

  // 设置字符集,要与数据库的一致,否则中文会出现乱码
  void character(char *charset);

  // 断开与数据库的连接
  int  disconnect();

  // 开始事务
  int  beginwork();

  // 提交事务
  int  commitwork();

  // 回滚事务
  int  rollbackwork();
;

/* SQL语言数据操作类 */
class sqlstatement

public:
  // 与数据库连接池的状态,0-未绑定,1-已绑定
  int m_state;

  connection *m_conn;

  PGresult *m_res;

  // SQL语句
  char m_sql[10240];

  // 执行结果
  CDA_DEF m_cda;

  int m_sqltype;  // 待执行的SQL语句的类型,0-查询语句;1-非查询语句

  // 自动提交标志,0-关闭自动提交;1-开启自动提交
  int m_autocommitopt;

  sqlstatement();
  sqlstatement(connection *conn);

 ~sqlstatement();

  // 设定数据库连接
  int  connect(connection *conn);

  // 断开与数据库连接的连接,在Tuxedo和CICS的服务中,
  // 一定要用disconnect()断开与数据库的连接,否则会用尽数据库的光标资源
  int  disconnect();

  // 分析SQL语句,支持存储过程,采用立刻分析的方式,即时返回分析结果
  int  prepare(char *fmt,...);

  int   m_nParamsIn;
  char *m_paramValuesIn[MAXPARAMS];
  char  m_paramValuesInVal[MAXPARAMS][MAXFIELDLENGTH+1];
  char *m_paramValuesInPtr[MAXPARAMS];
  char  m_paramTypesIn[MAXPARAMS][15];
  int   m_paramLengthsIn[MAXPARAMS];

  // 结合输入变量的地址
  int  bindin(unsigned int position,int    *value);
  int  bindin(unsigned int position,long   *value);
  int  bindin(unsigned int position,unsigned int  *value);
  int  bindin(unsigned int position,unsigned long *value);
  int  bindin(unsigned int position,float *value);
  int  bindin(unsigned int position,double *value);
  int  bindin(unsigned int position,char   *value,unsigned int len);

  int   m_nParamsOut;
  char  m_paramTypesOut[MAXPARAMS][15];
  char *m_paramValuesOut[MAXPARAMS];
  int   m_paramLengthsOut[MAXPARAMS];

  // 结合输出变量的地址
  int  bindout(unsigned int position,int    *value);
  int  bindout(unsigned int position,long   *value);
  int  bindout(unsigned int position,unsigned int  *value);
  int  bindout(unsigned int position,unsigned long *value);
  int  bindout(unsigned int position,float *value);
  int  bindout(unsigned int position,double *value);
  int  bindout(unsigned int position,char   *value,unsigned int len);

  // 执行SQL语句
  int  execute();

  // 如果SQL语句不需要输入和输出变量,可以直接执行。
  int  execute(const char *fmt,...);

  int  m_respos;
  int  m_restotal;

  int  next();
;
#endif

2._postgresql.cpp

#include "_postgresql.h"

connection::connection()

  m_conn=0;

  m_state = 0;

  memset(&m_cda,0,sizeof(m_cda));

  m_cda.rc=-1;
  strcpy(m_cda.message,"database not open.");

  // 数据库种类
  memset(m_dbtype,0,sizeof(m_dbtype));
  strcpy(m_dbtype,"postgresql");


connection::~connection()

  disconnect();


int connection::connecttodb(char *connstr,char *charset,unsigned int autocommitopt)

  // 如果已连接上数据库,就不再连接
  // 所以,如果想重连数据库,必须显示的调用disconnect()方法后才能重连
  if (m_state == 1) return 0;

  m_conn=PQconnectdb(connstr);

  if (PQstatus(m_conn) != CONNECTION_OK)
  
    m_cda.rc=-1; strcpy(m_cda.message,PQerrorMessage(m_conn)); return -1;
  

  m_state = 1;

  // 设置字符集
  character(charset);

  m_autocommitopt=autocommitopt;

  beginwork();

  return 0;


// 设置字符集,要与数据库的一致,否则中文会出现乱码
void connection::character(char *charset)

  PQsetClientEncoding(m_conn, charset);

  return;


int connection::disconnect()

  memset(&m_cda,0,sizeof(m_cda));

  if (m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open."); return -1;
  

  rollbackwork();

  PQfinish(m_conn);

  m_conn=0;

  m_state = 0;

  return 0;


// 开始事务
int connection::beginwork()

  memset(&m_cda,0,sizeof(m_cda));

  if (m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open."); return -1;
  

  if (m_autocommitopt==1) return 0;

  PGresult *m_res = PQexec(m_conn,"BEGIN");

  if (PQresultStatus(m_res) != PGRES_COMMAND_OK)
  
    m_cda.rc=-1; strcpy(m_cda.message,PQerrorMessage(m_conn)); 
  

  PQclear(m_res); m_res=0;

  return m_cda.rc;


int connection::commitwork()

  memset(&m_cda,0,sizeof(m_cda));

  if (m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open."); return -1;
  

  if (m_autocommitopt==1) return 0;

  PGresult *m_res = PQexec(m_conn,"COMMIT");

  if (PQresultStatus(m_res) != PGRES_COMMAND_OK)
  
    m_cda.rc=-1; strcpy(m_cda.message,PQerrorMessage(m_conn));
  

  PQclear(m_res); m_res=0;

  beginwork();

  return m_cda.rc;


int connection::rollbackwork()

  memset(&m_cda,0,sizeof(m_cda));

  if (m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open."); return -1;
  

  if (m_autocommitopt==1) return 0;

  PGresult *m_res = PQexec(m_conn,"ROLLBACK");

  if (PQresultStatus(m_res) != PGRES_COMMAND_OK)
  
    m_cda.rc=-1; strcpy(m_cda.message,PQerrorMessage(m_conn));
  

  PQclear(m_res); m_res=0;

  beginwork();

  return m_cda.rc;



sqlstatement::sqlstatement()

  m_state=0;

  memset(&m_cda,0,sizeof(m_cda));

  memset(m_sql,0,sizeof(m_sql));

  m_res=0;

  m_conn=0;

  m_cda.rc=-1;

  strcpy(m_cda.message,"sqlstatement not connect to connection.\\n");

  m_nParamsIn=0;
  memset(m_paramValuesIn,0,sizeof(m_paramValuesIn));
  memset(m_paramValuesInVal,0,sizeof(m_paramValuesInVal));
  memset(m_paramValuesInPtr,0,sizeof(m_paramValuesInPtr));
  memset(m_paramLengthsIn,0,sizeof(m_paramLengthsIn));

  for (int ii=0;ii<MAXPARAMS;ii++)
  
    m_paramValuesIn[ii]=m_paramValuesInVal[ii];
  

  m_respos=0;

  m_restotal=0;

  m_nParamsOut=0;


sqlstatement::sqlstatement(connection *in_conn)

  sqlstatement();

  connect(in_conn);


sqlstatement::~sqlstatement()

  disconnect();


int sqlstatement::disconnect()

  if (m_state == 0) return 0;

  memset(&m_cda,0,sizeof(m_cda));

  if (m_res!= 0)  PQclear(m_res); m_res=0; 

  m_state=0;

  memset(&m_cda,0,sizeof(m_cda));

  memset(m_sql,0,sizeof(m_sql));

  m_cda.rc=-1;
  strcpy(m_cda.message,"cursor not open.");

  PQclear(m_res); m_res=0;

  m_nParamsIn=0;
  memset(m_paramValuesInVal,0,sizeof(m_paramValuesInVal));
  memset(m_paramLengthsIn,0,sizeof(m_paramLengthsIn));
  memset(m_paramTypesIn,0,sizeof(m_paramTypesIn));

  m_respos=0;
  m_restotal=0;
  m_nParamsOut=0;
  memset(m_paramValuesOut,0,sizeof(m_paramValuesOut));
  memset(m_paramTypesOut,0,sizeof(m_paramTypesOut));
  memset(m_paramLengthsOut,0,sizeof(m_paramLengthsOut));

  return 0;


int sqlstatement::connect(connection *in_conn)

  // 注意,一个sqlstatement在程序中只能连一个connection,不允许连多个connection
  // 所以,只要这个光标已打开,就不允许再次打开,直接返回成功
  if ( m_state == 1 ) return 0;

  memset(&m_cda,0,sizeof(m_cda));

  m_conn=in_conn;

  // 如果数据库连接类的指针为空,直接返回失败
  if (m_conn == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open.\\n"); return -1;
  

  // 如果数据库没有连接好,直接返回失败
  if (m_conn->m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"database not open.\\n"); return -1;
  

  m_state = 1;  // 光标成功打开

  return 0;


int sqlstatement::prepare(char *fmt,...)

  memset(&m_cda,0,sizeof(m_cda));

  if (m_state == 0)
  
    m_cda.rc=-1; strcpy(m_cda.message,"cursor not open.\\n"); return -1;
  

  memset(m_sql,0,sizeof(m_sql));

  va_list ap;
  va_start(ap,fmt);
  vsnprintf(m_sql,10000,fmt,ap);

  int ilen=strlen(m_sql);

  // PG数据库与Oracle不同,是采用$,不是:
  for (int ii=0;ii<ilen;ii++)
  
    if ( (m_sql[ii]==':') && (isdigit(m_sql[ii+1])!=0) ) m_sql[ii]='$';
  

  PQclear(m_res); m_res=0;

  m_nParamsIn=0;
  // m_paramValuesIn在这里不能被初始化
  memset(m_paramValuesInVal,0以上是关于C/C++19_postgresql.h,_postgresql.cpp的主要内容,如果未能解决你的问题,请参考以下文章

3.3_部分代码

断言行 5120 pos 12 失败:'child = _child' 不正确

断言失败:第 6075 行 pos 12:'child == _child':不正确

Python - 排序

流畅python学习笔记:第十三章:重载运算符__add__,__iadd__,__radd__,__mul__,__rmul__,__neg__,__eq__,__invert__,__pos__(

C ++:终止调用'std :: out_of_range'what():basic_string :: substr:__pos(1)> this->size()(0)