Skip to content

Commit cbc66cf

Browse files
authored
Merge pull request #48 from skipperbent/v4-development
Version 4.1.3
2 parents 42131d2 + 54f706d commit cbc66cf

File tree

5 files changed

+1771
-1617
lines changed

5 files changed

+1771
-1617
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ composer install pecee/pixie
170170
- [**Delete**](#delete)
171171
- [Transactions](#transactions)
172172
- [Get Built Query](#get-built-query)
173+
- [Get QueryObject from last executed query](#get-queryobject-from-last-executed-query)
173174
- [Sub Queries and Nested Queries](#sub-queries-and-nested-queries)
174175
- [Get PDO Instance](#get-pdo-instance)
175176
- [Fetch results as objects of specified class](#fetch-results-as-objects-of-specified-class)
@@ -824,6 +825,17 @@ Calling the `getRawSql()` method will return a query including bindings like thi
824825
SELECT * FROM my_table where `id` = 3
825826
```
826827

828+
#### Get QueryObject from last executed query
829+
830+
You can also retrieve the query-object from the last executed query.
831+
832+
**Example:**
833+
834+
```php
835+
$queryString = $qb->getLastQuery()->getRawSql();
836+
```
837+
838+
827839
### Sub Queries and Nested Queries
828840

829841
Rarely but you may need to do sub queries or nested queries. Pixie is powerful enough to do this for you. You can create different query objects and use the `$queryBuilder->subQuery()` method.

src/Pecee/Pixie/Connection.php

Lines changed: 183 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -4,151 +4,193 @@
44

55
use Pecee\Pixie\ConnectionAdapters\IConnectionAdapter;
66
use Pecee\Pixie\QueryBuilder\QueryBuilderHandler;
7+
use Pecee\Pixie\QueryBuilder\QueryObject;
78

89
/**
910
* Class Connection
1011
*
1112
* @package Pecee\Pixie
1213
*/
13-
class Connection {
14-
15-
/**
16-
* @var Connection
17-
*/
18-
protected static $storedConnection;
19-
20-
/**
21-
* Connection adapter (i.e. Mysql, Pgsql, Sqlite)
22-
*
23-
* @var IConnectionAdapter
24-
*/
25-
protected $adapter;
26-
27-
/**
28-
* @var array
29-
*/
30-
protected $adapterConfig;
31-
32-
/**
33-
* @var \PDO
34-
*/
35-
protected $pdoInstance;
36-
37-
/**
38-
* @var EventHandler
39-
*/
40-
protected $eventHandler;
41-
42-
/**
43-
* @param string $adapter Adapter name or class
44-
* @param array $adapterConfig
45-
*/
46-
public function __construct($adapter, array $adapterConfig) {
47-
if (($adapter instanceof IConnectionAdapter) === false) {
48-
/* @var $adapter IConnectionAdapter */
49-
$adapter = '\Pecee\Pixie\ConnectionAdapters\\' . ucfirst(strtolower($adapter));
50-
$adapter = new $adapter();
51-
}
52-
53-
$this
54-
->setAdapter($adapter)
55-
->setAdapterConfig($adapterConfig)
56-
->connect();
57-
58-
// Create event dependency
59-
$this->eventHandler = new EventHandler();
60-
}
61-
62-
/**
63-
* @return Connection
64-
*/
65-
public static function getStoredConnection(): Connection {
66-
return static::$storedConnection;
67-
}
68-
69-
/**
70-
* Create the connection adapter
71-
*/
72-
public function connect() {
73-
// Build a database connection if we don't have one connected
74-
75-
$pdo = $this->adapter->connect($this->adapterConfig);
76-
$this->setPdoInstance($pdo);
77-
78-
// Preserve the first database connection with a static property
79-
if (static::$storedConnection === null) {
80-
static::$storedConnection = $this;
81-
}
82-
}
83-
84-
/**
85-
* @return IConnectionAdapter
86-
*/
87-
public function getAdapter(): IConnectionAdapter {
88-
return $this->adapter;
89-
}
90-
91-
/**
92-
* @return array
93-
*/
94-
public function getAdapterConfig(): array {
95-
return $this->adapterConfig;
96-
}
97-
98-
/**
99-
* @return EventHandler
100-
*/
101-
public function getEventHandler(): EventHandler {
102-
return $this->eventHandler;
103-
}
104-
105-
/**
106-
* @return \PDO
107-
*/
108-
public function getPdoInstance(): \PDO {
109-
return $this->pdoInstance;
110-
}
111-
112-
/**
113-
* Returns an instance of Query Builder
114-
*
115-
* @return QueryBuilderHandler
116-
* @throws Exception
117-
*/
118-
public function getQueryBuilder(): QueryBuilderHandler {
119-
return new QueryBuilderHandler($this);
120-
}
121-
122-
/**
123-
* @param IConnectionAdapter $adapter
124-
*
125-
* @return \Pecee\Pixie\Connection
126-
*/
127-
public function setAdapter(IConnectionAdapter $adapter): Connection {
128-
$this->adapter = $adapter;
129-
130-
return $this;
131-
}
132-
133-
/**
134-
* @param array $adapterConfig
135-
*
136-
* @return \Pecee\Pixie\Connection
137-
*/
138-
public function setAdapterConfig(array $adapterConfig): Connection {
139-
$this->adapterConfig = $adapterConfig;
140-
141-
return $this;
142-
}
143-
144-
/**
145-
* @param \PDO $pdo
146-
*
147-
* @return \Pecee\Pixie\Connection
148-
*/
149-
public function setPdoInstance(\PDO $pdo): Connection {
150-
$this->pdoInstance = $pdo;
151-
152-
return $this;
153-
}
14+
class Connection
15+
{
16+
17+
/**
18+
* @var Connection
19+
*/
20+
protected static $storedConnection;
21+
22+
/**
23+
* Connection adapter (i.e. Mysql, Pgsql, Sqlite)
24+
*
25+
* @var IConnectionAdapter
26+
*/
27+
protected $adapter;
28+
29+
/**
30+
* @var array
31+
*/
32+
protected $adapterConfig;
33+
34+
/**
35+
* @var \PDO
36+
*/
37+
protected $pdoInstance;
38+
39+
/**
40+
* @var EventHandler
41+
*/
42+
protected $eventHandler;
43+
44+
/**
45+
* @var QueryObject|null
46+
*/
47+
protected $lastQuery;
48+
49+
/**
50+
* @param string $adapter Adapter name or class
51+
* @param array $adapterConfig
52+
*/
53+
public function __construct($adapter, array $adapterConfig)
54+
{
55+
if (($adapter instanceof IConnectionAdapter) === false) {
56+
/* @var $adapter IConnectionAdapter */
57+
$adapter = '\Pecee\Pixie\ConnectionAdapters\\' . ucfirst(strtolower($adapter));
58+
$adapter = new $adapter();
59+
}
60+
61+
$this
62+
->setAdapter($adapter)
63+
->setAdapterConfig($adapterConfig)
64+
->connect();
65+
66+
// Create event dependency
67+
$this->eventHandler = new EventHandler();
68+
}
69+
70+
/**
71+
* @return Connection
72+
*/
73+
public static function getStoredConnection(): Connection
74+
{
75+
return static::$storedConnection;
76+
}
77+
78+
/**
79+
* Create the connection adapter
80+
*/
81+
public function connect()
82+
{
83+
// Build a database connection if we don't have one connected
84+
85+
$pdo = $this->adapter->connect($this->adapterConfig);
86+
$this->setPdoInstance($pdo);
87+
88+
// Preserve the first database connection with a static property
89+
if (static::$storedConnection === null) {
90+
static::$storedConnection = $this;
91+
}
92+
}
93+
94+
/**
95+
* @return IConnectionAdapter
96+
*/
97+
public function getAdapter(): IConnectionAdapter
98+
{
99+
return $this->adapter;
100+
}
101+
102+
/**
103+
* @return array
104+
*/
105+
public function getAdapterConfig(): array
106+
{
107+
return $this->adapterConfig;
108+
}
109+
110+
/**
111+
* @return EventHandler
112+
*/
113+
public function getEventHandler(): EventHandler
114+
{
115+
return $this->eventHandler;
116+
}
117+
118+
/**
119+
* @return \PDO
120+
*/
121+
public function getPdoInstance(): \PDO
122+
{
123+
return $this->pdoInstance;
124+
}
125+
126+
/**
127+
* Returns an instance of Query Builder
128+
*
129+
* @return QueryBuilderHandler
130+
* @throws \Pecee\Pixie\Exception
131+
*/
132+
public function getQueryBuilder(): QueryBuilderHandler
133+
{
134+
return new QueryBuilderHandler($this);
135+
}
136+
137+
/**
138+
* @param IConnectionAdapter $adapter
139+
*
140+
* @return static
141+
*/
142+
public function setAdapter(IConnectionAdapter $adapter): Connection
143+
{
144+
$this->adapter = $adapter;
145+
146+
return $this;
147+
}
148+
149+
/**
150+
* @param array $adapterConfig
151+
*
152+
* @return static
153+
*/
154+
public function setAdapterConfig(array $adapterConfig): Connection
155+
{
156+
$this->adapterConfig = $adapterConfig;
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* @param \PDO $pdo
163+
*
164+
* @return static
165+
*/
166+
public function setPdoInstance(\PDO $pdo): Connection
167+
{
168+
$this->pdoInstance = $pdo;
169+
170+
return $this;
171+
}
172+
173+
/**
174+
* Set query-object for last executed query.
175+
*
176+
* @param QueryObject $query
177+
* @return static
178+
*/
179+
public function setLastQuery(QueryObject $query)
180+
{
181+
$this->lastQuery = $query;
182+
183+
return $this;
184+
}
185+
186+
/**
187+
* Get query-object from last executed query.
188+
*
189+
* @return QueryObject|null
190+
*/
191+
public function getLastQuery()
192+
{
193+
return $this->lastQuery;
194+
}
195+
154196
}

0 commit comments

Comments
 (0)