Skip to content

Commit 8c8b7df

Browse files
committed
v1.1.1
1 parent b4c9238 commit 8c8b7df

File tree

10 files changed

+136
-43
lines changed

10 files changed

+136
-43
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Dimitar Ivanov
3+
Copyright (c) 2020-2024 Dimitar Ivanov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"name": "riverside/php-orm",
33
"description": "PHP ORM micro-library and query builder",
44
"type": "library",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"keywords": ["php", "orm", "pdo", "mysql", "database"],
77
"homepage": "https://github.com/riverside/php-orm",
88
"license": "MIT",
99
"authors": [
1010
{
1111
"name": "Dimitar Ivanov",
12-
"email": "[email protected]",
13-
"homepage": "https://zinoui.com",
12+
"email": "[email protected]",
13+
"homepage": "https://github.com/riverside",
1414
"role": "Developer"
1515
}
1616
],

docs/api.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<meta name="twitter:url" content="https://riverside.github.io/php-orm/">
1313
<meta name="twitter:site" content="@zino_ui">
1414
<meta name="twitter:creator" content="@DimitarIvanov">
15-
<meta name="twitter:domain" content="zinoui.com">
1615
<meta property="og:title" content="PHP ORM">
1716
<meta property="og:site_name" content="Zino UI">
1817
<meta property="og:url" content="https://riverside.github.io/php-orm/">
@@ -108,6 +107,12 @@
108107
<li><a href="#expr-toString">__toString()</a></li>
109108
</ul>
110109
</li>
110+
<li><a href="#except">Exception</a></li>
111+
<li><a href="#base">Base</a>
112+
<ul class="nav">
113+
<li><a href="#base-throwException">throwException()</a></li>
114+
</ul>
115+
</li>
111116
<li><a href="#cfg">Configuration</a>
112117
<ul class="nav">
113118
<li><a href="#cfg-construct">__construct()</a></li>
@@ -339,6 +344,15 @@ <h3 class="h4" id="expr-toString">__toString()</h3>
339344
<p>Returns the value as string.</p>
340345
<pre class="prettyprint">echo $expr;</pre>
341346
</section>
347+
<!-- Exception -->
348+
<h2 class="h3" id="except">Exception</h2>
349+
<!-- Base -->
350+
<h2 class="h3" id="base">Base</h2>
351+
<section>
352+
<h3 class="h4" id="base-throwException">throwException(string $message, int $code=null, \Throwable $previous=null)</h3>
353+
<p>Throws an exception.</p>
354+
<pre class="prettyprint">$this->throwException('This is an exception');</pre>
355+
</section>
342356
<!-- Configuration -->
343357
<h2 class="h3" id="cfg">Configuration</h2>
344358
<section>

src/Base.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace PhpOrm;
3+
4+
/**
5+
* Class Base
6+
*
7+
* @package PhpOrm
8+
*/
9+
class Base
10+
{
11+
/**
12+
* Throws an exception
13+
*
14+
* @param string $message
15+
* @param int|null $code
16+
* @param \Throwable|null $previous
17+
* @throws Exception
18+
*/
19+
public function throwException(string $message, int $code=null, \Throwable $previous=null)
20+
{
21+
throw new Exception($message, $code, $previous);
22+
}
23+
}

src/Connection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @package PhpOrm
88
*/
9-
class Connection
9+
class Connection extends Base
1010
{
1111
/**
1212
* Configuration instance
@@ -36,7 +36,6 @@ public function __construct(Configuration $configuration)
3636
* Gets PDO instance
3737
*
3838
* @return \PDO|null
39-
* @throws \Exception
4039
*/
4140
public function getDbh()
4241
{
@@ -115,7 +114,7 @@ public function getDsn(): string
115114
* Create a PDO instance
116115
*
117116
* @return Connection
118-
* @throws \Exception
117+
* @throws Exception
119118
*/
120119
public function connect(): Connection
121120
{
@@ -125,7 +124,7 @@ public function connect(): Connection
125124
$this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES,true);
126125

127126
} catch (\PDOException $e) {
128-
throw new \Exception($e->getMessage(), $e->getCode());
127+
$this->throwException($e->getMessage(), $e->getCode(), $e->getPrevious());
129128
}
130129

131130
return $this;
@@ -147,7 +146,7 @@ public function disconnect(): Connection
147146
* Destroys, then creates a new PDO instance
148147
*
149148
* @return Connection
150-
* @throws \Exception
149+
* @throws Exception
151150
*/
152151
public function reconnect(): Connection
153152
{

src/DB.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @package PhpOrm
88
*/
9-
class DB
9+
class DB extends Base
1010
{
1111
protected $attributes = array();
1212

@@ -53,7 +53,7 @@ class DB
5353
/**
5454
* DB constructor.
5555
*
56-
* @throws \Exception
56+
* @throws Exception
5757
*/
5858
public function __construct() {
5959
$this->mount();
@@ -64,7 +64,7 @@ public function __construct() {
6464
*
6565
* @param int $value
6666
* @return DB
67-
* @throws \Exception
67+
* @throws Exception
6868
*/
6969
public function autoCommit(int $value): DB
7070
{
@@ -80,7 +80,7 @@ public function autoCommit(int $value): DB
8080
* Begin a new transaction
8181
*
8282
* @return DB
83-
* @throws \Exception
83+
* @throws Exception
8484
*/
8585
public function begin(): DB
8686
{
@@ -225,7 +225,7 @@ protected function buildWhere(): string
225225
* Commits the current transaction, making its changes permanent
226226
*
227227
* @return DB
228-
* @throws \Exception
228+
* @throws Exception
229229
*/
230230
public function commit(): DB
231231
{
@@ -246,7 +246,7 @@ public static function config(string $filename) {
246246

247247
/**
248248
* @return int|null
249-
* @throws \Exception
249+
* @throws Exception
250250
*/
251251
public function count(): ?int
252252
{
@@ -297,7 +297,7 @@ public function debug(bool $value): DB
297297
/**
298298
* @param null $modifiers
299299
* @return int|null
300-
* @throws \Exception
300+
* @throws Exception
301301
*/
302302
public function delete($modifiers = null): ?int
303303
{
@@ -345,7 +345,7 @@ protected function dump()
345345
/**
346346
* @param $value
347347
* @return array|null
348-
* @throws \Exception
348+
* @throws Exception
349349
*/
350350
public function find($value): ?array
351351
{
@@ -362,25 +362,23 @@ public function find($value): ?array
362362
/**
363363
* @param string $statement
364364
* @return bool
365-
* @throws \Exception
365+
* @throws Exception
366366
*/
367367
protected function fire(string $statement): bool
368368
{
369+
try {
369370
$this->sth = $this->dbh->prepare($statement);
370-
if (!$this->sth) {
371-
throw new \Exception('Preparing statement failed.');
372-
}
373-
374-
if (!$this->sth->execute($this->params)) {
375-
throw new \Exception('Executing statement failed.');
371+
return $this->sth->execute($this->params);
372+
} catch (\PDOException $e) {
373+
$this->throwException($e->getMessage());
376374
}
377375

378376
return true;
379377
}
380378

381379
/**
382380
* @return array|null
383-
* @throws \Exception
381+
* @throws Exception
384382
*/
385383
public function first(): ?array
386384
{
@@ -393,7 +391,7 @@ public function first(): ?array
393391

394392
/**
395393
* @return array|null
396-
* @throws \Exception
394+
* @throws Exception
397395
*/
398396
public function get(): ?array
399397
{
@@ -430,7 +428,7 @@ public function having(string $value = null): DB
430428
* @param array $data
431429
* @param null $modifiers
432430
* @return int|null
433-
* @throws \Exception
431+
* @throws Exception
434432
*/
435433
public function insert(array $data, $modifiers = null): ?int
436434
{
@@ -490,7 +488,7 @@ public function limit(int $rowCount = null, int $offset = null): DB
490488

491489
/**
492490
* @return DB
493-
* @throws \Exception
491+
* @throws Exception
494492
*/
495493
protected function mount(): DB
496494
{
@@ -504,19 +502,19 @@ protected function mount(): DB
504502

505503
if (!is_file(self::$config))
506504
{
507-
throw new \Exception("File '".self::$config."' not found.");
505+
$this->throwException("File '".self::$config."' not found.");
508506
}
509507
$database = include self::$config;
510508
if (!isset($database[$this->connection]))
511509
{
512-
throw new \Exception("Connection not found.");
510+
$this->throwException("Connection not found.");
513511
}
514512
$opts = $database[$this->connection];
515513
foreach (array('username', 'password', 'database', 'host', 'port', 'driver', 'charset', 'collation') as $key)
516514
{
517515
if (!array_key_exists($key, $opts))
518516
{
519-
throw new \Exception("The '$key' index was not found in config.");
517+
$this->throwException("The '$key' index was not found in config.");
520518
}
521519
}
522520

@@ -574,7 +572,7 @@ public static function raw(string $value): Expression
574572
*
575573
* @param string $identifier
576574
* @return DB
577-
* @throws \Exception
575+
* @throws Exception
578576
*/
579577
public function releaseSavepoint(string $identifier): DB
580578
{
@@ -590,7 +588,7 @@ public function releaseSavepoint(string $identifier): DB
590588
* @param array $data
591589
* @param null $modifiers
592590
* @return int|null
593-
* @throws \Exception
591+
* @throws Exception
594592
*/
595593
public function replace(array $data, $modifiers = null): ?int
596594
{
@@ -644,7 +642,7 @@ public function rightJoin(string $table, string $conditions): DB
644642
* Rolls back the current transaction, canceling its changes
645643
*
646644
* @return DB
647-
* @throws \Exception
645+
* @throws Exception
648646
*/
649647
public function rollback(): DB
650648
{
@@ -661,7 +659,7 @@ public function rollback(): DB
661659
*
662660
* @param string $identifier
663661
* @return DB
664-
* @throws \Exception
662+
* @throws Exception
665663
*/
666664
public function rollbackToSavepoint(string $identifier): DB
667665
{
@@ -678,7 +676,7 @@ public function rollbackToSavepoint(string $identifier): DB
678676
*
679677
* @param string $identifier
680678
* @return DB
681-
* @throws \Exception
679+
* @throws Exception
682680
*/
683681
public function savepoint(string $identifier): DB
684682
{
@@ -730,7 +728,7 @@ public function truncate(): bool
730728
* @param array $data
731729
* @param null $modifiers
732730
* @return int
733-
* @throws \Exception
731+
* @throws Exception
734732
*/
735733
public function update(array $data, $modifiers = null): int
736734
{
@@ -788,7 +786,7 @@ public function update(array $data, $modifiers = null): int
788786
* @param array $data
789787
* @param null $modifiers
790788
* @return int|null
791-
* @throws \Exception
789+
* @throws Exception
792790
*/
793791
public function upsert(array $data, $modifiers = null): ?int
794792
{
@@ -802,7 +800,7 @@ public function upsert(array $data, $modifiers = null): ?int
802800
/**
803801
* @param string $column
804802
* @return string
805-
* @throws \Exception
803+
* @throws Exception
806804
*/
807805
public function value(string $column): string
808806
{

src/Exception.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace PhpOrm;
3+
4+
/**
5+
* Class Exception
6+
*
7+
* @package PhpOrm
8+
*/
9+
class Exception extends \Exception
10+
{
11+
12+
}

tests/BaseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpOrm\Tests;
3+
4+
use PhpOrm\Base;
5+
use PhpOrm\Exception;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class BaseTest extends TestCase
9+
{
10+
public function testException()
11+
{
12+
$this->expectException(Exception::class);
13+
$base = new Base();
14+
$base->throwException('Text exception');
15+
}
16+
}

0 commit comments

Comments
 (0)