Skip to content

Commit 5e24c9f

Browse files
committed
Define primary table, add getter
1 parent 48b5066 commit 5e24c9f

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ class BaseBuilder
185185
*/
186186
protected $db;
187187

188+
/**
189+
* Name of the primary table for this instance.
190+
* Tracked separately because $QBFrom gets escaped
191+
* and prefixed.
192+
*
193+
* @var string
194+
*/
195+
protected $tableName;
196+
188197
/**
189198
* ORDER BY random keyword
190199
*
@@ -286,6 +295,7 @@ public function __construct($tableName, ConnectionInterface &$db, array $options
286295

287296
$this->db = $db;
288297

298+
$this->tableName = $tableName;
289299
$this->from($tableName);
290300

291301
if (! empty($options))
@@ -330,6 +340,18 @@ public function testMode(bool $mode = true)
330340

331341
//--------------------------------------------------------------------
332342

343+
/**
344+
* Gets the name of the primary table.
345+
*
346+
* @return string
347+
*/
348+
public function getTable(): string
349+
{
350+
return $this->tableName;
351+
}
352+
353+
//--------------------------------------------------------------------
354+
333355
/**
334356
* Returns an array of bind values and their
335357
* named parameters for binding in the Query object later.

tests/system/Database/Builder/BaseTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,32 @@ public function testDbReturnsConnection()
2626

2727
$this->assertInstanceOf(MockConnection::class, $result);
2828
}
29+
30+
//--------------------------------------------------------------------
31+
32+
public function testGetTableReturnsTable()
33+
{
34+
$builder = $this->db->table('jobs');
35+
36+
$result = $builder->getTable();
37+
$this->assertEquals('jobs', $result);
38+
}
39+
40+
public function testGetTableIgnoresFrom()
41+
{
42+
$builder = $this->db->table('jobs');
43+
44+
$builder->from('foo');
45+
$result = $builder->getTable();
46+
$this->assertEquals('jobs', $result);
47+
}
48+
49+
public function testGetTableRespectsFrom()
50+
{
51+
$builder = $this->db->table('jobs');
52+
53+
$builder->from('foo', true);
54+
$result = $builder->getTable();
55+
$this->assertEquals('foo', $result);
56+
}
2957
}

0 commit comments

Comments
 (0)