![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 00003 00004 class DB_Sync { 00005 00006 var $db1; 00007 var $db2; 00008 00009 var $table1; 00010 var $table2; 00011 00012 var $table1Data; 00013 var $table2Data; 00014 00015 var $renamed = array(); 00016 var $listeners = array(); 00017 00018 function DB_Sync($db1, $db2, $table1=null, $table2=null, $renamed=null){ 00019 $this->db1 = $db1; 00020 $this->db2 = $db2; 00021 00022 $this->init($table1, $table2, $renamed); 00023 } 00024 00025 00034 function equals($table1=null, $table2=null){ 00035 00036 $this->init($table1,$table2); 00037 echo "Now here"; 00038 print_r($this->table1Data); 00039 print_r($this->table2Data); 00040 return ( $this->table1Data == $this->table2Data ); 00041 00042 } 00043 00044 function init($table1, $table2, $renamed=null){ 00045 00046 if ( isset($table1) and isset($table2) and ($table1 != $this->table1 || $table2 != $this->table2) ){ 00047 00048 00049 $this->table1 = $table1; 00050 $this->table2 = $table2; 00051 if ( isset($renamed) ) $this->renamed = $renamed; 00052 $this->loadTableData(); 00053 00054 } 00055 } 00056 00057 function checkTableNames(){ 00058 00059 if ( !isset($this->table1) ){ 00060 trigger_error("Attempt to load data for tables in DB_Sync, but table 1 has not been specified.", E_USER_ERROR); 00061 } 00062 00063 if ( !isset($this->table2) ){ 00064 trigger_error("Attempt to load data for tables in DB_Sync, but table 2 has not been specified.", E_USER_ERROR); 00065 } 00066 00067 if ( !preg_match('/^[a-zA-Z0-9_]+$/', $this->table1) ){ 00068 trigger_error("The table '{$this->table1}' has an invalid name.", E_USER_ERROR); 00069 } 00070 00071 if ( !preg_match('/^[a-zA-Z0-9_]+$/', $this->table2) ){ 00072 trigger_error("The table '{$this->table2}' has an invalid name.", E_USER_ERROR); 00073 } 00074 00075 } 00076 00077 00078 00082 function loadTableData(){ 00083 00084 $this->checkTableNames(); 00085 00086 $res = mysql_query("show full fields from `".$this->table1."`", $this->db1); 00087 if ( !$res ) trigger_error(mysql_error($this->db1)); 00088 00089 00090 $this->table1Data = array(); 00091 while ( $row = mysql_fetch_assoc($res) ){ 00092 $this->table1Data[$row['Field']] = $row; 00093 } 00094 00095 @mysql_free_result($res); 00096 00097 00098 $res = mysql_query("show columns from `".$this->table2."`", $this->db2); 00099 if ( !$res ) trigger_error(mysql_error($this->db2)); 00100 00101 $this->table2Data = array(); 00102 while ( $row = mysql_fetch_assoc($res) ){ 00103 $this->table2Data[$row['Field']] = $row; 00104 } 00105 00106 @mysql_free_result($res); 00107 00108 00109 } 00110 00116 function fieldArrayToSQLDef($field){ 00117 00118 if ( $field['Default'] ){ 00119 00120 if ( strcasecmp($field['Default'], 'NULL') === 0 ){ 00121 00122 $default = 'default NULL'; 00123 00124 } else if ( $field['Default'] ) { 00125 00126 $default = 'default \''.$field['Default'].'\''; 00127 } else { 00128 $default = ''; 00129 } 00130 00131 } else { 00132 00133 $default = ''; 00134 00135 } 00136 00137 if ( $field['Collation'] and strcasecmp($field['Collation'],'null') !== 0){ 00138 00139 $charset = 'CHARACTER SET '.substr($field['Collation'],0, strpos($field['Collation'], '_')).' COLLATE '. 00140 $field['Collation']; 00141 00142 } else { 00143 00144 $charset = ''; 00145 } 00146 00147 if ( $field['Null'] ){ 00148 00149 $null = ( ( strcasecmp('yes',$field['Null'])===0 ) ? '' : 'NOT NULL'); 00150 } else { 00151 $null = ''; 00152 } 00153 00154 00155 00156 00157 00158 return "`{$field['Field']}` {$field['Type']} {$charset} {$null} {$field['Extra']} {$default}"; 00159 00160 } 00161 00165 function syncField($fieldname, $after=null, $renameMap=null){ 00166 00167 if (isset($renameMap) ) $this->renamed = $renameMap; 00168 00169 00170 00171 if ( !isset($this->table1Data[$fieldname]) ){ 00172 00173 // Table 1 does not have this field... see if it has been renamed. 00174 00175 if ( isset($this->renamed[$fieldname]) ){ 00176 00177 $newname = @$this->renamed[$fieldname]; 00178 00179 if ( !$newname ){ 00180 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); 00181 } 00182 00183 $sql = "alter table `{$this->table2}` change `{$fieldname}` ".$this->fieldArrayToSQLDef($this->table1Data[$newname]); 00184 $res = mysql_query($sql, $this->db2); 00185 if ( !$res ) trigger_error(mysql_error($this->db2), E_USER_ERROR); 00186 00187 } else { 00188 00189 trigger_error("Attempt to syncronize field '{$fieldname}' but the source table has no such field.", E_USER_ERROR); 00190 } 00191 00192 00193 } else if ( !isset( $this->table2Data[$fieldname] ) ) { 00194 00195 $sql = "alter table `{$this->table2}` add ".$this->fieldArrayToSQLDef($this->table1Data[$fieldname]); 00196 if ( isset($after) ){ 00197 $sql .= "after `{$after}`"; 00198 } 00199 $res = mysql_query($sql, $this->db2); 00200 if ( !$res ) trigger_error($sql."\n".mysql_error($this->db2), E_USER_ERROR); 00201 00202 } else if ( $this->table1Data[$fieldname] != $this->table2Data[$fieldname] ) { 00203 00204 $sql = "alter table `{$this->table2}` change `{$fieldname}` ".$this->fieldArrayToSQLDef($this->table1Data[$fieldname]); 00205 $res = mysql_query($sql, $this->db2); 00206 if ( !$res ) trigger_error(mysql_error($this->db2), E_USER_ERROR); 00207 00208 } else { 00209 00210 // nothing to do here. 00211 } 00212 } 00213 00214 00215 00216 function syncTables($table1=null, $table2=null, $renamedMap=null){ 00217 00218 $this->init($table1, $table2, $renamedMap); 00219 00220 if (!$this->equals() ){ 00221 echo "Here"; 00222 $positions = array(); 00223 $fieldnames = array_keys($this->table1Data); 00224 $i=0; 00225 foreach ($fieldnames as $f){ 00226 $positions[$f] = $i++; 00227 00228 } 00229 00230 $fields = array_merge(array_keys($this->table1Data), array_keys($this->table2Data)); 00231 $fields = array_unique($fields); 00232 print_r($fields); 00233 foreach ($fields as $field ){ 00234 if ( isset( $positions[$field] ) and $positions[$field] > 0 ){ 00235 $after = $fieldnames[$positions[$field]-1]; 00236 } else if ( isset($positions[$field]) ){ 00237 $after = "first"; 00238 } else { 00239 $after = null; 00240 } 00241 $this->syncField($field, $after); 00242 } 00243 } 00244 00245 00246 00247 00248 } 00249 } 00250 00251 ?>