lane4core/dbconnection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lane4core/dbconnection

最新稳定版本:1.0.3

Composer 安装命令:

composer require lane4core/dbconnection

包简介

DbConnection is a factory to create PDO database connections and delivers a professional PDO connection wrapper.

README 文档

README

A flexible package for creating and managing database connections (PDO-based) with support for MySQL/MariaDB, PostgresSQL, and SQLite.

Overview

This package provides a central factory to create PDO-based connections in a consistent and standardized way. It includes specific connection implementations for common database drivers (MySQL/MariaDB, PostgreSQL, SQLite).

Purpose:

  • Unified management of connection parameters
  • Standardized PDO options
  • Extensible behavior through custom wrappers
  • Improved testability and replaceability

Supported Connection Types

The package includes implementations for the following connection types:

  • MySQL / MariaDBMySql
  • PostgreSQLPostgres
  • SQLiteSqlite

Each connection class handles the appropriate DSN construction and automatically initializes the corresponding PDO connection with sensible default settings.

Installation

Using Composer:

composer require lane4core/dbconnection

For local development:

git clone https://github.com/lane4core/dbconnection.git
cd dbconnection
make install

Configuration

All connection classes expect a configuration array with parameters such as:

[
    'driver'   => 'mysql' | 'pgsql',
    'host'     => 'localhost',
    'port'     => 3306 | 5432,
    'database' => 'mydb',
    'user'     => 'user',
    'password' => 'secret',
    'charset'  => 'utf8mb4',
    'options'  => [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    ],
]

For SQLite:

[
    'driver'   => 'sqlite',
    'database' => ':memory:', // or file path
]

Examples

MySQL / MariaDB

use Lane4Core\DbConnection\Support\MySql;

$config = [
    'driver'   => 'mysql',
    'host'     => '127.0.0.1',
    'port'     => 3306,
    'database' => 'testdb',
    'user'     => 'dbuser',
    'password' => 'dbpass',
    'charset'  => 'utf8mb4',
];

$connection = new MySql($config);

$stmt = $connection->pdo()->query('SELECT * FROM users');
$rows = $stmt->fetchAll();

PostgreSQL

use Lane4Core\DbConnection\Support\Postgres;

$config = [
    'driver'   => 'pgsql',
    'host'     => 'localhost',
    'port'     => 5432,
    'database' => 'testdb',
    'user'     => 'pguser',
    'password' => 'pgpass',
];

$connection = new Postgres($config);

$stmt = $connection->pdo()->query('SELECT * FROM accounts');
$data = $stmt->fetchAll();

SQLite (In-Memory)

use Lane4Core\DbConnection\Support\SqLite;

$config = [
    'driver'   => 'sqlite',
    'database' => ':memory:',
];

$connection = new Sqlite($config);
$pdo = $connection->pdo();

$pdo->exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
$pdo->exec("INSERT INTO test (name) VALUES ('Alice')");
$result = $pdo->query('SELECT * FROM test')->fetchAll();

Extension / Custom Wrappers

If you want to implement custom behaviors such as logging, query profiling, or caching:

  1. Create your own wrapper class that encapsulates or extends PDO.
  2. Use this wrapper in your custom connection class.
  3. Add hooks or events to execute additional actions before/after queries.

Tests

The project includes PHPUnit tests under tests/.

make phpunit

CI & Code Quality

The repository uses or recommends the following tools:

  • PHPStan for static analysis (Level 8)
  • PHPCS for coding standards
  • PHPUnit for unit tests (100% code coverage)
  • GitHub Actions for CI/CD

Typical CI steps:

  1. composer install
  2. make phpcs
  3. make phpstan
  4. make phpunit-coverage

License

This project is licensed under the MIT License – see LICENSE in the repository.

Enjoy and good luck with DbConnection!

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-10