<?php
/**
* @deprecated thinkphp pgSql 数据库链接 执行增删改查 需要安装pdo 与pdo_pgsql 扩展
* @datetime 2022-01-25 12:00
* Class PGSqlService
* @author 飞,只是一种状态
*/
class PGSqlService
{
protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库
protected $_dbType = 'pgsql';
protected $_pconnect = true; //是否使用长连接
protected $_host = 'localhost';
protected $_port = 3306;
protected $_user = 'root';
protected $_pass = 'root';
protected $_dbName = 'pg_db'; //数据库名
protected $_sql = false; //最后一条sql语句
protected $_where = '';
protected $_order = '';
protected $_limit = '';
protected $_field = '*';
protected $_clear = 0; //状态,0表示查询条件干净,1表示查询条件污染
protected $_trans = 0; //事务指令数
protected $_dev = '';
/**
* @deprecated 初始化类
* @param array $conf 数据库配置
* PGSqlService constructor.
*/
public function __construct($conf = array()) {
ini_set('memory_limit', '-1');
class_exists('PDO') or die('PDO: 此类不存在');
if(!empty($conf))
{
$this->_dbType = $conf['type'];
$this->_host = $conf['hostname'];
$this->_port = $conf['hostport'];
$this->_user = $conf['username'];
$this->_pass = $conf['password'];
$this->_dbName = $conf['database'];
$this->_dev = $conf['pg_dev'];
}
if ( is_null(self::$_dbh) ) { $this->_connect(); }
}
/**
* @deprecated 连接数据库的方法 整合
*/
protected function _connect() {
$dsn = $this->_dbType.':host='.$this->_host.';port='.$this->_port.';dbname='.$this->_dbName;
$options = $this->_pconnect ? array(\PDO::ATTR_PERSISTENT=>true) : array();
try {
$dbh = new \PDO($dsn, $this->_user, $this->_pass, $options);
$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //设置如果sql语句执行错误则抛出异常,事务会自动回滚
$dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的仿真效果(防SQL注入)
} catch (\PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
$dbh->exec("SET CLIENT_ENCODING='UTF-8';");
self::$_dbh = $dbh;
}
/**
* @deprecated 字段和表名添加 `符号
* 保证指令中使用关键字不出错 针对mysql
* @param string $value
* @return string
*/
protected function _addChar($value) {
if ('*'==$value ||
thinkphp 连接pgsql 类执行增删改查 【源码】前提安装 pdo 与pdo_pgsql依赖
于 2022-01-25 12:03:20 首次发布