1 | <?php |
||
13 | abstract class PdoConfiguration implements DbInterface, Logger\LoggerInterface |
||
14 | { |
||
15 | use Logger\LoggerTrait; |
||
16 | |||
17 | private $settings = array(); |
||
18 | private $fetchMode = \PDO::FETCH_ASSOC; |
||
19 | private $instance = null; |
||
20 | |||
21 | /** |
||
22 | * @return string |
||
23 | */ |
||
24 | 3 | protected function getLoggerName() |
|
28 | |||
29 | /** |
||
30 | * @param array $params |
||
31 | */ |
||
32 | 44 | public function __construct($params = array()) |
|
36 | |||
37 | /** |
||
38 | * Database to connect to |
||
39 | * @return string|null |
||
40 | */ |
||
41 | 4 | protected function getDatabase() |
|
45 | |||
46 | /** |
||
47 | * Host to connect to |
||
48 | * @return string |
||
49 | */ |
||
50 | 6 | protected function getHost() |
|
54 | |||
55 | /** |
||
56 | * Dsn Driver to use |
||
57 | * @return string |
||
58 | */ |
||
59 | 5 | protected function getDsnDriver() |
|
63 | |||
64 | /** |
||
65 | * Retrieve settings |
||
66 | * @param null $param |
||
67 | * @return array|null |
||
68 | */ |
||
69 | 7 | protected function getSettings($param = null) |
|
73 | |||
74 | /** |
||
75 | * Options for PDO Settings |
||
76 | * @return array |
||
77 | */ |
||
78 | 7 | protected function getOptions() |
|
79 | { |
||
80 | return array( |
||
81 | 7 | \PDO::ATTR_PERSISTENT => (bool)( |
|
82 | 7 | isset($this->settings['PDOBddPersistant']) ? $this->settings['PDOBddPersistant'] : false |
|
83 | ), |
||
84 | 7 | \PDO::ATTR_EMULATE_PREPARES => true, |
|
85 | 7 | \PDO::MYSQL_ATTR_LOCAL_INFILE => true, |
|
86 | ); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Login to use |
||
91 | * @return string |
||
92 | */ |
||
93 | 6 | protected function getLogin() |
|
97 | |||
98 | /** |
||
99 | * Password to use |
||
100 | * @return string |
||
101 | */ |
||
102 | 6 | protected function getPassword() |
|
106 | |||
107 | /** |
||
108 | * Get string for dsn construction |
||
109 | * @return string |
||
110 | */ |
||
111 | 4 | protected function getDsn() |
|
112 | { |
||
113 | 4 | $driver = $this->getDsnDriver(); |
|
114 | 4 | $host = $this->getHost(); |
|
115 | 4 | $database = $this->getDatabase(); |
|
116 | 4 | $dsn = $driver . ":host=" . $host; |
|
117 | 4 | if (!is_null($database)) { |
|
118 | 1 | $dsn .= ";dbname=" . $database; |
|
119 | } |
||
120 | 4 | return $dsn; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return int |
||
125 | */ |
||
126 | 3 | public function getFetchMode() |
|
130 | |||
131 | /** |
||
132 | * @param int $fetchMode |
||
133 | * @return $this |
||
134 | */ |
||
135 | 1 | public function setFetchMode($fetchMode = \PDO::FETCH_ASSOC) |
|
143 | |||
144 | /** |
||
145 | * @param string $sql |
||
146 | * @return bool |
||
147 | * @throws Exception |
||
148 | */ |
||
149 | 2 | public function rawExecute($sql) |
|
159 | |||
160 | /** |
||
161 | * Fetch a row for a given statement |
||
162 | * @param object $statement |
||
163 | * @param int $cursorOrientation Cursor orientation (next by default) |
||
164 | * @param int $cursorOffset Cursor offset (0 by default) |
||
165 | * @return array |
||
166 | * @throws Exception |
||
167 | */ |
||
168 | 3 | public function fetchRow($statement, $cursorOrientation = self::CURSOR_NEXT, $cursorOffset = 0) |
|
182 | |||
183 | /** |
||
184 | * @return \PDO |
||
185 | * @throws Exception |
||
186 | */ |
||
187 | 7 | public function getDriver() |
|
205 | |||
206 | /** |
||
207 | * @param \Pdo $instance |
||
208 | * @return $this |
||
209 | */ |
||
210 | abstract protected function defaultConfiguration(\Pdo $instance); |
||
211 | |||
212 | /** |
||
213 | * @param string $dsn |
||
214 | * @param string $login |
||
215 | * @param string $password |
||
216 | * @param array $options |
||
217 | * @return \PDO |
||
218 | */ |
||
219 | 1 | protected function getPdo($dsn, $login = null, $password = null, array $options = null) |
|
223 | |||
224 | /** |
||
225 | * Force reconnection |
||
226 | * @return $this |
||
227 | */ |
||
228 | 1 | public function forceReconnect() |
|
233 | } |
||
234 |