Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit 3ed2cda

Browse files
committed
Add prepare check
1 parent f5b38e3 commit 3ed2cda

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

src/DeleteQuery.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PDO;
66
use PDOStatement;
7+
use RuntimeException;
78

89
/**
910
* Delete Query.
@@ -221,11 +222,19 @@ public function execute(): bool
221222
/**
222223
* Prepares a statement for execution and returns a statement object.
223224
*
225+
* @throws RuntimeException
226+
*
224227
* @return PDOStatement
225228
*/
226229
public function prepare(): PDOStatement
227230
{
228-
return $this->pdo->prepare($this->build());
231+
$statement = $this->pdo->prepare($this->build());
232+
233+
if (!$statement instanceof PDOStatement) {
234+
throw new RuntimeException('The database statement could not be prepared.');
235+
}
236+
237+
return $statement;
229238
}
230239

231240
/**

src/FunctionExpression.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function __construct(string $value, Quoter $quoter)
2929
/**
3030
* Alias.
3131
*
32-
* @param string|null $alias Alias
32+
* @param string $alias Alias
3333
*
3434
* @return $this The self instance
3535
*/
36-
public function alias(string $alias = null): self
36+
public function alias(string $alias): self
3737
{
3838
$clone = clone $this;
3939
$clone->value = sprintf('%s AS %s', $clone->value, $this->quoter->quoteName($alias));

src/InsertQuery.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PDO;
66
use PDOStatement;
7+
use RuntimeException;
78

89
/**
910
* Insert Query.
@@ -149,11 +150,19 @@ public function execute(): bool
149150
/**
150151
* Prepare statement.
151152
*
153+
* @throws RuntimeException
154+
*
152155
* @return PDOStatement The pdo statement
153156
*/
154157
public function prepare(): PDOStatement
155158
{
156-
return $this->pdo->prepare($this->build());
159+
$statement = $this->pdo->prepare($this->build());
160+
161+
if (!$statement instanceof PDOStatement) {
162+
throw new RuntimeException('The database statement could not be prepared.');
163+
}
164+
165+
return $statement;
157166
}
158167

159168
/**

src/SelectQuery.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PDO;
66
use PDOStatement;
7+
use RuntimeException;
78

89
/**
910
* Select Query.
@@ -631,11 +632,19 @@ public function execute(): PDOStatement
631632
/**
632633
* Prepares a statement for execution and returns a statement object.
633634
*
635+
* @throws RuntimeException
636+
*
634637
* @return PDOStatement The pdo statement
635638
*/
636639
public function prepare(): PDOStatement
637640
{
638-
return $this->pdo->prepare($this->build());
641+
$statement = $this->pdo->prepare($this->build());
642+
643+
if (!$statement instanceof PDOStatement) {
644+
throw new RuntimeException('The database statement could not be prepared.');
645+
}
646+
647+
return $statement;
639648
}
640649

641650
/**

src/UpdateQuery.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PDO;
66
use PDOStatement;
7+
use RuntimeException;
78

89
/**
910
* Update Query.
@@ -226,11 +227,19 @@ public function execute(): bool
226227
/**
227228
* Prepares a statement for execution and returns a statement object.
228229
*
230+
* @throws RuntimeException
231+
*
229232
* @return PDOStatement The PDOStatement
230233
*/
231234
public function prepare(): PDOStatement
232235
{
233-
return $this->pdo->prepare($this->build());
236+
$statement = $this->pdo->prepare($this->build());
237+
238+
if (!$statement instanceof PDOStatement) {
239+
throw new RuntimeException('The database statement could not be prepared.');
240+
}
241+
242+
return $statement;
234243
}
235244

236245
/**

tests/QuoterTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace Odan\Database\Test;
66

@@ -87,7 +87,8 @@ public function testQuoteName()
8787
$this->assertSame('`dbname`.`tablename`', $quoter->quoteName('dbname.tablename'));
8888
$this->assertSame('`dbname`.`tablename`.`field`', $quoter->quoteName('dbname.tablename.field'));
8989
// Alias.field AS thing
90-
$this->assertSame('`dbname`.`tablename`.`field` AS `thing`', $quoter->quoteName('dbname.tablename.field AS thing'));
90+
$this->assertSame('`dbname`.`tablename`.`field` AS `thing`',
91+
$quoter->quoteName('dbname.tablename.field AS thing'));
9192

9293
$this->assertSame('`.`', $quoter->quoteName('.'));
9394
$this->assertSame('`?`', $quoter->quoteName('?'));
@@ -130,4 +131,22 @@ public function testQuoteNames()
130131
$row = ['a', 'a.b', 'a.b.c', new RawExp('a.z')];
131132
$this->assertSame(['`a`', '`a`.`b`', '`a`.`b`.`c`', 'a.z'], $quoter->quoteNames($row));
132133
}
134+
135+
/**
136+
* Test.
137+
*
138+
* @return void
139+
*/
140+
public function testQuoteByFields()
141+
{
142+
$quoter = $this->getConnection()->getQuoter();
143+
$this->assertSame([], $quoter->quoteByFields([]));
144+
145+
$row = ['a', 'a.b', 'a.b.c', new RawExp('a.z')];
146+
$this->assertSame(['`a`', '`a`.`b`', '`a`.`b`.`c`', 'a.z'], $quoter->quoteByFields($row));
147+
148+
$row = ['ÿ', "\0", "'", '"'];
149+
$this->assertSame(['`ÿ`', '``', "`'`", '`"`'], $quoter->quoteByFields($row));
150+
}
151+
133152
}

0 commit comments

Comments
 (0)