Completed
Push — master ( f3489f...dc3ccc )
by Gabriel
01:55
created

SQLDumper::groupInserts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 26
     */
51
    protected $groupInserts = false;
52 26
53 26
54 26
    /**
55 26
     * @param string    Host for the DB connection
56
     * @param string    Name of the DB
57
     * @param string    Username used for the DB connection
58 26
     * @param string    Password used for the DB connection
59 26
     */
60 26
    public function __construct(string $host, string $dbname, string $username, string $password = '') 
61
    {
62
        $this->host = $host;
63
        $this->dbname = $dbname;
64
        $this->username = $username;
65
        $this->password = $password;
66
67 26
        //Init main TableDumperCollection
68
        $this->listTableDumpers = new TableDumperCollection;
69 26
        $this->connect();
70 26
    }
71 26
72
    /**
73 26
     * Connects to the SQL database
74 26
     * 
75 26
     * @return void
76 26
     */
77
    protected function connect(): void
78
    {
79
        $this->db = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname, 
80
                            $this->username, 
81
                            $this->password,
82
                            [
83 2
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
84
                            ]
85 2
        );
86
    }
87
88
    /**
89
     * Get the main list of TableDumper
90
     * 
91
     * @return TableDumperCollection Returns the list of table dumpers as TableDumperCollection
92
     */
93
    public function getListTableDumpers(): TableDumperCollection
94
    {
95 18
        return $this->listTableDumpers;
96
    }
97 18
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
    public function table($table): TableDumper
106
    {
107 6
        return $this->listTableDumpers->addTable($table);
108
    }
109 6
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 2
    public function listTables($listTables): TableDumperCollection
118
    {   
119
        return $this->listTableDumpers->addListTables($listTables);
120 2
    }
121 2
122 2
    /**
123
     * Set a TableDumperCollection for all the tables in the database, in order to specify certain dump options on them
124 2
     * 
125 2
     * @return TableDumperCollection    Returns a TableDumperCollection
126
     */
127
    public function allTables(): TableDumperCollection
128 2
    {
129
        //Fetch all table names
130 2
        $stmt = $this->db->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema=:dbname');
131
        $stmt->bindValue(':dbname', $this->dbname, PDO::PARAM_STR);
132
        $stmt->execute();
133
134
        $listRows = $stmt->fetchAll(PDO::FETCH_COLUMN);
135
        $stmt->closeCursor();
136
137
        //Call list tables with returned table names
138
        $listDumpers = $this->listTables($listRows);
139
140 14
        return $listDumpers;
141
    }
142
143 14
    /**
144
     * Writes the complete dump of the database to the given stream
145 14
     * 
146 14
     * @param  resource Stream to write to
147 14
     * 
148
     * @return void
149 14
     */
150 14
    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
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=0;\r\n");
154
155
        $this->listTableDumpers->dump($this->db, $stream, $this->groupDrops, $this->groupInserts);
156
157
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=1;\r\n");
158
    }
159 2
160
    /**
161 2
     * Creates and saves the dump to a file
162 2
     * 
163 2
     * @param  string   File name for the dump
164 2
     * 
165
     * @return void
166
     */
167
    public function save(string $filename): void
168
    {
169
        $stream = fopen($filename, 'w');
170
171
        //BOM
172
        fwrite($stream, "\xEF\xBB\xBF");
173
        
174
        $this->dump($stream);
175
        fclose($stream);
176
    }
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
}