Skip to content

Commit a69453f

Browse files
committed
Adding bug fixes and updates for 2.1. Fixes #139.
1 parent 3d53dd5 commit a69453f

File tree

9 files changed

+13
-95
lines changed

9 files changed

+13
-95
lines changed

src/AbstractStatement.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use PDO;
1111
use PDOException;
12+
use PDOStatement;
1213

1314
abstract class AbstractStatement implements QueryInterface
1415
{
@@ -28,22 +29,10 @@ public function __construct(PDO $dbh)
2829
*
2930
* @return mixed
3031
*/
31-
public function execute()
32+
public function execute(): PDOStatement
3233
{
3334
$stmt = $this->dbh->prepare($this->__toString());
34-
35-
try {
36-
$success = $stmt->execute($this->getValues());
37-
if (!$success) {
38-
list($state, $code, $message) = $stmt->errorInfo();
39-
40-
// We are not in exception mode, raise error.
41-
trigger_error("SQLSTATE[{$state}] [{$code}] {$message}", E_USER_ERROR);
42-
}
43-
} catch (PDOException $e) {
44-
// We are in exception mode, carry on.
45-
throw $e;
46-
}
35+
$stmt->execute($this->getValues());
4736

4837
return $stmt;
4938
}

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Database extends PDO
1919
*
2020
* @codeCoverageIgnore
2121
*/
22-
public function __construct(string $dsn, string $username = null, string $password = null, array $options = [])
22+
public function __construct(string $dsn, ?string $username = null, ?string $password = null, array $options = [])
2323
{
2424
parent::__construct($dsn, $username, $password, $options + $this->getDefaultOptions());
2525
}

src/Statement/Delete.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,4 @@ public function __toString(): string
110110

111111
return $sql;
112112
}
113-
114-
/**
115-
* @return int
116-
*/
117-
public function execute()
118-
{
119-
return parent::execute()->rowCount();
120-
}
121113
}

src/Statement/Insert.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,4 @@ public function getValues(): array
223223

224224
return $values;
225225
}
226-
227-
/**
228-
* @return int|string
229-
*/
230-
public function execute()
231-
{
232-
parent::execute();
233-
234-
return $this->dbh->lastInsertId();
235-
}
236226
}

src/Statement/Update.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ public function set(string $column, $value): self
7272
*/
7373
public function getValues(): array
7474
{
75-
$values = array_values($this->pairs);
75+
$values = [];
76+
foreach ($this->pairs as $value) {
77+
if ($value instanceof QueryInterface) {
78+
$values = array_merge($values, $value->getValues());
79+
} else {
80+
$values[] = $value;
81+
}
82+
}
7683

7784
if ($this->where !== null) {
7885
$values = array_merge($values, $this->where->getValues());
@@ -143,12 +150,4 @@ public function __toString(): string
143150

144151
return $sql;
145152
}
146-
147-
/**
148-
* @return int
149-
*/
150-
public function execute()
151-
{
152-
return parent::execute()->rowCount();
153-
}
154153
}

tests/AbstractStatementTest.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,12 @@ public function __toString(): string
5252
};
5353
}
5454

55-
public function testExecuteSuccess()
55+
public function testExecute()
5656
{
5757
$this->mock
5858
->method('execute')
5959
->willReturn(true);
6060

6161
$this->assertEquals($this->mock, $this->subject->execute());
6262
}
63-
64-
public function testExecuteException()
65-
{
66-
$this->mock
67-
->method('execute')
68-
->willThrowException(new PDOException('message', 100));
69-
70-
$this->expectException(PDOException::class);
71-
$this->expectExceptionCode(100);
72-
$this->expectExceptionMessage('message');
73-
74-
$this->subject->execute();
75-
}
76-
77-
public function testExecuteFailure()
78-
{
79-
$this->mock
80-
->method('execute')
81-
->willReturn(false);
82-
83-
$this->expectError();
84-
$this->expectErrorMessage('SQLSTATE[HY100] [100] near "bogus": syntax error');
85-
86-
$this->subject->execute();
87-
}
8863
}

tests/Statement/DeleteTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,4 @@ public function testGetValuesWithLimit()
167167
$this->assertIsArray($this->subject->getValues());
168168
$this->assertCount(2, $this->subject->getValues());
169169
}
170-
171-
public function testExecute()
172-
{
173-
$this->subject
174-
->from('test');
175-
176-
$this->assertEquals(1, $this->subject->execute());
177-
}
178170
}

tests/Statement/InsertTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,4 @@ public function testGetValuesWithDuplicate()
200200
$this->assertIsArray($this->subject->getValues());
201201
$this->assertCount(2, $this->subject->getValues());
202202
}
203-
204-
public function testExecute()
205-
{
206-
$this->subject
207-
->into('test')
208-
->columns('id')
209-
->values(1);
210-
211-
$this->assertEquals(1, $this->subject->execute());
212-
}
213203
}

tests/Statement/UpdateTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,4 @@ public function testGetValuesEmpty()
198198
$this->assertIsArray($this->subject->getValues());
199199
$this->assertEmpty($this->subject->getValues());
200200
}
201-
202-
public function testExecute()
203-
{
204-
$this->subject
205-
->table('test')
206-
->set('col', 'value');
207-
208-
$this->assertEquals(1, $this->subject->execute());
209-
}
210201
}

0 commit comments

Comments
 (0)