@@ -16,7 +16,7 @@ static public function autoload($name)
16
16
$ name = preg_replace ("/([A-Z]{1})/ " , "_$1 " , $ name );
17
17
// Remove any underscores directly after a \
18
18
$ name = str_replace ("\\_ " , "\\" , $ name );
19
- // Append our main namespace
19
+ // Prepend our main namespace
20
20
$ name = "sqldiff " .strtolower ($ name );
21
21
22
22
$ base = dirname (dirname (__FILE__ ))."/ " ;
@@ -35,12 +35,12 @@ public function __construct(Datasource $from, Datasource $to)
35
35
{
36
36
$ this ->from = $ from ;
37
37
$ this ->to = $ to ;
38
-
39
- $ this ->compare ();
40
38
}
41
39
42
- private function compare ()
40
+ public function compare ()
43
41
{
42
+ $ changes = array ();
43
+
44
44
$ from_tables = $ this ->from ->get_tables ();
45
45
$ to_tables = $ this ->to ->get_tables ();
46
46
@@ -50,54 +50,67 @@ private function compare()
50
50
{
51
51
if ($ from_table ->name == $ to_table ->name )
52
52
{
53
- $ this ->compare_tables ($ from_table , $ to_table );
53
+ $ these_changes = $ this ->compare_tables ($ from_table , $ to_table );
54
+ $ changes = array_merge ($ changes , $ these_changes );
54
55
break ;
55
56
}
56
57
}
57
58
}
59
+
60
+ return $ changes ;
58
61
}
59
62
60
63
private function compare_tables ($ from , $ to )
61
64
{
65
+ $ changes = array ();
62
66
$ no_match = array ();
63
67
// For each of the columns in $from check to see if they exist in $to
64
- foreach ($ from ->columns as $ from_column )
68
+ foreach ($ from ->columns as $ from_key => $ from_column )
65
69
{
66
- foreach ($ to ->columns as $ to_column )
70
+ foreach ($ to ->columns as $ to_key => $ to_column )
67
71
{
68
72
if ($ from_column ->name == $ to_column ->name )
69
73
{
70
- $ this ->compare_columns ($ from_column , $ to_column );
74
+ $ these_changes = $ this ->compare_columns ($ from , $ to , $ from_key , $ to_key );
75
+ $ changes = array_merge ($ changes , $ these_changes );
71
76
unset($ no_match [$ from_column ->name ]);
72
77
break ;
73
78
}
74
79
else
75
80
{
76
- $ no_match [$ from_column ->name ] = true ;
81
+ $ no_match [$ from_column ->name ] = array ( $ from , $ from_key ) ;
77
82
}
78
83
}
79
84
}
80
85
81
86
if (!empty ($ no_match ))
82
87
{
83
- /**
84
- * @todo Deal with new columns that are in $from but not $to
85
- */
88
+ foreach ($ no_match as $ name => $ details )
89
+ {
90
+ $ changes [] = new \SqlDiff \Change ("add " , null , $ details [0 ], $ details [1 ], null , null );
91
+ }
86
92
}
93
+ return $ changes ;
87
94
}
88
95
89
- private function compare_columns ($ from , $ to )
96
+ private function compare_columns ($ from , $ to, $ from_key , $ to_key )
90
97
{
98
+ $ changes = array ();
99
+ $ from_col = $ from ->columns [$ from_key ];
100
+ $ to_col = $ to ->columns [$ to_key ];
101
+
91
102
// Check the column type
92
- if ($ from ->type != $ to ->type )
103
+ if ($ from_col ->type != $ to_col ->type )
93
104
{
94
- echo $ from -> name . " type: " . $ from-> type . " -> " . $ to-> type . PHP_EOL ;
105
+ $ changes [] = new \ SqlDiff \ Change ( " edit " , " type " , $ from, $ from_key , $ to, $ to_key ) ;
95
106
}
96
107
97
108
// Check the column length
98
- if ($ from ->length != $ to ->length )
109
+ if ($ from_col ->length != $ to_col ->length )
99
110
{
100
- echo $ from -> name . " length: " . $ from-> length . " -> " . $ to-> length . PHP_EOL ;
111
+ $ changes [] = new \ SqlDiff \ Change ( " edit " , " length " , $ from, $ from_key , $ to, $ to_key ) ;
101
112
}
113
+
114
+ return $ changes ;
102
115
}
103
116
}
0 commit comments