forked from opengaussexamples/examples
!22 php connector with openGauss
Merge pull request !22 from woov/master
This commit is contained in:
commit
9bf700fb36
|
|
@ -0,0 +1,156 @@
|
|||
# PHP Connector with OpenGauss
|
||||
|
||||
# 说明
|
||||
|
||||
```jsx
|
||||
@author woov(吴未)<wooovi@qq.com>
|
||||
@version 2.0
|
||||
@since 2022.08.15
|
||||
```
|
||||
|
||||
# 特性
|
||||
|
||||
- 全功能ORM
|
||||
- 每个特性都经过了测试的重重考验
|
||||
- 开发者友好
|
||||
|
||||
# CRUD接口
|
||||
|
||||
## 连接openGauss数据库
|
||||
|
||||
```php
|
||||
$conn = new gauss_class($host,$user,$pass,$dbname,$port);
|
||||
```
|
||||
|
||||
连接是通过创建 gauss 基类的实例而建立的。连接成功返回其他gauss函数需要的资源**conn**,连接失败提示错误并返回false。
|
||||
|
||||
**参数说明**
|
||||
|
||||
[Untitled](https://www.notion.so/3eec90eb2c19413ebdea798814a2812e)
|
||||
|
||||
## 关闭连接
|
||||
|
||||
```php
|
||||
$conn = new gauss_class($host,$user,$pass,$dbname,$port);
|
||||
//在此使用连接
|
||||
|
||||
//运行完成关闭连接
|
||||
$conn = null;
|
||||
```
|
||||
|
||||
连接数据成功后,返回一个 gauss类的实例给脚本,此连接在 gauss 对象的生存周期中保持活动。要想关闭连接,需要销毁对象以确保所有剩余到它的引用都被删除,可以赋一个 **`null`** 值给对象变量。如果不明确地这么做,PHP 在脚本结束时会自动关闭连接。关闭由所给资源$conn指到的Opengauss数据库的非持久连接。
|
||||
|
||||
## 创建
|
||||
|
||||
```php
|
||||
$arr = array('username'=>'test1','pass'=>123456,'real_name'=>'test1');
|
||||
$conn->Create('test',$arr);
|
||||
```
|
||||
|
||||
## 查询
|
||||
|
||||
```php
|
||||
|
||||
$conn->Select('id','user');
|
||||
$conn->Where("username = ?",'test1');
|
||||
$data = $conn->Find('test');
|
||||
```
|
||||
|
||||
```php
|
||||
$conn->Select('*');
|
||||
$conn->Where("username = 'test1'");
|
||||
$data = $conn->First('test');
|
||||
```
|
||||
|
||||
## 更新
|
||||
|
||||
```php
|
||||
$conn->Where("username = 'test'");
|
||||
$arr = array('pass'=>'123');
|
||||
$data = $conn->Update('test',$arr);
|
||||
```
|
||||
|
||||
## 删除
|
||||
|
||||
```php
|
||||
$conn->Where("username = 'test1'");
|
||||
$data = $conn->Delete('test');
|
||||
```
|
||||
|
||||
## 原生SQL
|
||||
|
||||
## query -执行查询
|
||||
|
||||
```php
|
||||
$result = $conn -> query($sql);
|
||||
```
|
||||
|
||||
## fetch_all - 从结果中提取所有行作为一个数组
|
||||
|
||||
```php
|
||||
$data = $conn -> fetch_all($result);
|
||||
```
|
||||
|
||||
**fetch_all()** 从结果资源中返回一个包含有所有的行(元组/记录)的数组。如果没有更多行可供提取,则返回 **`false`**。
|
||||
|
||||
## fetch_assoc -提取一行作为关联数组
|
||||
|
||||
```php
|
||||
$data = $conn -> fetch_assoc($result);
|
||||
```
|
||||
|
||||
**fetch_assoc()** 它只返回一个关联数组。
|
||||
|
||||
## fetch_array -提取一行作为数组
|
||||
|
||||
```php
|
||||
$data = $conn -> fetch_array($result)
|
||||
```
|
||||
|
||||
**fetch_array()**返回一个与所提取的行(元组/记录)相一致的数组。如果没有更多行可供提取,则返回 **`false`**。
|
||||
|
||||
## 其他函数
|
||||
|
||||
## numRows -获取行的数目
|
||||
|
||||
```php
|
||||
$count = $conn -> numRow($result);
|
||||
```
|
||||
|
||||
**numRow()返回result**中的行的数目。其中result参数是由query()函数返回的查询结果资源号。出错则返回-1。
|
||||
|
||||
## affectedRows -获取受影响的记录数目
|
||||
|
||||
```php
|
||||
$count = $conn -> affectedRows($result)
|
||||
```
|
||||
|
||||
**affectesRows()**返回**query()**中执行的INSERT,UPDATE和DELECT查询后受到影响的记录数目(包括实例/记录/行)。如果本函数没有影响到任何记录,则返回0。其中result参数是由query()函数返回的查询结果资源号。
|
||||
|
||||
### connReset -重置连接
|
||||
|
||||
```php
|
||||
$conn->connRest();
|
||||
```
|
||||
|
||||
**connReset()成功返回`ture`**,否则返回`**false()**`(注意:返回类型为bool)。
|
||||
|
||||
## dbname -获取表名
|
||||
|
||||
```php
|
||||
$res = $conn -> dbname();
|
||||
```
|
||||
|
||||
## isbusy -获知连接是否为忙
|
||||
|
||||
```php
|
||||
$isbusy = $conn->isbusy()
|
||||
if ($isbusy){
|
||||
echo 'busy';
|
||||
}
|
||||
else{
|
||||
echo 'free';
|
||||
}
|
||||
```
|
||||
|
||||
当连接忙时,函数返回值为ture(bool),否则为false
|
||||
|
|
@ -0,0 +1,200 @@
|
|||
<?php
|
||||
|
||||
class PhpContion
|
||||
{
|
||||
protected $sql_select = "";
|
||||
protected $sql_where = "";
|
||||
protected $sql_order = "";
|
||||
public $conn = "";
|
||||
|
||||
public function __construct($host, $user, $pass, $dbname, $port = '8888')
|
||||
{
|
||||
$c_host = sprintf("host=%s", $host);
|
||||
$c_port = sprintf("port=%s", $port);
|
||||
$c_dbname = sprintf("dbname=%s", $dbname);
|
||||
$c_credentials = sprintf("user=%s password=%s", $user, $pass);
|
||||
$conn = pg_connect("$c_host $c_port $c_dbname $c_credentials");
|
||||
$this->conn = $conn;
|
||||
if (!$conn) {
|
||||
echo "数据库连接失败";
|
||||
return false;
|
||||
} else {
|
||||
return $conn;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
pg_close($this->conn);
|
||||
}
|
||||
|
||||
function Select(...$args)
|
||||
{
|
||||
$sql = "select ";
|
||||
for ($i = 0; $i < count($args); $i++) {
|
||||
if ($i < count($args) - 1) {
|
||||
$sql = $sql . $args[$i] . ",";
|
||||
continue;
|
||||
}
|
||||
$sql = $sql . $args[$i] . " ";
|
||||
}
|
||||
$this->sql_select = $sql;
|
||||
}
|
||||
|
||||
function Where($query, $value = "")
|
||||
{
|
||||
if (gettype($value) == 'integer') {
|
||||
$sql = str_replace("?", $value, $query);
|
||||
} else {
|
||||
$sql = str_replace("?", "'" . $value . "'", $query);
|
||||
}
|
||||
if ($this->sql_where == "") {
|
||||
$this->sql_where = "where " . $sql;
|
||||
return;
|
||||
}
|
||||
$this->sql_where = $this->sql_where . " and " . $sql;
|
||||
}
|
||||
|
||||
function Find($table)
|
||||
{
|
||||
if ($this->sql_select == '' or $this->sql_where == '') {
|
||||
exit("error:not use function Select or function Where");
|
||||
}
|
||||
$sql = $this->sql_select . "from " . $table . " " . $this->sql_where;
|
||||
if ($this->sql_order != "") {
|
||||
$sql .= $this->sql_order;
|
||||
}
|
||||
$data = $this->getAll($sql);
|
||||
$this->sql_select = '';
|
||||
$this->sql_where = '';
|
||||
return $data;
|
||||
}
|
||||
|
||||
function First($table)
|
||||
{
|
||||
if ($this->sql_select == '' or $this->sql_where == '') {
|
||||
exit("error:not use function Select or function Where");
|
||||
}
|
||||
$sql = $this->sql_select . "from " . $table . " " . $this->sql_where . " limit 1";
|
||||
$data = $this->getOne($sql);
|
||||
$this->sql_select = '';
|
||||
$this->sql_where = '';
|
||||
return $data;
|
||||
}
|
||||
|
||||
function Order($value)
|
||||
{
|
||||
$this->sql_order = " order by " . $value;
|
||||
}
|
||||
|
||||
function Delete($table)
|
||||
{
|
||||
if ($this->sql_where == '') {
|
||||
exit("error:not use function Where");
|
||||
}
|
||||
$sql = 'delete from ' . $table . ' ' . $this->sql_where;
|
||||
$this->sql_where = "";
|
||||
$res = $this->query($sql);
|
||||
if ($res) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function Create($table, $data)
|
||||
{
|
||||
$sql = "INSERT INTO " . $table . " (" . implode(",", array_keys($data)) . ") VALUES ('" . implode("','", $data) . "')";
|
||||
$this->query($sql);
|
||||
}
|
||||
|
||||
function Update($table, $data)
|
||||
{
|
||||
if ($this->sql_where == '') {
|
||||
exit("error:not use function Where");
|
||||
}
|
||||
$sql = "UPDATE " . $table . " SET ";
|
||||
foreach ($data as $key => $value) {
|
||||
$sql .= "{$key} ='{$value}',";
|
||||
}
|
||||
$sql = rtrim($sql, ',');
|
||||
$sql = $sql . $this->sql_where;
|
||||
$this->sql_where = "";
|
||||
$res = $this->query($sql);
|
||||
if ($res) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function getOne($sql)
|
||||
{
|
||||
$result = $this->query($sql);
|
||||
$data = array();
|
||||
if ($result) {
|
||||
$data = pg_fetch_assoc($result);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
function getAll($sql)
|
||||
{
|
||||
|
||||
$result = $this->query($sql);
|
||||
$data = array();
|
||||
if ($result) {
|
||||
try {
|
||||
while ($row = pg_fetch_assoc($result)) {
|
||||
$data[] = $row;
|
||||
}
|
||||
return $data;
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function query($sql)
|
||||
{
|
||||
return pg_query($this->conn, $sql);
|
||||
}
|
||||
|
||||
function numRows($result)
|
||||
{
|
||||
return pg_num_rows($result);
|
||||
}
|
||||
|
||||
function affactedRows($result)
|
||||
{
|
||||
return pg_affected_rows($result);
|
||||
}
|
||||
|
||||
function connReset()
|
||||
{
|
||||
return pg_connection_reset($this->conn);
|
||||
}
|
||||
|
||||
function dbName()
|
||||
{
|
||||
return pg_dbname($this->conn);
|
||||
}
|
||||
|
||||
function fetch_all($result)
|
||||
{
|
||||
return pg_fetch_all($result);
|
||||
}
|
||||
|
||||
function fetch_array($result)
|
||||
{
|
||||
return pg_fetch_array($result);
|
||||
}
|
||||
|
||||
function fetch_assos($result)
|
||||
{
|
||||
return pg_fetch_assoc($result);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue