SQLDumper::listTables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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