1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright (c) 2017-present brian ridley |
5
|
|
|
* @author brian ridley <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ptlis\GrepDb; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\DriverManager; |
13
|
|
|
use ptlis\GrepDb\Replace\Replace; |
14
|
|
|
use ptlis\GrepDb\Replace\Result\RowReplaceResult; |
15
|
|
|
use ptlis\GrepDb\Search\Result\RowSearchResult; |
16
|
|
|
use ptlis\GrepDb\Search\Search; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Perform a search or a search & replace on the database. |
20
|
|
|
*/ |
21
|
|
|
final class GrepDb |
22
|
|
|
{ |
23
|
|
|
/** @var Connection */ |
24
|
|
|
private $connection; |
25
|
|
|
|
26
|
|
|
/** @var Search */ |
27
|
|
|
private $search; |
28
|
|
|
|
29
|
|
|
/** @var Replace */ |
30
|
|
|
private $replace; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
string $username, |
35
|
|
|
string $password, |
36
|
|
|
string $host, |
37
|
|
|
int $port = 3306 |
38
|
|
|
) { |
39
|
|
|
$this->connection = DriverManager::getConnection([ |
40
|
|
|
'user' => $username, |
41
|
|
|
'password' => $password, |
42
|
|
|
'host' => $host, |
43
|
|
|
'port' => $port, |
44
|
|
|
'driver' => 'pdo_mysql' |
45
|
|
|
]); |
46
|
|
|
$this->search = new Search(); |
47
|
|
|
$this->replace = new Replace(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Performs search on the specified database and table. |
52
|
|
|
* |
53
|
|
|
* @param string $databaseName |
54
|
|
|
* @param string $tableName |
55
|
|
|
* @param string $searchTerm |
56
|
|
|
* @return \Generator|RowSearchResult[] |
57
|
|
|
*/ |
58
|
|
|
public function searchTable( |
59
|
|
|
string $databaseName, |
60
|
|
|
string $tableName, |
61
|
|
|
string $searchTerm |
62
|
|
|
): \Generator { |
63
|
|
|
$resultList = $this->search->searchTable($this->connection, $databaseName, $tableName, $searchTerm); |
64
|
|
|
foreach ($resultList as $result) { |
65
|
|
|
yield $result; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Performs search on all tables in the specified database. |
71
|
|
|
* |
72
|
|
|
* @param string $databaseName |
73
|
|
|
* @param string $searchTerm |
74
|
|
|
* @return \Generator|RowSearchResult[] |
75
|
|
|
*/ |
76
|
|
|
public function searchDatabase( |
77
|
|
|
string $databaseName, |
78
|
|
|
string $searchTerm |
79
|
|
|
): \Generator { |
80
|
|
|
$resultList = $this->search->searchDatabase($this->connection, $databaseName, $searchTerm); |
81
|
|
|
foreach ($resultList as $result) { |
82
|
|
|
yield $result; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Performs search on all available databases. |
88
|
|
|
* |
89
|
|
|
* @param string $searchTerm |
90
|
|
|
* @return \Generator|RowSearchResult[] |
91
|
|
|
*/ |
92
|
|
|
public function searchServer( |
93
|
|
|
string $searchTerm |
94
|
|
|
): \Generator { |
95
|
|
|
$resultList = $this->search->searchServer($this->connection, $searchTerm); |
96
|
|
|
foreach ($resultList as $result) { |
97
|
|
|
yield $result; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Performs search and replacement on the specified database and table. |
103
|
|
|
* |
104
|
|
|
* @param string $databaseName |
105
|
|
|
* @param string $tableName |
106
|
|
|
* @param string $searchTerm |
107
|
|
|
* @param string $replaceTerm |
108
|
|
|
* @return \Generator|RowReplaceResult[] |
109
|
|
|
*/ |
110
|
|
|
public function replaceTable( |
111
|
|
|
string $databaseName, |
112
|
|
|
string $tableName, |
113
|
|
|
string $searchTerm, |
114
|
|
|
string $replaceTerm |
115
|
|
|
): \Generator { |
116
|
|
|
$resultList = $this->replace->replaceTable( |
117
|
|
|
$this->connection, |
118
|
|
|
$databaseName, |
119
|
|
|
$tableName, |
120
|
|
|
$searchTerm, |
121
|
|
|
$replaceTerm |
122
|
|
|
); |
123
|
|
|
foreach ($resultList as $result) { |
124
|
|
|
yield $result; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Performs search and replacement on all tables in the specified database. |
130
|
|
|
* |
131
|
|
|
* @param string $databaseName |
132
|
|
|
* @param string $searchTerm |
133
|
|
|
* @param string $replaceTerm |
134
|
|
|
* @return \Generator|RowReplaceResult[] |
135
|
|
|
*/ |
136
|
|
|
public function replaceDatabase( |
137
|
|
|
string $databaseName, |
138
|
|
|
string $searchTerm, |
139
|
|
|
string $replaceTerm |
140
|
|
|
): \Generator { |
141
|
|
|
$resultList = $this->replace->replaceDatabase($this->connection, $databaseName, $searchTerm, $replaceTerm); |
142
|
|
|
foreach ($resultList as $result) { |
143
|
|
|
yield $result; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |