Util   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 11
c 3
b 2
f 0
lcom 0
cbo 2
dl 0
loc 106
rs 10

2 Methods

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