定制 ride/lib-database 二次开发

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

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

ride/lib-database

Composer 安装命令:

composer require ride/lib-database

包简介

Ride library for database abstraction.

README 文档

README

Database abstraction library of the PHP Ride framework.

Code Sample

Check this code sample to see some of the possibilities of this library:

<?php

use ride\library\database\driver\Driver;
use ride\library\database\exception\DatabaseException;
use ride\library\database\manipulation\condition\SimpleCondition;
use ride\library\database\manipulation\expression\FieldExpression;
use ride\library\database\manipulation\expression\ScalarExpression;
use ride\library\database\manipulation\expression\TableExpression;
use ride\library\database\manipulation\statement\SelectStatement;
use ride\library\database\DatabaseManager;
use ride\library\database\Dsn;
use ride\library\log\Log;

function createDatabaseManager(Log $log) {
    $databaseManager = new DatabaseManager();
    $databaseManager->setLog($log);
    $databaseManager->registerDriver('mysql', 'ride\\library\\database\\driver\\PdoDriver');
    $databaseManager->registerDriver('sqlite', 'ride\\library\\database\\driver\\PdoDriver');
    $databaseManager->registerDriver('postgres', 'ride\\library\\database\\driver\\PostgresPdoDriver');
    
    $databaseManager->registerConnection('my-database', new Dsn('mysql://user:pass@host/database'));
    $databaseManager->registerConnection('my-2nd-database', new Dsn('sqlite:///path/to/file'));
    
    return $databaseManager;
}

function getConnection(DatabaseManager $databaseManager) {
    $connections = $databaseManager->getConnections();
    
    // get the default connection
    $connection = $databaseManager->getConnection();
    
    if ($databaseManager->hasConnection('my-database')) {
        // get my connection
        $connection = $databaseManager->getConnection('my-database');
        
        // get my connection but don't connect just yet
        $connection = $databaseManager->getConnection('my-database', false);
    }
    
    if (!$connection->isConnected()) {
        $connection->connect();
    }
    
    return $connection;
}

function executeSelectSql(Driver $connection) {
    $sql = 
        'SELECT ' . $connection->quoteIdentifier('id') . ' ' . 
        'FROM ' . $connection->quoteIdentifier('MyTable') . ' ' .
        'WHERE ' . $connection->quoteIdentifier('name') . ' LIKE ' . $connection->quoteValue('%Ride%');
        
    $result = $connection->execute($sql);
    
    // get the columns or the column count
    $columns = $result->getColumns();
    $columnCount = $result->getColumnCount();

    // same for rows
    $rows = $result->getRows(); 
    $rowCount = $result->getRowCount();

    // get the first or the last row
    $firstRow = $result->getFirst();
    $lastRow = $result->getLast();

    // you can loop the result straight
    foreach ($result as $row) {
        echo $row['id'];
    }
}
    
function executeInsertSql(Driver $connection) {
    try {
        $connection->beginTransaction();
        
        $sql = 
            'INSERT INTO ' . $connection->quoteIdentifier('MyTable') . ' ' .
            'VALUES (' . $connection->quoteValue('My name') . ')';
            
        $result = $connection->execute($sql);
        
        if ($connection->isTransactionStarted()) {
            $connection->commitTransaction();
        } 
    
        $connection->getLastInsertId();
    } catch (DatabaseException $exception) {
        $connection->rollback();
        
        throw $exception;
    }
}

function executeStatement(Driver $connection) {
    $statement = new SelectStatement(new FieldExpression('id'));
    $statement->addField($field);
    $statement->addTable(new TableExpression('MyTable'));
    $statement->addCondition(new SimpleCondition(new FieldExpression('name'), new ScalarExpression('%Ride%'), '='));
    
    $result = $connection->executeStatement($statement);
}

Related Modules

Installation

You can use Composer to install this library.

composer require ride/lib-database

Convert utf8 to utf8mb4

Show character set and collation variables:

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

Set character set and collate on database:

ALTER DATABASE db CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

Set character set and collate on table:

ALTER TABLE table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Generate ALTER TABLE queries to set character set and collate:

SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db';

Converting the data in the columns is not required, since utf8mb4 is backwards compatible with utf8.

ride/lib-database 适用场景与选型建议

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

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

围绕 ride/lib-database 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-21