HTML/FlexyFramework/Generator.php
[pear] / HTML / FlexyFramework / Generator.php
1 <?php
2
3
4 require_once 'DB/DataObject/Generator.php';
5
6
7 class HTML_FlexyFramework_Generator extends DB_DataObject_Generator 
8 {
9     // block class generation.
10     static $generateClasses = false;
11     
12     function generateClasses()
13     {
14         //echo "GENERATE CLASSES?";
15         if (!HTML_FlexyFramework_Generator::$generateClasses ) {
16             return;
17         }
18         
19         parent::generateClasses();
20     }
21     
22     
23     function generateReaders()
24     {
25         $options = &PEAR::getStaticProperty('DB_DataObject','options');
26         
27         $out = array();
28         foreach($this->tables as $this->table) {
29             $this->table        = trim($this->table);
30             
31             $out = array_merge($out, $this->_generateReader($this->table));
32             
33             
34         }
35         //echo '<PRE>';print_r($out);exit;
36          
37         file_put_contents($options["ini_{$this->_database}"] . '.reader', serialize($out));
38          
39     }
40     function _generateReader($table)
41     {
42         $DB = $this->getDatabaseConnection();
43         $dbtype = $DB->phptype;
44         $def = $this->_definitions[$table] ;
45         $ret = array();
46         foreach($def as $t) {
47              switch (strtoupper($t->type)) {
48
49                 case 'INT':
50                 case 'INT2':    // postgres
51                 case 'INT4':    // postgres
52                 case 'INT8':    // postgres
53                 case 'SERIAL4': // postgres
54                 case 'SERIAL8': // postgres
55                 case 'INTEGER':
56                 case 'TINYINT':
57                 case 'SMALLINT':
58                 case 'MEDIUMINT':
59                 case 'BIGINT':
60                 // wierd ones..
61                 case 'YEAR':
62                 
63                     $ret[$table.'.'.$t->name] = array('type' => 'int');
64                     continue;
65                
66                 case 'REAL':
67                 case 'DOUBLE':
68                 case 'DOUBLE PRECISION': // double precision (firebird)
69                 case 'FLOAT':
70                 case 'FLOAT4': // real (postgres)
71                 case 'FLOAT8': // double precision (postgres)
72                 case 'DECIMAL':
73                 case 'MONEY':  // mssql and maybe others
74                 case 'NUMERIC':
75                 case 'NUMBER': // oci8 
76                     $ret[$table.'.'.$t->name] = array('type' => 'float'); //???
77                     break;
78                     
79                 case 'BIT':
80                 case 'BOOL':   
81                 case 'BOOLEAN':   
82                     $ret[$table.'.'.$t->name] = array('type' => 'boolean'); //???
83                     // postgres needs to quote '0'
84                     if ($dbtype == 'pgsql') {
85                         ///$type +=  DB_DATAOBJECT_STR;
86                     }
87                     break;
88                     
89                 case 'STRING':
90                 case 'CHAR':
91                 case 'VARCHAR':
92                 case 'VARCHAR2':
93                 case 'TINYTEXT':
94                 
95                 case 'ENUM':
96                 case 'SET':         // not really but oh well
97                 
98                 case 'POINT':       // mysql geometry stuff - not really string - but will do..
99                 
100                 case 'TIMESTAMPTZ': // postgres
101                 case 'BPCHAR':      // postgres
102                 case 'INTERVAL':    // postgres (eg. '12 days')
103                 
104                 case 'CIDR':        // postgres IP net spec
105                 case 'INET':        // postgres IP
106                 case 'MACADDR':     // postgress network Mac address.
107                 
108                 case 'INTEGER[]':   // postgres type
109                 case 'BOOLEAN[]':   // postgres type
110                 
111                 
112                 case 'TEXT':
113                 case 'MEDIUMTEXT':
114                 case 'LONGTEXT':
115                 case 'BLOB':       /// these should really be ignored!!!???
116                 case 'TINYBLOB':
117                 case 'MEDIUMBLOB':
118                 case 'LONGBLOB':
119                 
120                 case 'CLOB': // oracle character lob support
121                 
122                 case 'BYTEA':   // postgres blob support..
123                     $ret[$table.'.'.$t->name] =  $t->name; // strings are not sent as arrays..
124                    // $type = DB_DATAOBJECT_STR;
125                     break;
126                 
127                 
128                 
129                 
130                 case 'DATE':    
131                     $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d'); //???
132                     break;
133                     
134                 case 'TIME':    
135                     $ret[$table.'.'.$t->name] = $t->name; // technically not...
136                     break;    
137                     
138                 
139                 case 'DATETIME': 
140                     $ret[$table.'.'.$t->name] = array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s'); //???
141                     break;    
142                     
143                 case 'TIMESTAMP': // do other databases use this???
144                     
145                     $ret[$table.'.'.$t->name] =   ($dbtype == 'mysql') ?
146                          array('type' => 'float') : 
147                         array('type' => 'date', 'dateFormat' => 'Y-m-d H:i:s');
148                     break;    
149                     
150                 
151                 
152                     
153                     
154                 default:     
155                     $ret[$table.'.'.$t->name] = $t->name;
156                     break;
157             }
158         }
159         
160         return $ret;
161         
162         
163     }
164
165     static function writeCache($iniCacheTmp, $iniCache)
166     {
167          
168         $ff = HTML_FlexyFramework::get();
169         
170        //var_dump($iniCacheTmp);
171        // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
172         // only unpdate if nothing went wrong.
173         if (filesize($iniCacheTmp)) {
174             if (file_exists($iniCache)) {
175                 unlink($iniCache);
176             }
177             rename($iniCacheTmp, $iniCache);
178         }
179         
180         // readers..
181         if (filesize($iniCacheTmp.'.reader')) {
182             if (file_exists($iniCache.'.reader')) {
183                 unlink($iniCache.'.reader');
184             }
185             rename($iniCacheTmp.'.reader', $iniCache.'.reader');
186         }
187         
188         
189         // merge and set links..
190         
191         $inis = explode(PATH_SEPARATOR,$ff->dataObjectsOriginalIni);
192         $links = array();
193         foreach($inis as $ini) {
194             $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
195             if (!file_exists($ini)) {
196                 // try scanning the directory for another ini file..
197                 $ar = glob(dirname($ini).'/*.ini');
198                 if (empty($ar)) {
199                     continue;
200                 }
201                 sort($ar);
202                 // first file.. = with links removed..
203                 $ini = preg_replace('/\.links\./' , '.', $ar[0]);
204                 $ini = preg_replace('/\.ini$/', '.links.ini', $ini);
205             }
206             $links = array_merge_recursive($links , parse_ini_file($ini, true));
207             
208         }
209         $iniLinksCache = preg_replace('/\.ini$/', '.links.ini', $iniCache);
210         $out = array();
211         foreach($links as $tbl=>$ar) {
212             $out[] = '['. $tbl  .']';
213             foreach ($ar as $k=>$v) {
214                 $out[] = $k . '=' .$v;
215             }
216             $out[] = '';
217         }
218         if (count($out)) {
219             file_put_contents($iniLinksCache. '.tmp', implode("\n", $out));
220             if (file_exists($iniLinksCache)) {
221                 unlink($iniLinksCache);
222             }
223             rename($iniLinksCache. '.tmp', $iniLinksCache);
224         }
225     }
226     
227     
228 }