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['_dumpDatabase'])){
33 $this->dumpDatabase();
36 // if(!empty($q['_dumpTable'])){
37 // $this->dumpTable($q['_dumpTable']);
42 function syncDatabase()
44 //DB_DataObject::debugLevel(1);
45 global $_DB_DATAOBJECT;
46 $x = DB_DataObject::factory('builder_tables');
47 $x->whereAdd("name != ''"); // real tables only..
48 $mine = $this->fetchAll('name', 'id');
50 // ensure everything is loaded...
51 $tq = DB_DataObject::factory('builder_tables');
54 $tables = $_DB_DATAOBJECT['INI'][$tq->_database];
58 $t = array_keys($tables);
62 // for postgres we can get descriptions - this should just fail in Mysql..
64 $dsn = HTML_FlexyFramework::get()->database;
66 if (preg_match('/^pgsql:/', $dsn )) {
67 $tq = DB_DataObject::factory('builder_tables');
69 select relname, obj_description( oid) as desc FROM pg_catalog.pg_class
71 while ($tq->fetch()) {
72 $desc[$tq->relname] = $tq->desc;
75 // DB_DataObjecT::DebugLevel(1);
76 $tq = DB_DataObject::factory('builder_tables');
78 require_once 'Services/JSON.php';
84 if (preg_match('/__keys$/', $k)) {
87 // check it can be constructed..
88 // this might be problematic for 'new' tables...
89 $do = DB_DataObject::factory($k);
90 if (!is_a($do,'DB_DataObject')) {
94 // get's the module part out of the dataobject class name
95 // assumes '_' is not used in module name.
98 substr(get_class($do), 0, -1 * (strlen('_DataObject_') + strlen($k)+ 1))
101 // should get 'ZZZ' part.. : XXX_ZZZZ_DataObject_xx_Builder
102 if (!isset($modids[$mod])) {
103 $x = DB_DataObject::factory('builder_tables');
108 if (!$x->find(true)) {
111 $modids[$mod] = $x->id;
119 'descrip' => isset($desc[$k]) ? $desc[$k] : '',
120 'dbschema' => Services_JSON::stringify($this->tableSchema($k),null,4),
121 // 'parent_id' => $modids[$mod],
125 if (isset($mine[$k])) {
129 if (empty($do->parent_id) || $do->parent_id < 1) {
130 // allow user to modify this..
131 $do->parent_id = $modids[$mod];
138 $do->parent_id = $modids[$mod];
148 function tableSchema($tn)
150 static $cache = array();
151 static $types= array();
152 $do = DB_DataObject::factory($tn);
153 $tn = $do->tableName(); // cleaned up!
156 if (isset($cache[$tn])) {
160 // get a description if available..
165 $dsn = HTML_FlexyFramework::get()->database;
166 if (preg_match('/^pgsql:/', $dsn)) {
169 // DB_DataObject::DebugLevel(1);
171 c.column_name as name,
172 pgd.description as desc
173 FROM pg_catalog.pg_statio_all_tables as st
174 inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
175 inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
177 c.table_schema = 'public' and c.table_name = '{$tn}'
179 while($dd->fetch()) {
180 $desc[$dd->name] = $dd->desc;
183 $defs = $dd->getDatabaseConnection()->tableInfo($tn);
185 foreach($defs as $i=>$c) {
186 $defs[$i]['desc'] = isset($desc[$c['name']]) ? $desc[$c['name']] : '';
193 // function dumpTable($tn)
195 // $roo = HTML_FlexyFramework::get()->page;
201 // $do = DB_DataObject::factory($tn);
202 // if (!is_a($do,'DB_DataObject')) {
206 // $dsn = HTML_FlexyFramework::get()->database;
208 // $database = explode('@', $dsn);
210 // $ui = str_replace('mysql://', '', $database[0]);
212 // $user = array_shift(explode(':', $ui));
213 // $pw = array_pop(explode(':', $ui));
215 // $host = array_shift(explode('/', $database[1]));
216 // $dn = array_pop(explode('/', $database[1]));
218 // $cmd = "mysqldump -u{$user} ";
220 // $cmd .= "-p{$pw} ";
223 // $cmd .= "--no-create-info summit {$tn}";
225 // require_once 'System.php';
227 // $tmpdir = System::mktemp("-d dump");
229 // $path = $tmpdir . '/' . $tn . '.sql';
235 // $data = ob_get_contents();
239 // file_put_contents($path, $data);
241 // header('Content-Description: File Transfer');
242 // header ('Content-Type: application/octet-stream');
243 // header("Content-Disposition: attachment; filename=\"".basename($path)."\";" );
244 // header("Expires: 0");
245 // header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
246 // header("Pragma: public");
247 // header('Content-Length: ' . filesize($path));
256 function dumpDatabase()
258 $roo = HTML_FlexyFramework::get()->page;
260 $dsn = HTML_FlexyFramework::get()->database;
262 $database = explode('@', $dsn);
264 $ui = str_replace('mysql://', '', $database[0]);
266 $user = array_shift(explode(':', $ui));
267 $pw = array_pop(explode(':', $ui));
269 $host = array_shift(explode('/', $database[1]));
270 $dn = array_pop(explode('/', $database[1]));
272 $cmd = "mysqldump -u{$user} ";
277 $cmd .= "--extended-insert=FALSE --no-create-info {$dn}";
279 require_once 'System.php';
281 $tmpdir = System::mktemp("-d dump");
283 $path = $tmpdir . '/' . $dn . '.sql';
289 $data = ob_get_contents();
293 file_put_contents($path, $data);
295 header('Content-Description: File Transfer');
296 header ('Content-Type: application/octet-stream');
297 header("Content-Disposition: attachment; filename=\"".basename($path)."\";" );
298 header("Expires: 0");
299 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
300 header("Pragma: public");
301 header('Content-Length: ' . filesize($path));