2017 © Pedro Peláez
 

library yesql-php

A clone of the wonderful yesql clojure library, for php

image

nulpunkt/yesql-php

A clone of the wonderful yesql clojure library, for php

  • Monday, July 2, 2018
  • by nulpunkt
  • Repository
  • 6 Watchers
  • 19 Stars
  • 187 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 8 Versions
  • 70 % Grown

The README.md

yesql-php

This is a clone of the wonderful yesql library from clojure. The idea is to have a seperate sql file for queries, which you can then access as methods on a class., (*1)

I built for fun on a Friday, liked it a lot and now we are using it where I work. So I guess it's production ready., (*2)

Installation

Use composer to require:, (*3)

"nulpunkt/yesql-php": "^1"

Examples!

You need to make a repository of queries:, (*4)

$pdo = new PDO($host, $user, $pass); // Fill in the blanks
$r = new Nulpunkt\Yesql\Repository($pdo, "my-queries/queries.sql");

Fetching many rows

in queries.sql we can put:, (*5)

-- name: getAllRows
-- This will fetch all rows from test_table
select * from test_table;

which will allow us to call, (*6)

$r->getAllRows();

Inserting a row

A database without rows is not of much use, lets insert some data:, (*7)

-- name: insertRow(thing)
insert into test_table (something) values (:thing)

// returns the insertId
$r->insertRow('a thing');



As default, yesql will simply bind all params passed to the called function, to the query associated with it. We'll see how to make mappers further down. ## Updating a row Maybe we need to fix some exsisting data ```sql -- name: updateRow(id, thing) update test_table set something = :thing where id = :id
```php // returns the number of rows touched by the update $r->updateRow(3, 'fixed thing');

## Fetching a single row yesql-php support different modlines, lets say we know we only need to get one row: ```sql -- name: getById(id) oneOrMany: one select * from test_table where id = :id;
```php // Fetches one row with id 3 $r->getById(3);

## Fetching and mapping rows in one go Maybe we want to return a modified version of the row. By specifying a rowFunc, we can have a function called, on every row returned: ```sql -- name: getMappedById(id) oneOrMany: one rowFunc: MyObject::mapRow select * from test_table where id = :id
```php class MyObject { public static function mapRow($r) { return ['id' => $r['id'], 'ohwow' => $r['something']]; } } // return one row, with keys id and ohwow $r->getMappedById(3);

## Mapping rows to objects Sometimes an object is want you want, rowClass got your back: ```sql -- name: getObjectById(id) oneOrMany: one rowClass: MyObject select * from test_table where id = :id
```php class MyObject { } // return one row, which is an instance of MyObject with id and something set $r->getObjectById(3);

## Mapping data on the way in We may need to map our domain objects to be able to insert them into the database. ```sql -- name: insertObject inFunc: MyObject::toRow insert into test_table (id, something) values (:id, :something)
```php class MyObject { // $i will be the arguments passed to insertObject public static function toRow($i, $o) { return ['id' => $i, 'something' => $o->something]; } } $o = new MyObject; $r->insertObject($i, $o)

The Versions

02/07 2018

dev-master

9999999-dev

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

02/07 2018

dev-readme-titles

dev-readme-titles

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

14/07 2016

v1.3.2

1.3.2.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

15/04 2016

v1.3.1

1.3.1.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

31/03 2016

v1.3.0

1.3.0.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

22/03 2016

v1.2.0

1.2.0.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

18/03 2016

v1.1.0

1.1.0.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires

18/03 2016

v1.0.0

1.0.0.0

A clone of the wonderful yesql clojure library, for php

  Sources   Download

MIT

The Development Requires