Completed
Push — develop ( 111dce...b8d476 )
by Timothy
02:35
created

Util::backup_structure()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 3
nop 0
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2016 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query\Drivers\Mysql;
16
17
use PDO;
18
use Query\Drivers\AbstractUtil;
19
20
/**
21
 * MySQL-specific backup, import and creation methods
22
 */
23
class Util extends AbstractUtil {
24
25
	/**
26
	 * Create an SQL backup file for the current database's structure
27
	 *
28
	 * @return string
29
	 */
30
	public function backupStructure()
31
	{
32
		$string = [];
33
34
		// Get databases
35
		$dbs = $this->getDriver()->getDbs();
36
37
		foreach($dbs as &$d)
38
		{
39
			// Skip built-in dbs
40
			if ($d == 'mysql')
41
			{
42
				continue;
43
			}
44
45
			// Get the list of tables
46
			$tables = $this->getDriver()->driverQuery("SHOW TABLES FROM `{$d}`", TRUE);
47
48
			foreach($tables as $table)
49
			{
50
				$array = $this->getDriver()->driverQuery("SHOW CREATE TABLE `{$d}`.`{$table}`", FALSE);
51
				$row = current($array);
52
53
				if ( ! isset($row['Create Table']))
54
				{
55
					continue;
56
				}
57
58
59
				$string[] = $row['Create Table'];
60
			}
61
		}
62
63
		return implode("\n\n", $string);
64
	}
65
66
	/**
67
	 * Create an SQL backup file for the current database's data
68
	 *
69
	 * @param array $exclude
70
	 * @return string
71
	 */
72
	public function backupData($exclude=[])
73
	{
74
		$tables = $this->getDriver()->getTables();
75
76
		// Filter out the tables you don't want
77
		if( ! empty($exclude))
78
		{
79
			$tables = array_diff($tables, $exclude);
80
		}
81
82
		$outputSql = '';
83
84
		// Select the rows from each Table
85
		foreach($tables as $t)
86
		{
87
			$sql = "SELECT * FROM `{$t}`";
88
			$res = $this->getDriver()->query($sql);
0 ignored issues
show
Unused Code introduced by
The call to DriverInterface::query() has too many arguments starting with $sql.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
89
			$rows = $res->fetchAll(PDO::FETCH_ASSOC);
90
91
			// Skip empty tables
92
			if (count($rows) < 1)
93
			{
94
				continue;
95
			}
96
97
			// Nab the column names by getting the keys of the first row
98
			$columns = @array_keys($rows[0]);
99
100
			$insertRows = [];
101
102
			// Create the insert statements
103
			foreach($rows as $row)
104
			{
105
				$row = array_values($row);
106
107
				// Workaround for Quercus
108
				foreach($row as &$r)
109
				{
110
					$r = $this->getDriver()->quote($r);
111
				}
112
				$row = array_map('trim', $row);
113
114
				$rowString = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');';
115
116
				$row = NULL;
117
118
				$insertRows[] = $rowString;
119
			}
120
121
			$outputSql .= "\n\n".implode("\n", $insertRows)."\n";
122
		}
123
124
		return $outputSql;
125
	}
126
}