Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
Sync.php
Go to the documentation of this file.
1 <?php
2 
3 
4 class DB_Sync {
5 
6  var $db1;
7  var $db2;
8 
9  var $table1;
10  var $table2;
11 
14 
15  var $renamed = array();
16  var $listeners = array();
17 
18  function DB_Sync($db1, $db2, $table1=null, $table2=null, $renamed=null){
19  $this->db1 = $db1;
20  $this->db2 = $db2;
21 
22  $this->init($table1, $table2, $renamed);
23  }
24 
25 
34  function equals($table1=null, $table2=null){
35 
36  $this->init($table1,$table2);
37  echo "Now here";
38  print_r($this->table1Data);
39  print_r($this->table2Data);
40  return ( $this->table1Data == $this->table2Data );
41 
42  }
43 
44  function init($table1, $table2, $renamed=null){
45 
46  if ( isset($table1) and isset($table2) and ($table1 != $this->table1 || $table2 != $this->table2) ){
47 
48 
49  $this->table1 = $table1;
50  $this->table2 = $table2;
51  if ( isset($renamed) ) $this->renamed = $renamed;
52  $this->loadTableData();
53 
54  }
55  }
56 
57  function checkTableNames(){
58 
59  if ( !isset($this->table1) ){
60  trigger_error("Attempt to load data for tables in DB_Sync, but table 1 has not been specified.", E_USER_ERROR);
61  }
62 
63  if ( !isset($this->table2) ){
64  trigger_error("Attempt to load data for tables in DB_Sync, but table 2 has not been specified.", E_USER_ERROR);
65  }
66 
67  if ( !preg_match('/^[a-zA-Z0-9_]+$/', $this->table1) ){
68  trigger_error("The table '{$this->table1}' has an invalid name.", E_USER_ERROR);
69  }
70 
71  if ( !preg_match('/^[a-zA-Z0-9_]+$/', $this->table2) ){
72  trigger_error("The table '{$this->table2}' has an invalid name.", E_USER_ERROR);
73  }
74 
75  }
76 
77 
78 
82  function loadTableData(){
83 
84  $this->checkTableNames();
85 
86  $res = mysql_query("show full fields from `".$this->table1."`", $this->db1);
87  if ( !$res ) trigger_error(mysql_error($this->db1));
88 
89 
90  $this->table1Data = array();
91  while ( $row = mysql_fetch_assoc($res) ){
92  $this->table1Data[$row['Field']] = $row;
93  }
94 
95  @mysql_free_result($res);
96 
97 
98  $res = mysql_query("show columns from `".$this->table2."`", $this->db2);
99  if ( !$res ) trigger_error(mysql_error($this->db2));
100 
101  $this->table2Data = array();
102  while ( $row = mysql_fetch_assoc($res) ){
103  $this->table2Data[$row['Field']] = $row;
104  }
105 
106  @mysql_free_result($res);
107 
108 
109  }
110 
117 
118  if ( $field['Default'] ){
119 
120  if ( strcasecmp($field['Default'], 'NULL') === 0 ){
121 
122  $default = 'default NULL';
123 
124  } else if ( $field['Default'] ) {
125 
126  $default = 'default \''.$field['Default'].'\'';
127  } else {
128  $default = '';
129  }
130 
131  } else {
132 
133  $default = '';
134 
135  }
136 
137  if ( $field['Collation'] and strcasecmp($field['Collation'],'null') !== 0){
138 
139  $charset = 'CHARACTER SET '.substr($field['Collation'],0, strpos($field['Collation'], '_')).' COLLATE '.
140  $field['Collation'];
141 
142  } else {
143 
144  $charset = '';
145  }
146 
147  if ( $field['Null'] ){
148 
149  $null = ( ( strcasecmp('yes',$field['Null'])===0 ) ? '' : 'NOT NULL');
150  } else {
151  $null = '';
152  }
153 
154 
155 
156 
157 
158  return "`{$field['Field']}` {$field['Type']} {$charset} {$null} {$field['Extra']} {$default}";
159 
160  }
161 
165  function syncField($fieldname, $after=null, $renameMap=null){
166 
167  if (isset($renameMap) ) $this->renamed = $renameMap;
168 
169 
170 
171  if ( !isset($this->table1Data[$fieldname]) ){
172 
173  // Table 1 does not have this field... see if it has been renamed.
174 
175  if ( isset($this->renamed[$fieldname]) ){
176 
177  $newname = @$this->renamed[$fieldname];
178 
179  if ( !$newname ){
180  trigger_error("Attempt to rename field '{$fieldname}' in table '{$this->table2}' to {$newname} but the source table '{$this->table1}' has no such field to copy.", E_USER_ERROR);
181  }
182 
183  $sql = "alter table `{$this->table2}` change `{$fieldname}` ".$this->fieldArrayToSQLDef($this->table1Data[$newname]);
184  $res = mysql_query($sql, $this->db2);
185  if ( !$res ) trigger_error(mysql_error($this->db2), E_USER_ERROR);
186 
187  } else {
188 
189  trigger_error("Attempt to syncronize field '{$fieldname}' but the source table has no such field.", E_USER_ERROR);
190  }
191 
192 
193  } else if ( !isset( $this->table2Data[$fieldname] ) ) {
194 
195  $sql = "alter table `{$this->table2}` add ".$this->fieldArrayToSQLDef($this->table1Data[$fieldname]);
196  if ( isset($after) ){
197  $sql .= "after `{$after}`";
198  }
199  $res = mysql_query($sql, $this->db2);
200  if ( !$res ) trigger_error($sql."\n".mysql_error($this->db2), E_USER_ERROR);
201 
202  } else if ( $this->table1Data[$fieldname] != $this->table2Data[$fieldname] ) {
203 
204  $sql = "alter table `{$this->table2}` change `{$fieldname}` ".$this->fieldArrayToSQLDef($this->table1Data[$fieldname]);
205  $res = mysql_query($sql, $this->db2);
206  if ( !$res ) trigger_error(mysql_error($this->db2), E_USER_ERROR);
207 
208  } else {
209 
210  // nothing to do here.
211  }
212  }
213 
214 
215 
216  function syncTables($table1=null, $table2=null, $renamedMap=null){
217 
218  $this->init($table1, $table2, $renamedMap);
219 
220  if (!$this->equals() ){
221  echo "Here";
222  $positions = array();
223  $fieldnames = array_keys($this->table1Data);
224  $i=0;
225  foreach ($fieldnames as $f){
226  $positions[$f] = $i++;
227 
228  }
229 
230  $fields = array_merge(array_keys($this->table1Data), array_keys($this->table2Data));
231  $fields = array_unique($fields);
232  print_r($fields);
233  foreach ($fields as $field ){
234  if ( isset( $positions[$field] ) and $positions[$field] > 0 ){
235  $after = $fieldnames[$positions[$field]-1];
236  } else if ( isset($positions[$field]) ){
237  $after = "first";
238  } else {
239  $after = null;
240  }
241  $this->syncField($field, $after);
242  }
243  }
244 
245 
246 
247 
248  }
249 }
250 
251 ?>