|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk\Database; |
|
4
|
|
|
|
|
5
|
|
|
use PhpDocReader\Reader; |
|
6
|
|
|
use Silk\Exceptions\NoTableFoundException; |
|
7
|
|
|
use Zend\Db\TableGateway\AbstractTableGateway; |
|
8
|
|
|
use Zend\Db\TableGateway\Feature\FeatureSet; |
|
9
|
|
|
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature; |
|
10
|
|
|
use Silk\Database\AdapterPool; |
|
11
|
|
|
use Zend\Db\TableGateway\Feature\SequenceFeature; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class TableGateway |
|
15
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
|
16
|
|
|
* @package Silk\Database |
|
17
|
|
|
*/ |
|
18
|
|
|
class TableGateway extends AbstractTableGateway |
|
19
|
|
|
{ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($object) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->config = Reader::getConfig($object); |
|
25
|
|
|
|
|
26
|
|
|
if (!array_key_exists('table', $this->config)) |
|
27
|
|
|
throw new NoTableFoundException(); |
|
28
|
|
|
|
|
29
|
|
|
$this->table = $this->config['table']; |
|
30
|
|
|
|
|
31
|
|
|
if(!isset($this->config['adapter'])) |
|
32
|
|
|
$this->config['adapter'] = 'Default'; |
|
33
|
|
|
|
|
34
|
|
|
$adapterPool = new AdapterPool(); |
|
35
|
|
|
$this->adapter = $adapterPool->get($this->config['adapter']); |
|
36
|
|
|
|
|
37
|
|
|
$this->updateContext(); |
|
38
|
|
|
|
|
39
|
|
|
$platform = $this->adapter->getPlatform()->getName(); |
|
40
|
|
|
if(isset($this->config['schema']) && $platform == 'PostgreSQL'){ |
|
41
|
|
|
$pk = $this->config['primary_key']; |
|
42
|
|
|
$sq = $this->config['table'] . '_' . $pk . '_seq'; |
|
43
|
|
|
|
|
44
|
|
|
$this->featureSet = new FeatureSet(); |
|
45
|
|
|
$this->featureSet->addFeature(new SequenceFeature($pk, $sq)); |
|
46
|
|
|
$this->initialize(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function updateContext() |
|
51
|
|
|
{ |
|
52
|
|
|
$platform = $this->adapter->getPlatform()->getName(); |
|
53
|
|
|
|
|
54
|
|
|
if(isset($this->config['schema']) && $platform == 'MySQL'){ |
|
55
|
|
|
$sql = 'USE ' . $this->config['schema'] . ';'; |
|
56
|
|
|
$this->adapter->getDriver()->getConnection()->execute($sql); |
|
57
|
|
|
|
|
58
|
|
|
$sql = 'SET FOREIGN_KEY_CHECKS = FALSE;'; |
|
59
|
|
|
$this->adapter->getDriver()->getConnection()->execute($sql); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if(isset($this->config['schema']) && $platform == 'PostgreSQL'){ |
|
63
|
|
|
$sql = 'SET SCHEMA \'' . $this->config['schema'] . '\';'; |
|
64
|
|
|
$this->adapter->getDriver()->getConnection()->execute($sql); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|