Completed
Push — master ( 3d1041...cba42a )
by Gabriel
01:17
created

SQLDumper::dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
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 45
    public function __construct(string $host, string $dbname, string $username, string $password = '') 
61
    {
62 45
        $this->host = $host;
63 45
        $this->dbname = $dbname;
64 45
        $this->username = $username;
65 45
        $this->password = $password;
66
67
        //Init main TableDumperCollection
68 45
        $this->listTableDumpers = new TableDumperCollection;
69 45
        $this->connect();
70 45
    }
71
72
    /**
73
     * Connects to the SQL database
74
     * 
75
     * @return void
76
     */
77 45
    protected function connect(): void
78
    {
79 45
        $this->db = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname, 
80 45
                            $this->username, 
81 45
                            $this->password,
82
                            [
83 45
                                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
84
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
85
                            ]
86
        );
87 45
    }
88
89
    /**
90
     * Get the main list of TableDumper
91
     * 
92
     * @return TableDumperCollection Returns the list of table dumpers as TableDumperCollection
93
     */
94 3
    public function getListTableDumpers(): TableDumperCollection
95
    {
96 3
        return $this->listTableDumpers;
97
    }
98
99
    /**
100
     * Set a TableDumper for the given table in order to specify certain dump options on it
101
     * 
102
     * @param  Table|string The table to set
103
     * 
104
     * @return TableDumper  Returns a TableDumper
105
     */
106 27
    public function table($table): TableDumper
107
    {
108 27
        return $this->listTableDumpers->addTable($table);
109
    }
110
111
    /**
112
     * Set a TableDumperCollection for the given list of tables in order to specify certain dump options on them
113
     * 
114
     * @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) 
115
     * 
116
     * @return TableDumperCollection    Returns a TableDumperCollection
117
     */
118 15
    public function listTables($listTables): TableDumperCollection
119
    {   
120 15
        return $this->listTableDumpers->addListTables($listTables);
121
    }
122
123
    /**
124
     * Set a TableDumperCollection for all the tables in the database, in order to specify certain dump options on them
125
     * 
126
     * @return TableDumperCollection    Returns a TableDumperCollection
127
     */
128 9
    public function allTables(): TableDumperCollection
129
    {
130
        //Fetch all table names
131 9
        $stmt = $this->db->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema=:dbname');
132 9
        $stmt->bindValue(':dbname', $this->dbname, PDO::PARAM_STR);
133 9
        $stmt->execute();
134
135 9
        $listRows = $stmt->fetchAll(PDO::FETCH_COLUMN);
136 9
        $stmt->closeCursor();
137
138
        //Call list tables with returned table names
139 9
        $listDumpers = $this->listTables($listRows);
140
141 9
        return $listDumpers;
142
    }
143
144
    /**
145
     * Writes the complete dump of the database to the given stream
146
     * 
147
     * @param  resource Stream to write to
148
     * 
149
     * @return void
150
     */
151 27
    public function dump($stream): void
152
    {
153
        //TODO: Find a way to not use that an instead execute all foreign key constraints at the end
154 27
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=0;\r\n");
155
156 27
        $this->listTableDumpers->dump($this->db, $stream, $this->groupDrops, $this->groupInserts);
157
158 27
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=1;\r\n");
159 27
    }
160
161
    /**
162
     * Creates and saves the dump to a file
163
     * 
164
     * @param  string   File name for the dump
165
     * 
166
     * @return void
167
     */
168 3
    public function save(string $filename): void
169
    {
170 3
        $stream = fopen($filename, 'w');
171
        
172 3
        $this->dump($stream);
173 3
        fclose($stream);
174 3
    }
175
176
    /**
177
     * Set parameter that determines if DROP statements should be grouped together
178
     * @param  bool   $group Set to TRUE if it should be grouped, FALSE otherwise
179
     * @return void
180
     */
181 3
    public function groupDrops(bool $group): void
182
    {
183 3
        $this->groupDrops = $group;
184 3
    }
185
186
    /**
187
     * Set parameter that determines if INSERT statements should be grouped together
188
     * @param  bool   $group Set to TRUE if it should be grouped, FALSE otherwise
189
     * @return void
190
     */
191 3
    public function groupInserts(bool $group): void
192
    {
193 3
        $this->groupInserts = $group;
194
    }
195
}