1 | <?php |
||
7 | abstract class ConnectionBase implements ConnectionInterface |
||
8 | { |
||
9 | /** |
||
10 | * The connection parameters for the database server. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $connection_params = array('host' => '127.0.0.1', 'port' => 9306, 'socket' => null); |
||
15 | |||
16 | /** |
||
17 | * Internal connection object. |
||
18 | */ |
||
19 | protected $connection = null; |
||
20 | |||
21 | /** |
||
22 | * Sets one or more connection parameters. |
||
23 | * |
||
24 | * @param array $params Associative array of parameters and values. |
||
25 | */ |
||
26 | public function setParams(Array $params) |
||
27 | { |
||
28 | foreach ($params as $param => $value) { |
||
29 | $this->setParam($param, $value); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Set a single connection parameter. Valid parameters include: |
||
35 | * |
||
36 | * * string host - The hostname, IP address, or unix socket |
||
37 | * * int port - The port to the host |
||
38 | * * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2) |
||
39 | * |
||
40 | * @param string $param Name of the parameter to modify. |
||
41 | * @param mixed $value Value to which the parameter will be set. |
||
42 | */ |
||
43 | public function setParam($param, $value) |
||
44 | { |
||
45 | if ($param === 'host') { |
||
46 | if ($value === 'localhost') { |
||
47 | $value = '127.0.0.1'; |
||
48 | } elseif (stripos($value, 'unix:') === 0) { |
||
49 | $param = 'socket'; |
||
50 | } |
||
51 | } |
||
52 | if ($param === 'socket') { |
||
53 | if (stripos($value, 'unix:') === 0) { |
||
54 | $value = substr($value, 5); |
||
55 | } |
||
56 | $this->connection_params['host'] = null; |
||
57 | } |
||
58 | |||
59 | $this->connection_params[$param] = $value; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns the connection parameters (host, port, connection timeout) for the current instance. |
||
64 | * |
||
65 | * @return array $params The current connection parameters |
||
66 | */ |
||
67 | public function getParams() |
||
71 | |||
72 | /** |
||
73 | * Returns the current connection established. |
||
74 | * |
||
75 | * @return object Internal connection object |
||
76 | * @throws ConnectionException If no connection has been established or open |
||
77 | */ |
||
78 | public function getConnection() |
||
86 | |||
87 | /** |
||
88 | * Adds quotes around values when necessary. |
||
89 | * Based on FuelPHP's quoting function. |
||
90 | * |
||
91 | * @param Expression|string $value The input string, eventually wrapped in an expression to leave it untouched |
||
92 | * |
||
93 | * @return Expression|string|int The untouched Expression or the quoted string |
||
94 | */ |
||
95 | public function quote($value) |
||
118 | |||
119 | /** |
||
120 | * Calls $this->quote() on every element of the array passed. |
||
121 | * |
||
122 | * @param array $array The array of strings to quote |
||
123 | * |
||
124 | * @return array The array of quotes strings |
||
125 | */ |
||
126 | public function quoteArr(Array $array = array()) |
||
136 | } |
||
137 |