-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.php
69 lines (60 loc) · 1.55 KB
/
DB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Pinq\Demo\Sql;
use PDO;
use Pinq\Demo\Sql\Compilation\SqlCompilerConfiguration;
use Pinq\Demo\Sql\Providers\TableSourceInfo;
use Pinq\IRepository;
/**
* Entry point to the Pinq.Demo.Sql API.
*
* @author Elliot Levin <elliotlevin@hotmail.com>
*/
class DB
{
/**
* @var PDO
*/
protected $connection;
/**
* @var Providers\TableRepositoryProvider[]
*/
protected $tableProviders;
/**
* @var SqlCompilerConfiguration
*/
protected $compilerConfiguration;
public function __construct(PDO $connection)
{
$this->connection = $connection;
$this->compilerConfiguration = new SqlCompilerConfiguration($this->connection);
}
/**
* Gets the underlying PDO instance.
*
* @return PDO
*/
public function getConnection()
{
return $this->connection;
}
/**
* Gets a IRepository instance for the supplied table.
* Primary keys must be supplied for calls to ->clear() and ->apply()
*
* @param string $name
* @param string[]|null $primaryKeys
*
* @return IRepository
*/
public function table($name, array $primaryKeys = null)
{
if (!isset($this->tableProviders[$name])) {
$this->tableProviders[$name] = new Providers\TableRepositoryProvider(
$this->connection,
new TableSourceInfo($name, $primaryKeys),
$this->compilerConfiguration
);
}
return $this->tableProviders[$name]->createRepository();
}
}