All Projects → ClanCats → Hydrahon

ClanCats / Hydrahon

Licence: mit
🐉 Fast & standalone PHP MySQL Query Builder library.

Projects that are alternatives of or similar to Hydrahon

Diesel
A safe, extensible ORM and Query Builder for Rust
Stars: ✭ 7,702 (+3870.1%)
Mutual labels:  query-builder, mysql
Mysqldump Php
PHP version of mysqldump cli that comes with MySQL
Stars: ✭ 975 (+402.58%)
Mutual labels:  pdo, mysql
Reiner
萊納 - A MySQL wrapper which might be better than the ORMs and written in Golang
Stars: ✭ 19 (-90.21%)
Mutual labels:  query-builder, mysql
Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (+232.47%)
Mutual labels:  pdo, mysql
Nukeviet
NukeViet CMS is multi Content Management System. NukeViet CMS is the 1st open source content management system in Vietnam. NukeViet was awarded the Vietnam Talent 2011, the Ministry of Education and Training Vietnam officially encouraged to use.
Stars: ✭ 113 (-41.75%)
Mutual labels:  pdo, mysql
Fluentpdo
A PHP SQL query builder using PDO
Stars: ✭ 783 (+303.61%)
Mutual labels:  pdo, mysql
Dtool
数据生成器,数据库工具,数据库填充,伪数据,faker,mysql数据字典,数据库比对
Stars: ✭ 28 (-85.57%)
Mutual labels:  pdo, mysql
Dibi
Dibi - smart database abstraction layer
Stars: ✭ 373 (+92.27%)
Mutual labels:  pdo, mysql
Go Structured Query
Type safe SQL query builder and struct mapper for Go
Stars: ✭ 101 (-47.94%)
Mutual labels:  query-builder, mysql
Swpdo
Swoole Coroutine SQL component like PDO | 0成本迁移PDO到Swoole高性能协程客户端
Stars: ✭ 64 (-67.01%)
Mutual labels:  pdo, mysql
Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (+221.65%)
Mutual labels:  pdo, mysql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+988.14%)
Mutual labels:  query-builder, mysql
Microorm.dapper.repositories
CRUD for Dapper
Stars: ✭ 424 (+118.56%)
Mutual labels:  query-builder, mysql
Ezsql
PHP class to make interacting with a database ridiculusly easy
Stars: ✭ 804 (+314.43%)
Mutual labels:  pdo, mysql
Aura.sqlquery
Independent query builders for MySQL, PostgreSQL, SQLite, and Microsoft SQL Server.
Stars: ✭ 376 (+93.81%)
Mutual labels:  query-builder, mysql
Pecee Pixie
Lightweight, easy-to-use querybuilder for PHP inspired by Laravel Eloquent - but with less overhead.
Stars: ✭ 19 (-90.21%)
Mutual labels:  query-builder, mysql
Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (+61.34%)
Mutual labels:  pdo, mysql
Php Mysql Class
Simple MySQL class written in PHP, for interfacing with a MySQL database.
Stars: ✭ 349 (+79.9%)
Mutual labels:  pdo, mysql
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-70.62%)
Mutual labels:  pdo, mysql
Basicdb
PDO ile geliştirilmiş kullanımı kolay veritabanı sınıfıdır.
Stars: ✭ 127 (-34.54%)
Mutual labels:  pdo, mysql

Hydrahon

Hydrahon is a standalone database / SQL query builder written in PHP. It was built to enhance existing frameworks, libraries and applications that handle the database connection on their own. It does not come with a PDO or mysqli wrapper. The naming is heavily inspired by Eloquent and the Kohana Framework Database component.

What does that mean "Standalone query builder"?

Hydrahon only generates a query string and an array of parameters. On its own, it is not able to execute a query.

Build Status Packagist Packagist GitHub release

Status

  • The Hydrahon MySQL query builder is stable and used in production.
  • The Hydrahon AQL (Arango Query Language) query builder is currently in development.
  • A builder for Elasticsearch is on my mind but not in development.

Installation

Hydrahon follows PSR-4 autoloading and can be installed using composer:

$ composer require clancats/hydrahon

Documentation 💡

The full documentation can be found on clancats.io

Quick Start (MySQL) ⚡️

Hydrahon is designed to be a pretty generic query builder. So for this quick start, we stick with SQL.

Create a builder

Again this library is not built as a full database abstraction or ORM, it is only and will always be only a query builder. This means we need to implement the database connection and fetching by ourselves. The Hydrahon constructor therefore requires you to provide a callback function that does this, and returns the results.

In this example, we are going to use PDO

$connection = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    $statement = $connection->prepare($queryString);
    $statement->execute($queryParameters);

    // when the query is fetchable return all results and let hydrahon do the rest
    // (there's no results to be fetched for an update-query for example)
    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
    {
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
    }
});

And we are ready and set. The variable $h contains now a MySQL query builder.

Setup a simple table

To continue with our examples, we need to create a simple MySQL table.

CREATE TABLE `people` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT '',
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Inserting

Currently, we do not have any data, to fix this let's go and insert some.

// 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.
$people = $h->table('people');

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

Will execute the following query:

insert into `people` (`age`, `name`) values (?, ?), (?, ?), (?, ?)

As you can see Hydrahon automatically escapes the parameters.

However, because we are humans that get confused when there are hundreds of thousands of questions marks, I will continue to always display the runnable query:

insert into `people` (`age`, `name`) values (25, Ray), (30, John), (22, Ali)

Updating

Ah snap, time runs so fast, "Ray" is actually already 26.

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

Generating:

update `people` set `age` = 26 where `name` = 'Ray'

Currently, you might think: "Well isn't it much simpler to just write the SQL query? I mean the PHP code is even longer...".

You have to understand that these are some very very basic examples the Hydrahon query builder starts to shine when things get more complex. However, a "Quick Start" is just the wrong place for complicated stuff, so throw an eye on the full documentation.

Deleting

Dammit John, I hate you...

$people->delete()
    ->where('name', 'John')
->execute();

Generating:

delete from `people` where `name` = 'John'

Selecting

And finally, fetch the data.

$people->select()->get();

Generating:

select * from `people`

Result:

[
  {
    "id": "1",
    "name": "Ray",
    "age": "26"
  },
  {
    "id": "3",
    "name": "Ali",
    "age": "22"
  }
]

Notice that we use ->get() to actually fetch data, while we used ->execute() for our previous queries (updates, inserts and deletes). See the full documentation for more information about the Hydrahon runners methods.

Where conditions

For the next few examples, lets assume a larger dataset so that the queries make sense.

Chaining where conditions:

// select * from `people` where `age` = 21 and `name` like 'J%'
$people->select()
    ->where('age', 21)
    ->where('name', 'like', 'J%')
    ->get();

Notice how omitting the operator in the first condition ->where('age', 21) makes Hydrahon default to =.

By default all where conditions are defined with the and operator.

Different where operators:

// select * from `people` where `name` like 'J%' or `name` like 'I%'
$people->select()
    ->where('name', 'like', 'J%')
    ->orWhere('name', 'like', 'I%')
    ->get();

Please check the relevant section in the full documentation for more where-functions, like

  • whereIn()
  • whereNotIn()
  • whereNull()
  • whereNotNull()

Where scopes

Allowing you to group conditions:

// select * from `people` where ( `age` > 21 and `age` < 99 ) or `group` = admin
$people->select()
    ->where(function($q) 
    {
        $q->where('age', '>', 21);
        $q->where('age', '<', 99);
    })
    ->orWhere('group', 'admin')
    ->get();

Joins

Joining tables:

// select 
//     `people`.`name`, `groups`.`name` as `group_name` 
// from `people` 
// left join `groups` on `groups`.`id` = `people`.`group_id`
$people->select('people.name, groups.name as group_name')
    ->join('groups', 'groups.id', '=', 'people.group_id')
    ->get();

Grouping

Grouping data:

// select * from `people` group by `age`
$people->select()->groupBy('age')->get();

Ordering

Ordering data:

// select * from `people` order by `age` desc
$people->select()->orderBy('age', 'desc')->get();

// select * from `people` order by `age` desc, `name` asc
$people->select()->orderBy(['age' => 'desc', 'name' => 'asc'])->get();

Limiting data

Limit and offset:

// select * from `people` limit 0, 10
$people->select()->limit(10)->get();

// select * from `people` limit 100, 10
$people->select()->limit(100, 10)->get();

// select * from `people` limit 100, 10
$people->select()->limit(10)->offset(100)->get();

// select * from `people` limit 150, 30
$people->select()->page(5, 30)->get();

Small reminder this is the quick start, check out the full docs.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].