3 * Table Definition for builder_tables
5 require_once 'DB/DataObject.php';
7 class Pman_Builder_DataObjects_Builder_tables extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
20 /* the code above is auto generated do not remove the tag below */
24 function applyFilters($q, $au)
26 //DB_DataObject::debugLEvel(1);
27 if (!empty($q['_sync'])) {
28 $this->syncDatabase();
29 HTML_FlexyFramework::get()->page->jok("DONE");
32 if(!empty($q['_dumpTable'])){
33 $this->dumpTable($q['_dump']);
38 function syncDatabase()
40 //DB_DataObject::debugLevel(1);
41 global $_DB_DATAOBJECT;
42 $x = DB_DataObject::factory('builder_tables');
43 $x->whereAdd("name != ''"); // real tables only..
44 $mine = $this->fetchAll('name', 'id');
46 // ensure everything is loaded...
47 $tq = DB_DataObject::factory('builder_tables');
50 $tables = $_DB_DATAOBJECT['INI'][$tq->_database];
54 $t = array_keys($tables);
58 // for postgres we can get descriptions - this should just fail in Mysql..
60 $dsn = HTML_FlexyFramework::get()->database;
62 if (preg_match('/^pgsql:/', $dsn )) {
63 $tq = DB_DataObject::factory('builder_tables');
65 select relname, obj_description( oid) as desc FROM pg_catalog.pg_class
67 while ($tq->fetch()) {
68 $desc[$tq->relname] = $tq->desc;
71 // DB_DataObjecT::DebugLevel(1);
72 $tq = DB_DataObject::factory('builder_tables');
74 require_once 'Services/JSON.php';
80 if (preg_match('/__keys$/', $k)) {
83 // check it can be constructed..
84 // this might be problematic for 'new' tables...
85 $do = DB_DataObject::factory($k);
86 if (!is_a($do,'DB_DataObject')) {
90 // get's the module part out of the dataobject class name
91 // assumes '_' is not used in module name.
94 substr(get_class($do), 0, -1 * (strlen('_DataObject_') + strlen($k)+ 1))
97 // should get 'ZZZ' part.. : XXX_ZZZZ_DataObject_xx_Builder
98 if (!isset($modids[$mod])) {
99 $x = DB_DataObject::factory('builder_tables');
104 if (!$x->find(true)) {
107 $modids[$mod] = $x->id;
115 'descrip' => isset($desc[$k]) ? $desc[$k] : '',
116 'dbschema' => Services_JSON::stringify($this->tableSchema($k),null,4),
117 // 'parent_id' => $modids[$mod],
121 if (isset($mine[$k])) {
125 if (empty($do->parent_id) || $do->parent_id < 1) {
126 // allow user to modify this..
127 $do->parent_id = $modids[$mod];
134 $do->parent_id = $modids[$mod];
144 function tableSchema($tn)
146 static $cache = array();
147 static $types= array();
148 $do = DB_DataObject::factory($tn);
149 $tn = $do->tableName(); // cleaned up!
152 if (isset($cache[$tn])) {
156 // get a description if available..
161 $dsn = HTML_FlexyFramework::get()->database;
162 if (preg_match('/^pgsql:/', $dsn)) {
165 // DB_DataObject::DebugLevel(1);
167 c.column_name as name,
168 pgd.description as desc
169 FROM pg_catalog.pg_statio_all_tables as st
170 inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
171 inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
173 c.table_schema = 'public' and c.table_name = '{$tn}'
175 while($dd->fetch()) {
176 $desc[$dd->name] = $dd->desc;
179 $defs = $dd->getDatabaseConnection()->tableInfo($tn);
181 foreach($defs as $i=>$c) {
182 $defs[$i]['desc'] = isset($desc[$c['name']]) ? $desc[$c['name']] : '';
189 function dumpTable($tn)
191 $roo = HTML_FlexyFramework::get()->page;
197 $do = DB_DataObject::factory($tn);
198 if (!is_a($do,'DB_DataObject')) {
202 $dsn = HTML_FlexyFramework::get()->database;
204 $database = explode('@', $dsn);
206 $ui = str_replace('mysql://', '', $database[0]);
208 $user = array_shift(explode(':', $ui));
209 $pw = array_pop(explode(':', $ui));
211 $host = array_shift(explode('/', $database[1]));
212 $dn = array_pop(explode('/', $database[1]));
214 $cmd = "mysqldump -u{$user} ";
219 $cmd .= "--no-create-info summit {$tn}";
221 require_once 'System.php';
223 $tmpdir = System::mktemp("-d dump");
225 $path = $tmpdir . '/' . $tn . '.sql';
231 $data = ob_get_contents();
235 file_put_contents($path, $data);
237 header('Content-Description: File Transfer');
238 header ('Content-Type: application/octet-stream');
239 header("Content-Disposition: attachment; filename=\"".basename($path)."\";" );
240 header("Expires: 0");
241 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
242 header("Pragma: public");
243 header('Content-Length: ' . filesize($path));