38 print_r($this->table1Data);
39 print_r($this->table2Data);
40 return ( $this->table1Data == $this->table2Data );
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);
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);
67 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $this->table1) ){
68 trigger_error(
"The table '{$this->table1}' has an invalid name.", E_USER_ERROR);
71 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $this->table2) ){
72 trigger_error(
"The table '{$this->table2}' has an invalid name.", E_USER_ERROR);
86 $res = mysql_query(
"show full fields from `".$this->table1.
"`", $this->db1);
87 if ( !$res ) trigger_error(mysql_error($this->db1));
90 $this->table1Data = array();
91 while ( $row = mysql_fetch_assoc($res) ){
92 $this->table1Data[$row[
'Field']] = $row;
95 @mysql_free_result($res);
98 $res = mysql_query(
"show columns from `".$this->table2.
"`", $this->db2);
99 if ( !$res ) trigger_error(mysql_error($this->db2));
101 $this->table2Data = array();
102 while ( $row = mysql_fetch_assoc($res) ){
103 $this->table2Data[$row[
'Field']] = $row;
106 @mysql_free_result($res);
120 if ( strcasecmp(
$field[
'Default'],
'NULL') === 0 ){
122 $default =
'default NULL';
124 }
else if (
$field[
'Default'] ) {
126 $default =
'default \''.$field[
'Default'].
'\'';
137 if (
$field[
'Collation'] and strcasecmp(
$field[
'Collation'],
'null') !== 0){
139 $charset =
'CHARACTER SET '.substr(
$field[
'Collation'],0, strpos(
$field[
'Collation'],
'_')).
' COLLATE '.
149 $null = ( ( strcasecmp(
'yes',
$field[
'Null'])===0 ) ?
'' :
'NOT NULL');
158 return "`{$field['Field']}` {$field['Type']} {$charset} {$null} {$field['Extra']} {$default}";
167 if (isset($renameMap) ) $this->renamed = $renameMap;
175 if ( isset($this->renamed[$fieldname]) ){
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);
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);
189 trigger_error(
"Attempt to syncronize field '{$fieldname}' but the source table has no such field.", E_USER_ERROR);
193 }
else if ( !isset( $this->table2Data[$fieldname] ) ) {
195 $sql =
"alter table `{$this->table2}` add ".$this->fieldArrayToSQLDef($this->table1Data[$fieldname]);
196 if ( isset($after) ){
197 $sql .=
"after `{$after}`";
199 $res = mysql_query($sql, $this->db2);
200 if ( !$res ) trigger_error($sql.
"\n".mysql_error($this->db2), E_USER_ERROR);
202 }
else if ( $this->table1Data[$fieldname] != $this->table2Data[$fieldname] ) {
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);
222 $positions = array();
223 $fieldnames = array_keys($this->table1Data);
225 foreach ($fieldnames as
$f){
226 $positions[
$f] = $i++;
230 $fields = array_merge(array_keys($this->table1Data), array_keys($this->table2Data));
234 if ( isset( $positions[$field] ) and $positions[$field] > 0 ){
235 $after = $fieldnames[$positions[
$field]-1];
236 }
else if ( isset($positions[$field]) ){