Skip to content

Commit 30063c9

Browse files
committed
Refactoring Database to fire events with the query attached instead of saving all queries. Removed getQueries method from db. First step toward #105
1 parent 6864e6f commit 30063c9

File tree

11 files changed

+59
-170
lines changed

11 files changed

+59
-170
lines changed

system/Database/BaseConnection.php

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838

3939
use CodeIgniter\DatabaseException;
40+
use CodeIgniter\Hooks\Hooks;
4041

4142
/**
4243
* Class BaseConnection
@@ -188,23 +189,15 @@ abstract class BaseConnection implements ConnectionInterface
188189
*/
189190
public $failover = [];
190191

191-
/**
192-
* Whether to keep an in-memory history of queries
193-
* for debugging and timeline purposes.
194-
*
195-
* @var bool
196-
*/
197-
public $saveQueries = false;
198-
199192
//--------------------------------------------------------------------
200193

201194
/**
202-
* Array of query objects that have executed
195+
* The last query object that was executed
203196
* on this connection.
204197
*
205198
* @var array
206199
*/
207-
protected $queries = [];
200+
protected $lastQuery;
208201

209202
/**
210203
* Connection ID
@@ -495,40 +488,6 @@ abstract public function getVersion();
495488

496489
//--------------------------------------------------------------------
497490

498-
/**
499-
* Specifies whether this connection should keep queries objects or not.
500-
*
501-
* @param bool $save
502-
*/
503-
public function saveQueries($save = false)
504-
{
505-
$this->saveQueries = $save;
506-
507-
return $this;
508-
}
509-
510-
//--------------------------------------------------------------------
511-
512-
/**
513-
* Stores a new query with the object. This is primarily used by
514-
* the prepared queries.
515-
*
516-
* @param \CodeIgniter\Database\Query $query
517-
*
518-
* @return $this
519-
*/
520-
public function addQuery(Query $query)
521-
{
522-
if ($this->saveQueries)
523-
{
524-
$this->queries[] = $query;
525-
}
526-
527-
return $this;
528-
}
529-
530-
//--------------------------------------------------------------------
531-
532491
/**
533492
* Executes the query against the database.
534493
*
@@ -574,7 +533,9 @@ public function query(string $sql, $binds = null, $queryClass = 'CodeIgniter\\Da
574533

575534
$startTime = microtime(true);
576535

577-
536+
// Always save the last query so we can use
537+
// the getLastQuery() method.
538+
$this->lastQuery = $query;
578539

579540
// Run the query for real
580541
if (! $this->pretend && false === ($this->resultID = $this->simpleQuery($query->getQuery())))
@@ -583,19 +544,21 @@ public function query(string $sql, $binds = null, $queryClass = 'CodeIgniter\\Da
583544

584545
// @todo deal with errors
585546

586-
if ($this->saveQueries && ! $this->pretend)
547+
if (! $this->pretend)
587548
{
588-
$this->queries[] = $query;
549+
// Let others do something with this query.
550+
Hooks::trigger('DBQuery', $query);
589551
}
590552

591553
return new $resultClass($this->connID, $this->resultID);
592554
}
593555

594556
$query->setDuration($startTime);
595557

596-
if ($this->saveQueries && ! $this->pretend)
558+
if (! $this->pretend)
597559
{
598-
$this->queries[] = $query;
560+
// Let others do somethign with this query
561+
Hooks::trigger('DBQuery', $query);
599562
}
600563

601564
// If $pretend is true, then we just want to return
@@ -691,39 +654,14 @@ public function prepare(\Closure $func, array $options = [])
691654

692655
//--------------------------------------------------------------------
693656

694-
/**
695-
* Returns an array containing all of the
696-
*
697-
* @return array
698-
*/
699-
public function getQueries(): array
700-
{
701-
return $this->queries;
702-
}
703-
704-
//--------------------------------------------------------------------
705-
706-
/**
707-
* Returns the total number of queries that have been performed
708-
* on this connection.
709-
*
710-
* @return mixed
711-
*/
712-
public function getQueryCount()
713-
{
714-
return count($this->queries);
715-
}
716-
717-
//--------------------------------------------------------------------
718-
719657
/**
720658
* Returns the last query's statement object.
721659
*
722660
* @return mixed
723661
*/
724662
public function getLastQuery()
725663
{
726-
return end($this->queries);
664+
return $this->lastQuery;
727665
}
728666

729667
//--------------------------------------------------------------------
@@ -735,7 +673,7 @@ public function getLastQuery()
735673
*/
736674
public function showLastQuery()
737675
{
738-
return (string)end($this->queries);
676+
return (string)$this->lastQuery;
739677
}
740678

741679
//--------------------------------------------------------------------

system/Database/BasePreparedQuery.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace CodeIgniter\Database;
22

3+
use CodeIgniter\Hooks\Hooks;
4+
35
abstract class BasePreparedQuery implements PreparedQueryInterface
46
{
57
/**
@@ -115,8 +117,8 @@ public function execute(...$data)
115117

116118
$query->setDuration($startTime);
117119

118-
// Save it to the connection
119-
$this->db->addQuery($query);
120+
// Let others do something with this query
121+
Hooks::trigger('DBQuery', $query);
120122

121123
// Return a result object
122124
$resultClass = str_replace('PreparedQuery', 'Result', get_class($this));

system/Database/ConnectionInterface.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,6 @@ public function getVersion();
142142

143143
//--------------------------------------------------------------------
144144

145-
/**
146-
* Specifies whether this connection should keep queries objects or not.
147-
*
148-
* @param bool $doLog
149-
*/
150-
public function saveQueries($doLog = false);
151-
152-
//--------------------------------------------------------------------
153-
154145
/**
155146
* Orchestrates a query against the database. Queries must use
156147
* Database\Statement objects to store the query and build it.
@@ -192,25 +183,6 @@ public function table($tableName);
192183

193184
//--------------------------------------------------------------------
194185

195-
/**
196-
* Returns an array containing all of the
197-
*
198-
* @return array
199-
*/
200-
public function getQueries(): array;
201-
202-
//--------------------------------------------------------------------
203-
204-
/**
205-
* Returns the total number of queries that have been performed
206-
* on this connection.
207-
*
208-
* @return mixed
209-
*/
210-
public function getQueryCount();
211-
212-
//--------------------------------------------------------------------
213-
214186
/**
215187
* Returns the last query's statement object.
216188
*

system/Database/Postgre/Connection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,18 @@ public function connect($persistent = false)
8383
$this->buildDSN();
8484
}
8585

86+
// Strip pgsql if exists
87+
if (mb_strpos($this->DSN, 'pgsql:') === 0)
88+
{
89+
$this->DSN = mb_substr($this->DSN, 6);
90+
}
91+
92+
// Convert semicolons to spaces.
93+
$this->DSN = str_replace(';', ' ', $this->DSN);
94+
8695
$this->connID = $persistent === true
87-
? pg_pconnect($this->DSN) : pg_connect($this->DSN);
96+
? pg_pconnect($this->DSN)
97+
: pg_connect($this->DSN);
8898

8999
if ($this->connID !== false)
90100
{

tests/_support/Database/MockConnection.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class MockConnection extends BaseConnection
66

77
public $database;
88

9-
public $saveQueries = true;
9+
public $lastQuery;
1010

1111
//--------------------------------------------------------------------
1212

@@ -34,28 +34,20 @@ public function query(string $sql, $binds = null)
3434

3535
$startTime = microtime(true);
3636

37+
$this->lastQuery = $query;
38+
3739
// Run the query
3840
if (false === ($this->resultID = $this->simpleQuery($query->getQuery())))
3941
{
4042
$query->setDuration($startTime, $startTime);
4143

4244
// @todo deal with errors
4345

44-
if ($this->saveQueries)
45-
{
46-
$this->queries[] = $query;
47-
}
48-
4946
return false;
5047
}
5148

5249
$query->setDuration($startTime);
5350

54-
if ($this->saveQueries)
55-
{
56-
$this->queries[] = $query;
57-
}
58-
5951
$resultClass = str_replace('Connection', 'Result', get_class($this));
6052

6153
return new $resultClass($this->connID, $this->resultID);

tests/system/Database/BaseConnectionTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class BaseConnectionTest extends \CIUnitTestCase
4747
'failover' => [],
4848
'saveQueries' => true,
4949
];
50-
50+
5151
//--------------------------------------------------------------------
52-
53-
public function testSavesConfigOptions()
52+
53+
public function testSavesConfigOptions()
5454
{
5555
$db = new MockConnection($this->options);
56-
56+
5757
$this->assertSame('localhost', $db->hostname);
5858
$this->assertSame('first', $db->username);
5959
$this->assertSame('last', $db->password);
@@ -70,23 +70,22 @@ public function testSavesConfigOptions()
7070
$this->assertSame(false, $db->compress);
7171
$this->assertSame(true, $db->strictOn);
7272
$this->assertSame([], $db->failover);
73-
$this->assertSame(true, $db->saveQueries);
7473
}
75-
74+
7675
//--------------------------------------------------------------------
77-
78-
public function testConnectionThrowExceptionWhenCannotConnect()
76+
77+
public function testConnectionThrowExceptionWhenCannotConnect()
7978
{
8079
$db = new MockConnection($this->options);
81-
80+
8281
$this->setExpectedException('CodeIgniter\DatabaseException', 'Unable to connect to the database.');
83-
82+
8483
$db->shouldReturn('connect', false)
8584
->initialize();
8685
}
87-
86+
8887
//--------------------------------------------------------------------
89-
88+
9089
public function testCanConnectAndStoreConnection()
9190
{
9291
$db = new MockConnection($this->options);

tests/system/Database/Builder/InsertTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testThrowsExceptionOnNoValuesSet()
4646
}
4747

4848
//--------------------------------------------------------------------
49-
49+
5050
public function testInsertBatch()
5151
{
5252
$builder = $this->db->table('jobs');
@@ -61,25 +61,17 @@ public function testInsertBatch()
6161

6262
$builder->insertBatch($insertData, true, true);
6363

64-
$queries = $this->db->getQueries();
65-
66-
$q1 = $queries[0];
67-
$q2 = $queries[1];
64+
$query = $this->db->getLastQuery();
6865

69-
$this->assertTrue($q1 instanceof Query);
70-
$this->assertTrue($q2 instanceof Query);
66+
$this->assertTrue($query instanceof Query);
7167

72-
$raw1 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES (:description,:id,:name)";
73-
$raw2 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES (:description0,:id0,:name0)";
68+
$raw = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES (:description0,:id0,:name0)";
7469

75-
$this->assertEquals($raw1, str_replace("\n", ' ', $q1->getOriginalQuery() ));
76-
$this->assertEquals($raw2, str_replace("\n", ' ', $q2->getOriginalQuery() ));
70+
$this->assertEquals($raw, str_replace("\n", ' ', $query->getOriginalQuery() ));
7771

78-
$expected1 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('Theres something in your teeth',2,'Commedian')";
79-
$expected2 = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('Iam yellow',3,'Cab Driver')";
72+
$expected = "INSERT INTO \"jobs\" (\"description\", \"id\", \"name\") VALUES ('Iam yellow',3,'Cab Driver')";
8073

81-
$this->assertEquals($expected1, str_replace("\n", ' ', $q1->getQuery() ));
82-
$this->assertEquals($expected2, str_replace("\n", ' ', $q2->getQuery() ));
74+
$this->assertEquals($expected, str_replace("\n", ' ', $query->getQuery() ));
8375
}
8476

8577
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)