定制 eufony/dbal 二次开发

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

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

eufony/dbal

Composer 安装命令:

composer require eufony/dbal

包简介

Zero-configuration extensible PHP database abstraction layer library.

README 文档

README

Packagist Downloads GitHub Stars Issues
License Community Built

eufony/dbal provides an abstraction layer over SQL to ease development using relational database management systems and to prevent lock-in to a specific SQL flavor.

eufony/dbal is a PHP library that handles interfacing with the relational database of your choice. It uses an expressive syntax that is then translated on-the-fly to the appropriate syntax for your SQL flavor. Using it, you'll enjoy all the benefits of plain SQL, plus:

  • PSR-3 compatible logging of database events.
  • Transparent caching of query results using any PSR-6 or PSR-16 compliant caching implementation.
  • Easy protection against SQL injection attacks using prepared statements.
  • No-fuss serialization / unserialization of any valid PHP type.

Interested? Here's how to get started.

FAQ

Why not write my own SQL queries directly?

The problem with plain old SQL is that "SQL" unfortunately does not refer to a single thing. Instead, it comes in different "flavors", each of which have slight variations on their syntax and supported features. Nobody writes "SQL" queries. You always have to target a specific flavor.

"Okay, what if I only use SQL functions and syntax that are part of a universal SQL standard?"

While there is such a thing as an SQL "standard" adopted by both ANSI and ISO, you'll quickly face two problems when trying to write queries that comply with it:

  1. Some very basic functionality, such as that of the MySQL LIMIT and OFFSET keywords, are inexplicably missing from the standard instruction set. In such situations, you're forced to either rewrite your query in hacky ways that try to accomplish the same result, or give up and only target a specific flavor, which might (will) cause portability problems later on.

  2. Different SQL flavors comply with the standard to a wildly varying degree. While PostgreSQL and SQLite are mostly compliant (at least, with the core standard, disregarding the various extensions), you'll still come across small differences that break things in very subtle ways that might otherwise go unnoticed. Trying to take these into account will add significant overhead to development and will generally make it unmotivating to work on the backend infrastructure.

eufony/dbal takes care of these problem for you. As an abstraction layer, you, as a developer, don't have to worry at all about the syntax that comes out the other end. If you want to migrate flavors, you only need to switch to any of the ready-made driver implementations; or, if one doesn't exist, contribute and make your own. Additionally, you'll get to enjoy some creature comforts when interacting with your database from PHP; such as transparent logging, caching, and conversion between PHP and SQL data types.

Why not use a more well-established, mature project?

Because trusting your critical infrastructure to untested, unfunded, emerging projects is fun and exciting!

On a more serious note, eufony/dbal was started as a sister project to eufony/orm, an Object Relational Mapping library that aims to rethink the disadvantages of relational database models with an inventive pragmatic approach. It was concluded during early development that supporting multiple SQL flavors was impractical without an abstraction layer on top of SQL.

As such, the project was split into two, with eufony/orm providing the "top" and eufony/dbal providing the "bottom" halves, in much the same way as other popular projects such as the Doctrine ORM. Unlike the Doctrine project, however, eufony/dbal also tries to create an expressive syntax for using its query builders, as they are something that the end-user (you) can reasonably be expected to interact with.

Getting started

Installation

eufony/dbal is released as a Packagist package and can be easily installed via Composer with:

composer require "eufony/dbal:v1.x-dev"

Warning: This package does not have any stable releases yet (not even a v0.x pre-release) and is currently unstable. Expect frequent breaking changes and instability!

Basic Usage

For a more detailed documentation, see here.

eufony/dbal is a "zero-configuration" library, making it blazingly fast to get started. Just define a new database connection and (optionally) give it a name, like so:

$driver = /* ... */;
$database = new Connection($driver, key: "default");

The driver can be any implementation of the driver interface. Out of the box, eufony/dbal supports PostgreSQL, MySQL, and SQLite:

// PostgreSQL
$postgres = new PostgreSQLDriver($dsn, $user, $password);

// MySQL
$mysql = new MySQLDriver($dsn, $user, $password);

// SQLite
$sqlite = new SQLiteDriver($path); // $path can also be `:memory:` for an ephemeral database

All three drivers use the PHP PDO extension under the hood.

Once your connection is activated, you can immediately start building and sending queries to the database:

// Define the query
$query = Select::from("users");

// You can also extend the query using loops, conditional logic, etc.
if ($fetch_ids_only) {
    $query = $query->fields(["id"]);
}

// Generate the query string and send it to the database for execution
$users = $query->execute();

You can find a list of example queries here.

Contributing

Found a bug or a missing feature? Report it over at the issue tracker.

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://www.gnu.org/licenses/.

eufony/dbal 适用场景与选型建议

eufony/dbal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dbal」 「sql」 「eufony」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 eufony/dbal 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2022-07-21