DataObjects/Builder_tables.php
[Pman.Builder] / DataObjects / Builder_tables.php
1 <?php
2 /**
3  * Table Definition for builder_tables
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Builder_DataObjects_Builder_tables extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'builder_tables';      // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                            // string(128)  not_null
15     public $descrip;                         // string(254)  not_null
16     public $parent_id;                       // int(11)  not_null multiple_key
17     public $dbschema;                        // blob(65535)  not_null blob
18
19     
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22     
23     
24     function applyFilters($q, $au)
25     {
26         //DB_DataObject::debugLEvel(1);
27         if (!empty($q['_sync'])) {
28             $this->syncDatabase();
29             HTML_FlexyFramework::get()->page->jok("DONE");
30         }
31        
32     }
33     
34     function syncDatabase()
35     {
36         //DB_DataObject::debugLevel(1);
37         global $_DB_DATAOBJECT;
38         $x = DB_DataObject::factory('builder_tables');
39         $x->whereAdd("name != ''"); // real tables only..
40         $mine = $this->fetchAll('name', 'id');
41         
42         // ensure everything is loaded...
43         $tq = DB_DataObject::factory('builder_tables');
44         $tq->table();
45         $tq->links();
46         $tables = $_DB_DATAOBJECT['INI'][$tq->_database];
47         
48          
49         $ret = array();
50         $t = array_keys($tables);
51         sort($t);
52         
53         
54         // for postgres we can get descriptions - this should just fail in Mysql..
55         $desc= array();
56         $dsn = HTML_FlexyFramework::get()->database;
57         
58         if (preg_match('/^pgsql:/', $dsn )) {
59             $tq = DB_DataObject::factory('builder_tables');
60             $tq->query( "
61                 select relname, obj_description( oid) as desc FROM pg_catalog.pg_class
62                 ");
63             while ($tq->fetch()) {
64                 $desc[$tq->relname] = $tq->desc;
65             }
66         }
67         //   DB_DataObjecT::DebugLevel(1);
68         $tq = DB_DataObject::factory('builder_tables');
69         
70         require_once 'Services/JSON.php';
71         
72         $modids = array();
73         
74         
75         foreach( $t as $k) {
76             if (preg_match('/__keys$/', $k)) {
77                 continue;
78             }
79             // check it can be constructed..
80             // this might be problematic for 'new' tables...
81             $do = DB_DataObject::factory($k);
82             if (!is_a($do,'DB_DataObject')) {
83                 continue;
84             }
85             
86             // get's the module part out of the dataobject class name
87             // assumes '_' is not used in module name.
88             $mod = array_pop(
89                     explode('_',
90                         substr(get_class($do), 0, -1 * (strlen('_DataObject_') + strlen($k)+ 1))
91                     ));
92              
93             // should get 'ZZZ' part.. : XXX_ZZZZ_DataObject_xx_Builder
94             if (!isset($modids[$mod])) {
95                 $x = DB_DataObject::factory('builder_tables');
96                 $x->parent_id =0;
97                 $x->name = '';
98                 $x->descrip = $mod;
99                 $x->dbschema = '';
100                 if (!$x->find(true)) {
101                     $x->insert();
102                 } 
103                  $modids[$mod] = $x->id; 
104                 
105             }
106             
107             
108             
109             $set = array(
110                 'name' => $k,
111                 'descrip' => isset($desc[$k]) ? $desc[$k] : '',
112                 'dbschema' => Services_JSON::stringify($this->tableSchema($k),null,4),
113                // 'parent_id' =>  $modids[$mod],
114             );
115             
116             $do = clone($tq);
117             if (isset($mine[$k])) { 
118                 $do->get($mine[$k]);
119                 $dd = clone($do);
120                 $do->setFrom($set);
121                 if (empty($do->parent_id) || $do->parent_id < 1) {
122                     // allow user to modify this..
123                     $do->parent_id = $modids[$mod];
124                 }
125                 $do->update($dd);
126                 continue;
127             }
128             
129             $do->setFrom($set);
130             $do->parent_id = $modids[$mod];
131             $do->insert();
132         
133         }
134             
135         
136         
137     }
138     
139     
140     function tableSchema($tn)
141     {
142         static  $cache = array();
143         static  $types= array();
144         $do = DB_DataObject::factory($tn);
145         $tn = $do->tableName(); // cleaned up!
146
147
148         if (isset($cache[$tn])) {
149             return $cache[$tn];
150         }
151
152         // get a description if available..
153        
154              
155         $desc = array();
156         $dd = clone($do);
157         $dsn = HTML_FlexyFramework::get()->database;
158         if (preg_match('/^pgsql:/', $dsn)) {
159             
160             
161            // DB_DataObject::DebugLevel(1);
162             $dd->query("SELECT
163                     c.column_name as name,
164                     pgd.description as desc
165                 FROM pg_catalog.pg_statio_all_tables as st
166                     inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
167                     inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
168                 WHERE
169                     c.table_schema = 'public' and c.table_name = '{$tn}'
170             ");
171             while($dd->fetch()) {
172                 $desc[$dd->name] = $dd->desc;
173             }
174         }
175         $defs =  $dd->getDatabaseConnection()->tableInfo($tn);
176         // add descriptions?
177         foreach($defs as $i=>$c) {
178             $defs[$i]['desc'] = isset($desc[$c['name']]) ? $desc[$c['name']] : '';
179         }
180         $cache[$tn]= $defs;
181      
182         return $cache[$tn];
183     }
184     
185 }