shiros/luna-sql 问题修复 & 功能扩展

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

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

shiros/luna-sql

Composer 安装命令:

composer require shiros/luna-sql

包简介

Luna Module - SQL

README 文档

README

pipeline status coverage report

# Luna Module - SQL A PHP **SQL Module** designed for managing database access in the **Luna Framework**. **Robust**, **Flexible**, and **Developer-Friendly** - Simplifying SQL operations and data access in your PHP projects.

[[TOC]]

ℹ️ About the Project

This project is developed in PHP 8.2 and is part of the Luna Framework ecosystem.

The Luna SQL Module provides a structured way to handle SQL connections, execute queries, manage transactions, and build repositories for data access.

Detailed documentation is available in the Wiki: Luna Wiki.

Key Features

  • Connection and Execution: Manage connections and execute SQL queries through a dedicated manager.
  • Repository Pattern: Build repositories to encapsulate data access.
  • Transactions: Orchestrate transactional workflows for safe database operations.
  • Dependency Injection: Seamless integration with Luna’s DI for service wiring.
  • PSR-4 Autoloading: Clean and autoloaded code structure.
  • PHP 8.2 Features: Implements the latest PHP features, promoting clean, predictable code.

🔧 Dependencies

It uses PHP 8.2+, ensuring compatibility with modern features.

This module depends on the following:

Refer to the composer.json file for additional details.

⚙️ Setup and Installation

To use the Luna SQL module, follow the steps below:

Step 1: Install via Composer

Ensure Composer is installed, then execute the following in your root project folder:

composer require shiros/luna-sql

Autoloading is handled by Composer (PSR‑4). When used within a Luna application, the module is auto‑discovered via composer.jsonluna.module:

{
    "luna": {
        "module": "Luna\\SQL\\Module"
    }
}

Step 2: Autoload the Module

The module supports PSR-4 autoloading. If you're using the Luna Framework, it’s automatically available via the Luna module declaration. Otherwise, make sure to include Composer’s autoloader:

require 'vendor/autoload.php';

🚀 Usage Example

Refer to the official documentation for advanced examples and further details.

Configure a connection

Configure a connection to your database in the file config/storage/sql.
Here's a quick example of how to configure the SQL connection:

return [
    'Databases' => [
        'default' => [
            'driver'   => 'mysql',
            'host'     => 'localhost',
            'port'     => 3306,
            'database' => 'my_database',
            'username' => 'my_username',
            'password' => '*my_password*',
        ],
        
        // You can add as many connections as you want.
    ]
];

Execute a query

Here's a quick example of how to use the SQL manager to execute a query:

// Generate the SQL manager instance
$sqlManager = new SqlManager();

// Execute the query
$results = $sqlManager->execute(
    connection: 'default', // a configured connection name or a connection instance
    query     : 'SELECT * FROM users WHERE id = :id', 
    variables : ['id' => 42]
); 

// Output: The '$result' contains driver-specific data or a normalized result depending on your configuration.

Use a repository

Create a repository to encapsulate queries and reuse them across your application:

class UserRepository extends AbstractSQLRepository {
    /**
     * Find a user by its ID.
     * 
     * @param int $id
     * @return array|null
     */
    public function findById(int $id) : ?array {
        return $this->execute(
            connection: 'default', 
            query     : 'SELECT * FROM users WHERE id = :id',
            variables : ['id' => $id]
        );
    }
}

Note: Services and repositories can be wired through the DI configuration (see your config/services and DI module configuration).

📄 Testing

This project uses PHPUnit for testing, you can run the test suite as follows.

Step 1: Install development dependencies

Before running the test suite, ensure all project dependencies, including development dependencies, are installed. Use Composer to handle this:

composer install

This command will fetch all the required libraries and ensure your project setup is complete.

Step 2 (optional): Start local databases for real-connection tests

Most of the suite runs without any external service. A few tests in tests/Connection open a real connection against MySQL and PostgreSQL to validate SQLConnection end-to-end; when no database is reachable, these tests are skipped rather than failed.

See the Docker section below for the containerized flow that runs these tests against real databases.

Step 3: Execute the Test Suite

Once dependencies are installed, you can execute the test suite using PHPUnit.
This ensures all the functionality of the framework is working as expected:

vendor/bin/phpunit --configuration phpunit.xml --colors=always

The test results will be displayed in your console. Colored output simplifies understandièng the testing status:

  • Green: Tests passed successfully.
  • Red: Tests failed.
  • Yellow: Warnings or skipped tests.

For more details on the tests, explore the /tests directory. It contains comprehensive unit tests covering various parts of the framework.

🐳 Docker

A Docker Compose setup is available under .docker/.

None of the services publish ports to the host; everything runs on Compose's internal network, so the suite is run through the php service.

Build the image

docker compose -f .docker/compose.yml build php

Run the test suite

docker compose -f .docker/compose.yml run --rm php vendor/bin/phpunit --configuration phpunit.xml --colors=always

The run automatically starts mysql/postgres as dependencies of php, so the real-connection tests in tests/Connection run against actual databases instead of being skipped. The php container reaches them over their service aliases, using the same SQL_TEST_* variable names GitLab CI injects (see .gitlab-ci.yml):

VariableValue (docker-compose network)
SQL_TEST_MYSQL_HOSTmysql
SQL_TEST_MYSQL_PORT3306
SQL_TEST_MYSQL_DATABASEluna_test
SQL_TEST_MYSQL_USERluna
SQL_TEST_MYSQL_PASSWORDsecret
SQL_TEST_POSTGRES_HOSTpostgres
SQL_TEST_POSTGRES_PORT5432
SQL_TEST_POSTGRES_DATABASEluna_test
SQL_TEST_POSTGRES_USERluna
SQL_TEST_POSTGRES_PASSWORDsecret

Update dependencies

The vendor/ is baked into the image at build time. If you change composer.json/composer.lock, either rebuild the image or run Composer inside the running dependency graph:

docker compose -f .docker/compose.yml run --rm php composer install

Stop the services

docker compose -f .docker/compose.yml down

📃 License

This project is licensed under the MIT License, allowing you to use and modify this project freely.
See the LICENSE file for more details.

👨‍💻 Authors and Contributors

This project was created and is maintained by Alexandre Caillot (Shiroe_sama), with contributions from the community.

Authors

Contributors

We thank the following contributors for making this project better:

shiros/luna-sql 适用场景与选型建议

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

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

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

围绕 shiros/luna-sql 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-09