Neevo Public API
  • Namespace
  • Class
  • Tree

Namespaces

  • Neevo
    • Nette
  • None
  • PHP

Classes

  • Neevo
  • NeevoBaseStmt
  • NeevoCacheFile
  • NeevoCacheMemcache
  • NeevoCacheMemory
  • NeevoCacheSession
  • NeevoConnection
  • NeevoDriverMySQL
  • NeevoDriverMySQLi
  • NeevoDriverPgSQL
  • NeevoDriverSQLite2
  • NeevoDriverSQLite3
  • NeevoLiteral
  • NeevoLoader
  • NeevoObserverMap
  • NeevoParser
  • NeevoResult
  • NeevoResultIterator
  • NeevoRow
  • NeevoStmt

Interfaces

  • INeevoCache
  • INeevoDriver
  • INeevoObservable
  • INeevoObserver

Exceptions

  • NeevoDriverException
  • NeevoException
  • NeevoImplementationException
  1 <?php
  2 /**
  3  * Neevo - Tiny database layer for PHP. (http://neevo.smasty.net)
  4  *
  5  * This source file is subject to the MIT license that is bundled
  6  * with this package in the file license.txt.
  7  *
  8  * Copyright (c) 2011 Martin Srank (http://smasty.net)
  9  *
 10  */
 11 
 12 
 13 /**
 14  * Neevo MySQLi driver (PHP extension 'mysqli')
 15  *
 16  * Driver configuration:
 17  *  - host => MySQL server name or address
 18  *  - port (int) => MySQL server port
 19  *  - socket
 20  *  - username
 21  *  - password
 22  *  - database => database to select
 23  *  - charset => Character encoding to set (defaults to utf8)
 24  *  - peristent (bool) => Try to find a persistent link
 25  *  - unbuffered (bool) => Sends query without fetching and buffering the result
 26  *
 27  *  - resource (instance of mysqli) => Existing MySQLi link
 28  *  - lazy, table_prefix... => see NeevoConnection
 29  *
 30  * @author Martin Srank
 31  * @package Neevo\Drivers
 32  */
 33 class NeevoDriverMySQLi extends NeevoParser implements INeevoDriver {
 34 
 35 
 36     /** @var mysqli_result */
 37     private $resource;
 38 
 39     /** @var bool */
 40     private $unbuffered;
 41 
 42     /** @var int */
 43     private $affectedRows;
 44 
 45 
 46     /**
 47      * Check for required PHP extension.
 48      * @return void
 49      * @throws NeevoDriverException
 50      */
 51     public function __construct(NeevoBaseStmt $statement = null){
 52         if(!extension_loaded("mysqli"))
 53             throw new NeevoDriverException("Cannot instantiate Neevo MySQLi driver - PHP extension 'mysqli' not loaded.");
 54         if($statement instanceof NeevoBaseStmt)
 55             parent::__construct($statement);
 56     }
 57 
 58 
 59     /**
 60      * Create connection to database.
 61      * @param array $config Configuration options
 62      * @return void
 63      * @throws NeevoException
 64      */
 65     public function connect(array $config){
 66 
 67         // Defaults
 68         $defaults = array(
 69             'resource' => null,
 70             'charset' => 'utf8',
 71             'username' => ini_get('mysqli.default_user'),
 72             'password' => ini_get('mysqli.default_pw'),
 73             'socket' => ini_get('mysqli.default_socket'),
 74             'port' => ini_get('mysqli.default_port'),
 75             'host' => ini_get('mysqli.default_host'),
 76             'persistent' => false,
 77             'unbuffered' => false
 78         );
 79 
 80         $config += $defaults;
 81 
 82         // Connect
 83         if($config['resource'] instanceof mysqli)
 84             $this->resource = $config['resource'];
 85         else
 86             $this->resource = new mysqli($config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket']);
 87 
 88         if($this->resource->connect_errno)
 89             throw new NeevoException($this->resource->connect_error, $this->resource->connect_errno);
 90 
 91         // Set charset
 92         if($this->resource instanceof mysqli){
 93             $ok = @$this->resource->set_charset($config['charset']);
 94             if(!$ok) $this->runQuery("SET NAMES ".$config['charset']);
 95         }
 96 
 97         $this->unbuffered = $config['unbuffered'];
 98     }
 99 
100 
101     /**
102      * Close the connection.
103      * @return void
104      */
105     public function closeConnection(){
106         @$this->resource->close();
107     }
108 
109 
110     /**
111      * Free memory used by given result set.
112      * @param mysqli_result $resultSet
113      * @return bool
114      */
115     public function freeResultSet($resultSet){
116         return true;
117     }
118 
119 
120     /**
121      * Execute given SQL statement.
122      * @param string $queryString
123      * @return mysqli_result|bool
124      * @throws NeevoException
125      */
126     public function runQuery($queryString){
127 
128         $this->affectedRows = false;
129         $result = $this->resource->query($queryString, $this->unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
130 
131         $error = str_replace('You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use', 'Syntax error', $this->resource->error);
132         if($error && $result === false)
133             throw new NeevoException("Query failed. $error", $this->resource->errno, $queryString);
134 
135         $this->affectedRows = $this->resource->affected_rows;
136         return $result;
137     }
138 
139 
140     /**
141      * Begin a transaction if supported.
142      * @param string $savepoint
143      * @return void
144      */
145     public function beginTransaction($savepoint = null){
146         $this->runQuery($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
147     }
148 
149 
150     /**
151      * Commit statements in a transaction.
152      * @param string $savepoint
153      * @return void
154      */
155     public function commit($savepoint = null){
156         $this->runQuery($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
157     }
158 
159 
160     /**
161      * Rollback changes in a transaction.
162      * @param string $savepoint
163      * @return void
164      */
165     public function rollback($savepoint = null){
166         $this->runQuery($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
167     }
168 
169 
170     /**
171      * Fetch row from given result set as an associative array.
172      * @param mysqli_result $resultSet
173      * @return array
174      */
175     public function fetch($resultSet){
176         return $resultSet->fetch_assoc();
177     }
178 
179 
180     /**
181      * Move internal result pointer.
182      * @param mysqli_result $resultSet
183      * @param int
184      * @return bool
185      * @throws NeevoDriverException
186      */
187     public function seek($resultSet, $offset){
188         if($this->unbuffered)
189             throw new NeevoDriverException('Cannot seek on unbuffered result.');
190         return $resultSet->data_seek($offset);
191     }
192 
193 
194     /**
195      * Get the ID generated in the INSERT statement.
196      * @return int
197      */
198     public function getInsertId(){
199         return $this->resource->insert_id;
200     }
201 
202 
203     /**
204      * Randomize result order.
205      * @param NeevoBaseStmt $statement
206      * @return void
207      */
208     public function randomizeOrder(NeevoBaseStmt $statement){
209         $statement->order('RAND()');
210     }
211 
212 
213     /**
214      * Get the number of rows in the given result set.
215      * @param mysqli_result $resultSet
216      * @return int|FALSE
217      * @throws NeevoDriverException
218      */
219     public function getNumRows($resultSet){
220         if($this->unbuffered)
221             throw new NeevoDriverException('Cannot seek on unbuffered result.');
222         if($resultSet instanceof mysqli_result)
223             return $resultSet->num_rows;
224         return false;
225     }
226 
227 
228     /**
229      * Get the number of affected rows in previous operation.
230      * @return int
231      */
232     public function getAffectedRows(){
233         return $this->affectedRows;
234     }
235 
236 
237     /**
238      * Escape given value.
239      * @param mixed $value
240      * @param string $type
241      * @return mixed
242      * @throws InvalidArgumentException
243      */
244     public function escape($value, $type){
245         switch($type){
246             case Neevo::BOOL:
247                 return $value ? 1 :0;
248 
249             case Neevo::TEXT:
250                 return "'". $this->resource->real_escape_string($value) ."'";
251 
252             case Neevo::IDENTIFIER:
253                 return str_replace('`*`', '*', '`' . str_replace('.', '`.`', str_replace('`', '``', $value)) . '`');
254 
255             case Neevo::BINARY:
256                 return "_binary'" . mysqli_real_escape_string($this->resource, $value) . "'";
257 
258             case Neevo::DATETIME:
259                 return ($value instanceof DateTime) ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
260 
261             default:
262                 throw new InvalidArgumentException('Unsupported data type.');
263                 break;
264         }
265     }
266 
267 
268     /**
269      * Decode given value.
270      * @param mixed $value
271      * @param string $type
272      * @return mixed
273      * @throws InvalidArgumentException
274      */
275     public function unescape($value, $type){
276         if($type === Neevo::BINARY)
277             return $value;
278         throw new InvalidArgumentException('Unsupported data type.');
279     }
280 
281 
282     /**
283      * Get the PRIMARY KEY column for given table.
284      * @param string $table
285      * @return string
286      */
287     public function getPrimaryKey($table){
288         $key = '';
289         $q = $this->runQuery('SHOW FULL COLUMNS FROM '.$table);
290         while($col = $this->fetch($q)){
291             if(strtolower($col['Key']) === 'pri' && $key === '')
292                 $key = $col['Field'];
293         }
294         return $key;
295     }
296 
297 
298     /**
299      * Get types of columns in given result set.
300      * @param mysqli_result $resultset
301      * @param string $table
302      * @return array
303      */
304     public function getColumnTypes($resultSet, $table){
305         static $colTypes;
306         if(empty($colTypes)){
307             $constants = get_defined_constants(true);
308             foreach($constants['mysqli'] as $type => $code){
309                 if(strncmp($type, 'MYSQLI_TYPE_', 12) === 0)
310                     $colTypes[$code] = strtolower(substr($type, 12));
311             }
312             $colTypes[MYSQLI_TYPE_LONG] = $colTypes[MYSQLI_TYPE_SHORT] = $colTypes[MYSQLI_TYPE_TINY] = 'int';
313         }
314 
315         $cols = array();
316         while($field = $resultSet->fetch_field()){
317             $cols[$field->name] = $colTypes[$field->type];
318         }
319         return $cols;
320     }
321 
322 
323     /*  ************  NeevoParser overrides  ************  */
324 
325 
326     /**
327      * Parse UPDATE statement.
328      * @return string
329      */
330     protected function parseUpdateStmt(){
331         $sql = parent::parseUpdateStmt();
332         return $this->applyLimit($sql . $this->clauses[3]);
333     }
334 
335 
336     /**
337      * Parse DELETE statement.
338      * @return string
339      */
340     protected function parseDeleteStmt(){
341         $sql = parent::parseDeleteStmt();
342         return $this->applyLimit($sql . $this->clauses[3]);
343     }
344 
345 
346 }
347 
Neevo Public API API documentation generated by ApiGen 2.8.0