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

src/Query/Drivers/Sqlite/Util.php (3 issues)

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\Sqlite;
17
18
use Query\Drivers\AbstractUtil;
19
20
/**
21
 * SQLite-specific backup, import and creation methods
22
 *
23
 * @package Query
24
 * @subpackage Drivers
25
 * @method mixed query(string $sql)
26
 * @method string quote(string $str)
27
 */
28
class Util extends AbstractUtil {
29
30
	/**
31
	 * Create an SQL backup file for the current database's data
32
	 *
33
	 * @param array $excluded
34
	 * @return string
35
	 */
36
	public function backup_data($excluded=[])
37
	{
38
		// Get a list of all the objects
39
		$sql = 'SELECT DISTINCT "name"
40
				FROM "sqlite_master"
41
				WHERE "type"=\'table\'';
42
43
		if( ! empty($excluded))
44
		{
45
			$sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')";
46
		}
47
48
		$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...
49
		$result = $res->fetchAll(\PDO::FETCH_ASSOC);
50
51
		unset($res);
52
53
		$output_sql = '';
54
55
		// Get the data for each object
56
		foreach($result as $r)
57
		{
58
			$sql = 'SELECT * FROM "'.$r['name'].'"';
59
			$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...
60
			$obj_res = $res->fetchAll(\PDO::FETCH_ASSOC);
61
62
			unset($res);
63
64
			// If the row is empty, continue;
65
			if (empty($obj_res))
66
			{
67
				continue;
68
			}
69
70
			// Nab the column names by getting the keys of the first row
71
			$columns = array_keys(current($obj_res));
72
73
			$insert_rows = [];
74
75
			// Create the insert statements
76
			foreach($obj_res as $row)
77
			{
78
				$row = array_values($row);
79
80
				// Quote values as needed by type
81
				for($i=0, $icount=count($row); $i<$icount; $i++)
82
				{
83
					$row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->get_driver()->quote($row[$i]);
84
				}
85
86
				$row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
87
88
				unset($row);
89
90
				$insert_rows[] = $row_string;
91
			}
92
93
			unset($obj_res);
94
95
			$output_sql .= "\n\n".implode("\n", $insert_rows);
96
		}
97
98
		return $output_sql;
99
	}
100
101
	// --------------------------------------------------------------------------
102
103
	/**
104
	 * Create an SQL backup file for the current database's structure
105
	 *
106
	 * @return string
107
	 */
108
	public function backup_structure()
109
	{
110
		// Fairly easy for SQLite...just query the master table
111
		$sql = 'SELECT "sql" FROM "sqlite_master"';
112
		$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...
113
		$result = $res->fetchAll(\PDO::FETCH_ASSOC);
114
115
		$sql_array = [];
116
117
		foreach($result as $r)
118
		{
119
			$sql_array[] = $r['sql'];
120
		}
121
122
		return implode(";\n", $sql_array) . ";";
123
	}
124
}
125
// End of sqlite_util.php