定制 hasirciogli/hdb 二次开发

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

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

hasirciogli/hdb

最新稳定版本:1.0.1

Composer 安装命令:

composer require hasirciogli/hdb

包简介

Database class like laravel database

README 文档

README

README.md for Hasirciogli\Hdb\Database Class

Introduction

This README file provides an overview of the Hasirciogli\Hdb\Database class, a PHP class designed to simplify database interactions, offering a structure similar to Laravel's database layer.

Features

  • Connection Management: Establishes a PDO connection to a MySQL database based on configuration provided through an interface (DatabaseConfigInterface).
  • Query Building: Facilitates the construction of SQL queries using a chainable method approach.
  • Prepared Statements: Employs prepared statements to prevent SQL injection vulnerabilities.
  • Data Binding: Allows for binding of parameters to queries for dynamic data handling.
  • Execution and Retrieval: Executes the built query and retrieves results as associative arrays or a single row.

Installation

1. Composer (Recommended):

If you're using Composer in your project, add the package to your composer.json file:

"require": {
    "hasirciogli/hdb": "^1.0" // Replace with the specific version you want
}

Then, run composer install to download the package.

2. Manual Download:

Download the Hasirciogli\Hdb directory and include it in your project's file structure.

Usage

1. Configuration:

Create a class that implements the DatabaseConfigInterface. This interface defines properties for database credentials and connection details:

interface DatabaseConfigInterface
{
    const DB_HOST = 'localhost';
    const DB_NAME = 'your_database_name';
    const DB_USER = 'your_username';
    const DB_PASS = 'your_password';
}

2. Database Connection:

Instantiate the Database class, passing an instance of your configuration class:

use Hasirciogli\Hdb\Database;
use MyProject\DatabaseConfig; // Replace with your config class path

$db = Database::cfun(new DatabaseConfig());

3. Building Queries:

Use chainable methods to construct your SQL queries:

$users = $db->Select('users')
            ->Where('isActive', true)
            ->OrderBy('username', 'ASC')
            ->Get('all'); // Get all results as an array

4. Prepared Statements and Data Binding:

The class utilizes prepared statements and data binding automatically. You don't need to manually escape values:

$userId = 123;
$user = $db->Select('users')
            ->Where('id', $userId)
            ->Get(); // Get a single user row

5. Additional Methods:

  • Use(string $DbName): Selects a specific database within the connection.
  • LastInsertId(): Retrieves the last inserted ID after an INSERT operation.
  • Insert(string $TableName, $Dataset): Builds an INSERT query with the specified table name and data.
  • CustomSql(string $Sql): Allows for execution of raw SQL queries.

Error Handling

The CheckDB() method throws an exception if a database connection cannot be established. Handle this exception in your code to provide appropriate error messages.

Security Considerations

While the Database class utilizes prepared statements, it's still recommended to validate user input before using it in queries to prevent potential security issues.

Contribution

We welcome contributions to improve this library! Fork the repository on GitHub and submit pull requests with your enhancements.

License

This library is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

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