Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • DatabaseReflection
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 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: namespace Nette\Database\Reflection;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Reflection metadata class for a database. TEMPORARY SOLUTION
 20:  *
 21:  * @author     Jakub Vrana
 22:  */
 23: class DatabaseReflection extends Nette\Object
 24: {
 25:     const FIELD_TEXT = 'string',
 26:         FIELD_BINARY = 'bin',
 27:         FIELD_BOOL = 'bool',
 28:         FIELD_INTEGER = 'int',
 29:         FIELD_FLOAT = 'float',
 30:         FIELD_DATETIME = 'datetime';
 31: 
 32:     /** @var string */
 33:     private $primary;
 34: 
 35:     /** @var string */
 36:     private $foreign;
 37: 
 38:     /** @var string */
 39:     private $table;
 40: 
 41: 
 42: 
 43:     /**
 44:      * Create conventional structure.
 45:      * @param  string %s stands for table name
 46:      * @param  string %1$s stands for key used after ->, %2$s for table name
 47:      * @param  string %1$s stands for key used after ->, %2$s for table name
 48:      */
 49:     public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
 50:     {
 51:         $this->primary = $primary;
 52:         $this->foreign = $foreign;
 53:         $this->table = $table;
 54:     }
 55: 
 56: 
 57: 
 58:     public function getPrimary($table)
 59:     {
 60:         return sprintf($this->primary, $table);
 61:     }
 62: 
 63: 
 64: 
 65:     public function getReferencingColumn($name, $table)
 66:     {
 67:         return $this->getReferencedColumn($table, $name);
 68:     }
 69: 
 70: 
 71: 
 72:     public function getReferencedColumn($name, $table)
 73:     {
 74:         if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '$)', $name, $match)) {
 75:             $name = $match[1];
 76:         }
 77:         return sprintf($this->foreign, $name, $table);
 78:     }
 79: 
 80: 
 81: 
 82:     public function getReferencedTable($name, $table)
 83:     {
 84:         return sprintf($this->table, $name, $table);
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Heuristic type detection.
 91:      * @param  string
 92:      * @return string
 93:      * @internal
 94:      */
 95:     public static function detectType($type)
 96:     {
 97:         static $types, $patterns = array(
 98:             'BYTEA|BLOB|BIN' => self::FIELD_BINARY,
 99:             'TEXT|CHAR' => self::FIELD_TEXT,
100:             'YEAR|BYTE|COUNTER|SERIAL|INT|LONG' => self::FIELD_INTEGER,
101:             'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => self::FIELD_FLOAT,
102:             'TIME|DATE' => self::FIELD_DATETIME,
103:             'BOOL|BIT' => self::FIELD_BOOL,
104:         );
105: 
106:         if (!isset($types[$type])) {
107:             $types[$type] = 'string';
108:             foreach ($patterns as $s => $val) {
109:                 if (preg_match("#$s#i", $type)) {
110:                     return $types[$type] = $val;
111:                 }
112:             }
113:         }
114:         return $types[$type];
115:     }
116: 
117: }
118: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0