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

Driver::getTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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\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()
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)
78
	{
79
		$returnRows = [];
80
81
		foreach(parent::getFks($table) as $row)
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 View Code Duplication
		foreach($data as $union)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
		{
137
			$vals = array_map([$this, 'quote'], $union);
138
			$sql .= "UNION SELECT " . implode(',', $vals) . "\n";
139
		}
140
141
		return [$sql, NULL];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($sql, NULL); (array<string|null>) is incompatible with the return type of the parent method Query\Drivers\AbstractDriver::insertBatch of type null|array<string|array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
142
	}
143
}