Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
riverside committed Jun 22, 2024
1 parent b4c9238 commit 8c8b7df
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 43 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Dimitar Ivanov
Copyright (c) 2020-2024 Dimitar Ivanov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "riverside/php-orm",
"description": "PHP ORM micro-library and query builder",
"type": "library",
"version": "1.1.0",
"version": "1.1.1",
"keywords": ["php", "orm", "pdo", "mysql", "database"],
"homepage": "https://github.com/riverside/php-orm",
"license": "MIT",
"authors": [
{
"name": "Dimitar Ivanov",
"email": "[email protected]",
"homepage": "https://zinoui.com",
"email": "[email protected]",
"homepage": "https://github.com/riverside",
"role": "Developer"
}
],
Expand Down
16 changes: 15 additions & 1 deletion docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<meta name="twitter:url" content="https://riverside.github.io/php-orm/">
<meta name="twitter:site" content="@zino_ui">
<meta name="twitter:creator" content="@DimitarIvanov">
<meta name="twitter:domain" content="zinoui.com">
<meta property="og:title" content="PHP ORM">
<meta property="og:site_name" content="Zino UI">
<meta property="og:url" content="https://riverside.github.io/php-orm/">
Expand Down Expand Up @@ -108,6 +107,12 @@
<li><a href="#expr-toString">__toString()</a></li>
</ul>
</li>
<li><a href="#except">Exception</a></li>
<li><a href="#base">Base</a>
<ul class="nav">
<li><a href="#base-throwException">throwException()</a></li>
</ul>
</li>
<li><a href="#cfg">Configuration</a>
<ul class="nav">
<li><a href="#cfg-construct">__construct()</a></li>
Expand Down Expand Up @@ -339,6 +344,15 @@ <h3 class="h4" id="expr-toString">__toString()</h3>
<p>Returns the value as string.</p>
<pre class="prettyprint">echo $expr;</pre>
</section>
<!-- Exception -->
<h2 class="h3" id="except">Exception</h2>
<!-- Base -->
<h2 class="h3" id="base">Base</h2>
<section>
<h3 class="h4" id="base-throwException">throwException(string $message, int $code=null, \Throwable $previous=null)</h3>
<p>Throws an exception.</p>
<pre class="prettyprint">$this->throwException('This is an exception');</pre>
</section>
<!-- Configuration -->
<h2 class="h3" id="cfg">Configuration</h2>
<section>
Expand Down
23 changes: 23 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace PhpOrm;

/**
* Class Base
*
* @package PhpOrm
*/
class Base
{
/**
* Throws an exception
*
* @param string $message
* @param int|null $code
* @param \Throwable|null $previous
* @throws Exception
*/
public function throwException(string $message, int $code=null, \Throwable $previous=null)
{
throw new Exception($message, $code, $previous);
}
}
9 changes: 4 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @package PhpOrm
*/
class Connection
class Connection extends Base
{
/**
* Configuration instance
Expand Down Expand Up @@ -36,7 +36,6 @@ public function __construct(Configuration $configuration)
* Gets PDO instance
*
* @return \PDO|null
* @throws \Exception
*/
public function getDbh()
{
Expand Down Expand Up @@ -115,7 +114,7 @@ public function getDsn(): string
* Create a PDO instance
*
* @return Connection
* @throws \Exception
* @throws Exception
*/
public function connect(): Connection
{
Expand All @@ -125,7 +124,7 @@ public function connect(): Connection
$this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES,true);

} catch (\PDOException $e) {
throw new \Exception($e->getMessage(), $e->getCode());
$this->throwException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

return $this;
Expand All @@ -147,7 +146,7 @@ public function disconnect(): Connection
* Destroys, then creates a new PDO instance
*
* @return Connection
* @throws \Exception
* @throws Exception
*/
public function reconnect(): Connection
{
Expand Down
58 changes: 28 additions & 30 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @package PhpOrm
*/
class DB
class DB extends Base
{
protected $attributes = array();

Expand Down Expand Up @@ -53,7 +53,7 @@ class DB
/**
* DB constructor.
*
* @throws \Exception
* @throws Exception
*/
public function __construct() {
$this->mount();
Expand All @@ -64,7 +64,7 @@ public function __construct() {
*
* @param int $value
* @return DB
* @throws \Exception
* @throws Exception
*/
public function autoCommit(int $value): DB
{
Expand All @@ -80,7 +80,7 @@ public function autoCommit(int $value): DB
* Begin a new transaction
*
* @return DB
* @throws \Exception
* @throws Exception
*/
public function begin(): DB
{
Expand Down Expand Up @@ -225,7 +225,7 @@ protected function buildWhere(): string
* Commits the current transaction, making its changes permanent
*
* @return DB
* @throws \Exception
* @throws Exception
*/
public function commit(): DB
{
Expand All @@ -246,7 +246,7 @@ public static function config(string $filename) {

/**
* @return int|null
* @throws \Exception
* @throws Exception
*/
public function count(): ?int
{
Expand Down Expand Up @@ -297,7 +297,7 @@ public function debug(bool $value): DB
/**
* @param null $modifiers
* @return int|null
* @throws \Exception
* @throws Exception
*/
public function delete($modifiers = null): ?int
{
Expand Down Expand Up @@ -345,7 +345,7 @@ protected function dump()
/**
* @param $value
* @return array|null
* @throws \Exception
* @throws Exception
*/
public function find($value): ?array
{
Expand All @@ -362,25 +362,23 @@ public function find($value): ?array
/**
* @param string $statement
* @return bool
* @throws \Exception
* @throws Exception
*/
protected function fire(string $statement): bool
{
try {
$this->sth = $this->dbh->prepare($statement);
if (!$this->sth) {
throw new \Exception('Preparing statement failed.');
}

if (!$this->sth->execute($this->params)) {
throw new \Exception('Executing statement failed.');
return $this->sth->execute($this->params);
} catch (\PDOException $e) {
$this->throwException($e->getMessage());
}

return true;
}

/**
* @return array|null
* @throws \Exception
* @throws Exception
*/
public function first(): ?array
{
Expand All @@ -393,7 +391,7 @@ public function first(): ?array

/**
* @return array|null
* @throws \Exception
* @throws Exception
*/
public function get(): ?array
{
Expand Down Expand Up @@ -430,7 +428,7 @@ public function having(string $value = null): DB
* @param array $data
* @param null $modifiers
* @return int|null
* @throws \Exception
* @throws Exception
*/
public function insert(array $data, $modifiers = null): ?int
{
Expand Down Expand Up @@ -490,7 +488,7 @@ public function limit(int $rowCount = null, int $offset = null): DB

/**
* @return DB
* @throws \Exception
* @throws Exception
*/
protected function mount(): DB
{
Expand All @@ -504,19 +502,19 @@ protected function mount(): DB

if (!is_file(self::$config))
{
throw new \Exception("File '".self::$config."' not found.");
$this->throwException("File '".self::$config."' not found.");
}
$database = include self::$config;
if (!isset($database[$this->connection]))
{
throw new \Exception("Connection not found.");
$this->throwException("Connection not found.");
}
$opts = $database[$this->connection];
foreach (array('username', 'password', 'database', 'host', 'port', 'driver', 'charset', 'collation') as $key)
{
if (!array_key_exists($key, $opts))
{
throw new \Exception("The '$key' index was not found in config.");
$this->throwException("The '$key' index was not found in config.");
}
}

Expand Down Expand Up @@ -574,7 +572,7 @@ public static function raw(string $value): Expression
*
* @param string $identifier
* @return DB
* @throws \Exception
* @throws Exception
*/
public function releaseSavepoint(string $identifier): DB
{
Expand All @@ -590,7 +588,7 @@ public function releaseSavepoint(string $identifier): DB
* @param array $data
* @param null $modifiers
* @return int|null
* @throws \Exception
* @throws Exception
*/
public function replace(array $data, $modifiers = null): ?int
{
Expand Down Expand Up @@ -644,7 +642,7 @@ public function rightJoin(string $table, string $conditions): DB
* Rolls back the current transaction, canceling its changes
*
* @return DB
* @throws \Exception
* @throws Exception
*/
public function rollback(): DB
{
Expand All @@ -661,7 +659,7 @@ public function rollback(): DB
*
* @param string $identifier
* @return DB
* @throws \Exception
* @throws Exception
*/
public function rollbackToSavepoint(string $identifier): DB
{
Expand All @@ -678,7 +676,7 @@ public function rollbackToSavepoint(string $identifier): DB
*
* @param string $identifier
* @return DB
* @throws \Exception
* @throws Exception
*/
public function savepoint(string $identifier): DB
{
Expand Down Expand Up @@ -730,7 +728,7 @@ public function truncate(): bool
* @param array $data
* @param null $modifiers
* @return int
* @throws \Exception
* @throws Exception
*/
public function update(array $data, $modifiers = null): int
{
Expand Down Expand Up @@ -788,7 +786,7 @@ public function update(array $data, $modifiers = null): int
* @param array $data
* @param null $modifiers
* @return int|null
* @throws \Exception
* @throws Exception
*/
public function upsert(array $data, $modifiers = null): ?int
{
Expand All @@ -802,7 +800,7 @@ public function upsert(array $data, $modifiers = null): ?int
/**
* @param string $column
* @return string
* @throws \Exception
* @throws Exception
*/
public function value(string $column): string
{
Expand Down
12 changes: 12 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace PhpOrm;

/**
* Class Exception
*
* @package PhpOrm
*/
class Exception extends \Exception
{

}
16 changes: 16 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace PhpOrm\Tests;

use PhpOrm\Base;
use PhpOrm\Exception;
use PHPUnit\Framework\TestCase;

class BaseTest extends TestCase
{
public function testException()
{
$this->expectException(Exception::class);
$base = new Base();
$base->throwException('Text exception');
}
}
Loading

0 comments on commit 8c8b7df

Please sign in to comment.