Completed
Push — develop ( 682a70...91eb81 )
by Timothy
01:28
created

src/Drivers/Sqlite/Driver.php (1 issue)

Labels
Severity

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 declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7.1
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2018 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\Sqlite;
16
17
use PDO;
18
use PDOStatement;
19
use Query\Drivers\AbstractDriver;
20
use Query\Drivers\DriverInterface;
21
22
/**
23
 * SQLite specific class
24
 */
25
class Driver extends AbstractDriver implements DriverInterface {
26
27
	/**
28
	 * Reference to the last executed sql query
29
	 *
30
	 * @var PDOStatement
31
	 */
32
	protected $statement;
33
34
	/**
35
	 * SQLite has a truncate optimization,
36
	 * but no support for the actual keyword
37
	 * @var boolean
38
	 */
39
	protected $hasTruncate = FALSE;
40
41
	/**
42
	 * Open SQLite Database
43
	 *
44
	 * @param string $dsn
45
	 * @param string $user
46
	 * @param string $pass
47
	 * @param array $driverOptions
48
	 */
49
	public function __construct($dsn, $user=NULL, $pass=NULL, array $driverOptions=[])
50
	{
51
		if (strpos($dsn, 'sqlite:') === FALSE)
52
		{
53
			$dsn = "sqlite:{$dsn}";
54
		}
55
56
		parent::__construct($dsn, $user, $pass);
57
	}
58
59
	/**
60
	 * List tables for the current database
61
	 *
62
	 * @return mixed
63
	 */
64
	public function getTables(): array
65
	{
66
		$sql = $this->sql->tableList();
67
		$res = $this->query($sql);
68
		return db_filter($res->fetchAll(PDO::FETCH_ASSOC), 'name');
69
	}
70
71
	/**
72
	 * Retrieve foreign keys for the table
73
	 *
74
	 * @param string $table
75
	 * @return array
76
	 */
77
	public function getFks($table): array
78
	{
79
		$returnRows = [];
80
81
		foreach(parent::getFks($table) as $row)
0 ignored issues
show
The expression parent::getFks($table) of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
82
		{
83
			$returnRows[] = [
84
				'child_column' => $row['from'],
85
				'parent_table' => $row['table'],
86
				'parent_column' => $row['to'],
87
				'update' => $row['on_update'],
88
				'delete' => $row['on_delete']
89
			];
90
		}
91
92
		return $returnRows;
93
	}
94
95
	/**
96
	 * Create sql for batch insert
97
	 *
98
	 * @codeCoverageIgnore
99
	 * @param string $table
100
	 * @param array $data
101
	 * @return string
102
	 */
103
	public function insertBatch($table, $data=[])
104
	{
105
		// If greater than version 3.7.11, supports the same syntax as
106
		// MySQL and Postgres
107
		if (version_compare($this->getAttribute(PDO::ATTR_SERVER_VERSION), '3.7.11', '>='))
108
		{
109
			return parent::insertBatch($table, $data);
110
		}
111
112
		// --------------------------------------------------------------------------
113
		// Otherwise, do a union query as an analogue to a 'proper' batch insert
114
		// --------------------------------------------------------------------------
115
116
		// Each member of the data array needs to be an array
117
		if ( ! is_array(current($data)))
118
		{
119
			return NULL;
120
		}
121
122
		// Start the block of sql statements
123
		$table = $this->quoteTable($table);
124
		$sql = "INSERT INTO {$table} \n";
125
126
		// Create a key-value mapping for each field
127
		$first = array_shift($data);
128
		$cols = [];
129
		foreach($first as $colname => $datum)
130
		{
131
			$cols[] = $this->_quote($datum) . ' AS ' . $this->quoteIdent($colname);
132
		}
133
		$sql .= "SELECT " . implode(', ', $cols) . "\n";
134
135
		foreach($data as $union)
136
		{
137
			$vals = array_map([$this, 'quote'], $union);
138
			$sql .= "UNION SELECT " . implode(',', $vals) . "\n";
139
		}
140
141
		return [$sql, NULL];
142
	}
143
}