From 3458cf85320678d34c2ca39235424afebb2cec6a Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 Sep 2022 18:05:59 -0400 Subject: [PATCH 1/3] Readme --- Readme.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..d949f8b --- /dev/null +++ b/Readme.md @@ -0,0 +1,68 @@ +# QueryBuilder + +## Requirements ++ PHP 8+ (Should work with 7+, but haven't tested it). ++ `Psr\Container\ContainerInterface` implementation like [`php-di/php-di`](https://packagist.org/packages/php-di/php-di). + +## Installation + +```composer.json``` +``` +{ + ... + "repositories": [ + { + "type": "git", + "path": "https://git.provm.cl/ProVM/query_builder.git" + } + ], + "require": { + ... + "provm/query_builder": "^1.0", + ... + }, + ... +} +``` + +## Setup + +``` +$container->set(ProVM\Concept\Database\Query\Select::class, function(Psr\Container\ContainerInterface $container) { + return $container->get(ProVM\Database\Query\MySQL\Select::class); +}); +$container->set(ProVM\Concept\Database\Query\Insert::class, function(Psr\Container\ContainerInterface $container) { + return $container->get(ProVM\Database\Query\MySQL\Insert::class); +}); +$container->set(ProVM\Concept\Database\Query\Update::class, function(Psr\Container\ContainerInterface $container) { + return $container->get(ProVM\Database\Query\MySQL\Update::class); +}); +$container->set(ProVM\Concept\Database\Query\Delete::class, function(Psr\Container\ContainerInterface $container) { + return $container->get(ProVM\Database\Query\MySQL\Delete::class); +}); +``` + +## Usage + +QueryBuilder +``` +include_once 'vendor/autoload.php'; +$qb = new QueryBuilder(); + +$query = $qb->select(); +$query = $qb->insert(); +$query = $qb->update(); +$query = $qb->delete(); +``` + +### Queries + +Select +``` +$query = $qb->select(); +$query = $qb->select(['id', 'column1']); + +$query = $query->from('table'); +$query = $query->joins([['table2', 'table1.column = table2.column']]); +$query = $query->where([['table2.column2', 10]]); +``` From 47d38a6595ee750e76faa05e95664ad0d502b790 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 Sep 2022 22:13:42 -0400 Subject: [PATCH 2/3] FIX dependency --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index a983212..7cfe75d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,6 @@ } ], "require": { - "psr/container": "^2.0" }, "autoload": { "psr-4": { From 7750ab95c6e81ae28e71af0252da26feb7c19503 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 Sep 2022 22:13:48 -0400 Subject: [PATCH 3/3] Readme --- Readme.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/Readme.md b/Readme.md index d949f8b..a78b9f7 100644 --- a/Readme.md +++ b/Readme.md @@ -49,20 +49,70 @@ QueryBuilder include_once 'vendor/autoload.php'; $qb = new QueryBuilder(); -$query = $qb->select(); -$query = $qb->insert(); -$query = $qb->update(); -$query = $qb->delete(); +$query = $qb->select(<*columns>); +$query = $qb->insert(); +$query = $qb->update(
); +$query = $qb->delete(
); ``` ### Queries -Select -``` -$query = $qb->select(); -$query = $qb->select(['id', 'column1']); +#### Select +`SELECT FROM
[ JOIN ON
. = .][ WHERE [ AND|OR ]][ GROUP BY [, ]][ HAVING [ AND|OR ]][ ORDER BY ASC|DESC[, ASC|DESC]]` -$query = $query->from('table'); -$query = $query->joins([['table2', 'table1.column = table2.column']]); -$query = $query->where([['table2.column2', 10]]); +Mysql also include `[ LIMIT [ OFFSET ]]` ``` +$query = $qb->select(); // use '*' for columns +or +$query = $qb->select()->select(['id', 'column1',]); +or +$query = $qb->select(['id', 'column1',]); + +$query->from('table1'); +$query->joins([['table2', 'table1.column1 = table2.column21'],]); +$query->where(['table2.column22 = 10',]); +$query->groupBy(['table1.column1',]); +$query->having(['table1.column1 < 10',]); +$query->orderBy(['table2.column22',]); +$query->limit(10, 10); +``` + +#### Insert +`INSERT INTO
[ ()]` +with two options +`
SET = [, = ][ WHERE [ AND|OR ]]` +``` +$query = $qb->update('table'); +$query->set([['column1', 10],]); or $query->set([['column' => 'column1', 'value' => 10],]); +$query->where(['column2 = 10', ]); +``` + +#### Delete +`DELETE FROM
WHERE [ AND|OR ]` +``` +$query = $qb->delete('table'); +$query->where(['column1 = 10',]); +``` + + +Pass the query to a string +``` +$str = $query->build(); +or +$str = "{$query}"; +``` + +## TODO +Implement other databases