1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: class NDatabaseReflection extends NObject
21: {
22: const FIELD_TEXT = 'string',
23: FIELD_BINARY = 'bin',
24: FIELD_BOOL = 'bool',
25: FIELD_INTEGER = 'int',
26: FIELD_FLOAT = 'float',
27: FIELD_DATETIME = 'datetime';
28:
29:
30: private $primary;
31:
32:
33: private $foreign;
34:
35:
36: private $table;
37:
38:
39:
40: 41: 42: 43: 44: 45:
46: public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
47: {
48: $this->primary = $primary;
49: $this->foreign = $foreign;
50: $this->table = $table;
51: }
52:
53:
54:
55: public function getPrimary($table)
56: {
57: return sprintf($this->primary, $table);
58: }
59:
60:
61:
62: public function getReferencingColumn($name, $table)
63: {
64: return $this->getReferencedColumn($table, $name);
65: }
66:
67:
68:
69: public function getReferencedColumn($name, $table)
70: {
71: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '$)', $name, $match)) {
72: $name = $match[1];
73: }
74: return sprintf($this->foreign, $name, $table);
75: }
76:
77:
78:
79: public function getReferencedTable($name, $table)
80: {
81: return sprintf($this->table, $name, $table);
82: }
83:
84:
85:
86: 87: 88: 89: 90: 91:
92: public static function detectType($type)
93: {
94: static $types, $patterns = array(
95: 'BYTEA|BLOB|BIN' => self::FIELD_BINARY,
96: 'TEXT|CHAR' => self::FIELD_TEXT,
97: 'YEAR|BYTE|COUNTER|SERIAL|INT|LONG' => self::FIELD_INTEGER,
98: 'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => self::FIELD_FLOAT,
99: 'TIME|DATE' => self::FIELD_DATETIME,
100: 'BOOL|BIT' => self::FIELD_BOOL,
101: );
102:
103: if (!isset($types[$type])) {
104: $types[$type] = 'string';
105: foreach ($patterns as $s => $val) {
106: if (preg_match("#$s#i", $type)) {
107: return $types[$type] = $val;
108: }
109: }
110: }
111: return $types[$type];
112: }
113:
114: }
115: