colly
2017-07-26 c1236314ef1122e14edd46c9fe4102fc16e2ce46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
 
/** Information about tables and columns structure
*/
interface NotORM_Structure {
    
    /** Get primary key of a table in $db->$table()
    * @param string
    * @return string
    */
    function getPrimary($table);
    
    /** Get column holding foreign key in $table[$id]->$name()
    * @param string
    * @param string
    * @return string
    */
    function getReferencingColumn($name, $table);
    
    /** Get target table in $table[$id]->$name()
    * @param string
    * @param string
    * @return string
    */
    function getReferencingTable($name, $table);
    
    /** Get column holding foreign key in $table[$id]->$name
    * @param string
    * @param string
    * @return string
    */
    function getReferencedColumn($name, $table);
    
    /** Get table holding foreign key in $table[$id]->$name
    * @param string
    * @param string
    * @return string
    */
    function getReferencedTable($name, $table);
    
    /** Get sequence name, used by insert
    * @param string
    * @return string
    */
    function getSequence($table);
    
}
 
 
 
/** Structure described by some rules
*/
class NotORM_Structure_Convention implements NotORM_Structure {
    protected $primary, $foreign, $table, $prefix;
    
    /** Create conventional structure
    * @param string %s stands for table name
    * @param string %1$s stands for key used after ->, %2$s for table name
    * @param string %1$s stands for key used after ->, %2$s for table name
    * @param string prefix for all tables
    */
    function __construct($primary = 'id', $foreign = '%s_id', $table = '%s', $prefix = '') {
        $this->primary = $primary;
        $this->foreign = $foreign;
        $this->table = $table;
        $this->prefix = $prefix;
    }
    
    function getPrimary($table) {
        return sprintf($this->primary, $this->getColumnFromTable($table));
    }
    
    function getReferencingColumn($name, $table) {
        return $this->getReferencedColumn(substr($table, strlen($this->prefix)), $this->prefix . $name);
    }
    
    function getReferencingTable($name, $table) {
        return $this->prefix . $name;
    }
    
    function getReferencedColumn($name, $table) {
        return sprintf($this->foreign, $this->getColumnFromTable($name), substr($table, strlen($this->prefix)));
    }
    
    function getReferencedTable($name, $table) {
        return $this->prefix . sprintf($this->table, $name, $table);
    }
    
    function getSequence($table) {
        return null;
    }
    
    protected function getColumnFromTable($name) {
        if ($this->table != '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '$)', $name, $match)) {
            return $match[1];
        }
        return $name;
    }
    
}
 
 
 
/** Structure reading meta-informations from the database
*/
class NotORM_Structure_Discovery implements NotORM_Structure {
    protected $connection, $cache, $structure = array();
    protected $foreign;
    
    /** Create autodisovery structure
    * @param PDO
    * @param NotORM_Cache
    * @param string use "%s_id" to access $name . "_id" column in $row->$name
    */
    function __construct(PDO $connection, NotORM_Cache $cache = null, $foreign = '%s') {
        $this->connection = $connection;
        $this->cache = $cache;
        $this->foreign = $foreign;
        if ($cache) {
            $this->structure = $cache->load("structure");
        }
    }
    
    /** Save data to cache
    */
    function __destruct() {
        if ($this->cache) {
            $this->cache->save("structure", $this->structure);
        }
    }
    
    function getPrimary($table) {
        $return = &$this->structure["primary"][$table];
        if (!isset($return)) {
            $return = "";
            foreach ($this->connection->query("EXPLAIN $table") as $column) {
                if ($column[3] == "PRI") { // 3 - "Key" is not compatible with PDO::CASE_LOWER
                    if ($return != "") {
                        $return = ""; // multi-column primary key is not supported
                        break;
                    }
                    $return = $column[0];
                }
            }
        }
        return $return;
    }
    
    function getReferencingColumn($name, $table) {
        $name = strtolower($name);
        $return = &$this->structure["referencing"][$table];
        if (!isset($return[$name])) {
            foreach ($this->connection->query("
                SELECT TABLE_NAME, COLUMN_NAME
                FROM information_schema.KEY_COLUMN_USAGE
                WHERE TABLE_SCHEMA = DATABASE()
                AND REFERENCED_TABLE_SCHEMA = DATABASE()
                AND REFERENCED_TABLE_NAME = " . $this->connection->quote($table) . "
                AND REFERENCED_COLUMN_NAME = " . $this->connection->quote($this->getPrimary($table)) //! may not reference primary key
            ) as $row) {
                $return[strtolower($row[0])] = $row[1];
            }
        }
        return $return[$name];
    }
    
    function getReferencingTable($name, $table) {
        return $name;
    }
    
    function getReferencedColumn($name, $table) {
        return sprintf($this->foreign, $name);
    }
    
    function getReferencedTable($name, $table) {
        $column = strtolower($this->getReferencedColumn($name, $table));
        $return = &$this->structure["referenced"][$table];
        if (!isset($return[$column])) {
            foreach ($this->connection->query("
                SELECT COLUMN_NAME, REFERENCED_TABLE_NAME
                FROM information_schema.KEY_COLUMN_USAGE
                WHERE TABLE_SCHEMA = DATABASE()
                AND REFERENCED_TABLE_SCHEMA = DATABASE()
                AND TABLE_NAME = " . $this->connection->quote($table) . "
            ") as $row) {
                $return[strtolower($row[0])] = $row[1];
            }
        }
        return $return[$column];
    }
    
    function getSequence($table) {
        return null;
    }
    
}