-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.php
295 lines (265 loc) · 8.03 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
require 'vendor/autoload.php';
use \Dbff\Element\Charset;
use \Dbff\Element\Column;
use \Dbff\Element\Type;
use \Dbff\Element\TypeProps\IntProps;
use \Dbff\Element\TypeProps\CharProps;
use \Dbff\Element\Table;
use \Dbff\DbffableCollection as Collection;
use \Dbff\Element\Index;
use \Dbff\Element\Database;
use \Dbff\Parser\TypeParser;
use \Dbff\Parser\IndexParser;
use \Dbff\Parser\ColumnParser;
use \Dbff\Parser\TableParser;
use \Dbff\Builder\TypeBuilder;
use \Dbff\Builder\IndexBuilder;
use \Dbff\Builder\ColumnBuilder;
use \Dbff\Builder\TableBuilder;
$dbffer = new \Dbff\Dbffer();
/*
* Using dbff structures, filling manually
*/
// Types
$type1 = new Type('int', new IntProps(10, true, false));
$type2 = new Type('int', new IntProps(8, true, true));
$type3 = new Type('text', new CharProps(100, new Charset('utf-8')));
showDbff($dbffer->compare($type1, $type2), 'Types');
/*
* [
* 'props' => [ // type props are different
* 'length' => [ // different length: 1st element has length 10, 2nd — 8
* 0 => 10,
* 1 => 8,
* ],
* 'zerofill' => [ // zerofill options: 1st element hasn't zerofill, 2nd has
* 0 => false,
* 1 => true,
* ],
* ],
* ]
*/
showDbff($dbffer->compare($type1, $type3), 'Types');
/*
* [
* 'name' => [ // type names are different: 1st type is int, 2nd — text
* 0 => 'int',
* 1 => 'text',
* ],
* ]
*/
// Columns
$col1 = new Column('id', $type1, true, 0, true);
$col2 = new Column('id2', $type2, true, 100, false); // Names are not compared
showDbff($dbffer->compare($col1, $col2), 'Columns');
/*
* [
* 'type' => [ // types are different...
* 'props' => [ // ...to be more precise — types props are different...
* 'length' => [ // ...1st has length 10, 2nd — 8
* 0 => 10,
* 1 => 8,
* ],
* 'zerofill' => [
* 0 => false,
* 1 => true,
* ],
* ],
* ],
* 'default' => [ // default values for these columns are different
* 0 => 0, // 1st has default 0
* 1 => 100, // 2nd — 100
* ],
* 'isautoinc' => [ // 1st is autoincrement column, 2nd is not
* 0 => true,
* 1 => false,
* ],
* ]
*/
// Tables
$index = new Index('', 'primary', ['id'], 'hash');
$table1 = new Table(
'Post',
new Collection([$col1, $col2]),
new Collection([]),
false,
new Charset('', 'utf8_general_ci'),
'InnoDB',
20
);
$table2 = new Table(
'Post',
new Collection([$col1, $col2]),
new Collection([$index]),
true,
new Charset('', 'utf8_general_ci'),
'InnoDB',
20
);
// temporary field doesn't compare
showDbff($dbffer->compare($table1, $table2), 'Tables');
/*
* [
* 'indices' => [ // indices are different
* 'length' => [ // 1st table has no indices, 2nd has one
* 0 => 0,
* 1 => 1,
* ],
* 'names' => [
* 0 => [],
* 1 => [
* 0 => '' // 2nd table has primary key (the only index with empty name)
* ],
* ],
* ],
* ]
*/
// Databases
$table3 = new Table(
'Post2',
new Collection([$col1]),
new Collection([]),
true,
new Charset('', 'utf8_general_ci'),
'MyISAM',
0
);
$db1 = new Database('db1', new Collection([$table1, $table2]), new Charset('utf8', 'utf8_general_ci'));
$db2 = new Database('db2', new Collection([$table1, $table3]), new Charset('utf16', 'utf8_general_ci'));
showDbff($dbffer->compare($db1, $db2), 'Databases');
/*
* [
* 'tables' => [ // tables have differences
* 'length' => [ // 1st db has 1 table, 2nd — 2
* 0 => 1,
* 1 => 2,
* ],
* 'names' => [
* 0 => [], // 1st db misses table `Post2` ...
* 1 => [ // ... or 2nd db has extra table `Post2`, depends on your logic
* 1 => 'Post2',
* ],
* ],
* 'dbff' => [ // tables diff
* 'Post' => [ // tables `Post` are different
* 'indices' => [ // and so on ...
* 'length' => [
* 0 => 1,
* 1 => 0,
* ],
* 'names' => [
* 0 => [
* 0 => '',
* ],
* 1 => [],
* ],
* ],
* ],
* ],
* ],
* 'charset' => [
* 0 => 'utf8',
* 1 => 'utf16',
* ],
* ]
*/
/*
* Using parsers
*/
$columnParser = new ColumnParser();
$columnStruct = $columnParser->parse("`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT");
showDbff($columnStruct, 'Column struct');
/*
* string is parsed into struct
*
* [
* 'name' => 'id', // column's name
* 'type' => 'INT(10) UNSIGNED', // type was not parsed because column parser knows nothing about types
* 'null' => false, // can be null? no
* 'autoinc' => true, // is autoincrement? yes
* 'default' => '', // default value — empty string means zero value of column's type
* ]
*/
$typeParser = new TypeParser();
$typeStruct = $typeParser->parse($columnStruct['type']);
showDbff($typeStruct, 'Type struct');
/*
* type struct depends on concrete type group
*
* [
* 'group' => 'int', // one of integer types
* 'type' => 'int', // INT type
* 'length' => 10, // output length
* 'unsigned' => false, // unsigned or not
* 'zerofill' => true, // fill with zero values? yes
* ]
*/
/*
* Using builders: create elements directly from strings
*/
$builder = new TableBuilder(
new TableParser(),
new ColumnBuilder(
new ColumnParser(),
new TypeBuilder(new TypeParser())
),
new IndexBuilder(new IndexParser())
);
// or you can use \Dbff\Builder\DefaultBuildersFactory::createTableBuilder();
$table1 = $builder->createFromString(
"CREATE TABLE `Post` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`anketa_id` INT(10) UNSIGNED,
`data` TEXT NOT NULL COLLATE utf8_general_ci,
`flags` TINYINT(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `user` (`anketa_id`)
)
ENGINE=InnoDB
AUTO_INCREMENT=51
COLLATE=utf8_unicode_ci;"
);
$table2 = $builder->createFromString(
"CREATE TABLE `Post` (
`id` INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`anketa_id` INT(10) UNSIGNED,
`data` TEXT NOT NULL COLLATE utf8_unicode_ci,
`flags` TINYINT(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `user` (`anketa_id`)
)
COLLATE=utf8_unicode_ci"
);
showDbff($dbffer->compare($table1, $table2), 'Tables');
/*
* run and look at the results
*/
// look at the DatabaseBuiler::createFromString phpdoc comment to find out string format
$dbBuilder = \Dbff\Builder\DefaultBuildersFactory::createDatabaseBuilder();
$db1 = $dbBuilder->createFromString(
"create database `database` default character set utf8
CREATE TABLE `Post` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
)"
);
$db2 = $dbBuilder->createFromDbAndTablesStrings(
"create database `database` default character set utf8",
[
"CREATE TABLE `User` (
`name` VARCHAR(100),
PRIMARY KEY (`name`),
)"
]
);
showDbff($dbffer->compare($db1, $db2), 'Databases');
// simple output helper
function showDbff($dbff, $comment = '')
{
if ($comment) {
echo $comment . "\n";
}
var_export($dbff);
echo "\n\n";
}