定制 lehbyos/super-pdo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lehbyos/super-pdo

Composer 安装命令:

composer require lehbyos/super-pdo

包简介

Utility class, created as a wrapper to PDO

README 文档

README

Wrapper class to PDO, with helper methods to common task in CRUD operations

General purpose

This class provides a very thin layer over PDO, giving some helpful methods to manipulate data.

Configuration

SuperPDO has a singleton implementation, and can manage multiple database connections, identifying every one of them with a name.

As a convention, SuperPDO asumes that the main connection is called default, using that name as a default value.

To add a new connection to SuperPDO, you must use the static addConnection() method.

SuperPDO::addConnection(string $name, string $dsn, ?string $username = null, ?string $password = null)
  • $name is the name (alias) of the connection.This name must be unique, and SuperPDO will throw an exception if the name was already added.
  • $dsn is the connection string, as required by PDO.
  • $username and $password are the login data to the database, if required.

The connections have lazy initialization, thus they are created only when required by the connection() method.

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

This is the method to get a connection, with the given name. This call will throw an exception if there is no connection with the given name, or if the connection could not be fulfilled.

Getting data

Multiple rows

It's a very common scenario to perform the next steps to get some data from 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

On every query, you must to repeat this lines, creating a lot of boilerplate code.

With superPDO, all the code above can be rewritten as

//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
}

As you can see, the last code is cleaner and shorter than the first one.

The parameters could also be positional (not named), witch generate even shorter code.

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
}

One data row

SuperPDO can get just one data row.

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
}

In a very restrictive way, SuperPDO can ensure that a query just create ONE row, throwing and exception if the query gives zero or more than one data row.

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
}

Inserting, updating or deleting data

Insert, update or delete data from a database usually requires almost the same steps to be performed.

//$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

The same result can be achieved with superPDO on this way

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.
}

lehbyos/super-pdo 适用场景与选型建议

lehbyos/super-pdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lehbyos/super-pdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lehbyos/super-pdo 我们能提供哪些服务?
定制开发 / 二次开发

基于 lehbyos/super-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-09-12