File tree Expand file tree Collapse file tree 8 files changed +127
-2
lines changed Expand file tree Collapse file tree 8 files changed +127
-2
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ services:
12
12
' --log_bin=binlog' ,
13
13
' --max_binlog_size=8M' ,
14
14
' --binlog_format=row' ,
15
- ' --server-id=1'
15
+ ' --server-id=1' ,
16
+ ' --binlog_rows_query_log_events=ON'
16
17
]
17
18
environment :
18
19
- MYSQL_ROOT_PASSWORD=root
Original file line number Diff line number Diff line change @@ -17,4 +17,5 @@ enum ConstEventsNames: string
17
17
case TABLE_MAP = 'tableMap ' ;
18
18
case WRITE = 'write ' ;
19
19
case FORMAT_DESCRIPTION = 'format description ' ;
20
+ case ROWS_QUERY = 'rows_query ' ;
20
21
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MySQLReplication \Event \DTO ;
6
+
7
+ use MySQLReplication \Definitions \ConstEventsNames ;
8
+ use MySQLReplication \Event \EventInfo ;
9
+ use function get_object_vars ;
10
+ use const PHP_EOL ;
11
+
12
+ class RowsQueryDTO extends EventDTO
13
+ {
14
+ private ConstEventsNames $ type = ConstEventsNames::ROWS_QUERY ;
15
+
16
+ public function __construct (
17
+ EventInfo $ eventInfo ,
18
+ public readonly string $ query ,
19
+ ) {
20
+ parent ::__construct ($ eventInfo );
21
+ }
22
+
23
+ public function __toString (): string
24
+ {
25
+ return PHP_EOL .
26
+ '=== Event ' . $ this ->getType () . ' === ' . PHP_EOL .
27
+ 'Date: ' . $ this ->eventInfo ->getDateTime () . PHP_EOL .
28
+ 'Log position: ' . $ this ->eventInfo ->pos . PHP_EOL .
29
+ 'Event size: ' . $ this ->eventInfo ->size . PHP_EOL .
30
+ 'Query: ' . $ this ->query . PHP_EOL ;
31
+ }
32
+
33
+ public function getType (): string
34
+ {
35
+ return $ this ->type ->value ;
36
+ }
37
+
38
+ public function jsonSerialize (): array
39
+ {
40
+ return get_object_vars ($ this );
41
+ }
42
+ }
Original file line number Diff line number Diff line change @@ -120,6 +120,11 @@ private function makeEvent(BinaryDataReader $binaryDataReader): ?EventDTO
120
120
);
121
121
}
122
122
123
+ // The Rows Query Log Event will be triggered with enabled MySQL Config `binlog_rows_query_log_events`
124
+ if ($ eventInfo ->type === ConstEventType::ROWS_QUERY_LOG_EVENT ->value ) {
125
+ return (new RowsQueryEvent ($ eventInfo , $ binaryDataReader , $ this ->binLogServerInfo ))->makeRowsQueryDTO ();
126
+ }
127
+
123
128
if ($ eventInfo ->type === ConstEventType::FORMAT_DESCRIPTION_EVENT ->value ) {
124
129
return new FormatDescriptionEventDTO ($ eventInfo );
125
130
}
Original file line number Diff line number Diff line change 13
13
use MySQLReplication \Event \DTO \MariaDbGtidLogDTO ;
14
14
use MySQLReplication \Event \DTO \QueryDTO ;
15
15
use MySQLReplication \Event \DTO \RotateDTO ;
16
+ use MySQLReplication \Event \DTO \RowsQueryDTO ;
16
17
use MySQLReplication \Event \DTO \TableMapDTO ;
17
18
use MySQLReplication \Event \DTO \UpdateRowsDTO ;
18
19
use MySQLReplication \Event \DTO \WriteRowsDTO ;
@@ -35,6 +36,7 @@ public static function getSubscribedEvents(): array
35
36
ConstEventsNames::MARIADB_GTID ->value => 'onMariaDbGtid ' ,
36
37
ConstEventsNames::FORMAT_DESCRIPTION ->value => 'onFormatDescription ' ,
37
38
ConstEventsNames::HEARTBEAT ->value => 'onHeartbeat ' ,
39
+ ConstEventsNames::ROWS_QUERY ->value => 'onRowsQuery ' ,
38
40
];
39
41
}
40
42
@@ -93,6 +95,11 @@ public function onHeartbeat(HeartbeatDTO $event): void
93
95
$ this ->allEvents ($ event );
94
96
}
95
97
98
+ public function onRowsQuery (RowsQueryDTO $ event ): void
99
+ {
100
+ $ this ->allEvents ($ event );
101
+ }
102
+
96
103
protected function allEvents (EventDTO $ event ): void
97
104
{
98
105
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MySQLReplication \Event ;
6
+
7
+ use MySQLReplication \Event \DTO \RowsQueryDTO ;
8
+
9
+ /**
10
+ * The Rows_query event is within the binary log when the MySQL Option `binlog_rows_query_log_events`
11
+ * is enabled.
12
+ *
13
+ * @see https://dev.mysql.com/doc/dev/mysql-server/latest/classRows__query__log__event.html
14
+ */
15
+ class RowsQueryEvent extends EventCommon
16
+ {
17
+ public function makeRowsQueryDTO (): RowsQueryDTO
18
+ {
19
+ // $this->binaryDataReader->advance(1);
20
+ return new RowsQueryDTO (
21
+ $ this ->eventInfo ,
22
+ $ this ->binaryDataReader ->read ($ this ->binaryDataReader ->readInt8 ()),
23
+ );
24
+ }
25
+ }
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ protected function setUp(): void
41
41
->withHost ('0.0.0.0 ' )
42
42
->withPassword ('root ' )
43
43
->withPort (3306 )
44
- ->withEventsIgnore ([ConstEventType:: GTID_LOG_EVENT -> value ] );
44
+ ->withEventsIgnore ($ this -> getIgnoredEvents () );
45
45
46
46
$ this ->connect ();
47
47
@@ -83,6 +83,14 @@ public function connect(): void
83
83
$ this ->connection ->executeStatement ('SET SESSION sql_mode = \'\'; ' );
84
84
}
85
85
86
+ protected function getIgnoredEvents (): array
87
+ {
88
+ return [
89
+ ConstEventType::GTID_LOG_EVENT ->value , // Generally in here
90
+ ConstEventType::ROWS_QUERY_LOG_EVENT ->value , // Just debugging, there is a special test for it
91
+ ];
92
+ }
93
+
86
94
protected function getEvent (): EventDTO
87
95
{
88
96
if ($ this ->mySQLReplicationFactory === null ) {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MySQLReplication \Tests \Integration ;
6
+
7
+ use MySQLReplication \Definitions \ConstEventType ;
8
+ use MySQLReplication \Event \DTO \QueryDTO ;
9
+ use MySQLReplication \Event \DTO \RowsQueryDTO ;
10
+
11
+ final class RowsQueryTest extends BaseCase
12
+ {
13
+ public function testThatTheEditingQueryIsReadFromBinLog (): void
14
+ {
15
+ $ this ->connection ->executeStatement (
16
+ 'CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id)) '
17
+ );
18
+
19
+ $ insertQuery = 'INSERT INTO test (data) VALUES( \'Hello \') /* Foo:Bar; */ ' ;
20
+ $ this ->connection ->executeStatement ($ insertQuery );
21
+
22
+ // The Create Table Query ... irrelevant content for this test
23
+ self ::assertInstanceOf (QueryDTO::class, $ this ->getEvent ());
24
+ // The BEGIN Query ... irrelevant content for this test
25
+ self ::assertInstanceOf (QueryDTO::class, $ this ->getEvent ());
26
+
27
+ $ rowsQueryEvent = $ this ->getEvent ();
28
+ self ::assertInstanceOf (RowsQueryDTO::class, $ rowsQueryEvent );
29
+ self ::assertSame ($ insertQuery , $ rowsQueryEvent ->query );
30
+ }
31
+
32
+ protected function getIgnoredEvents (): array
33
+ {
34
+ return [ConstEventType::GTID_LOG_EVENT ->value ];
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments