Neevo Public API
  • Namespace
  • Class
  • Tree

Namespaces

  • Neevo
    • Cache
    • Drivers
    • Nette
  • PHP

Classes

  • BaseStatement
  • Connection
  • Literal
  • Manager
  • Parser
  • Result
  • ResultIterator
  • Row
  • Statement

Interfaces

  • DriverInterface
  • ObservableInterface
  • ObserverInterface

Exceptions

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