Skip to content

Commit

Permalink
Merge pull request #5 from skipperbent/v3-development
Browse files Browse the repository at this point in the history
V3 development
  • Loading branch information
skipperbent authored Apr 29, 2017
2 parents d52e515 + d7fbd12 commit a5602f1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ composer install pecee/pixie

- [Connection](#connection)
- [Multiple Connection](#alias)
- [SQLite and PostgreSQL Config Sample](sqlite-and-postgresql-config-sample)
- [SQLite and PostgreSQL Config Sample](#sqlite-and-postgresql-config-sample)
- [Query](#query)
- [**Select**](#select)
- [Table prefix](table-prefix)
- [Table prefix](#table-alias)
- [Get Easily](#get-easily)
- [Multiple Selects](#multiple-selects)
- [Select Distinct](#select-distinct)
Expand Down Expand Up @@ -198,13 +198,26 @@ However this is not required.
$qb->table(array('mytable1', 'mytable2'));
```

### Table prefix
### Table alias

You can easily set the table prefix by using
You can easily set the table alias by using

```php
$qb->addPrefix('post', 'child');
$qb->where('id', '=', 2);
$qb
->table(['table1' => 'foo1'])
->join('table2', 'table2.person_id', '=', 'foo1.id');
```

You can change the alias anytime by using

```php
$qb->alias($table, $alias);
```

Output:

```
SELECT * FROM `table1` AS foo1 INNER JOIN `cb_table2` ON `cb_table2`.`person_id` = `cb_foo1`.`id`
```

### Get Easily
Expand Down
10 changes: 6 additions & 4 deletions src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public function select($statements)
if (isset($statements['tables'])) {
$tables = [];

foreach ($statements['tables'] as $table) {
$t = ($table instanceof Raw) ? $table : '`' . $table . '`';
if (isset($statements['prefixes'][strtolower($table)])) {
$t .= ' AS ' . $statements['prefixes'][strtolower($table)];
foreach ($statements['tables'] as $prefix => $table) {

if (is_numeric($prefix) === false) {
$t = $t = ($table instanceof Raw) ? $prefix : '`' . $prefix . '` AS ' . strtolower($table);
} else {
$t = ($table instanceof Raw) ? $table : '`' . $table . '`';
}

$tables[] = $t;
Expand Down
30 changes: 26 additions & 4 deletions src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,35 @@ public function query($sql, array $bindings = [])
return $this;
}

public function addPrefix($table, $prefix)
/**
* Add or change table alias
* Example: table AS alias
*
* @param string $table
* @param string $alias
* @return QueryBuilderHandler
*/
public function alias($table, $alias)
{
$this->addStatement('prefixes', [$table => strtolower($prefix)]);

$this->statements['tables'][$this->tablePrefix . $table] = strtolower($alias);
return $this;
}

/**
* Add or change table alias
* Example: table AS alias
*
* @deprecated This method will be removed in the near future, please use QueryBuilderHandler::alias instead.
* @see QueryBuilderHandler::alias
* @param string $table
* @param string $alias
* @return QueryBuilderHandler
*/
public function prefix($table, $alias)
{
return $this->alias($table, $alias);
}

/**
* @param $sql
* @param array $bindings
Expand All @@ -164,7 +186,7 @@ public function statement($sql, $bindings = [])
/**
* Get all rows
* @throws Exception
* @return \stdClass|null
* @return \stdClass|array|null
*/
public function get()
{
Expand Down
10 changes: 10 additions & 0 deletions tests/Pecee/Pixie/QueryBuilderBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function testSelectFlexibility()
);
}

public function testAlias()
{
$query = $this->builder->table(['table1' => 'foo1'])
->alias('table1', 'foo2')
->join('table2', 'table2.person_id', '=', 'foo2.id');

$this->assertEquals('SELECT * FROM `cb_table1` AS foo2 INNER JOIN `cb_table2` ON `cb_table2`.`person_id` = `cb_foo2`.`id`',
$query->getQuery()->getRawSql());
}

public function testSelectQuery()
{
$subQuery = $this->builder->table('person_details')->select('details')->where('person_id', '=', 3);
Expand Down

0 comments on commit a5602f1

Please sign in to comment.