Completed
Push — master ( dc3ccc...e475aa )
by Gabriel
01:58
created

SQLDumper::listTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Y0lk\SQLDumper;
3
4
use PDO;
5
6
/**
7
 * SQLDumper is used to create a complete dump from a SQL database
8
 *
9
 * @author Gabriel Jean <[email protected]>
10
 */
11
class SQLDumper
12
{
13
    /**
14
     * @var string  Host for the DB connection
15
     */
16
    protected $host = '';
17
18
    /**
19
     * @var string  Name of the DB
20
     */
21
    protected $dbname = '';
22
23
    /**
24
     * @var string  Username used for the DB connection
25
     */
26
    protected $username = '';
27
28
    /**
29
     * @var string  Password used for the DB connection
30
     */
31
    protected $password = '';
32
33
    /**
34
     * @var PDO PDO instance of the DB
35
     */
36
    protected $db = NULL;
37
38
    /**
39
     * @var TableDumperCollection   Contains all TableDumper objects that will be used for this dump
40
     */
41
    protected $listTableDumpers;
42
43
    /**
44
     * @var boolean Determines if DROP statements should be grouped together
45
     */
46
    protected $groupDrops = false;
47
48
    /**
49
     * @var boolean Determines if INSERT statements should be grouped together
50
     */
51
    protected $groupInserts = false;
52
53
54
    /**
55
     * @param string    Host for the DB connection
56
     * @param string    Name of the DB
57
     * @param string    Username used for the DB connection
58
     * @param string    Password used for the DB connection
59
     */
60 39
    public function __construct(string $host, string $dbname, string $username, string $password = '') 
61
    {
62 39
        $this->host = $host;
63 39
        $this->dbname = $dbname;
64 39
        $this->username = $username;
65 39
        $this->password = $password;
66
67
        //Init main TableDumperCollection
68 39
        $this->listTableDumpers = new TableDumperCollection;
69 39
        $this->connect();
70 39
    }
71
72
    /**
73
     * Connects to the SQL database
74
     * 
75
     * @return void
76
     */
77 39
    protected function connect(): void
78
    {
79 39
        $this->db = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname, 
80 39
                            $this->username, 
81 39
                            $this->password,
82
                            [
83 39
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
84
                            ]
85
        );
86 39
    }
87
88
    /**
89
     * Get the main list of TableDumper
90
     * 
91
     * @return TableDumperCollection Returns the list of table dumpers as TableDumperCollection
92
     */
93 3
    public function getListTableDumpers(): TableDumperCollection
94
    {
95 3
        return $this->listTableDumpers;
96
    }
97
98
    /**
99
     * Set a TableDumper for the given table in order to specify certain dump options on it
100
     * 
101
     * @param  Table|string The table to set
102
     * 
103
     * @return TableDumper  Returns a TableDumper
104
     */
105 27
    public function table($table): TableDumper
106
    {
107 27
        return $this->listTableDumpers->addTable($table);
108
    }
109
110
    /**
111
     * Set a TableDumperCollection for the given list of tables in order to specify certain dump options on them
112
     * 
113
     * @param  TableDumperCollection|array<TableDumper|Table|string>    The list of tables to set (either as a TableDumperCollection, or an array containing either TableDumper objects, Table objects or table names) 
114
     * 
115
     * @return TableDumperCollection    Returns a TableDumperCollection
116
     */
117 9
    public function listTables($listTables): TableDumperCollection
118
    {   
119 9
        return $this->listTableDumpers->addListTables($listTables);
120
    }
121
122
    /**
123
     * Set a TableDumperCollection for all the tables in the database, in order to specify certain dump options on them
124
     * 
125
     * @return TableDumperCollection    Returns a TableDumperCollection
126
     */
127 3
    public function allTables(): TableDumperCollection
128
    {
129
        //Fetch all table names
130 3
        $stmt = $this->db->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema=:dbname');
131 3
        $stmt->bindValue(':dbname', $this->dbname, PDO::PARAM_STR);
132 3
        $stmt->execute();
133
134 3
        $listRows = $stmt->fetchAll(PDO::FETCH_COLUMN);
135 3
        $stmt->closeCursor();
136
137
        //Call list tables with returned table names
138 3
        $listDumpers = $this->listTables($listRows);
139
140 3
        return $listDumpers;
141
    }
142
143
    /**
144
     * Writes the complete dump of the database to the given stream
145
     * 
146
     * @param  resource Stream to write to
147
     * 
148
     * @return void
149
     */
150 21
    public function dump($stream): void
151
    {
152
        //TODO: Find a way to not use that an instead execute all foreign key constraints at the end
153 21
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=0;\r\n");
154
155 21
        $this->listTableDumpers->dump($this->db, $stream, $this->groupDrops, $this->groupInserts);
156
157 21
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=1;\r\n");
158 21
    }
159
160
    /**
161
     * Creates and saves the dump to a file
162
     * 
163
     * @param  string   File name for the dump
164
     * 
165
     * @return void
166
     */
167 3
    public function save(string $filename): void
168
    {
169 3
        $stream = fopen($filename, 'w');
170
171
        //BOM
172 3
        fwrite($stream, "\xEF\xBB\xBF");
173
        
174 3
        $this->dump($stream);
175 3
        fclose($stream);
176 3
    }
177
178
    /**
179
     * Set parameter that determines if DROP statements should be grouped together
180
     * @param  bool   $group Set to TRUE if it should be grouped, FALSE otherwise
181
     * @return void
182
     */
183
    public function groupDrops(bool $group): void
184
    {
185
        $this->groupDrops = $group;
186
    }
187
188
    /**
189
     * Set parameter that determines if INSERT statements should be grouped together
190
     * @param  bool   $group Set to TRUE if it should be grouped, FALSE otherwise
191
     * @return void
192
     */
193
    public function groupInserts(bool $group): void
194
    {
195
        $this->groupInserts = $group;
196
    }
197
}