1 | <?php |
||
44 | class Connection implements ConnectionInterface |
||
45 | { |
||
46 | /** |
||
47 | * Connection constructor. |
||
48 | * |
||
49 | * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the |
||
50 | * database. In general, a DSN consists of the PDO driver name, followed by a colon, |
||
51 | * followed by the PDO driver-specific connection syntax. See the docs |
||
52 | * for<b>\PDO::__construct()</b> for full details. |
||
53 | * @param string|null $username The user name for the DSN string. This parameter is optional for some PDO drivers. |
||
54 | * Example: SqlLite since it just a file in a directory somewhere it doesn't need |
||
55 | * this. |
||
56 | * @param string|null $password The password for the DSN string. This parameter is optional for some PDO drivers. |
||
57 | * Example: SqlLite since it just a file in a directory somewhere it doesn't need |
||
58 | * this. |
||
59 | * @param array|null $options A key=>value array of driver-specific connection options. |
||
60 | * |
||
61 | * @throws \PDOException The \PDO constructor always throws an exception even if you are setting it to use errors |
||
62 | * instead. That setting will only effect any of the follow interacts with \PDO. |
||
63 | * @see \PDO |
||
64 | */ |
||
65 | public function __construct(string $dsn, string $username = null, string $password = null, array $options = null) |
||
71 | /** |
||
72 | * Allow any other underlying \PDO methods be called without having to wrap them all. |
||
73 | * |
||
74 | * The wrapper methods of this class are expected to cover what Yapeal-ng itself uses from \PDO but allows the rest |
||
75 | * as well through this method. |
||
76 | * |
||
77 | * @param string $name Name of any \PDO methods not defined above. |
||
78 | * @param array $arguments Method arguments as a name=>value array. |
||
79 | * |
||
80 | * @return mixed |
||
81 | * @throws \BadMethodCallException |
||
82 | */ |
||
83 | public function __call(string $name, array $arguments) |
||
94 | /** |
||
95 | * Initiates a transaction. |
||
96 | * |
||
97 | * @return bool <b>true</b> on success or may return <b>false</b> or emits <b>\PDOException</b> (depending on error |
||
98 | * handling). |
||
99 | * @throws \PDOException |
||
100 | */ |
||
101 | public function beginTransaction(): bool |
||
105 | /** |
||
106 | * Commits a transaction. |
||
107 | * |
||
108 | * @return bool <b>true</b> on success or may return <b>false</b> or emits <b>\PDOException</b> (depending on error |
||
109 | * handling). |
||
110 | * @throws \PDOException |
||
111 | */ |
||
112 | public function commit(): bool |
||
116 | /** |
||
117 | * Execute an SQL statement and return the number of affected rows. |
||
118 | * |
||
119 | * Wrapper to allow proper type hinting. |
||
120 | * |
||
121 | * @param string $statement This must be a valid SQL statement for the target database server. This can _not_ be |
||
122 | * used with anything that returns a result set like SELECT etc. If you need that use |
||
123 | * <b>\PDO::query</b> or preferably <b>\PDO::prepare</b> and <b>\PDOStatement:execute</b> |
||
124 | * for better safety and improved repeated query handling. |
||
125 | * |
||
126 | * @return int|false <b>\PDO::exec</b> returns the number of rows that were modified or deleted by the SQL statement |
||
127 | * you issued. If no rows were affected, <b>\PDO::exec</b> returns 0. <b>\PDO::exec<b> may return <b>false<b> or |
||
128 | * emits <b>\PDOException</b> (depending on error handling). |
||
129 | * @throws \PDOException |
||
130 | */ |
||
131 | public function exec(string $statement) |
||
135 | /** |
||
136 | * Checks if inside a transaction. |
||
137 | * |
||
138 | * @return bool <b>true</b> if a transaction is currently active, and <b>false</b> if not. |
||
139 | */ |
||
140 | public function inTransaction(): bool |
||
144 | /** |
||
145 | * Flag to indicate if non-wrapper methods of underlying \PDO connection can be used. |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function isExposingPdo(): bool |
||
153 | /** |
||
154 | * Flag to mark if the under-laying PDO driver has already been set to it's most SQL-92 compatible mode or not. |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isSql92Mode(): bool |
||
162 | /** |
||
163 | * Prepares a statement for execution and returns a statement object. |
||
164 | * |
||
165 | * @param string $statement This must be a valid SQL statement for the target database server. |
||
166 | * @param array $driver_options This array holds one or more key=>value pairs to set attribute values for the |
||
167 | * <b>\PDOStatement</b> object that this method returns. You would most commonly |
||
168 | * use this to set the <b>\PDO::ATTR_CURSOR</b> value to <b>\PDO::CURSOR_SCROLL</b> |
||
169 | * to request a scrollable cursor. Some drivers have driver specific options that may |
||
170 | * be set at prepare-time. |
||
171 | * |
||
172 | * @return \PDOStatement|false If the database server successfully prepares the statement, <b>\PDO::prepare</b> |
||
173 | * returns a <b>\PDOStatement</b> object. If the database server cannot successfully prepare the statement, |
||
174 | * <b>\PDO::prepare</b> returns <b>false</b> or emits <b>\PDOException</b> (depending on error handling). |
||
175 | * @throws \PDOException |
||
176 | */ |
||
177 | public function prepare(string $statement, array $driver_options = []) |
||
181 | /** |
||
182 | * Executes an SQL statement, returning a result set as a PDOStatement object. |
||
183 | * |
||
184 | * Wrapper do to the reasons given in NOTE. |
||
185 | * |
||
186 | * __NOTE:__ |
||
187 | * According to PHP this method doesn't accept any parameters including $statement which would make it kind of |
||
188 | * pointless. After take a look at the code for PDO I believe it has to do with a bug in how they try forcing an |
||
189 | * error for the no parameters case but it's some what unclear what's going on really. I'm going to give it |
||
190 | * something cleaner here and deal with it in Connection class wrapper. |
||
191 | * |
||
192 | * @param string $statement The SQL statement to prepare and execute. |
||
193 | * @param int $mode The fetch mode must be one of the \PDO::FETCH_* constants. |
||
194 | * @param mixed $arguments The second and following parameters are the same as the parameters for |
||
195 | * \PDOStatement::setFetchMode. |
||
196 | * |
||
197 | * @return false|\PDOStatement <b>\PDO::query</b> returns a PDOStatement object, or <b>\PDO::query</b> may return |
||
198 | * <b>false</b> or emits <b>\PDOException</b> (depending on error handling). |
||
199 | * @throws \PDOException |
||
200 | * @see \PDOStatement::setFetchMode For a full description of the second and following parameters. |
||
201 | */ |
||
202 | public function query(string $statement, int $mode = \PDO::FETCH_BOTH, ...$arguments) |
||
209 | /** |
||
210 | * Rolls back a transaction. |
||
211 | * |
||
212 | * @return bool <b>true</b> on success or may return <b>false</b> or emits <b>\PDOException</b> (depending on error |
||
213 | * handling). |
||
214 | * @throws \PDOException |
||
215 | */ |
||
216 | public function rollBack(): bool |
||
220 | /** |
||
221 | * Set an attribute. |
||
222 | * |
||
223 | * @param int $attribute |
||
224 | * @param mixed $value |
||
225 | * |
||
226 | * @return bool <b>true</b> on success or may return <b>false</b> or emits <b>\PDOException</b> (depending on error |
||
227 | * handling). |
||
228 | * @throws \PDOException |
||
229 | */ |
||
230 | public function setAttribute(int $attribute, $value): bool |
||
234 | /** |
||
235 | * Used to decide if method pass through to the underlying \PDO connect are allowed for non-wrapper methods. |
||
236 | * |
||
237 | * @param bool $value |
||
238 | * |
||
239 | * @return self Fluent interface. |
||
240 | */ |
||
241 | public function setExposingPdo(bool $value = false): self |
||
246 | /** |
||
247 | * @param bool $value |
||
248 | * |
||
249 | * @return self Fluent interface |
||
250 | */ |
||
251 | public function setSql92Mode(bool $value = true): self |
||
256 | /** |
||
257 | * @var bool $exposingPdo |
||
258 | */ |
||
259 | private $exposingPdo; |
||
260 | /** |
||
261 | * @var \PDO $pdo |
||
262 | */ |
||
263 | private $pdo; |
||
264 | /** |
||
265 | * @var bool $sql92Mode |
||
266 | */ |
||
267 | private $sql92Mode; |
||
268 | } |
||
269 | |||
270 |