Mark V
1: <?php
2: namespace MOC\V\Component\Database\Component\Bridge\Repository;
3:
4: use Doctrine\DBAL\Connection;
5: use Doctrine\DBAL\DriverManager;
6: use Doctrine\DBAL\Query\QueryBuilder;
7: use Doctrine\DBAL\Statement;
8: use MOC\V\Component\Database\Component\Bridge\Bridge;
9: use MOC\V\Component\Database\Component\Exception\ComponentException;
10: use MOC\V\Component\Database\Component\Exception\Repository\NoConnectionException;
11: use MOC\V\Component\Database\Component\IBridgeInterface;
12: use MOC\V\Component\Database\Component\Parameter\Repository\DatabaseParameter;
13: use MOC\V\Component\Database\Component\Parameter\Repository\DriverParameter;
14: use MOC\V\Component\Database\Component\Parameter\Repository\HostParameter;
15: use MOC\V\Component\Database\Component\Parameter\Repository\PasswordParameter;
16: use MOC\V\Component\Database\Component\Parameter\Repository\PortParameter;
17: use MOC\V\Component\Database\Component\Parameter\Repository\UsernameParameter;
18: use MOC\V\Core\AutoLoader\AutoLoader;
19:
20: /**
21: * Class Doctrine2DBAL
22: *
23: * @package MOC\V\Component\Database\Component\Bridge
24: */
25: class Doctrine2DBAL extends Bridge implements IBridgeInterface
26: {
27:
28: /** @var Connection $Connection */
29: private $Connection = null;
30:
31: /**
32: *
33: */
34: public function __construct()
35: {
36:
37: AutoLoader::getNamespaceAutoLoader('Doctrine\DBAL',
38: __DIR__.'/../../../Vendor/Doctrine2DBAL/2.5.0/lib');
39: AutoLoader::getNamespaceAutoLoader('Doctrine\Common',
40: __DIR__.'/../../../Vendor/Doctrine2Common/2.5.0/lib');
41: }
42:
43: /**
44: * @param UsernameParameter $Username
45: * @param PasswordParameter $Password
46: * @param DatabaseParameter $Database
47: * @param DriverParameter $Driver
48: * @param HostParameter $Host
49: * @param PortParameter $Port
50: *
51: * @param int $Timeout
52: *
53: * @return IBridgeInterface
54: * @throws ComponentException
55: */
56: public function registerConnection(
57: UsernameParameter $Username,
58: PasswordParameter $Password,
59: DatabaseParameter $Database,
60: DriverParameter $Driver,
61: HostParameter $Host,
62: PortParameter $Port,
63: $Timeout = 5
64: ) {
65:
66: try {
67: $Connection = DriverManager::getConnection(array(
68: 'driver' => $Driver->getDriver(),
69: 'user' => $Username->getUsername(),
70: 'password' => $Password->getPassword(),
71: 'host' => $Host->getHost(),
72: 'dbname' => $Database->getDatabase(),
73: 'port' => $Port->getPort(),
74: 'driverOptions' => array(
75: \PDO::ATTR_TIMEOUT => $Timeout,
76: \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
77: )
78: ));
79: } catch (\Exception $E) {
80: // @codeCoverageIgnoreStart
81: throw new ComponentException($E->getMessage(), $E->getCode(), $E);
82: // @codeCoverageIgnoreEnd
83: }
84:
85: try {
86: $Connection->connect();
87: } catch (\Exception $E) {
88: throw new ComponentException($E->getMessage(), $E->getCode(), $E);
89: }
90:
91: $this->Connection = $Connection;
92: return $this;
93: }
94:
95: /**
96: * WARNING: this may drop out with no replacement
97: *
98: * @return Connection
99: * @throws NoConnectionException
100: * @codeCoverageIgnore
101: */
102: public function getConnection()
103: {
104:
105: return $this->prepareConnection();
106: }
107:
108: /**
109: * @throws NoConnectionException
110: * @return Connection
111: */
112: private function prepareConnection()
113: {
114:
115: if (null === $this->Connection) {
116: // @codeCoverageIgnoreStart
117: throw new NoConnectionException();
118: // @codeCoverageIgnoreEnd
119: }
120: return $this->Connection;
121: }
122:
123: /**
124: * WARNING: this may drop out with no replacement
125: *
126: * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
127: * @throws NoConnectionException
128: * @codeCoverageIgnore
129: */
130: public function getSchemaManager()
131: {
132:
133: return $this->prepareConnection()->getSchemaManager();
134: }
135:
136: /**
137: * WARNING: this may drop out with no replacement
138: *
139: * @return QueryBuilder
140: * @throws NoConnectionException
141: * @codeCoverageIgnore
142: */
143: public function getQueryBuilder()
144: {
145:
146: return $this->prepareConnection()->createQueryBuilder();
147: }
148:
149: /**
150: * @return int
151: * @throws \Doctrine\DBAL\DBALException
152: */
153: public function executeWrite()
154: {
155:
156: $Query = $this->prepareQuery();
157: return $this->prepareConnection()->executeUpdate($Query[0], $Query[1], $Query[2]);
158: }
159:
160: /**
161: * @return array
162: */
163: private function prepareQuery()
164: {
165:
166: /** @var Statement $Statement */
167: $Statement = array_pop(self::$StatementList);
168: $ParameterCount = substr_count($Statement, '?');
169: $QueryValue = array();
170: $QueryType = array();
171: for ($Run = 0; $Run < $ParameterCount; $Run++) {
172: $Parameter = array_pop(self::$ParameterList);
173: array_unshift($QueryValue, $Parameter[0]);
174: array_unshift($QueryType, $Parameter[1]);
175: }
176: return array($Statement, $QueryValue, $QueryType);
177: }
178:
179: /**
180: * @return array
181: * @throws \Doctrine\DBAL\DBALException
182: */
183: public function executeRead()
184: {
185:
186: $Query = $this->prepareQuery();
187: return $this->prepareConnection()->executeQuery($Query[0], $Query[1], $Query[2])->fetchAll();
188: }
189: }
190: