lehbyos/super-pdo

工具类,作为PDO的包装器创建

dev-master 2024-09-20 10:51 UTC

This package is auto-updated.

Last update: 2024-09-20 10:52:36 UTC


README

作为PDO的包装器类,提供CRUD操作中的常用任务的辅助方法

通用

该类在PDO之上提供了一个非常薄的层,提供了一些有助于操作数据的方法。

配置

SuperPDO采用单例实现,可以管理多个数据库连接,每个连接都用一个名称来标识。

按照惯例,SuperPDO假定主要连接名为default,使用该名称作为默认值。

要将新连接添加到SuperPDO,必须使用静态addConnection()方法。

SuperPDO::addConnection(string $name, string $dsn, ?string $username = null, ?string $password = null)
  • $name是连接的名称(别名)。此名称必须是唯一的,如果该名称已被添加,SuperPDO将抛出异常。
  • $dsn是连接字符串,如PDO要求。
  • $username$password是数据库登录信息(如果需要)。

连接具有延迟初始化,因此它们仅在连接()方法需要时才创建。

SuperPDO::connection(string $name = "default"): SuperPDO

这是获取具有给定名称的连接的方法。如果不存在具有给定名称的连接,或者无法满足连接,此调用将抛出异常。

获取数据

多行

执行以下步骤从PDO获取数据的场景非常常见

//$conn is a PDO instance.
$stmt = $conn->prepare('select * from some_table where some_field = :value and other_field = :other_value');
$stmt->bindValue(':value' => $value1);
$stmt->bindValue(':other_value' => $value2);
if (!$stmt->execute()){
  //handle the error, free resources  
}
$data = array();
while($obj = $stmt->fetchObject()){
  $resp[] = $obj;
}
$stmt->closeCursor();

//$data has all the data

在每次查询中,都必须重复这些行,从而产生大量的样板代码。

使用superPDO,上述所有代码都可以重写为

//Asumming that SuperPDO only have a connection configured with the name "default"
try{
  $data = SuperPDO::connection()->selectQuery(
    'select * from some_table where some_field = :value and other_field = :other_value',
    [':value' => $value1, ':other_value' => $value2] 
  );
}
catch(Exception $e){
   //handle error; an exception will be throw if the SQL has error or if the execution of the statement fails
}

如您所见,最后的代码比第一个代码更干净、更短。

参数也可以是位置性的(未命名),这将生成更短的代码。

try{
  $data = SuperPDO::connection()->selectQuery(
    'select * from some_table where some_field = ? and other_field = ?',
    [$value1, $value2] 
  );
}
catch(Exception $e){
   //handle error; an exception will be throw if the SQL has error or if the execution of the statement fails
}

一行数据

SuperPDO可以获取一行数据。

try{
  $usr = SuperPDO::connection()->singleRowQuery('select * from users where user_id = ?', [$id]);
}
catch(Exception $e){
   //handle error; an exception will be throw if the SQL has error or if the execution of the statement fails
}

以非常严格的方式,SuperPDO可以确保查询只创建一行,如果查询返回零行或多于一行数据,则抛出异常。

try{
  $usr = SuperPDO::connection()->uniqueRowQuery('select * from users where user_creation_date = ?', [$date]);
}
catch(Exception $e){
   //handle error; an exception will be throw if the SQL has error or if the execution of the statement fails
}

插入、更新或删除数据

从数据库插入、更新或删除数据通常需要执行几乎相同的步骤。

//$conn is a PDO instance
$stmt = $conn->prepare('insert into my_table values(:field1, :field2, :field3, :field4, :field5');
$stmt->bindValue(':field1' => $value1);
$stmt->bindValue(':field2' => $value2);
$stmt->bindValue(':field3' => $value3);
$stmt->bindValue(':field4' => $value4);
$stmt->bindValue(':field5' => $value5);

$rows = $stmt->execute();
if ($rows === false){
  //error executing SQL; handle this
}
if ($rows !== 1){
  //usually, PDO return the number of affected rows; 1 insert means that 1 rows should be affected.
  //if that is not the case, then an error has occurred; handle it.
}

//the rest of your code

使用superPDO可以通过这种方式达到相同的结果

try{
  $rows = SuperPDO::connection()->executeStatement(
    'insert into my_table values(:field1, :field2, :field3, :field4, :field5',
    [':field1' => $value1, ':field2' => $value2, ':field3' => $value3, ':field4' => $value4, ':field5' => $value5]
  );
  if ($rows !== 1){
    //Rows affected doesn't match with the current operation
  }
}
catch(Exception $e){
  //Execution error of the SQL statement; handle it.
}