Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlBuilder: Added alias support #119

Closed
wants to merge 1 commit into from
Closed

Conversation

@norbe
Copy link
Contributor

norbe commented Mar 4, 2016

This patch allows to specify alias for given table chain. Aliases can be even nested.

I have also added some checks if table alias is unique. Old behaviour in some cases silently skip tables or just works weird. It can found some hidden bugs in application. For example $context->table('book')->select('book.author.column') was translated to 'book.column'.

@norbe norbe force-pushed the norbe:kh-added-alias-support branch 2 times, most recently from a8819a7 to 9521ed6 Mar 4, 2016
@norbe

This comment has been minimized.

Copy link
Contributor Author

norbe commented Mar 5, 2016

Can someone tell me what is wrong in SqlBuilder.addAlias().phpt?

From "[FOUND] tests/Database/Table/SqlBuilder.addAlias().phpt -1 bytes of whitespaces" I really don't know what is wrong..

@Unlink

This comment has been minimized.

Copy link
Contributor

Unlink commented Mar 5, 2016

You should use code-checker -f to fix this.

@janpecha

This comment has been minimized.

@norbe norbe force-pushed the norbe:kh-added-alias-support branch from 9521ed6 to f43aaf3 Mar 6, 2016
@norbe

This comment has been minimized.

Copy link
Contributor Author

norbe commented Mar 6, 2016

@Unlink, @janpecha: Thanks

@norbe

This comment has been minimized.

Copy link
Contributor Author

norbe commented Mar 20, 2016

@dg Can I do somethink for merge of this feature?

@@ -406,6 +406,19 @@ public function having($having, ...$params)
}


/**
* Alias table

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

Aliases table.

/**
* Alias table
* @example ':book:book_tag.tag', 'tg'
* @param string $tableChain

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

Please remove $tableChain

* @param string $alias
* @return self
*/
public function alias($tableChain, $alias) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

{ on next line

/** @var string currently parsing alias for joins */
protected $currentAlias = NULL;

/** array of depending tables */

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

@var

protected $reservedTableNames = [];

/** @var array of table aliases */
protected $aliases = [];

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

two spaces

@@ -329,6 +343,34 @@ public function getConditions()
}


/**
* Add alias

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

Adds alias. (with dot)

@@ -329,6 +343,34 @@ public function getConditions()
}


/**
* Add alias
* @param string $chain

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

remove $chain

* Add alias
* @param string $chain
* @param string $alias
* @throws \Nette\InvalidArgumentException

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

@return void

* @param string $alias
* @throws \Nette\InvalidArgumentException
*/
public function addAlias($chain, $alias) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

{ new line

$this->aliases[$alias] = $chain;
}

protected function checkUniqueTableName($tableName, $chain) {

This comment has been minimized.

Copy link
@dg
}

protected function checkUniqueTableName($tableName, $chain) {
if(isset($this->aliases[$tableName]) && ('.' . $tableName === $chain)) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space after if

if(isset($this->aliases[$tableName]) && ('.' . $tableName === $chain)) {
$chain = $this->aliases[$tableName];
}
if(isset($this->reservedTableNames[$tableName])){

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space after if and before {

$chain = $this->aliases[$tableName];
}
if(isset($this->reservedTableNames[$tableName])){
if($this->reservedTableNames[$tableName] === $chain) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space

@@ -448,15 +490,33 @@ public function parseJoinsCb(& $joins, $match)
// do not make a join when referencing to the current table column - inner conditions
// check it only when not making backjoin on itself - outer condition
if ($keyMatches[0]['del'] === '.') {
if(count($keyMatches) > 1 && ($parent === $keyMatches[0]['key'] || $parentAlias === $keyMatches[0]['key'])) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space after if

$tableChain = NULL;
foreach ($keyMatches as $index => $keyMatch) {
$isLast = !isset($keyMatches[$index+1]);
if(!$index && isset($this->aliases[$keyMatch['key']])){

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

spaces


// if we are joining itself (parent table), we must alias joining table
if ($parent === $table) {
if($this->currentAlias && $isLast) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space

}

$joins[$tableAlias . $column] = [$table, $tableAlias, $parentAlias, $column, $primary];
$tableChain .= $keyMatch['del'] . $tableAlias;
if(!$isLast || !$this->currentAlias) {

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space

}
$sqlBuilder->addAlias(':book(translator)', 'book1');
$sqlBuilder->addAlias(':book:book_tag', 'book2');
Assert::exception(function() use ($sqlBuilder){

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space before {

$sqlBuilder->addAlias(':book', 'book1');
}, '\Nette\InvalidArgumentException');

Assert::exception(function() use ($sqlBuilder){ // reserved by base table name

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space before {

$sqlBuilder->addAlias(':book', 'author');
}, '\Nette\InvalidArgumentException');

Assert::exception(function() use ($sqlBuilder){

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space before {

}, '\Nette\InvalidArgumentException');

$sqlBuilder->addAlias(':book', 'tag');
Assert::exception(function() use ($sqlBuilder){

This comment has been minimized.

Copy link
@dg

dg Mar 20, 2016

Member

space before {

@norbe norbe force-pushed the norbe:kh-added-alias-support branch from f43aaf3 to 99fe59e Mar 21, 2016
@norbe

This comment has been minimized.

Copy link
Contributor Author

norbe commented Apr 1, 2016

Anything else?

@dg dg closed this in 1329476 Apr 1, 2016
@dg

This comment has been minimized.

Copy link
Member

dg commented Apr 1, 2016

@norbe Thank you!

@norbe

This comment has been minimized.

Copy link
Contributor Author

norbe commented Apr 1, 2016

Can I check coding standard automatically somehow for future?

@dg

This comment has been minimized.

Copy link
Member

dg commented Apr 1, 2016

Unfortunatelly I haven't tool for checking coding style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.