forked from https://github.com/jstayton/Miner
A dead simple PHP class for building SQL statements. No manual string concatenation necessary.
Developed by Justin Stayton while at Monk Development.
- PHP > 7.0.0
The recommended installation method is through Composer, a dependency manager for PHP.
In command line:
composer require phpcom-lab/miner
Or, just add phpcom-lab/miner
to your project's composer.json
file:
{
"require": {
"phpcom-lab/miner": "*"
}
}
More details can be found over at Packagist.
Composing SQL with Miner is very similar to writing it by hand, as much of the syntax maps directly to methods:
$miner = Miner::create()
->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->getSql(); // Or $miner->getStatement();
returns the full SQL string with placeholders (?), and
$miner->getBoundedParams(); // Or $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->getSql(false); // Or $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 = Miner::create($PDO)
$miner = new Miner($PDO);
or after
$miner->setPdoConnection($PDO);
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 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 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 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
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);
- from
- innerJoin
- leftJoin
- rightJoin
- join
- getFrom
- getFromAlias
- getFromString
- getJoinString
- mergeFromInto
- mergeJoinInto
- where
- andWhere
- orWhere
- whereIn
- whereNotIn
- whereBetween
- whereNotBetween
- openWhere
- closeWhere
- getWherePlaceholderValues
- getWhereString
- mergeWhereInto
- having
- andHaving
- orHaving
- havingIn
- havingNotIn
- havingBetween
- havingNotBetween
- openHaving
- closeHaving
- getHavingPlaceholderValues
- getHavingString
- mergeHavingInto
- execute
- getSql
- getStatement
- getBoundedParams
- getPlaceholderValues
- isSelect
- isInsert
- isReplace
- isUpdate
- isDelete
- __toString
- mergeInto
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 @jstayton.
- 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.