Skip to content

Commit deaec53

Browse files
committed
Support for references from different tables.
1 parent 67fc596 commit deaec53

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

src/Components/Reference.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Reference extends Component
4040
/**
4141
* The referenced table.
4242
*
43-
* @var string
43+
* @var Expression
4444
*/
4545
public $table;
4646

@@ -61,7 +61,7 @@ class Reference extends Component
6161
/**
6262
* Constructor.
6363
*
64-
* @param string $table The name of the table referenced.
64+
* @param Expression $table The name of the table referenced.
6565
* @param array $columns The columns referenced.
6666
* @param OptionsArray $options The options.
6767
*/
@@ -117,7 +117,15 @@ public static function parse(Parser $parser, TokensList $list, array $options =
117117
}
118118

119119
if ($state === 0) {
120-
$ret->table = $token->value;
120+
$ret->table = Expression::parse(
121+
$parser,
122+
$list,
123+
array(
124+
'noAlias' => true,
125+
'skipColumn' => true,
126+
'noBrackets' => true,
127+
)
128+
);
121129
$state = 1;
122130
} elseif ($state === 1) {
123131
$ret->columns = ArrayObj::parse($parser, $list)->values;
@@ -143,7 +151,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
143151
public static function build($component, array $options = array())
144152
{
145153
return trim(
146-
Context::escape($component->table)
154+
$component->table
147155
. ' (' . implode(', ', Context::escape($component->columns)) . ') '
148156
. $component->options
149157
);

src/Utils/Table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static function getForeignKeys($statement)
5656
);
5757

5858
if (!empty($field->references)) {
59-
$tmp['ref_table_name'] = $field->references->table;
59+
$tmp['ref_db_name'] = $field->references->table->database;
60+
$tmp['ref_table_name'] = $field->references->table->table;
6061
$tmp['ref_index_list'] = $field->references->columns;
6162

6263
if (($opt = $field->references->options->has('ON UPDATE'))) {

tests/Components/ReferenceTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SqlParser\Tests\Components;
44

55
use SqlParser\Parser;
6+
use SqlParser\Components\Expression;
67
use SqlParser\Components\Reference;
78

89
use SqlParser\Tests\TestCase;
@@ -13,13 +14,13 @@ class ReferenceTest extends TestCase
1314
public function testParse()
1415
{
1516
$component = Reference::parse(new Parser(), $this->getTokensList('tbl (id)'));
16-
$this->assertEquals('tbl', $component->table);
17+
$this->assertEquals('tbl', $component->table->table);
1718
$this->assertEquals(array('id'), $component->columns);
1819
}
1920

2021
public function testBuild()
2122
{
22-
$component = new Reference('tbl', array('id'));
23+
$component = new Reference(new Expression('`tbl`'), array('id'));
2324
$this->assertEquals('`tbl` (`id`)', Reference::build($component));
2425
}
2526
}

tests/Utils/TableTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ public function getForeignKeysProvider()
4747
array(
4848
'constraint' => 'fk_payment_customer',
4949
'index_list' => array('customer_id'),
50+
'ref_db_name' => null,
5051
'ref_table_name' => 'customer',
5152
'ref_index_list' => array('customer_id'),
5253
'on_update' => 'CASCADE',
5354
),
5455
array(
5556
'constraint' => 'fk_payment_rental',
5657
'index_list' => array('rental_id'),
58+
'ref_db_name' => null,
5759
'ref_table_name' => 'rental',
5860
'ref_index_list' => array('rental_id'),
5961
'on_delete' => 'SET_NULL',
@@ -62,6 +64,7 @@ public function getForeignKeysProvider()
6264
array(
6365
'constraint' => 'fk_payment_staff',
6466
'index_list' => array('staff_id'),
67+
'ref_db_name' => null,
6568
'ref_table_name' => 'staff',
6669
'ref_index_list' => array('staff_id'),
6770
'on_update' => 'CASCADE',
@@ -97,6 +100,7 @@ public function getForeignKeysProvider()
97100
array(
98101
'constraint' => 'fk_address_city',
99102
'index_list' => array('city_id'),
103+
'ref_db_name' => null,
100104
'ref_table_name' => 'city',
101105
'ref_index_list' => array('city_id'),
102106
'on_update' => 'CASCADE',

0 commit comments

Comments
 (0)