2017 © Pedro PelĂĄez
 

library magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

image

mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  • Friday, May 11, 2018
  • by mouf
  • Repository
  • 7 Watchers
  • 14 Stars
  • 20,856 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 22 Versions
  • 24 % Grown

The README.md

Latest Stable Version Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status, (*1)

What is Magic-query?

Magic-query is a PHP library that helps you work with complex SQL queries., (*2)

It comes with 3 great features:, (*3)

Installation

Simply use the composer package:, (*4)

{
    "require": {
        "mouf/magic-query": "^1.2"
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

, (*5)

Automatically discard unused parameters with MagicParameters

Just write the query with all possible parameters., (*6)

use Mouf\Database\MagicQuery;

$sql = "SELECT * FROM users WHERE name LIKE :name AND country LIKE :country";

// Get a MagicQuery object.
$magicQuery = new MagicQuery();

// Let's pass only the "name" parameter
$result = $magicQuery->build($sql, [ "name" => "%John%" ]);
// $result = SELECT * FROM users WHERE name LIKE '%John%'
// Did you notice how the bit about the country simply vanished?

// Let's pass no parameter at all!
$result2 = $magicQuery->build($sql, []);
// $result2 = SELECT * FROM users
// The whole WHERE condition disappeared because it is not needed anymore!

Curious to know how this work? Check out the parameters guide!, (*7)

, (*8)

Automatically guess JOINs with MagicJoin!

Fed up of writing joins in SQL? Let MagicQuery do the work for you!, (*9)

Seriously? Yes! All you have to do is:, (*10)

  • Pass a Doctrine DBAL connection to MagicQuery's constructor. MagicQuery will analyze your schema.
  • In your SQL query, replace the tables with magicjoin(start_table)
  • For each column of your query, use the complete name ([table_name].[column_name] instead of [column_name] alone)

Let's assume your database schema is:, (*11)

Sample database schema, (*12)

Using MagicJoin, you can write this SQL query:, (*13)

SELECT users.* FROM MAGICJOIN(users) WHERE groups.name = 'Admins' AND country.name='France';

and it will automatically be transformed into this:, (*14)

SELECT users.* FROM users 
    LEFT JOIN users_groups ON users.user_id = users_groups.user_id
    LEFT JOIN groups ON groups.group_id = users_groups.group_id
    LEFT JOIN country ON country.country_id = users.country_id
WHERE groups.name = 'Admins' AND country.name='France';

And the code is so simple!, (*15)

use Mouf\Database\MagicQuery;

$sql = "SELECT users.* FROM MAGICJOIN(users) WHERE groups.name = 'Admins' AND country.name='France'";

// Get a MagicQuery object.
// $conn is a Doctrine DBAL connection.
$magicQuery = new MagicQuery($conn);

$completeSql = $magicQuery->build($sql);
// $completeSql contains the complete SQL request, with all joins.

Want to know more? Check out the MagicJoin guide!, (*16)

, (*17)

Use Twig templating in your SQL queries!

Discarding unused parameters and auto-joining keys is not enough? You have very specific needs? Say hello to Twig integration!, (*18)

Using Twig integration, you can directly add Twig conditions right into your SQL., (*19)

use Mouf\Database\MagicQuery;

$sql = "SELECT users.* FROM users {% if isAdmin %} WHERE users.admin = 1 {% endif %}";

$magicQuery = new MagicQuery();
// By default, Twig integration is disabled. You need to enable it.
$magicQuery->setEnableTwig(true);

$completeSql = $magicQuery->build($sql, ['isAdmin' => true]);
// Parameters are passed to the Twig SQL query, and the SQL query is returned.
Heads up! The Twig integration cannot be used to insert parameters into the SQL query. You should use classic SQL parameters for this. This means that instead if writing {{ id }}, you should write :id.

Want to know more? Check out the MagicTwig guide!, (*20)

Is it a MySQL only tool?

No. By default, your SQL is parsed and then rewritten using the MySQL dialect, but you use any kind of dialect known by Doctrine DBAL. Magic-query optionally uses Doctrine DBAL. You can pass a Connection object as the first parameter of the MagicQuery constructor. Magic-query will then use the matching dialect., (*21)

For instance:, (*22)

$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
    'url' => 'sqlite:///somedb.sqlite',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

$magicQuery = new \Mouf\Database\MagicQuery($conn);

Also, if you have no connection to your database configured but you want to generate SQL in some specific dialect, you can instead set the DBAL database platform used:, (*23)

$magicQuery->setOutputDialect(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform());
$magicQuery = new \Mouf\Database\MagicQuery();

What about performances?

MagicQuery does a lot to your query. It will parse it, render it internally as a tree of SQL nodes, etc... This processing is time consuming. So you should definitely consider using a cache system. MagicQuery is compatible with Doctrine Cache. You simply have to pass a Doctrine Cache instance has the second parameter of the constructor., (*24)

use Mouf\Database\MagicQuery;
use Doctrine\Common\Cache\ApcCache;

// $conn is a Doctrine connection
$magicQuery = new MagicQuery($conn, new ApcCache());

Any problem?

With MagicQuery, a lot happens to your SQL query. In particular, it is parsed using a modified version of the php-sql-parser library. If you face any issues with a complex query, it is likely there is a bug in the parser. Please open an issue on Github and we'll try to fix it., (*25)

The Versions

17/08 2016

1.1.x-dev

1.1.9999999.9999999-dev http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

17/08 2016

v1.1.6

1.1.6.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

16/06 2016

v1.1.5

1.1.5.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

13/06 2016

v1.1.4

1.1.4.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

28/04 2016

v1.1.3

1.1.3.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

14/04 2016

v1.1.2

1.1.2.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

04/04 2016

v1.1.1

1.1.1.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

29/02 2016

v1.1.0

1.1.0.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

29/12 2015

v1.1.0-beta1

1.1.0.0-beta1 http://mouf-php.com/packages/mouf/magic-query

A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

17/09 2015

1.0.x-dev

1.0.9999999.9999999-dev http://mouf-php.com/packages/mouf/magic-query

A very clever library to generate PHP prepared statement with a variable number of parameters... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf

17/09 2015

v1.0.0

1.0.0.0 http://mouf-php.com/packages/mouf/magic-query

A very clever library to generate PHP prepared statement with a variable number of parameters... and much more!

  Sources   Download

MIT

The Requires

 

The Development Requires

database query mouf