Skip to content

Commit

Permalink
Support for references from different tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Ungureanu committed Oct 3, 2015
1 parent 67fc596 commit deaec53
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/Components/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Reference extends Component
/**
* The referenced table.
*
* @var string
* @var Expression
*/
public $table;

Expand All @@ -61,7 +61,7 @@ class Reference extends Component
/**
* Constructor.
*
* @param string $table The name of the table referenced.
* @param Expression $table The name of the table referenced.
* @param array $columns The columns referenced.
* @param OptionsArray $options The options.
*/
Expand Down Expand Up @@ -117,7 +117,15 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if ($state === 0) {
$ret->table = $token->value;
$ret->table = Expression::parse(
$parser,
$list,
array(
'noAlias' => true,
'skipColumn' => true,
'noBrackets' => true,
)
);
$state = 1;
} elseif ($state === 1) {
$ret->columns = ArrayObj::parse($parser, $list)->values;
Expand All @@ -143,7 +151,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
public static function build($component, array $options = array())
{
return trim(
Context::escape($component->table)
$component->table
. ' (' . implode(', ', Context::escape($component->columns)) . ') '
. $component->options
);
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static function getForeignKeys($statement)
);

if (!empty($field->references)) {
$tmp['ref_table_name'] = $field->references->table;
$tmp['ref_db_name'] = $field->references->table->database;
$tmp['ref_table_name'] = $field->references->table->table;
$tmp['ref_index_list'] = $field->references->columns;

if (($opt = $field->references->options->has('ON UPDATE'))) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Components/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SqlParser\Tests\Components;

use SqlParser\Parser;
use SqlParser\Components\Expression;
use SqlParser\Components\Reference;

use SqlParser\Tests\TestCase;
Expand All @@ -13,13 +14,13 @@ class ReferenceTest extends TestCase
public function testParse()
{
$component = Reference::parse(new Parser(), $this->getTokensList('tbl (id)'));
$this->assertEquals('tbl', $component->table);
$this->assertEquals('tbl', $component->table->table);
$this->assertEquals(array('id'), $component->columns);
}

public function testBuild()
{
$component = new Reference('tbl', array('id'));
$component = new Reference(new Expression('`tbl`'), array('id'));
$this->assertEquals('`tbl` (`id`)', Reference::build($component));
}
}
4 changes: 4 additions & 0 deletions tests/Utils/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public function getForeignKeysProvider()
array(
'constraint' => 'fk_payment_customer',
'index_list' => array('customer_id'),
'ref_db_name' => null,
'ref_table_name' => 'customer',
'ref_index_list' => array('customer_id'),
'on_update' => 'CASCADE',
),
array(
'constraint' => 'fk_payment_rental',
'index_list' => array('rental_id'),
'ref_db_name' => null,
'ref_table_name' => 'rental',
'ref_index_list' => array('rental_id'),
'on_delete' => 'SET_NULL',
Expand All @@ -62,6 +64,7 @@ public function getForeignKeysProvider()
array(
'constraint' => 'fk_payment_staff',
'index_list' => array('staff_id'),
'ref_db_name' => null,
'ref_table_name' => 'staff',
'ref_index_list' => array('staff_id'),
'on_update' => 'CASCADE',
Expand Down Expand Up @@ -97,6 +100,7 @@ public function getForeignKeysProvider()
array(
'constraint' => 'fk_address_city',
'index_list' => array('city_id'),
'ref_db_name' => null,
'ref_table_name' => 'city',
'ref_index_list' => array('city_id'),
'on_update' => 'CASCADE',
Expand Down
Loading

0 comments on commit deaec53

Please sign in to comment.