DataObjects/Builder_tables.php
[Pman.Builder] / ERM.php
diff --git a/ERM.php b/ERM.php
index 13d8ff6..3b735a7 100644 (file)
--- a/ERM.php
+++ b/ERM.php
@@ -17,62 +17,167 @@ class Pman_Builder_ERM extends Pman
         
        }
     
-    function get()
+    function get($tbl)
     {
+        
+        
         //echo '<PRE>';
+        global $_DB_DATAOBJECT;
+        // DB_DataObject::debugLevel(1);
         $tq = DB_DataObject::factory('Person');
-        $tq->query('SHOW TABLES');
-         
-        $table = array();
-        while ($tq->fetch()) {
-            $v = array_values($tq->toArray());
-            $tables[$v[0]] = array();
-        }
-        //print_r($tables);
-        //DB_DataObject::debugLevel(1);
-        foreach($tables as $t=>$a) {
+        $tq->table();
+        $tq->links();
+        $tables = $_DB_DATAOBJECT['INI'][$tq->_database];
+        
+        if (!isset($_GET['table'])) {
+            $ret = array();
+            $t = array_keys($tables);
+            sort($t);
+            
+            // for postgres we can get descriptions - this should just fail in Mysql..
+            $desc= array();
+           // DB_DataObjecT::DebugLevel(1);
             $tq = DB_DataObject::factory('Person');
-            $tq->query('DESCRIBE  `' . $t . '`');
+            $tq->query( "
+                select relname, obj_description( oid) as desc FROM pg_catalog.pg_class");
             while ($tq->fetch()) {
-                $av = $tq->toArray();
-                $av['Null'] = ($av['Null'] == "NO") ? "NOT NULL" : ""; 
-                $av['Key'] = strlen($av['Key'] ) ?  "KEYS(". $av['Key'] . ")" : "";
-                $v = array_values($av);
-                
-                $n  =  array_shift($v);
-                $tables[$t][$n] = trim(implode(" " , $v));
-                
+                $desc[$tq->relname] = $tq->desc;
             }
+
             
-        
-        
+            
+            
+            
+            
+            foreach( $t as $k) {
+                if (preg_match('/__keys$/', $k)) {
+                    continue;
+                }
+                $do = DB_DataObject::factory($k);
+                if (!is_a($do,'DB_DataObject')) {
+                    continue;
+                }
+                $ret[] = array(
+                        'name' => $k,
+                        'desc' => isset($desc[$k]) ? $desc[$k] : ''
+                    );
+            }
+            $this->jdata($ret);
         }
-        //print_r($tables);
+        // this part is to find the table definition...
         
-        // load links..
-        $tq = DB_DataObject::factory('Person');
-        $tq->links();
-        //print_r($GLOBALS['_DB_DATAOBJECT']['LINKS'][$tq->_database]);
-        $links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$tq->_database];
-        foreach($links as $t=>$ar) {
-            foreach($ar as $k=>$ex) {
-                $links[$t][$k] = explode(":", $ex);
+        $do = DB_DataObject::factory($_GET['table']);
+        $links = $do->links();
+        $cols = $do->table();
+        $dos = array();
+       //    echo '<PRE>';print_R($links);
+        foreach($links as $k=>$v) {
+            $kv = explode(':', $v);
+            //print_R($kv);
+            $dos[$k]= DB_DataObject::factory($kv[0]);
+            if (!is_a($dos[$k], 'DB_DataObject')) {
+                echo '<PRE>'; print_r($dos[$k]);
             }
         }
-        $this->jdata(array(
-            'tables' =>$tables,
-            'links' => $links
-        ));
+        
+      
+        $desc =   $this->createRet($do);
+       // echo '<PRE>';print_R($desc);
+        $ret = array();
+        foreach($cols as $c => $ty) {
+           
+            if (!isset($links[$c]))  {
+                 $ret[] = $desc[$c];
+                continue;
+            }
+            // we need to add dependant information to column details so
+            // that combo box can determine how to use it..
+            
             
-       // print_r($links);
-        require_once 'Services/JSON.php';
-        $json = new Services_JSON();
-        echo "var tables = " .$json->encode( $tables) . "\n";
-        echo "var links = " .$json->encode( $links) . "\n";
+            
+            // colname_{remotename}_{col}
+            $kv = explode(':', $links[$c]);
+            //$ar = $this->createRet($dos[$c], $c . '_' . $kv[1] . '_');
+            $ar = $this->createRet($dos[$c], $c . '_' , $kv[1]);
+            $desc[$c]['maps_to'] = $kv[1];
+            $desc[$c]['deps'] = array_values($ar);
+            
+            $ret[] = $desc[$c];
+            foreach($ar as $cn => $r) {
+                $ret[] = $r;
+            }
+            
+            
+        }
+        // echo '<PRE>';print_R($ret);
         
+        $this->jdata($ret); 
         
         
-        exit;
+        
+        
+    }
+    function createRet($do, $pref='', $skip = '')
+    {
+        static  $desc = array();
+        static  $types= array();
+        $tn = $do->tableName();
+
+
+
+
+
+        // get a description if available..
+        if (!isset($desc[$tn])) {
+            
+            
+            
+            
+            
+            
+            $desc[$tn] = array();
+            $dd = clone($do);
+            
+           // DB_DataObject::DebugLevel(1);
+            $dd->query("SELECT
+                    c.column_name as name,
+                    pgd.description as desc
+                FROM pg_catalog.pg_statio_all_tables as st
+                    inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
+                    inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname)
+                WHERE
+                    c.table_schema = 'public' and c.table_name = '{$tn}'
+            ");
+            while($dd->fetch()) {
+                $desc[$tn][$dd->name] = $dd->desc;
+            }
+            
+            $defs =  $dd->getDatabaseConnection()->tableInfo($tn);
+            $types[$tn] = array();
+            foreach($defs as $c) {
+                $types[$tn][$c['name']] = $c['type'];
+            }
+            //echo '<PRE>';print_r($defs);
+            
+        }
+       
+       
+       
+        $ret = array();
+        foreach($do->table() as $k=>$ty) {
+            if ($k == $skip) {
+                continue;
+            }
+            $ret[$k] = array(
+                'table' => $tn,
+                'column' => $pref . $k,
+                'columnshort' => $k,
+                'ctype' => $types[$tn][$k], // should always work!
+                'desc' => isset($cache[$tn][$k]) ? $cache[$tn][$k] : '',
+            );
+        }
+        return $ret;
+