12 Commits
1.0.0 ... 1.0.5

Author SHA1 Message Date
946a0b0b5e Merge branch 'develop' into release 2022-09-12 21:00:03 -03:00
3b1902ed1a Include AND when not specified in condition 2022-09-12 20:59:27 -03:00
e7ab3fb8b7 FIX: ? placeholder was quoted 2022-09-09 15:42:07 -04:00
d4fecf977d Don't quote ? placeholder 2022-09-09 15:37:50 -04:00
a22a927bb4 FIX: make instead of get 2022-09-09 14:09:13 -04:00
758a4b7c7f Make new queries instead of reuse 2022-09-09 14:07:27 -04:00
2fd0af5c2f FIX 2022-09-09 12:19:03 -04:00
97d5ae8450 FIX: * columns in select 2022-09-09 12:15:22 -04:00
0f4438bd5f FIX dependency and add Readme 2022-09-08 22:14:18 -04:00
7750ab95c6 Readme 2022-09-08 22:13:48 -04:00
47d38a6595 FIX dependency 2022-09-08 22:13:42 -04:00
3458cf8532 Readme 2022-09-08 18:05:59 -04:00
5 changed files with 153 additions and 8 deletions

118
Readme.md Normal file
View File

@ -0,0 +1,118 @@
# 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(<*columns>);
$query = $qb->insert(<table>);
$query = $qb->update(<table>);
$query = $qb->delete(<table>);
```
### Queries
#### Select
`SELECT <columns> FROM <table>[ JOIN <table2> ON <table>.<column_1> = <table2>.<column_2>][ WHERE <expr1>[ AND|OR <expr2>]][ GROUP BY <column1>[, <column2>]][ HAVING <expr1>[ AND|OR <expr2>]][ ORDER BY <column1> ASC|DESC[, <column2> ASC|DESC]]`
Mysql also include `[ LIMIT <limit>[ OFFSET <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 <table>[ (<columns>)]`
with two options
`<select query>` or
` VALUES (<values>)`
```
$query = $qb->insert('table');
$query->columns(['column1',]);
$query->values(['value1',];
or
$query->select($select_query);
```
#### Update
`UPDATE <table> SET <column1> = <value1>[, <column2> = <value2>][ WHERE <expr1>[ AND|OR <expr2>]]`
```
$query = $qb->update('table');
$query->set([['column1', 10],]); or $query->set([['column' => 'column1', 'value' => 10],]);
$query->where(['column2 = 10', ]);
```
#### Delete
`DELETE FROM <table> WHERE <expr1>[ AND|OR <expr2>]`
```
$query = $qb->delete('table');
$query->where(['column1 = 10',]);
```
Pass the query to a string
```
$str = $query->build();
or
$str = "{$query}";
```
## TODO
Implement other databases

View File

@ -12,7 +12,6 @@
}
],
"require": {
"psr/container": "^2.0"
},
"autoload": {
"psr-4": {

View File

@ -65,7 +65,7 @@ abstract class Insert extends Query implements InsertInterface
}
public function addValue(int|string $value): InsertInterface
{
if (!is_numeric($value)) {
if (!is_numeric($value) and $value !== '?') {
$value = "'{$value}'";
}
$this->values []= $value;

View File

@ -51,6 +51,10 @@ abstract class Select extends Query implements SelectInterface
}
public function addColumn(string $column, ?string $alias = null): SelectInterface
{
if ($column === '*') {
$this->columns []= $column;
return $this;
}
$a = '';
if ($alias !== null) {
$a = " AS '{$alias}'";
@ -101,7 +105,19 @@ abstract class Select extends Query implements SelectInterface
}
public function getJoinString(): string
{
return implode(' ', $this->getJoins());
$str = [];
foreach ($this->getJoins() as $i => $join) {
if ($i === 0) {
$str []= $join;
continue;
}
if (!str_contains('and ', strtolower($join)) and !str_contains('or ', strtolower($join))) {
$str []= "AND {$join}";
continue;
}
$str []= $join;
}
return implode(' ', $str);
}
protected array $conditions;
public function setConditions(array $conditions): SelectInterface
@ -122,7 +138,19 @@ abstract class Select extends Query implements SelectInterface
}
public function getConditionString(): string
{
return implode(' ', $this->getConditions());
$str = [];
foreach ($this->getConditions() as $i => $condition) {
if ($i === 0) {
$str []= $condition;
continue;
}
if (!str_contains('and ', strtolower($condition)) and !str_contains('or ', strtolower($condition))) {
$str []= "AND {$condition}";
continue;
}
$str []= $condition;
}
return implode(' ', $str);
}
protected array $groups;
public function setGroups(array $groups): SelectInterface

View File

@ -20,18 +20,18 @@ class QueryBuilder implements QBInterface
public function select(array $columns = ['*']): Select
{
return $this->getContainer()->get(Select::class)->select($columns);
return $this->getContainer()->make(Select::class)->select($columns);
}
public function insert(string $table): Insert
{
return $this->getContainer()->get(Insert::class)->into($table);
return $this->getContainer()->make(Insert::class)->into($table);
}
public function update(string $table): Update
{
return $this->getContainer()->get(Update::class)->table($table);
return $this->getContainer()->make(Update::class)->table($table);
}
public function delete(string $table): Delete
{
return $this->getContainer()->get(Delete::class)->from($table);
return $this->getContainer()->make(Delete::class)->from($table);
}
}