jstayton/miner
最新稳定版本:v0.10.0
Composer 安装命令:
composer require jstayton/miner
包简介
A dead simple PHP class for building SQL statements. No manual string concatenation necessary.
关键字:
README 文档
README
NOTICE: This project is deprecated and no longer maintained. If you'd like to continue supporting a forked version, please reach out on Twitter (@kidjustino) to have it listed here.
Miner
A dead simple PHP class for building SQL statements. No manual string concatenation necessary.
Developed by Justin Stayton while at Monk Development.
Requirements
- PHP >= 5.1.0
Installation
Composer
The recommended installation method is through
Composer, a dependency manager for PHP. Just add
jstayton/miner to your project's composer.json file:
{
"require": {
"jstayton/miner": "*"
}
}
More details can be found over at Packagist.
Manually
- Copy
src/Miner.phpto your codebase, perhaps to thevendordirectory. - Add the
Minerclass to your autoloader orrequirethe file directly.
Getting Started
Composing SQL with Miner is very similar to writing it by hand, as much of the syntax maps directly to methods:
$Miner = new Miner(); $Miner->select('*') ->from('shows') ->innerJoin('episodes', 'show_id') ->where('shows.network_id', 12) ->orderBy('episodes.aired_on', Miner::ORDER_BY_DESC) ->limit(20);
Now that the statement is built,
$Miner->getStatement();
returns the full SQL string with placeholders (?), and
$Miner->getPlaceholderValues();
returns the array of placeholder values that can then be passed to your database connection or abstraction layer of choice. Or, if you'd prefer it all at once, you can get the SQL string with values already safely quoted:
$Miner->getStatement(false);
If you're using PDO, however, Miner makes executing the statement even easier:
$PDOStatement = $Miner->execute();
Miner works directly with your PDO connection, which can be passed during creation of the Miner object
$Miner = new Miner($PDO);
or after
$Miner->setPdoConnection($PDO);
Usage
SELECT
SELECT * FROM shows INNER JOIN episodes ON shows.show_id = episodes.show_id WHERE shows.network_id = 12 ORDER BY episodes.aired_on DESC LIMIT 20
With Miner:
$Miner->select('*') ->from('shows') ->innerJoin('episodes', 'show_id') ->where('shows.network_id', 12) ->orderBy('episodes.aired_on', Miner::ORDER_BY_DESC) ->limit(20);
INSERT
INSERT HIGH_PRIORITY shows SET network_id = 13, name = 'Freaks & Geeks', air_day = 'Tuesday'
With Miner:
$Miner->insert('shows') ->option('HIGH_PRIORITY') ->set('network_id', 13) ->set('name', 'Freaks & Geeks') ->set('air_day', 'Tuesday');
REPLACE
REPLACE shows SET network_id = 13, name = 'Freaks & Geeks', air_day = 'Monday'
With Miner:
$Miner->replace('shows') ->set('network_id', 13) ->set('name', 'Freaks & Geeks') ->set('air_day', 'Monday');
UPDATE
UPDATE episodes SET aired_on = '2012-06-25' WHERE show_id = 12 OR (name = 'Girlfriends and Boyfriends' AND air_day != 'Monday')
With Miner:
$Miner->update('episodes') ->set('aired_on', '2012-06-25') ->where('show_id', 12) ->openWhere(Miner::LOGICAL_OR) ->where('name', 'Girlfriends and Boyfriends') ->where('air_day', 'Monday', Miner::NOT_EQUALS) ->closeWhere();
DELETE
DELETE FROM shows WHERE show_id IN (12, 15, 20) LIMIT 3
With Miner:
$Miner->delete() ->from('shows') ->whereIn('show_id', array(12, 15, 20)) ->limit(3);
Methods
SELECT
INSERT
REPLACE
UPDATE
DELETE
OPTIONS
SET / VALUES
FROM
- from
- innerJoin
- leftJoin
- rightJoin
- join
- getFrom
- getFromAlias
- getFromString
- getJoinString
- mergeFromInto
- mergeJoinInto
WHERE
- where
- andWhere
- orWhere
- whereIn
- whereNotIn
- whereBetween
- whereNotBetween
- openWhere
- closeWhere
- getWherePlaceholderValues
- getWhereString
- mergeWhereInto
GROUP BY
HAVING
- having
- andHaving
- orHaving
- havingIn
- havingNotIn
- havingBetween
- havingNotBetween
- openHaving
- closeHaving
- getHavingPlaceholderValues
- getHavingString
- mergeHavingInto
ORDER BY
LIMIT
Statement
- execute
- getStatement
- getPlaceholderValues
- isSelect
- isInsert
- isReplace
- isUpdate
- isDelete
- __toString
- mergeInto
Connection
Feedback
Please open an issue to request a feature or submit a bug report. Or even if you just want to provide some feedback, I'd love to hear. I'm also available on Twitter as @kidjustino.
Contributing
- Fork it.
- Create your feature branch (
git checkout -b my-new-feature). - Commit your changes (
git commit -am 'Added some feature'). - Push to the branch (
git push origin my-new-feature). - Create a new Pull Request.
jstayton/miner 适用场景与选型建议
jstayton/miner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.26k 次下载、GitHub Stars 达 252, 最近一次更新时间为 2012 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「mysql」 「sqlite」 「postgresql」 「query」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jstayton/miner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jstayton/miner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jstayton/miner 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
The tenancy/tenancy database driver for sqlite
Query filtering in your frontend
Slimmed down concise interfaces and query builder for database queries and transactions which can be layered / decorated.
Database/ORM-agnostic query construction helper
Mapper for accessing resources in KWCMS
统计信息
- 总下载量: 12.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 253
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-09-17