roi/sql-clause-builder 问题修复 & 功能扩展

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

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

roi/sql-clause-builder

Composer 安装命令:

composer require roi/sql-clause-builder

包简介

Build Sql clause from php arrays

README 文档

README

Build SQL clause from an array.

Installation

To add the package to your dependencies, you should have composer installed on your machine. Run the command below in your project directory

composer require roi/sql-clause-builder

Namespace

You can access the class within this namespace \Roi\SqlClauseBuilder

Usage

In order to use the package you must include the auto-generated autoload.php file by composer usually in the location {project-root}/vendor at the top of your script.

If you are using a PHP framework like Laravel, Slim etc, you can skip the above because it would be automatically added.

SqlClauseBuilder requires you to first define an associative array of parameters that is acceptable. See sample below

$searchable_params = [
    'name'=>[
        'col'=> 'merchants.name', 
        'factors'=>[
            'like',
            'equalto'
        ], 
        'bind_as'=>PDO::PARAM_STR
    ],
    'type'=>[
        'col'=> 'merchants.type', 
        'factors'=>[
            'equalto'
        ], 
        'bind_as'=>PDO::PARAM_STR
    ],
] ;

Notice that each entry in the array above is an associative array with 3 defined keys, which are

  • col, a string which implies to the corresponding column in the table
  • factors, an array of SQL operations that can be performed on the specified column
  • bind_as, the explicit data type for the parameter. See https://www.php.net/manual/en/pdo.constants.php for a list options.

Please note that the keys are required.

Next, create an instance of SqlClauseBuilder with the defined searchable columns passed as the argument.

Note that the $searchable_params is the only argument required by the constructor

$clausebuilder = new \Roi\SqlClauseBuilder\SqlClauseBuilder($searchable_params) ;

Then you could easily fit this in a PDO prepared statement, binding the parameters ;

Next call the build method, passing the array you want built into a SQL clause

$data = ['name'=>['data'=>['Bob%'],'factor'=>'like'] ] ;
$clause = $clausebuilder->build($data) ; // returns ['clause'=> 'merchants.name LIKE ?', 'binds'=>[ ['Bob%',PDO::PARAM_STR] ] ]

The build method returns an array with structure like below

[
    'clause'=> 'merchants.name LIKE ?', 
    'binds'=>[ 
        [
            'Bob%',PDO::PARAM_STR
        ] 
    ] 
]

You can use the result in your query as demonstrated below

$query = $Pdo->prepare("SELECT * FROM table WHERE $clause['clause'] ") ;
foreach($clause['binds'] as $key=>$val){
  $query->bindParam($val[0], $val[1]) ;
}
$query->execute() ;

WHAT IS COVERED

The following operators are supported

  • like,
  • equalto
  • notequalto
  • greaterthan
  • lesserthan
  • greaterthanorequalto
  • lesserthanorequalto
  • between
  • notbetween
  • isnull
  • notisnull

TODO

  • add support for more operators
  • provide a method that allows users add operators

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-05-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固