Completed
Push — develop ( 2db7ad...3eb4d8 )
by Timothy
10:51
created

src/Query/Drivers/Mysql/Util.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 5.4
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2015 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
16
namespace Query\Drivers\Mysql;
17
18
use Query\Drivers\AbstractUtil;
19
20
/**
21
 * MySQL-specific backup, import and creation methods
22
 *
23
 * @package Query
24
 * @subpackage Drivers
25
 */
26
class Util extends AbstractUtil {
27
28
	/**
29
	 * Create an SQL backup file for the current database's structure
30
	 *
31
	 * @return string
32
	 */
33
	public function backup_structure()
34
	{
35
		$string = [];
36
37
		// Get databases
38
		$dbs = $this->get_driver()->get_dbs();
39
40
		foreach($dbs as &$d)
41
		{
42
			// Skip built-in dbs
43
			if ($d == 'mysql')
44
			{
45
				continue;
46
			}
47
48
			// Get the list of tables
49
			$tables = $this->get_driver()->driver_query("SHOW TABLES FROM `{$d}`", TRUE);
50
51
			foreach($tables as $table)
52
			{
53
				$array = $this->get_driver()->driver_query("SHOW CREATE TABLE `{$d}`.`{$table}`", FALSE);
54
				$row = current($array);
55
56
				if ( ! isset($row['Create Table']))
57
				{
58
					continue;
59
				}
60
61
62
				$string[] = $row['Create Table'];
63
			}
64
		}
65
66
		return implode("\n\n", $string);
67
	}
68
69
	// --------------------------------------------------------------------------
70
71
	/**
72
	 * Create an SQL backup file for the current database's data
73
	 *
74
	 * @param array $exclude
75
	 * @return string
76
	 */
77
	public function backup_data($exclude=[])
78
	{
79
		$tables = $this->get_driver()->get_tables();
80
81
		// Filter out the tables you don't want
82
		if( ! empty($exclude))
83
		{
84
			$tables = array_diff($tables, $exclude);
85
		}
86
87
		$output_sql = '';
88
89
		// Select the rows from each Table
90
		foreach($tables as $t)
91
		{
92
			$sql = "SELECT * FROM `{$t}`";
93
			$res = $this->get_driver()->query($sql);
0 ignored issues
show
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...
94
			$rows = $res->fetchAll(\PDO::FETCH_ASSOC);
95
96
			// Skip empty tables
97
			if (count($rows) < 1)
98
			{
99
				continue;
100
			}
101
102
			// Nab the column names by getting the keys of the first row
103
			$columns = @array_keys($rows[0]);
104
105
			$insert_rows = [];
106
107
			// Create the insert statements
108
			foreach($rows as $row)
109
			{
110
				$row = array_values($row);
111
112
				// Workaround for Quercus
113
				foreach($row as &$r)
114
				{
115
					$r = $this->get_driver()->quote($r);
116
				}
117
				$row = array_map('trim', $row);
118
119
				$row_string = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');';
120
121
				$row = NULL;
122
123
				$insert_rows[] = $row_string;
124
			}
125
126
			$output_sql .= "\n\n".implode("\n", $insert_rows)."\n";
127
		}
128
129
		return $output_sql;
130
	}
131
}
132
// End of mysql_util.php