-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractConnection.php
More file actions
160 lines (143 loc) · 3.08 KB
/
AbstractConnection.php
File metadata and controls
160 lines (143 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace Darya\Database;
use Darya\Events\Dispatchable;
use Darya\Storage\Query as StorageQuery;
/**
* Darya's abstract database connection.
*
* @author Chris Andrew <chris@hexus.io>
*/
abstract class AbstractConnection implements Connection
{
/**
* Connection object or link identifier.
*
* @var mixed
*/
protected $connection;
/**
* Whether the connection is currently active.
*
* @var bool
*/
protected $connected = false;
/**
* Connection details.
*
* @var array
*/
protected $details = array();
/**
* Connection options.
*
* @var array
*/
protected $options = array();
/**
* The event dispatcher.
*
* @var Dispatchable
*/
protected $eventDispatcher;
/**
* The result of the last query.
*
* @var \Darya\Database\Result
*/
protected $lastResult;
/**
* The storage query translator.
*
* @var \Darya\Database\Query\Translator
*/
protected $translator;
/**
* Instantiate a new database connection with the given credentials.
*
* The connection is not made upon instantiating the object, but instead
* after using either the `connect()` or `query()` methods.
*
* @param string $host Hostname to connect to
* @param string $user Username to login with
* @param string $pass Password to login with
* @param string $name Database to use
* @param int $port [optional] Port to connect to
*/
public function __construct($host, $user, $pass, $name, $port = null)
{
$this->details = array(
'host' => $host,
'user' => $user,
'pass' => $pass,
'name' => $name,
'port' => $port
);
}
/**
* Set an event dispatcher for the connection to use.
*
* @param Dispatchable $dispatcher
*/
public function setEventDispatcher(Dispatchable $dispatcher)
{
$this->eventDispatcher = $dispatcher;
}
/**
* Helper method for dispatching events. Silent if an event dispatcher is
* not set.
*
* @param string $name
* @param mixed $arguments [optional]
* @return array
*/
protected function event($name, array $arguments = array())
{
if ($this->eventDispatcher) {
return $this->eventDispatcher->dispatch($name, $arguments);
}
return array();
}
/**
* Determine whether there is an active connection to the database.
*
* @return bool
*/
public function connected()
{
return $this->connected;
}
/**
* Retrieve the query translator to use for this connection.
*
* @return \Darya\Database\Query\Translator
*/
abstract public function translator();
/**
* Translate a storage query to a database query for this connection.
*
* @param StorageQuery $storageQuery
* @return \Darya\Database\Query
*/
public function translate(StorageQuery $storageQuery)
{
return $this->translator()->translate($storageQuery);
}
/**
* Get the error produced by the last query, if any.
*
* @return \Darya\Database\Error
*/
public function error()
{
return $this->lastResult->error;
}
/**
* Get a detailed result array for the last query made by this connection.
*
* @return \Darya\Database\Result
*/
public function lastResult()
{
return $this->lastResult;
}
}