Skip to content

Commit

Permalink
Merge branch 'developer'
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Aug 26, 2016
2 parents 4aab7bf + 018aac9 commit 7c85e03
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 39 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
113 changes: 84 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ Hydrahon is a query builder, and only a query builder. It does not contain a PDO
[![Packagist](https://img.shields.io/packagist/l/clancats/hydrahon.svg)]()
[![GitHub release](https://img.shields.io/github/release/clancats/hydrahon.svg)](https://github.com/ClanCats/Hydrahon/releases)

## Status

**This library is still in work.**

- [x] SQL query structure
- [x] SQL select query builder
- [x] Mysql select query translator
- [x] SQL insert query builder and translator
- [x] SQL update query builder and translator
- [x] SQL delete query builder and translator
- [ ] Port more selection result helpers
- [ ] Clean up translation unit tests.

## Installation

Expand All @@ -30,9 +18,9 @@ Hydrahon follows `PSR-4` autoloading and can be installed using composer:
$ composer require 'clancats/hydrahon:dev-master'
```

## Usage
## Usage MySQL

### Creating hydrahon builder
### Create a builder

Again Hydrahon is **not** built as a database library, it's just a query builder. In this example, I'm going to present you an easy example of a PDO mysql implementation.

Expand All @@ -44,17 +32,18 @@ $hydrahon = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryStrin
$statement = $connection->prepare($queryString);
$statement->execute($queryParameters);

if ($query instanceof \ClanCats\Hydrahon\Query\Sql\Select)
if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
{
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
});
```

### SQL query builder
---

Please note that in the following examples the variable `$h` contains a Hydrahon query builder instance.
### Structure

* [Basics](#basics)
* [Select](#select)
* [Runners](#runners)
* [Basics](#basics)
Expand All @@ -63,26 +52,92 @@ Please note that in the following examples the variable `$h` contains a Hydrahon
* [Join](#joins)
* [Limit and Offset](#limit-offset-and-page)

#### Select
---

> Note: Please note that in the following examples the variable `$h` contains a Hydrahon query builder instance.
---

### Basics

Lets start with a super basic example:

#### Inserting:

```php
$h->table('people')->insert(
[
[
'name' => 'Ray',
'age' => 25,
],
[
'name' => 'John',
'age' => 30,
],
[
'name' => 'Ali',
'age' => 22,
],
])->execute();
```

#### Updating:

```php
$h->table('people')->update()->set('age', 26)->where('name', 'Ray')->execute();
```

#### Deleting:

```php
$h->table('people')->delete()->where('name', 'John')->execute();
```

#### Selecting:

```php
$h->table('people')->select()->get();
```

---

### SQL Select

In our example we are going to execute multiple operations on the same table, so instead of loading the table over and over again we store it in a variable.

```php
$users = $h->table('users');
```

##### Runners
Also, the examples do not show the `run` method, which has to be executed to (obviously) run the query.
#### Runners

The runner methods execute your query and return a result. There are many diffrent runner methods and each one acts like an helper. This means a runner method can modifiy your query and the result.

##### "Execute" method

The `execute` method is an alias of `executeResultFetcher`, this means the method just forwards the plain data that you return inside your `ClanCats\Hydrahon\Builder` instance callback.

```php
$users->select()->limit(10)->execute();
```

#### "Get" method

The default runner method is the `get` method which can do some operations on your data.

```php
$users->select('name')->where('age', '>', 18)->run();
$users->select(['name'])->where('age', '>', 22)->get();
```

There are also other runners to cover common use cases.
For example by setting the limit of your query to _one_, you will also receive just that one single result. (Not an array of results).

**single result**
```php
$users->select()->get(); // returns: array(array(name: joe))
$users->select()->limit(1)->get(); // returns: array(name: joe)
```

Instead of retrieving an array of results you can direclty access a single one.
#### "One" method

```php
$users->select()->where('name', 'jeffry')->one();
Expand Down Expand Up @@ -120,7 +175,7 @@ Sometimes you just need one value, for that we have the column function
$users->select()->where('name', 'johanna')->column('age');
```

##### Basics
#### Basics

Selecting everything

Expand Down Expand Up @@ -162,7 +217,7 @@ $users->select([$users->raw("max('age')")])
select max('age') from `users`
```

##### Where
#### Where
The `where` statement does not only apply to the `select` query, but also to update and `delete`.

```php
Expand Down Expand Up @@ -219,7 +274,7 @@ $users->select()->where('id', 'in', [213, 32, 53, 43]);
select * from `users` where `id` in (?, ?, ?, ?)
```

##### Ordering
#### Ordering

```php
$users->select()->orderBy('name');
Expand Down Expand Up @@ -261,7 +316,7 @@ $users->select()->orderBy(['name', 'created_at' => 'desc']);
select * from `users` order by `name` asc, `created_at` desc
```

##### Joins
#### Joins

The automatic escaping becomes really handy when working with multiple tables.

Expand All @@ -285,7 +340,7 @@ The default join type is `left`, for every join type there is its own method.
* `innerJoin`
* `outterJoin`

##### Limit, Offset and Page
#### Limit, Offset and Page

When setting the limit to just one entry, you will receive it as a single result and not as result collection.

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"role": "Developer"
}
],
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": { "ClanCats\\Hydrahon\\": "src/" }
},
Expand Down
6 changes: 0 additions & 6 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class Builder
'ClanCats\\Hydrahon\\Query\\Sql',
'ClanCats\\Hydrahon\\Translator\\Sqlite',
),

// MongoDB
'mongo' => array(
'ClanCats\\Hydrahon\\Query\\MongoDB',
'ClanCats\\Hydrahon\\Translator\\MongoDB',
),
);

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Query/Sql/Exists.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace ClanCats\Hydrahon\Query\Sql;
<?php

namespace ClanCats\Hydrahon\Query\Sql;

/**
* Exists query
Expand All @@ -11,7 +13,7 @@

use ClanCats\Hydrahon\BaseQuery;

class Exists extends BaseQuery
class Exists extends BaseQuery implements FetchableInterface
{
/**
* The select query we want to check if
Expand Down
13 changes: 13 additions & 0 deletions src/Query/Sql/FetchableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ClanCats\Hydrahon\Query\Sql;

/**
* Does currently nothing itself but helps identifying queries that
* can / should be fetched.
**
* @package Hydrahon
* @copyright 2015 Mario Döring
*/

interface FetchableInterface {}
2 changes: 1 addition & 1 deletion src/Query/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use ClanCats\Hydrahon\Query\Expression;

class Select extends SelectBase
class Select extends SelectBase implements FetchableInterface
{
/**
* fields to be selected
Expand Down
35 changes: 35 additions & 0 deletions tests/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,39 @@ public function testConsturctInvalidBuilderClass()
// but now it should fail
$hydrahon = new Builder('invalidtranslator', function() {});
}

/**
* Check query classes
*/
public function testQueryClassesAsExpected()
{
// simple select
$hydrahon = new Builder('mysql', function($query, $queryString, $queryParameters)
{
$this->assertInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\Select', $query);
$this->assertInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\FetchableInterface', $query);
});

$hydrahon->table('test')->select()->get();

// exists select
$hydrahon = new Builder('mysql', function($query, $queryString, $queryParameters)
{
$this->assertNotInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\Select', $query);
$this->assertInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\Exists', $query);
$this->assertInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\FetchableInterface', $query);
});

$hydrahon->table('test')->select()->exists();

// non select
$hydrahon = new Builder('mysql', function($query, $queryString, $queryParameters)
{
$this->assertNotInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\Select', $query);
$this->assertNotInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\FetchableInterface', $query);
$this->assertInstanceOf('ClanCats\\Hydrahon\\Query\\Sql\\Insert', $query);
});

$hydrahon->table('test')->insert(['foo' => 'bar'])->execute();
}
}

0 comments on commit 7c85e03

Please sign in to comment.