Completed
Push — master ( 6749c3...175485 )
by Gabriel
01:38
created

SQLDumper::getListTableDumpers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    /**
45
     * @param string    Host for the DB connection
46
     * @param string    Name of the DB
47
     * @param string    Username used for the DB connection
48
     * @param string    Password used for the DB connection
49
     */
50 39
    public function __construct($host, $dbname, $username, $password = '') 
51
    {
52 39
        $this->host = $host;
53 39
        $this->dbname = $dbname;
54 39
        $this->username = $username;
55 39
        $this->password = $password;
56
57
        //Init main TableDumperCollection
58 39
        $this->listTableDumpers = new TableDumperCollection;
59 39
        $this->connect();
60 39
    }
61
62
    /**
63
     * Connects to the SQL database
64
     * 
65
     * @return void
66
     */
67 39
    protected function connect() 
68
    {
69 39
        $this->db = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname, 
70 39
                            $this->username, 
71 39
                            $this->password,
72
                            [
73 26
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
74 39
                            ]
75 26
        );
76 39
    }
77
78
    /**
79
     * Get the main list of TableDumper
80
     * 
81
     * @return TableDumperCollection Returns the list of table dumpers as TableDumperCollection
82
     */
83 3
    public function getListTableDumpers()
84
    {
85 3
        return $this->listTableDumpers;
86
    }
87
88
    /**
89
     * Set a TableDumper for the given table in order to specify certain dump options on it
90
     * 
91
     * @param  Table|string The table to set
92
     * 
93
     * @return TableDumper  Returns a TableDumper
94
     */
95 27
    public function table($table)
96
    {
97 27
        return $this->listTableDumpers->addTable($table);
98
    }
99
100
    /**
101
     * Set a TableDumperCollection for the given list of tables in order to specify certain dump options on them
102
     * 
103
     * @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) 
104
     * 
105
     * @return TableDumperCollection    Returns a TableDumperCollection
106
     */
107 9
    public function listTables($listTables)
108
    {   
109 9
        return $this->listTableDumpers->addListTables($listTables);
110
    }
111
112
    /**
113
     * Set a TableDumperCollection for all the tables in the database, in order to specify certain dump options on them
114
     * 
115
     * @return TableDumperCollection    Returns a TableDumperCollection
116
     */
117 3
    public function allTables()
118
    {
119
        //Fetch all table names
120 3
        $stmt = $this->db->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema=:dbname');
121 3
        $stmt->bindValue(':dbname', $this->dbname, PDO::PARAM_STR);
122 3
        $stmt->execute();
123
124 3
        $listRows = $stmt->fetchAll(PDO::FETCH_COLUMN);
125 3
        $stmt->closeCursor();
126
127
        //Call list tables with returned table names
128 3
        $listDumpers = $this->listTables($listRows);
129
130 3
        return $listDumpers;
131
    }
132
133
    /**
134
     * Writes the complete dump of the database to the given stream
135
     * 
136
     * @param  resource Stream to write to
137
     * 
138
     * @return void
139
     */
140 21
    public function dump($stream)
141
    {
142
        //TODO: Find a way to not use that an instead execute all foreign key constraints at the end
143 21
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=0;\r\n");
144
145 21
        foreach ($this->listTableDumpers as $tableDumper) {
146 21
            $tableDumper->dump($this->db, $stream);
147 14
        }
148
149 21
        fwrite($stream, "SET FOREIGN_KEY_CHECKS=1;\r\n");
150 21
    }
151
152
    /**
153
     * Creates and saves the dump to a file
154
     * 
155
     * @param  string   File name for the dump
156
     * 
157
     * @return void
158
     */
159 3
    public function save($filename) 
160
    {
161 3
        $stream = fopen($filename, 'w');
162 3
        $this->dump($stream);
163 3
        fclose($stream);
164
    }
165
}