Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
MetadataTool.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/Table.php');
3 
15 
20  var $fieldDefs = null;
21 
25  var $tablename = null;
26 
31  var $columns = null;
32 
37  var $keyColumns = null;
38 
40  $this->tablename = $tablename;
41  }
42 
49  function isMetadataTable($tablename=null){
50  if ( !isset($tablename) ) $tablename = $this->tablename;
51  return (strstr( $tablename, '__metadata') == '__metadata');
52 
53  }
54 
61  function &getColumns($tablename=null, $usecache=true){
63  if (!isset($tablename) ) $tablename = $this->tablename;
64  $md_tablename = $tablename.'__metadata';
65  if ( !isset($this->columns) || !$usecache ){
66  $this->columns = array();
67  $sql = "show columns from `".$md_tablename."`";
68  $res = mysql_query($sql, $app->db());
69  if ( !$res ) trigger_error(mysql_error($app->db()), E_USER_ERROR);
70  if ( mysql_num_rows($res) == 0) trigger_error("No metadata table '{$md_tablename}' could be found.", E_USER_ERROR);
71 
72  while ( $row = mysql_fetch_assoc($res) ){
73  $this->columns[$row['Field']] = $row;
74  }
75  @mysql_free_result($res);
76  }
77  return $this->columns;
78 
79  }
80 
88  function &getKeyColumns($tablename=null, $usecache=true){
89  if (!isset($tablename) ) $tablename = $this->tablename;
90  $md_tablename = $tablename.'__metadata';
91  if ( !isset($this->keyColumns) || !$usecache ){
92  $this->keyColumns = array();
93  $cols = $this->getColumns($tablename, $usecache);
94  foreach (array_keys($cols) as $col){
95  if ( strcasecmp($this->columns[$col]['Key'], 'PRI') === 0 ){
96  $this->keyColumns[$this->columns[$col]['Field']] =& $this->columns[$col];
97  }
98  }
99  }
100 
101  return $this->keyColumns;
102 
103  }
104 
105 
106 
113  if ( !isset($tablename) ) $tablename = $this->tablename;
114  if ( !isset($this->fieldDefs) ){
115 
116 
117  import('Dataface/ConfigTool.php');
118  $configTool =& Dataface_ConfigTool::getInstance();
119  $this->fieldDefs = $configTool->loadConfig('metadata',$tablename);
120  foreach (array_keys($this->fieldDefs) as $key ){
121  $field =& $this->fieldDefs[$key];
122  $field['name'] = '__'.$key;
123  $field['Field'] = $field['name'];
124  if ( !isset($field['Type']) ) $field['Type'] = 'varchar(64)';
125  $this->fieldDefs['__'.$key] =& $field;
126  unset($this->fieldDefs[$key]);
127  unset($field);
128  }
129  }
130 
131  return $this->fieldDefs;
132 
133  }
134 
135 
144  if ( !isset($tablename) ) $tablename = $this->tablename;
147 
149 
150  if ( !Dataface_Table::tableExists($tablename.'__metadata', false) ){
151  $sql = "CREATE TABLE `{$tablename}__metadata` (
152  ";
153  foreach ($table->keys() as $field ){
154  $type = (strtolower($field['Type']) != 'container' ? $field['Type'] : 'varchar(64)');
155 
156  $sql .= "`{$field['name']}` {$type} DEFAULT NULL,
157  ";
158 
159  }
160  $metafields = $this->loadMetadataFieldDefs($tablename);
161  foreach ($metafields as $fieldname=>$field){
162  if ( @$field['Default'] ) $default = " DEFAULT '{$field['Default']}'";
163  else $default = '';
164  $sql .= "`{$fieldname}` {$field['Type']}{$default},";
165  }
166 
167  $keynames = array_keys($table->keys());
168  $sql .= "primary key (`".implode('`,`', $keynames)."`))";
169  $res = mysql_query($sql, $app->db());
170  if ( !$res ) trigger_error(mysql_error($res), E_USER_ERROR);
171  return true;
172 
173  }
174 
175  return false;
176 
177 
178  }
179 
189  if ( !isset($tablename) ) $tablename = $this->tablename;
193  $md_tablename = $tablename.'__metadata';
194  if ( !Dataface_Table::tableExists($md_tablename, false) ){
195  if ( $this->createMetadataTable($tablename) ) return true;
196  }
197  $cols =& $this->getColumns($tablename, false);
198 
199 
200  // First we have to go through all of the key fields of the subject table
201  // and make sure that they appear in the metadata table.
202  $updatePrimaryKey = false;
203  foreach ($table->keys() as $field){
204  if ( !isset($cols[$field['Field']]) ){
205  $updatePrimaryKey=true;
206  $default = ( @$field['Default'] ? " DEFAULT {$field['Default']}" : '');
207  $sql = "alter table `{$md_tablename}` add column `{$field['Field']}` {$field['Type']}{$default}";
208  $res = mysql_query($sql, $app->db());
209  if ( !$res ) trigger_error(mysql_error($app->db()), E_USER_ERROR);
210  }
211  }
212 
213  $table_keys =& $table->keys();
214 
215  //Next we have to go through all of the key fields in the metadata table ane make sure that they
216  // appear in the subject table primary keys.
217  foreach ($this->getKeyColumns($tablename, false) as $field){
218  if ( !isset($table_keys[$field['Field']]) ){
219  $updatePrimaryKey = true;
220  $sql = "alter table `{$md_tablename}` drop column `{$field['Field']}`";
221  $res = mysql_query($sql, $app->db());
222  if ( !$res ) trigger_error(mysql_error($app->db()), E_USER_ERROR);
223  }
224  }
225 
226  // If the primary key needed to be updated, we will update it now.
227  if ( $updatePrimaryKey ){
228  // The primary key needs to be updated
229  $sql = "drop primary key";
230  @mysql_query($sql, $app->db());
231  $sql = "alter table `{$md_tablename}` add primary key (`".implode('`,`',array_keys($table->keys()))."`)";
232  $res = mysql_query($sql, $app->db());
233  if ( !$res ) trigger_error(mysql_error($app->db()), E_USER_ERROR);
234 
235  }
236 
237  // Now we need to make sure that all of the prescribed meta fields are
238  // in the metadata field.
239 
240  $fielddefs = $this->loadMetadataFieldDefs($tablename);
241  $cols = $this->getColumns($tablename, false);
242 
243  foreach ($fielddefs as $field){
244  if ( !isset($cols[$field['Field']]) ){
245  $default = (@$field['Default'] ? " DEFAULT {$field['Default']}": '');
246  $sql = "alter table `{$md_tablename}` add column `{$field['Field']}` {$field['Type']}{$default}";
247  $res = mysql_query($sql, $app->db());
248  if ( !$res ) trigger_error(mysql_error($app->db()), E_USER_ERROR);
249  }
250  }
251  return true;
252 
253  }
254 }