Packages

  • dibi
    • drivers
    • nette
    • reflection
  • None
  • PHP

Classes

  • dibi
  • DibiConnection
  • DibiDataSource
  • DibiDateTime
  • DibiEvent
  • DibiFileLogger
  • DibiFirePhpLogger
  • DibiFluent
  • DibiObject
  • DibiResult
  • DibiResultIterator
  • DibiRow
  • DibiTranslator

Interfaces

  • IDataSource
  • IDibiDriver
  • IDibiReflector
  • IDibiResultDriver

Exceptions

  • DibiDriverException
  • DibiException
  • DibiNotImplementedException
  • DibiNotSupportedException
  • DibiPcreException
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * dibi - smart database abstraction layer (http://dibiphp.com)
  5:  *
  6:  * Copyright (c) 2005, 2012 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: 
 13: /**
 14:  * Check PHP configuration.
 15:  */
 16: if (version_compare(PHP_VERSION, '5.2.0', '<')) {
 17:     throw new Exception('dibi needs PHP 5.2.0 or newer.');
 18: }
 19: 
 20: @set_magic_quotes_runtime(FALSE); // intentionally @
 21: 
 22: 
 23: 
 24: require_once dirname(__FILE__) . '/libs/interfaces.php';
 25: require_once dirname(__FILE__) . '/libs/DibiDateTime.php';
 26: require_once dirname(__FILE__) . '/libs/DibiObject.php';
 27: require_once dirname(__FILE__) . '/libs/DibiLiteral.php';
 28: require_once dirname(__FILE__) . '/libs/DibiHashMap.php';
 29: require_once dirname(__FILE__) . '/libs/DibiException.php';
 30: require_once dirname(__FILE__) . '/libs/DibiConnection.php';
 31: require_once dirname(__FILE__) . '/libs/DibiResult.php';
 32: require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
 33: require_once dirname(__FILE__) . '/libs/DibiRow.php';
 34: require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
 35: require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
 36: require_once dirname(__FILE__) . '/libs/DibiFluent.php';
 37: require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php';
 38: require_once dirname(__FILE__) . '/libs/DibiEvent.php';
 39: require_once dirname(__FILE__) . '/libs/DibiFileLogger.php';
 40: require_once dirname(__FILE__) . '/libs/DibiFirePhpLogger.php';
 41: if (interface_exists('Nette\Diagnostics\IBarPanel') || interface_exists('IBarPanel')) {
 42:     require_once dirname(__FILE__) . '/Nette/DibiNettePanel.php';
 43: }
 44: 
 45: 
 46: 
 47: 
 48: 
 49: /**
 50:  * Interface for database drivers.
 51:  *
 52:  * This class is static container class for creating DB objects and
 53:  * store connections info.
 54:  *
 55:  * @author     David Grudl
 56:  * @package    dibi
 57:  */
 58: class dibi
 59: {
 60:     /** column type */
 61:     const TEXT = 's', // as 'string'
 62:         BINARY = 'bin',
 63:         BOOL = 'b',
 64:         INTEGER = 'i',
 65:         FLOAT = 'f',
 66:         DATE = 'd',
 67:         DATETIME = 't',
 68:         TIME = 't';
 69: 
 70:     const IDENTIFIER = 'n';
 71: 
 72:     /** @deprecated */
 73:     const FIELD_TEXT = dibi::TEXT,
 74:         FIELD_BINARY = dibi::BINARY,
 75:         FIELD_BOOL = dibi::BOOL,
 76:         FIELD_INTEGER = dibi::INTEGER,
 77:         FIELD_FLOAT = dibi::FLOAT,
 78:         FIELD_DATE = dibi::DATE,
 79:         FIELD_DATETIME = dibi::DATETIME,
 80:         FIELD_TIME = dibi::TIME;
 81: 
 82:     /** version */
 83:     const VERSION = '1.5-rc2',
 84:         REVISION = 'b964887 released on 2012-01-12';
 85: 
 86:     /** sorting order */
 87:     const ASC = 'ASC',
 88:         DESC = 'DESC';
 89: 
 90:     /** @var DibiConnection[]  Connection registry storage for DibiConnection objects */
 91:     private static $registry = array();
 92: 
 93:     /** @var DibiConnection  Current connection */
 94:     private static $connection;
 95: 
 96:     /** @var array  @see addHandler */
 97:     private static $handlers = array();
 98: 
 99:     /** @var string  Last SQL command @see dibi::query() */
100:     public static $sql;
101: 
102:     /** @var int  Elapsed time for last query */
103:     public static $elapsedTime;
104: 
105:     /** @var int  Elapsed time for all queries */
106:     public static $totalTime;
107: 
108:     /** @var int  Number or queries */
109:     public static $numOfQueries = 0;
110: 
111:     /** @var string  Default dibi driver */
112:     public static $defaultDriver = 'mysql';
113: 
114: 
115: 
116:     /**
117:      * Static class - cannot be instantiated.
118:      */
119:     final public function __construct()
120:     {
121:         throw new LogicException("Cannot instantiate static class " . get_class($this));
122:     }
123: 
124: 
125: 
126:     /********************* connections handling ****************d*g**/
127: 
128: 
129: 
130:     /**
131:      * Creates a new DibiConnection object and connects it to specified database.
132:      * @param  mixed   connection parameters
133:      * @param  string  connection name
134:      * @return DibiConnection
135:      * @throws DibiException
136:      */
137:     public static function connect($config = array(), $name = 0)
138:     {
139:         return self::$connection = self::$registry[$name] = new DibiConnection($config, $name);
140:     }
141: 
142: 
143: 
144:     /**
145:      * Disconnects from database (doesn't destroy DibiConnection object).
146:      * @return void
147:      */
148:     public static function disconnect()
149:     {
150:         self::getConnection()->disconnect();
151:     }
152: 
153: 
154: 
155:     /**
156:      * Returns TRUE when connection was established.
157:      * @return bool
158:      */
159:     public static function isConnected()
160:     {
161:         return (self::$connection !== NULL) && self::$connection->isConnected();
162:     }
163: 
164: 
165: 
166:     /**
167:      * Retrieve active connection.
168:      * @param  string   connection registy name
169:      * @return DibiConnection
170:      * @throws DibiException
171:      */
172:     public static function getConnection($name = NULL)
173:     {
174:         if ($name === NULL) {
175:             if (self::$connection === NULL) {
176:                 throw new DibiException('Dibi is not connected to database.');
177:             }
178: 
179:             return self::$connection;
180:         }
181: 
182:         if (!isset(self::$registry[$name])) {
183:             throw new DibiException("There is no connection named '$name'.");
184:         }
185: 
186:         return self::$registry[$name];
187:     }
188: 
189: 
190: 
191:     /**
192:      * Sets connection.
193:      * @param  DibiConnection
194:      * @return DibiConnection
195:      */
196:     public static function setConnection(DibiConnection $connection)
197:     {
198:         return self::$connection = $connection;
199:     }
200: 
201: 
202: 
203:     /**
204:      * Change active connection.
205:      * @param  string   connection registy name
206:      * @return void
207:      * @throws DibiException
208:      */
209:     public static function activate($name)
210:     {
211:         self::$connection = self::getConnection($name);
212:     }
213: 
214: 
215: 
216:     /********************* monostate for active connection ****************d*g**/
217: 
218: 
219: 
220:     /**
221:      * Generates and executes SQL query - Monostate for DibiConnection::query().
222:      * @param  array|mixed      one or more arguments
223:      * @return DibiResult|int   result set object (if any)
224:      * @throws DibiException
225:      */
226:     public static function query($args)
227:     {
228:         $args = func_get_args();
229:         return self::getConnection()->query($args);
230:     }
231: 
232: 
233: 
234:     /**
235:      * Executes the SQL query - Monostate for DibiConnection::nativeQuery().
236:      * @param  string           SQL statement.
237:      * @return DibiResult|int   result set object (if any)
238:      */
239:     public static function nativeQuery($sql)
240:     {
241:         return self::getConnection()->nativeQuery($sql);
242:     }
243: 
244: 
245: 
246:     /**
247:      * Generates and prints SQL query - Monostate for DibiConnection::test().
248:      * @param  array|mixed  one or more arguments
249:      * @return bool
250:      */
251:     public static function test($args)
252:     {
253:         $args = func_get_args();
254:         return self::getConnection()->test($args);
255:     }
256: 
257: 
258: 
259:     /**
260:      * Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test().
261:      * @param  array|mixed      one or more arguments
262:      * @return DibiDataSource
263:      */
264:     public static function dataSource($args)
265:     {
266:         $args = func_get_args();
267:         return self::getConnection()->dataSource($args);
268:     }
269: 
270: 
271: 
272:     /**
273:      * Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
274:      * @param  array|mixed    one or more arguments
275:      * @return DibiRow
276:      * @throws DibiException
277:      */
278:     public static function fetch($args)
279:     {
280:         $args = func_get_args();
281:         return self::getConnection()->query($args)->fetch();
282:     }
283: 
284: 
285: 
286:     /**
287:      * Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll().
288:      * @param  array|mixed    one or more arguments
289:      * @return array of DibiRow
290:      * @throws DibiException
291:      */
292:     public static function fetchAll($args)
293:     {
294:         $args = func_get_args();
295:         return self::getConnection()->query($args)->fetchAll();
296:     }
297: 
298: 
299: 
300:     /**
301:      * Executes SQL query and fetch first column - Monostate for DibiConnection::query() & fetchSingle().
302:      * @param  array|mixed    one or more arguments
303:      * @return string
304:      * @throws DibiException
305:      */
306:     public static function fetchSingle($args)
307:     {
308:         $args = func_get_args();
309:         return self::getConnection()->query($args)->fetchSingle();
310:     }
311: 
312: 
313: 
314:     /**
315:      * Executes SQL query and fetch pairs - Monostate for DibiConnection::query() & fetchPairs().
316:      * @param  array|mixed    one or more arguments
317:      * @return string
318:      * @throws DibiException
319:      */
320:     public static function fetchPairs($args)
321:     {
322:         $args = func_get_args();
323:         return self::getConnection()->query($args)->fetchPairs();
324:     }
325: 
326: 
327: 
328:     /**
329:      * Gets the number of affected rows.
330:      * Monostate for DibiConnection::getAffectedRows()
331:      * @return int  number of rows
332:      * @throws DibiException
333:      */
334:     public static function getAffectedRows()
335:     {
336:         return self::getConnection()->getAffectedRows();
337:     }
338: 
339: 
340: 
341:     /**
342:      * Gets the number of affected rows. Alias for getAffectedRows().
343:      * @return int  number of rows
344:      * @throws DibiException
345:      */
346:     public static function affectedRows()
347:     {
348:         return self::getConnection()->getAffectedRows();
349:     }
350: 
351: 
352: 
353:     /**
354:      * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
355:      * Monostate for DibiConnection::getInsertId()
356:      * @param  string     optional sequence name
357:      * @return int
358:      * @throws DibiException
359:      */
360:     public static function getInsertId($sequence=NULL)
361:     {
362:         return self::getConnection()->getInsertId($sequence);
363:     }
364: 
365: 
366: 
367:     /**
368:      * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
369:      * @param  string     optional sequence name
370:      * @return int
371:      * @throws DibiException
372:      */
373:     public static function insertId($sequence=NULL)
374:     {
375:         return self::getConnection()->getInsertId($sequence);
376:     }
377: 
378: 
379: 
380:     /**
381:      * Begins a transaction - Monostate for DibiConnection::begin().
382:      * @param  string  optional savepoint name
383:      * @return void
384:      * @throws DibiException
385:      */
386:     public static function begin($savepoint = NULL)
387:     {
388:         self::getConnection()->begin($savepoint);
389:     }
390: 
391: 
392: 
393:     /**
394:      * Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL).
395:      * @param  string  optional savepoint name
396:      * @return void
397:      * @throws DibiException
398:      */
399:     public static function commit($savepoint = NULL)
400:     {
401:         self::getConnection()->commit($savepoint);
402:     }
403: 
404: 
405: 
406:     /**
407:      * Rollback changes in a transaction - Monostate for DibiConnection::rollback().
408:      * @param  string  optional savepoint name
409:      * @return void
410:      * @throws DibiException
411:      */
412:     public static function rollback($savepoint = NULL)
413:     {
414:         self::getConnection()->rollback($savepoint);
415:     }
416: 
417: 
418: 
419:     /**
420:      * Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo().
421:      * @return DibiDatabaseInfo
422:      */
423:     public static function getDatabaseInfo()
424:     {
425:         return self::getConnection()->getDatabaseInfo();
426:     }
427: 
428: 
429: 
430:     /**
431:      * Import SQL dump from file - extreme fast!
432:      * @param  string  filename
433:      * @return int  count of sql commands
434:      */
435:     public static function loadFile($file)
436:     {
437:         return self::getConnection()->loadFile($file);
438:     }
439: 
440: 
441: 
442:     /**
443:      * Replacement for majority of dibi::methods() in future.
444:      */
445:     public static function __callStatic($name, $args)
446:     {
447:         //if ($name = 'select', 'update', ...') {
448:         //  return self::command()->$name($args);
449:         //}
450:         return call_user_func_array(array(self::getConnection(), $name), $args);
451:     }
452: 
453: 
454: 
455:     /********************* fluent SQL builders ****************d*g**/
456: 
457: 
458: 
459:     /**
460:      * @return DibiFluent
461:      */
462:     public static function command()
463:     {
464:         return self::getConnection()->command();
465:     }
466: 
467: 
468: 
469:     /**
470:      * @param  string    column name
471:      * @return DibiFluent
472:      */
473:     public static function select($args)
474:     {
475:         $args = func_get_args();
476:         return call_user_func_array(array(self::getConnection(), 'select'), $args);
477:     }
478: 
479: 
480: 
481:     /**
482:      * @param  string   table
483:      * @param  array
484:      * @return DibiFluent
485:      */
486:     public static function update($table, $args)
487:     {
488:         return self::getConnection()->update($table, $args);
489:     }
490: 
491: 
492: 
493:     /**
494:      * @param  string   table
495:      * @param  array
496:      * @return DibiFluent
497:      */
498:     public static function insert($table, $args)
499:     {
500:         return self::getConnection()->insert($table, $args);
501:     }
502: 
503: 
504: 
505:     /**
506:      * @param  string   table
507:      * @return DibiFluent
508:      */
509:     public static function delete($table)
510:     {
511:         return self::getConnection()->delete($table);
512:     }
513: 
514: 
515: 
516:     /********************* data types ****************d*g**/
517: 
518: 
519: 
520:     /**
521:      * @return DibiDateTime
522:      */
523:     public static function datetime($time = NULL)
524:     {
525:         trigger_error(__METHOD__ . '() is deprecated; create DibiDateTime object instead.', E_USER_WARNING);
526:         return new DibiDateTime($time);
527:     }
528: 
529: 
530: 
531:     /**
532:      * @deprecated
533:      */
534:     public static function date($date = NULL)
535:     {
536:         trigger_error(__METHOD__ . '() is deprecated; create DibiDateTime object instead.', E_USER_WARNING);
537:         return new DibiDateTime($date);
538:     }
539: 
540: 
541: 
542:     /********************* substitutions ****************d*g**/
543: 
544: 
545: 
546:     /**
547:      * Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes().
548:      * @return DibiHashMap
549:      */
550:     public static function getSubstitutes()
551:     {
552:         return self::getConnection()->getSubstitutes();
553:     }
554: 
555: 
556: 
557:     /** @deprecated */
558:     public static function addSubst($expr, $subst)
559:     {
560:         trigger_error(__METHOD__ . '() is deprecated; use dibi::getSubstitutes()->expr = val; instead.', E_USER_WARNING);
561:         self::getSubstitutes()->$expr = $subst;
562:     }
563: 
564: 
565: 
566:     /** @deprecated */
567:     public static function removeSubst($expr)
568:     {
569:         trigger_error(__METHOD__ . '() is deprecated; use unset(dibi::getSubstitutes()->expr) instead.', E_USER_WARNING);
570:         $substitutes = self::getSubstitutes();
571:         if ($expr === TRUE) {
572:             foreach ($substitutes as $expr => $foo) {
573:                 unset($substitutes->$expr);
574:             }
575:         } else {
576:             unset($substitutes->$expr);
577:         }
578:     }
579: 
580: 
581: 
582:     /** @deprecated */
583:     public static function setSubstFallback($callback)
584:     {
585:         trigger_error(__METHOD__ . '() is deprecated; use dibi::getSubstitutes()->setCallback() instead.', E_USER_WARNING);
586:         self::getSubstitutes()->setCallback($callback);
587:     }
588: 
589: 
590: 
591:     /********************* misc tools ****************d*g**/
592: 
593: 
594: 
595:     /**
596:      * Prints out a syntax highlighted version of the SQL command or DibiResult.
597:      * @param  string|DibiResult
598:      * @param  bool  return output instead of printing it?
599:      * @return string
600:      */
601:     public static function dump($sql = NULL, $return = FALSE)
602:     {
603:         ob_start();
604:         if ($sql instanceof DibiResult) {
605:             $sql->dump();
606: 
607:         } else {
608:             if ($sql === NULL) $sql = self::$sql;
609: 
610:             static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
611:             static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
612: 
613:             // insert new lines
614:             $sql = " $sql ";
615:             $sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
616: 
617:             // reduce spaces
618:             $sql = preg_replace('#[ \t]{2,}#', " ", $sql);
619: 
620:             $sql = wordwrap($sql, 100);
621:             $sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
622: 
623:             if (PHP_SAPI === 'cli') {
624:                 echo trim($sql) . "\n\n";
625:             } else {
626:                 // syntax highlight
627:                 $sql = htmlSpecialChars($sql);
628:                 $sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
629:                 echo '<pre class="dump">', trim($sql), "</pre>\n";
630:             }
631:         }
632: 
633:         if ($return) {
634:             return ob_get_clean();
635:         } else {
636:             ob_end_flush();
637:         }
638:     }
639: 
640: 
641: 
642:     private static function highlightCallback($matches)
643:     {
644:         if (!empty($matches[1])) // comment
645:             return '<em style="color:gray">' . $matches[1] . '</em>';
646: 
647:         if (!empty($matches[2])) // error
648:             return '<strong style="color:red">' . $matches[2] . '</strong>';
649: 
650:         if (!empty($matches[3])) // most important keywords
651:             return '<strong style="color:blue">' . $matches[3] . '</strong>';
652: 
653:         if (!empty($matches[4])) // other keywords
654:             return '<strong style="color:green">' . $matches[4] . '</strong>';
655:     }
656: 
657: }
658: 
dibi API documentation API documentation generated by ApiGen 2.3.0