|
| 1 | +<?php |
| 2 | +namespace Foolz\SphinxQL\Drivers; |
| 3 | + |
| 4 | +use Foolz\SphinxQL\Exception\ConnectionException; |
| 5 | +use Foolz\SphinxQL\Expression; |
| 6 | + |
| 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() |
| 68 | + { |
| 69 | + return $this->connection_params; |
| 70 | + } |
| 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() |
| 79 | + { |
| 80 | + if (!is_null($this->connection)) { |
| 81 | + return $this->connection; |
| 82 | + } |
| 83 | + |
| 84 | + throw new ConnectionException('The connection to the server has not been established yet.'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Wraps the input with identifiers when necessary. |
| 89 | + * |
| 90 | + * @param Expression|string $value The string to be quoted, or an Expression to leave it untouched |
| 91 | + * |
| 92 | + * @return Expression|string The untouched Expression or the quoted string |
| 93 | + */ |
| 94 | + public function quoteIdentifier($value) |
| 95 | + { |
| 96 | + if ($value instanceof Expression) { |
| 97 | + return $value->value(); |
| 98 | + } |
| 99 | + |
| 100 | + if ($value === '*') { |
| 101 | + return $value; |
| 102 | + } |
| 103 | + |
| 104 | + $pieces = explode('.', $value); |
| 105 | + |
| 106 | + foreach ($pieces as $key => $piece) { |
| 107 | + $pieces[$key] = '`'.$piece.'`'; |
| 108 | + } |
| 109 | + |
| 110 | + return implode('.', $pieces); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Calls $this->quoteIdentifier() on every element of the array passed. |
| 115 | + * |
| 116 | + * @param array $array An array of strings to be quoted |
| 117 | + * |
| 118 | + * @return array The array of quoted strings |
| 119 | + */ |
| 120 | + public function quoteIdentifierArr(Array $array = array()) |
| 121 | + { |
| 122 | + $result = array(); |
| 123 | + |
| 124 | + foreach ($array as $key => $item) { |
| 125 | + $result[$key] = $this->quoteIdentifier($item); |
| 126 | + } |
| 127 | + |
| 128 | + return $result; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Adds quotes around values when necessary. |
| 133 | + * Based on FuelPHP's quoting function. |
| 134 | + * |
| 135 | + * @param Expression|string $value The input string, eventually wrapped in an expression to leave it untouched |
| 136 | + * |
| 137 | + * @return Expression|string|int The untouched Expression or the quoted string |
| 138 | + */ |
| 139 | + public function quote($value) |
| 140 | + { |
| 141 | + if ($value === null) { |
| 142 | + return 'null'; |
| 143 | + } elseif ($value === true) { |
| 144 | + return "'1'"; |
| 145 | + } elseif ($value === false) { |
| 146 | + return "'0'"; |
| 147 | + } elseif ($value instanceof Expression) { |
| 148 | + // Use the raw expression |
| 149 | + return $value->value(); |
| 150 | + } elseif (is_int($value)) { |
| 151 | + return (int) $value; |
| 152 | + } elseif (is_float($value)) { |
| 153 | + // Convert to non-locale aware float to prevent possible commas |
| 154 | + return sprintf('%F', $value); |
| 155 | + } elseif (is_array($value)) { |
| 156 | + // Supports MVA attributes |
| 157 | + return '('.implode(',', $this->quoteArr($value)).')'; |
| 158 | + } |
| 159 | + |
| 160 | + return $this->escape($value); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Calls $this->quote() on every element of the array passed. |
| 165 | + * |
| 166 | + * @param array $array The array of strings to quote |
| 167 | + * |
| 168 | + * @return array The array of quotes strings |
| 169 | + */ |
| 170 | + public function quoteArr(Array $array = array()) |
| 171 | + { |
| 172 | + $result = array(); |
| 173 | + |
| 174 | + foreach ($array as $key => $item) { |
| 175 | + $result[$key] = $this->quote($item); |
| 176 | + } |
| 177 | + |
| 178 | + return $result; |
| 179 | + } |
| 180 | +} |
0 commit comments