stule-ru/modernpdo
Composer 安装命令:
composer require stule-ru/modernpdo
包简介
The source code of the simple PDO library.
README 文档
README
This repository contains code of ModernPDO, the library for working with databases using PDO.
The library is guaranteed to support the following databases: MySQL, MariaDB, PostgreSQL, SQLite3 and custom PDO.
📝 Table of Contents
🧐 About
This repository contains code of ModernPDO, a library for working with databases using PDO. There are basic tools for working with databases.
For example, here you can see classes for working with insert, delete, select etc.
You can use the library instead of PDO which makes project development easier.
🏁 Getting Started
Installing
The library uses composer autoloader for including files, so you need to install Composer. If you've never used Composer read the manual. You can install via command line or composer.json.
Using command line
- Run the command
composer require stule-ru/modernpdoin the console.
Using composer.json
- Add a require
"stule-ru/modernpdo": "^3.0.0"to your composer.json.
...
"require": {
...
"stule-ru/modernpdo": "^3.0.0"
...
}
...
- Run the command
composer installin the console.
🎈 Usage
About Structure
NOTE:
If you want to just use the library and not think about the implementation, install and use
Everyone else, welcome to hell =)
About Source Code
The source code contains:
- The general classes (ModernPDO.php, Factory.php, Escaper.php, etc.)
- The specific DBMS classes (they are located in /src/Drivers/*)
This allows append code rather than rewrite (MySQLDriver, PostgreSQLDriver, SQLite3Driver extend ModernPDO and etc.)
So, if you need to, you can extend all general classes to achieve your goals.
The general classes:
- Actions/* - query builders like Select, Update and etc.
- Conditions/* - condition builders for 'select' like Between, In and etc.
- Fields/* - field builders for 'create/update table' like IntField, TextField and etc.
- Functions/* - aggregate and scalar function builders like Count, Upper and etc.
- Keys/* - key builders for 'create/update table' like PrimaryKey, UniqueKey and etc.
- Traits* - traits with shared methods like where(), columns() and etc.
- ModernPDO - base class for working with database (methods exec(), query(), select() and etc.)
- Escaper - base class for escaping values.
- Factory - base class for making new actions, transactions and etc.
- Statement - base class for working with database response.
- Transaction - base class for working with transactions.
The drivers:
- MariaDB - driver for working with MariaDB.
- MySQL - driver for working with MySQL.
- PostgreSQL - driver for working with PostgreSQL.
- SQLite3 - driver for working with SQLite3.
About Tests
There are 2 types of tests: Integration and Unit.
Initialization Examples
use ModernPDO\ModernPDO; use ModernPDO\Drivers\MySQLDriver; use ModernPDO\Drivers\MariaDBDriver; use ModernPDO\Drivers\PostgreSQLDriver; use ModernPDO\Drivers\SQLite3Driver; // Initiolize by PDO $mpdo = new ModernPDO( pdo: $pdo, ); // Initiolize MySQL $mpdo = new MySQLDriver( host: $host, database: $database, username: $username, password: $password, charset: $charset, //port: $port, ); // Initiolize MariaDB $mpdo = new MariaDBDriver( host: $host, database: $database, username: $username, password: $password, charset: $charset, //port: $port, ); // Initiolize PostgreSQL $mpdo = new PostgreSQLDriver( host: $host, database: $database, username: $username, password: $password, //port: $port, ); // Initiolize SQLite3 $mpdo = new SQLite3Driver( mode: $mode, );
Queries Examples
// Row query $mpdo->exec('CREATE TABLE table_name (id int, name varchar(32));'); // Prepared queries $stmt = $mpdo->query("SELECT * FROM table_name", []); // Check query status if ($stmt->status()) { // Get counts $stmt->rowCount(); $stmt->columnCount(); $stmt->fetchColumn($column); // Fetch cell $stmt->fetchObject(); // Fetch row as object $stmt->fetch(); // Fetch row as array $stmt->fetchAll(); // Fetch all rows as array }
CRUD Examples
// // Insert example // // INSERT INTO table (id, name) VALUES (10, 'test'), (11, 'test') $mpdo->insert('table')->columns([ 'id', 'name', ])->values([ [10, 'test'], [11, 'test'], ])->execute(); // INSERT INTO table VALUES (12, 'test') $mpdo->insert('table')->values([ [12, 'test'], ])->execute(); // // Select examples // // SELECT * FROM table $mpdo->select('table')->rows(); // SELECT * FROM table WHERE id=10 LIMIT 1 $mpdo->select('table')->where('id', 10)->row(); // SELECT * FROM table WHERE id=10 AND name='test' LIMIT 1 $mpdo->select('table')->where('id', 10)->and('name', 'test')->row(); // SELECT id, name FROM table $mpdo->select('table')->columns(['id', 'name'])->rows(); // SELECT COUNT(*) FROM table $mpdo->select('table')->columns([new Count()])->cell(); // SELECT SUM(amount) FROM table WHERE id BETWEEN 10 AND 50 $mpdo->select('table')->columns([new Sum('amount')])->where('id', new Between(10, 50))->cell(); // SELECT table.id AS id, table.name AS name, join_table.lastname AS lastname FROM table INNER JOIN join_table ON table.id=join_table.id $mpdo->select('table')->columns([ 'id' => 'table.id', 'name' => 'table.name', 'lastname' => 'join_table.lastname', ])->innerJoin('join_table')->on('table.id', 'join_table.id')->rows(); // SELECT * FROM table ORDER BY id ASC $mpdo->select('table')->orderBy('id')->rows(); // SELECT * FROM table LIMIT 1 OFFSET 10 $mpdo->select('table')->limit(1, 10)->row(); // // Update example // // UPDATE table SET name='Mr. Gorski' WHERE id=10 $mpdo->update('table')->set(['name' => 'Mr. Gorski'])->where('id', 10)->execute(); // // Delete example // // DELETE FROM table WHERE id NOT IN (10, 11, 20) $mpdo->delete('table')->where('id', new NotIn([10, 11, 20]))->execute();
Table Examples
// // Create Table // // CREATE TABLE IF NOT EXISTS table (id INT NOT NULL, email TEXT NOT NULL, name VARCHAR(32) NOT NULL) $mpdo->createTable('table')->checkIfExists()->fields([ new IntField('id'), new TextField('email'), new VarcharField('name', 32), ])->execute(); // CREATE TABLE IF NOT EXISTS table (id INT UNSIGNED NULL DEFAULT 100) $mpdo->createTable('table')->checkIfExists()->fields([ new IntField('id', unsigned: true, canBeNull: true, default: 100), ])->execute(); // CREATE TABLE IF NOT EXISTS table (id INT NOT NULL, PRIMARY KEY (id)) $mpdo->createTable('table')->checkIfExists()->fields([ new IntField('id'), ])->keys([ new PrimaryKey('id'), ])->execute(); // // Update Table // // ALTER TABLE table RENAME TO new_table $mpdo->alterTable('table')->rename('new_table')->execute(); // ALTER TABLE table ADD COLUMN amount INT NOT NULL $mpdo->alterTable('table')->addColumns([ new IntField('amount'), ])->execute(); // ALTER TABLE table RENAME COLUMN column TO new_column $mpdo->alterTable('table')->renameColumns([ 'column' => 'new_column', ])->execute(); // ALTER TABLE table DROP COLUMN column $mpdo->alterTable('table')->dropColumns([ 'column', ])->execute(); // // Drop Table // // DROP TABLE IF EXISTS table $mpdo->dropTable('table')->checkIfExists()->execute();
Transaction Example
$transaction = $mpdo->transaction(); $transaction->begin(); try { if (!$transaction->isActive()) { // Your code... } // Your code... $transaction->commit(); } catch (\Throwable $ex) { $transaction->rollBack(); throw $ex; }
🔧 Running the tests
There are many tools for testing: Integration/Unit tests (PHPUnit), PHPStan, PSalm and CSFixer.
If you want to start them you need to run composer scripts in terminal
NOTE: You can not start integration tests
Levels
- PHPStan: Level 9
- PSalm: Level 1
Composer scripts
Tests
composer tests- runs all PHPUnit tests (do not run it)composer i-tests- runs integration PHPUnit tests (do not run it)composer u-tests- runs unit PHPUnit testscomposer ca-tests- defines all tests coverage (do not run it)composer ica-tests- defines integration tests coverage (do not run it)composer uca-tests- defines unit tests coverage
PHPStan
composer stan- writes errors to phpstan-report.xmlcomposer stan-bl- writes errors to phpstan-baseline.neon
PSalm
composer salm- writes errors to psalm-report.xmlcomposer salm-bl- writes errors to psalm-baseline.xml
Coding standards fixer
composer csfix- fixes coding standards in all PHP files
⛏️ Built Using
Languages
- PHP - version >= 8.1
Utilities
- PHPStan - static analyzer
- PSalm - static analyzer
- PHPUnit - testing framework
- PHP-CS-Fixer - coding standards fixer
✍️ Authors
stule-ru/modernpdo 适用场景与选型建议
stule-ru/modernpdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 442 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「pdo」 「php-mysql」 「modern-php」 「php-database」 「php-mariadb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stule-ru/modernpdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stule-ru/modernpdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stule-ru/modernpdo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Library communication and dealing with databases
VV database abstraction layer with query builder and DB structure models
Alfabank REST API integration
VV DB Driver over PDO extension
An out of Laravel Eloquent 5.1 extended use, and as a provider to different microframeworks.
统计信息
- 总下载量: 442
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-13