Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morris/dop
An immutable API on top of PDO to compose and execute SQL statements
https://github.com/morris/dop
immutable pdo php sql
Last synced: 22 days ago
JSON representation
An immutable API on top of PDO to compose and execute SQL statements
- Host: GitHub
- URL: https://github.com/morris/dop
- Owner: morris
- License: mit
- Created: 2016-09-22T14:35:02.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-09T16:27:29.000Z (over 4 years ago)
- Last Synced: 2024-10-11T12:57:42.311Z (about 1 month ago)
- Topics: immutable, pdo, php, sql
- Language: PHP
- Homepage:
- Size: 84 KB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# DOP
[![Build Status](https://travis-ci.org/morris/dop.svg?branch=master)](https://travis-ci.org/morris/dop)
[![Test Coverage](https://codeclimate.com/github/morris/dop/badges/coverage.svg)](https://codeclimate.com/github/morris/dop/coverage)DOP is an immutable API on top of [PDO](http://php.net/manual/en/book.pdo.php)
to compose and execute SQL statements.- Extended parameters (`::param` and `??`) allow binding to arbitrary values like arrays, `null` and SQL fragments.
- Provides helpers for writing common queries, e.g. selects, inserts, updates, deletes.
- Tested with **SQLite, PostgreSQL, and MySQL.**## Installation
DOP requires PHP >= 5.3.0 and PDO.
Install via [composer](https://getcomposer.org/):```sh
composer require morris/dop
```## Usage
```php
// Connect to a database
$pdo = new PDO('sqlite:blog.sqlite3');
$dop = new Dop\Connection($pdo);// Find posts by author IDs using DOP parametrization
$authorIds = [1, 2, 3];
$orderByTitle = $dop('ORDER BY title ASC');
$posts = $dop(
'SELECT * FROM post WHERE author_id IN (??) ??',
[$authorIds, $orderByTitle]
)->fetchAll();// Find published posts using DOP helpers for common queries
$posts = $dop->query('post')->where('is_published = ?', [1])->fetchAll();// Get categorizations of posts using DOP's map function
$categorizations = $dop(
'SELECT * FROM categorization WHERE post_id IN (??)',
[$dop->map($posts, 'id')]
)->fetchAll();// Find posts with more than 3 categorizations using a sub-query as a parameter
$catCount = $dop('SELECT COUNT(*) FROM categorization WHERE post_id = post.id');
$posts = $dop(
'SELECT * FROM post WHERE (::catCount) >= 3',
['catCount' => $catCount]
)->fetchAll();
```Internally, `??` and `::named` parameters are resolved before statement preparation.
Note that due to the current implementation using regular expressions,
you should *never* use quoted strings directly. Always use bound parameters.## Reference
See [API.md](API.md) for a complete API reference.
## Contributors
- [jayaddison](https://github.com/jayaddison)
Thanks!