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  * Core Neevo class.
 15  * @author Martin Srank
 16  * @package Neevo
 17  */
 18 class Neevo implements INeevoObservable, INeevoObserver {
 19 
 20 
 21     /** @var string Default Neevo driver */
 22     public static $defaultDriver = 'mysqli';
 23 
 24     /** @var string */
 25     private $last;
 26 
 27     /** @var int */
 28     private $queries = 0;
 29 
 30     /** @var NeevoConnection */
 31     private $connection;
 32 
 33     /** @var NeevoObserverMap */
 34     private $observers;
 35 
 36 
 37     // Neevo version
 38     const VERSION = '1.3',
 39         REVISION = '@VCREV@ released on @VCDATE@';
 40 
 41     // Data types
 42     const BOOL = 'b',
 43         INT = 'i',
 44         FLOAT = 'f',
 45         TEXT = 's',
 46         BINARY = 'bin',
 47         DATETIME = 'd',
 48         ARR = 'a',
 49         LITERAL = 'l',
 50         IDENTIFIER = 'id',
 51         SUBQUERY = 'sub';
 52 
 53     // Statement types
 54     const STMT_SELECT = 'stmt_select',
 55         STMT_INSERT = 'stmt_insert',
 56         STMT_UPDATE = 'stmt_update',
 57         STMT_DELETE = 'stmt_delete';
 58 
 59     // JOIN types
 60     const JOIN_LEFT = 'join_left',
 61         JOIN_INNER = 'join_inner';
 62 
 63     // Order types
 64     const ASC = 'ASC',
 65         DESC = 'DESC';
 66 
 67 
 68     /**
 69      * Configure Neevo and establish a connection.
 70      * Configuration can be different - see the API for your driver.
 71      * @param mixed $config Connection configuration.
 72      * @param INeevoCache $cache Cache to use.
 73      * @return void
 74      * @throws NeevoException
 75      */
 76     public function __construct($config, INeevoCache $cache = null){
 77         $this->connection = new NeevoConnection($config, $cache);
 78         $this->observers = new NeevoObserverMap;
 79         $this->attachObserver($this, self::QUERY);
 80     }
 81 
 82 
 83     /*  ************  Statement factories  ************  */
 84 
 85 
 86     /**
 87      * SELECT statement factory.
 88      * @param string|array $columns Array or comma-separated list (optional)
 89      * @param string $table
 90      * @return NeevoResult fluent interface
 91      */
 92     public function select($columns = null, $table = null){
 93         $result = new NeevoResult($this->connection, $columns, $table);
 94         foreach($this->observers as $observer){
 95             $result->attachObserver($observer, $this->observers->getEvent());
 96         }
 97         return $result;
 98     }
 99 
100 
101     /**
102      * INSERT statement factory.
103      * @param string $table
104      * @param array $values
105      * @return NeevoStmt fluent interface
106      */
107     public function insert($table, array $values){
108         $statement = NeevoStmt::createInsert($this->connection, $table, $values);
109         foreach($this->observers as $observer){
110             $statement->attachObserver($observer, $this->observers->getEvent());
111         }
112         return $statement;
113     }
114 
115 
116     /**
117      * UPDATE statement factory.
118      * @param string $table
119      * @param array $data
120      * @return NeevoStmt fluent interface
121      */
122     public function update($table, array $data){
123         $statement = NeevoStmt::createUpdate($this->connection, $table, $data);
124         foreach($this->observers as $observer){
125             $statement->attachObserver($observer, $this->observers->getEvent());
126         }
127         return $statement;
128     }
129 
130 
131     /**
132      * DELETE statement factory.
133      * @param string $table
134      * @return NeevoStmt fluent interface
135      */
136     public function delete($table){
137         $statement = NeevoStmt::createDelete($this->connection, $table);
138         foreach($this->observers as $observer){
139             $statement->attachObserver($observer, $this->observers->getEvent());
140         }
141         return $statement;
142     }
143 
144 
145     /**
146      * Import a SQL dump from given file.
147      *
148      * Based on implementation in Nette\Database,
149      * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com), new BSD license.
150      * @copyright 2004, 2011 David Grudl
151      * @param string $filename
152      * @return int Number of executed commands
153      */
154     public function loadFile($filename){
155         $this->connection->connect();
156         $abort = ignore_user_abort();
157         @set_time_limit(0);
158         ignore_user_abort(true);
159 
160         $handle = @fopen($filename, 'r');
161         if($handle === false){
162             ignore_user_abort($abort);
163             throw new NeevoException("Cannot open file '$filename' for SQL import.");
164         }
165 
166         $sql = '';
167         $count = 0;
168         while(!feof($handle)){
169             $content = fgets($handle);
170             $sql .= $content;
171             if(substr(rtrim($content), -1) === ';'){
172                 // Passed directly to driver without logging.
173                 $this->connection->getDriver()->runQuery($sql);
174                 $sql = '';
175                 $count++;
176             }
177         }
178         fclose($handle);
179         ignore_user_abort($abort);
180         return $count;
181     }
182 
183 
184     /*  ************  Transactions  ************  */
185 
186 
187     /**
188      * Begin a transaction if supported.
189      * @param string $savepoint
190      * @return Neevo fluent interface
191      */
192     public function begin($savepoint = null){
193         $this->connection->getDriver()->beginTransaction($savepoint);
194         $this->notifyObservers(INeevoObserver::BEGIN);
195         return $this;
196     }
197 
198 
199     /**
200      * Commit statements in a transaction.
201      * @param string $savepoint
202      * @return Neevo fluent interface
203      */
204     public function commit($savepoint = null){
205         $this->connection->getDriver()->commit($savepoint);
206         $this->notifyObservers(INeevoObserver::COMMIT);
207         return $this;
208     }
209 
210 
211     /**
212      * Rollback changes in a transaction.
213      * @param string $savepoint
214      * @return Neevo fluent interface
215      */
216     public function rollback($savepoint = null){
217         $this->connection->getDriver()->rollback($savepoint);
218         $this->notifyObservers(INeevoObserver::ROLLBACK);
219         return $this;
220     }
221 
222 
223     /*  ************  Implementation of INeevoObservable & INeevoObserver  ************  */
224 
225 
226     /**
227      * Attach an observer for debugging.
228      * @param INeevoObserver $observer
229      * @param int $event Event to attach the observer to.
230      * @return void
231      */
232     public function attachObserver(INeevoObserver $observer, $event){
233         $this->observers->attach($observer, $event);
234         $this->connection->attachObserver($observer, $event);
235         $e = new NeevoException;
236         $e->attachObserver($observer, $event);
237     }
238 
239 
240     /**
241      * Detach given observer.
242      * @param INeevoObserver $observer
243      * @return void
244      */
245     public function detachObserver(INeevoObserver $observer){
246         $this->connection->detachObserver($observer);
247         $this->observers->detach($observer);
248         $e = new NeevoException;
249         $e->detachObserver($observer);
250     }
251 
252 
253     /**
254      * Notify all observers attached to given event.
255      * @param int $event
256      * @return void
257      */
258     public function notifyObservers($event){
259         foreach($this->observers as $observer){
260             if($event & $this->observers->getEvent())
261                 $observer->updateStatus($this, $event);
262         }
263     }
264 
265 
266     /**
267      * Receive update from observable.
268      * @param INeevoObservable $observable
269      * @param int $event Event type
270      * @return void
271      */
272     public function updateStatus(INeevoObservable $observable, $event){
273         $this->last = $observable->__toString();
274         ++$this->queries;
275     }
276 
277 
278     /**
279      * Current NeevoConnection instance.
280      * @return NeevoConnection
281      */
282     public function getConnection(){
283         return $this->connection;
284     }
285 
286 
287     /**
288      * Last executed query.
289      * @return string
290      */
291     public function getLast(){
292         return $this->last;
293     }
294 
295 
296     /**
297      * Get number of executed queries.
298      * @return int
299      */
300     public function getQueries(){
301         return $this->queries;
302     }
303 
304 
305     /**
306      * Highlight given SQL code.
307      * @param string $sql
308      * @return string
309      */
310     public static function highlightSql($sql){
311         $keywords1 = 'SELECT|UPDATE|INSERT\s+INTO|DELETE|FROM|VALUES|SET|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|(?:LEFT |RIGHT |INNER )?JOIN';
312         $keywords2 = 'RANDOM|RAND|ASC|DESC|USING|AND|OR|ON|IN|IS|NOT|NULL|LIKE|TRUE|FALSE|AS';
313 
314         $sql = str_replace("\\'", '\\&#39;', $sql);
315         $sql = preg_replace_callback("~(/\\*.*\\*/)|($keywords1)|($keywords2)|('[^']+'|[0-9]+)~", array('Neevo', '_highlightCallback'), $sql);
316         $sql = str_replace('\\&#39;', "\\'", $sql);
317         return '<pre style="color:#555" class="sql-dump">' . trim($sql) . "</pre>\n";
318     }
319 
320 
321     private static function _highlightCallback($match){
322         // Comment
323         if(!empty($match[1]))
324             return '<em style="color:#999">' . $match[1] . '</em>';
325         // Basic keywords
326         if(!empty($match[2]))
327             return "\n" . '<strong style="color:#e71818">' . $match[2] . '</strong>';
328         // Other keywords
329         if(!empty($match[3]))
330             return '<strong style="color:#d59401">' . $match[3] . '</strong>';
331         // Values
332         if(!empty($match[4]) || $match[4] === '0')
333             return '<em style="color:#008000">' . $match[4] . '</em>';
334     }
335 
336 
337 }
338 
339 
340 
341 /**
342  * Representation of SQL literal.
343  * @author Martin Srank
344  * @package Neevo
345  */
346 class NeevoLiteral {
347 
348 
349     /** @var string */
350     public $value;
351 
352 
353     /**
354      * Create instance of SQL literal.
355      * @param string $value
356      * @return void
357      */
358     public function __construct($value) {
359         $this->value = $value;
360     }
361 
362 
363 }
364 
Neevo Public API API documentation generated by ApiGen 2.8.0