PHPackages                             timetoogo/pinq.demo.sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. timetoogo/pinq.demo.sql

ActiveLibrary[Database &amp; ORM](/categories/database)

timetoogo/pinq.demo.sql
=======================

A documented demo of the PINQ DSL query provider platform

11111[1 issues](https://github.com/TimeToogo/Pinq.Demo.Sql/issues)PHP

Since Aug 1Pushed 10y ago3 watchersCompare

[ Source](https://github.com/TimeToogo/Pinq.Demo.Sql)[ Packagist](https://packagist.org/packages/timetoogo/pinq.demo.sql)[ RSS](/packages/timetoogo-pinqdemosql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Pinq.Demo.Sql
=============

[](#pinqdemosql)

This is a demo of using the query provider functionality of PINQ. This library is based on [PINQ V3](https://github.com/TimeToogo/Pinq)and demonstrates the power of the DSL query provider API. **This is a proof of concept and should not be run in production.**

Overview
========

[](#overview)

This demo written in under 2 KLOC and implements a small section of the PINQ query API mapping it to a MySQL database backend. Tables are treated as collections and rows are represented as associative arrays.

```
use Pinq\Demo\Sql\DB;

$db = new DB($pdo);

$results = $db->table('customers')
    ->where(function ($row) { return $row['age'] orderByAscending(function ($row) { return $row['firstName']; })
    ->thenByAscending(function ($row) { return $row['lastName']; })
    ->take(50)
    ->select(function ($row) {
        return [
            'fullName'    => $row['firstName'] . ' ' . $row['lastName'],
            'address'     => $row['address'],
            'dateOfBirth' => $row['dateOfBirth'],
        ];
    })
    ->asArray()
```

The above query will be converted to a SQL equivalent:

```
SELECT
CONCAT(CONCAT(customers.firstName, ' '), customers.lastName) AS fullName,
customers.address AS address,
customers.dateOfBirth AS dateOfBirth
FROM (
    SELECT * FROM
    (
        SELECT * FROM
        (SELECT * FROM customers) AS customers
        WHERE (customers.age where(function)` = `WHERE `
- `->orderBy(function, direction)`, `->orderByAscending(function)`, `->orderByDescending(function)` = `ORDER BY  ASC|DESC`
- `->unique()` = `DISTINCT`
- `->take(amount)`, `->skip(amount)`, `->slice(skip, take)` = `LIMIT  OFFSET `
- `->select(function)` = `SELECT  AS ...`

**Request (data retrieval) API**

- `->asArray()`, `->getIterator()`, `->getTrueIterator()`, `->asTraversable()`, `->asCollection()` = `SELECT * FROM`

**Operation (data mutation) API**

- `->apply(function)` = `UPDATE  SET  = ...`
- `->clear()` = `DELETE FROM `

**PHP Expression Syntax**

- Binary operators: `+, -, *, /, %, ==, !=, &&, ||, >, >=,
