Skip to content

Commit

Permalink
Fixing this branch to be 2.0 compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed Jan 21, 2020
1 parent a32be98 commit e753cd9
Show file tree
Hide file tree
Showing 30 changed files with 150 additions and 118 deletions.
12 changes: 4 additions & 8 deletions src/AbstractStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO;

use PDO;
Expand All @@ -26,7 +24,7 @@ public function __construct(PDO $dbh)
}

/**
* @throws PDOException
* @throws DatabaseException
*
* @return mixed
*/
Expand All @@ -37,14 +35,12 @@ public function execute()
try {
$success = $stmt->execute($this->getValues());
if (!$success) {
list($state, $code, $message) = $stmt->errorInfo();
$info = $stmt->errorInfo();

// We are not in exception mode, raise error.
user_error("SQLSTATE[{$state}] [{$code}] {$message}", E_USER_ERROR);
throw new DatabaseException($info[2], $info[0]);
}
} catch (PDOException $e) {
// We are in exception mode, carry on.
throw $e;
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
}

return $stmt;
Expand Down
4 changes: 1 addition & 3 deletions src/AdvancedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO;

abstract class AdvancedStatement extends AbstractStatement
{
/** @var Clause\Join[] $join */
/** @var Clause\Join[] */
protected $join = [];

/** @var Clause\Conditional $where */
Expand Down
9 changes: 5 additions & 4 deletions src/Clause/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

use FaaPz\PDO\DatabaseException;
use FaaPz\PDO\QueryInterface;

class Conditional implements QueryInterface
Expand Down Expand Up @@ -48,6 +47,8 @@ public function getValues(): array
}

/**
* @throws DatabaseException
*
* @return string
*/
public function __toString(): string
Expand All @@ -57,7 +58,7 @@ public function __toString(): string
case 'BETWEEN':
case 'NOT BETWEEN':
if (count($this->getValues()) != 2) {
trigger_error('Conditional operator "BETWEEN" requires two arguments', E_USER_ERROR);
throw new DatabaseException('Conditional operator "BETWEEN" requires two arguments');
}

$sql .= ' (? AND ?)';
Expand All @@ -66,7 +67,7 @@ public function __toString(): string
case 'IN':
case 'NOT IN':
if (count($this->getValues()) < 1) {
trigger_error('Conditional operator "IN" requires at least one argument', E_USER_ERROR);
throw new DatabaseException('Conditional operator "IN" requires at least one argument');
}

$sql .= ' (' . substr(str_repeat('?, ', count($this->getValues())), 0, -2) . ')';
Expand Down
2 changes: 0 additions & 2 deletions src/Clause/Grouping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

class Grouping extends Conditional
Expand Down
9 changes: 5 additions & 4 deletions src/Clause/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

use FaaPz\PDO\DatabaseException;
use FaaPz\PDO\QueryInterface;
use FaaPz\PDO\Statement\Select;

Expand Down Expand Up @@ -44,6 +43,8 @@ public function getValues(): array
}

/**
* @throws DatabaseException
*
* @return string
*/
public function __toString(): string
Expand All @@ -53,7 +54,7 @@ public function __toString(): string
reset($this->subject);
$alias = key($this->subject);
if (!is_string($alias)) {
trigger_error('Invalid subject array, use string keys for alias', E_USER_ERROR);
throw new DatabaseException('Invalid subject array, use string keys for alias');
}

$table = $this->subject[$alias];
Expand All @@ -62,7 +63,7 @@ public function __toString(): string
}
$table .= " AS {$alias}";
} elseif (!is_string($this->subject)) {
trigger_error('Invalid subject value, use array with string key for alias', E_USER_ERROR);
throw new DatabaseException('Invalid subject value, use array with string key for alias');
}

$sql = "JOIN {$table} ON {$this->on}";
Expand Down
10 changes: 4 additions & 6 deletions src/Clause/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

use FaaPz\PDO\QueryInterface;
Expand All @@ -23,7 +21,7 @@ class Limit implements QueryInterface
* @param int $size
* @param int|null $offset
*/
public function __construct(int $size, ?int $offset = null)
public function __construct(int $size, int $offset = null)
{
$this->size = $size;
$this->offset = $offset;
Expand All @@ -35,7 +33,7 @@ public function __construct(int $size, ?int $offset = null)
public function getValues(): array
{
$values = [];
if ($this->offset !== null) {
if (isset($this->offset)) {
$values[] = $this->offset;
}
$values[] = $this->size;
Expand All @@ -48,8 +46,8 @@ public function getValues(): array
*/
public function __toString(): string
{
$sql = 'LIMIT ?';
if ($this->offset !== null) {
$sql = '?';
if (isset($this->offset)) {
$sql .= ', ?';
}

Expand Down
2 changes: 0 additions & 2 deletions src/Clause/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

use FaaPz\PDO\QueryInterface;
Expand Down
2 changes: 0 additions & 2 deletions src/Clause/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Clause;

use FaaPz\PDO\QueryInterface;
Expand Down
2 changes: 0 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO;

use PDO;
Expand Down
28 changes: 28 additions & 0 deletions src/DatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @license MIT
* @license http://opensource.org/licenses/MIT
*/

namespace FaaPz\PDO;

use Exception;
use RuntimeException;

class DatabaseException extends RuntimeException
{
/** @var string $code */
protected $code;

/**
* @param string $message
* @param string $code
* @param Exception|null $previous
*/
public function __construct(string $message = '', string $code = 'database_error', Exception $previous = null)
{
parent::__construct($message, 0, $previous);
$this->code = $code;
}
}
2 changes: 0 additions & 2 deletions src/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO;

interface QueryInterface
Expand Down
11 changes: 7 additions & 4 deletions src/Statement/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\AbstractStatement;
use FaaPz\PDO\Clause;
use FaaPz\PDO\DatabaseException;
use PDO;
use PDOStatement;

Expand Down Expand Up @@ -56,14 +55,18 @@ public function getValues(): array
}

/**
* @throws DatabaseException
*
* @return string
*/
public function __toString(): string
{
if (!$this->method instanceof Clause\Method) {
trigger_error('No method set for call statement', E_USER_ERROR);
throw new DatabaseException('No method is set for stored procedure call');
}

return "CALL {$this->method}";
$sql = "CALL {$this->method}";

return $sql;
}
}
11 changes: 7 additions & 4 deletions src/Statement/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\AdvancedStatement;
use FaaPz\PDO\DatabaseException;
use PDO;

class Delete extends AdvancedStatement
Expand Down Expand Up @@ -68,12 +67,14 @@ public function getValues(): array
}

/**
* @throws DatabaseException
*
* @return string
*/
public function __toString(): string
{
if (empty($this->table)) {
trigger_error('No table set for delete statement', E_USER_ERROR);
throw new DatabaseException('No table is set for deletion');
}

$sql = 'DELETE';
Expand Down Expand Up @@ -107,13 +108,15 @@ public function __toString(): string
}

if ($this->limit != null) {
$sql .= " {$this->limit}";
$sql .= " LIMIT {$this->limit}";
}

return $sql;
}

/**
* @throws DatabaseException
*
* @return int
*/
public function execute()
Expand Down
13 changes: 8 additions & 5 deletions src/Statement/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\AbstractStatement;
use FaaPz\PDO\DatabaseException;
use FaaPz\PDO\QueryInterface;
use PDO;

Expand Down Expand Up @@ -98,20 +97,22 @@ public function ignore(): self
}

/**
* @throws DatabaseException
*
* @return string
*/
public function __toString(): string
{
if (empty($this->table)) {
trigger_error('No table set for insert statement', E_USER_ERROR);
throw new DatabaseException('No table is set for insertion');
}

if (empty($this->columns)) {
trigger_error('No columns set for insert statement', E_USER_ERROR);
throw new DatabaseException('Missing columns for insertion');
}

if (empty($this->values) || count($this->columns) != count($this->values)) {
trigger_error('No values set for insert statement', E_USER_ERROR);
throw new DatabaseException('Missing values for insertion');
}

$placeholders = '';
Expand Down Expand Up @@ -157,6 +158,8 @@ public function getValues(): array
}

/**
* @throws DatabaseException
*
* @return int|string
*/
public function execute()
Expand Down
Loading

0 comments on commit e753cd9

Please sign in to comment.