Completed
Push — master ( a1f2b4...dcb765 )
by Chris
02:07
created

Sqlite::disconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Darya\Database\Connection;
3
4
use PDO;
5
use Darya\Database\AbstractConnection;
6
use Darya\Database\Error;
7
use Darya\Database\Result;
8
use Darya\Database\Query;
9
use Darya\Database\Query\Translator;
10
11
/**
12
 * Darya's SQLite database interface. Uses PDO.
13
 * 
14
 * @author Chris Andrew <[email protected]>
15
 */
16
class Sqlite extends AbstractConnection
17
{
18
	/**
19
	 * Create a new SQLite database connection.
20
	 * 
21
	 * Creates an in-memory database if no path is given.
22
	 * 
23
	 * @param string $path    [optional] The path to the SQLite database file
24
	 * @param array  $options [optional] Options for the SQLite PDO connection
25
	 */
26
	public function __construct($path = null, array $options = array())
27
	{
28
		$this->details['path'] = $path ?: ':memory:';
29
		$this->options = array_merge(array(
0 ignored issues
show
Bug introduced by
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
			'persistent' => false
31
		), $options);
32
	}
33
	
34
	/**
35
	 * Initiate the connection.
36
	 */
37
	public function connect()
38
	{
39
		if ($this->connected()) {
40
			return true;
41
		}
42
		
43
		$path = $this->details['path'];
44
		
45
		$this->connection = new PDO("sqlite:{$path}", null, null, array(
46
			PDO::ATTR_PERSISTENT => $this->options['persistent']
47
		));
48
		
49
		if ($this->connection->errorCode()) {
50
			return false;
51
		}
52
		
53
		$this->connected = true;
54
	}
55
	
56
	/**
57
	 * Close the connection.
58
	 */
59
	public function disconnect()
60
	{
61
		$this->connection = null;
62
	}
63
	
64
	/**
65
	 * Retrieve the query translator.
66
	 * 
67
	 * @return Translator
68
	 */
69
	public function translator()
70
	{
71
		if (!$this->translator) {
72
			$this->translator = new Translator\MySql;
73
		}
74
		
75
		return $this->translator;
76
	}
77
	
78
	/**
79
	 * Query the database.
80
	 * 
81
	 * @param Query|string $query
82
	 * @param array        $parameters [optional]
83
	 */
84
	public function query($query, array $parameters = array())
85
	{
86
		if (!($query instanceof Query)) {
87
			$query = new Query((string) $query, $parameters);
88
		}
89
		
90
		$this->connect();
91
		
92
		$statement = $this->connection->prepare($query->string);
93
		
94
		$error = $this->error();
95
		
96
		if ($error) {
97
			$this->lastResult = new Result($query, array(), array(), $error);
98
			
99
			return $this->lastResult;
100
		}
101
		
102
		$statement->execute($query->parameters);
103
		
104
		$data = $statement->fetchAll(PDO::FETCH_ASSOC);
105
		
106
		$info = array(
107
			'count' => $statement->rowCount(),
108
		);
109
		
110
		$this->lastResult = new Result($query, $data, $info);
111
		
112
		return $this->lastResult;
113
	}
114
	
115
	/**
116
	 * Retrieve error information regarding the last query or connection
117
	 * attempt.
118
	 * 
119
	 * Returns null if there is no error.
120
	 * 
121
	 * @return Error
122
	 */
123 View Code Duplication
	public function error()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
124
	{
125
		$connectionError = $this->connectionError();
126
		
127
		if ($connectionError) {
128
			return $connectionError;
129
		}
130
		
131
		if ($this->lastResult && $this->lastResult->error) {
132
			return $this->lastResult->error;
133
		}
134
		
135
		return null;
136
	}
137
	
138
	/**
139
	 * Retrieve error information from the PDO connection object.
140
	 * 
141
	 * Returns null if there is no error.
142
	 * 
143
	 * @return Error
144
	 */
145
	protected function connectionError()
146
	{
147
		if (!$this->connection) {
148
			return null;
149
		}
150
		
151
		$errorInfo = $this->connection->errorInfo();
152
		
153
		if (empty($errorInfo[1])) {
154
			return null;
155
		}
156
		
157
		return new Error($errorInfo[1], $errorInfo[2]);
158
	}
159
}
160