Skip to content

Commit

Permalink
Merge pull request #113 from DanielPlainview/master
Browse files Browse the repository at this point in the history
DatabaseException при отсутствии соединения
  • Loading branch information
dovg committed Jun 29, 2012
2 parents 5296afc + 0de68a0 commit d476b09
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
17 changes: 10 additions & 7 deletions core/DB/PgSQL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public function connect()
.($this->basename ? " dbname={$this->basename}" : null)
.($this->port ? " port={$this->port}" : null);

if ($this->persistent)
$this->link = pg_pconnect($conn);
else
$this->link = pg_connect($conn);

if (!$this->link)
try {
if ($this->persistent)
$this->link = pg_pconnect($conn);
else
$this->link = pg_connect($conn);
} catch (Exception $e) {
throw new DatabaseException(
'can not connect to PostgreSQL server: '.pg_errormessage()
'can not connect to PostgreSQL server: '.$e->getMessage(),
$e->getCode(),
$e
);
}

if ($this->encoding)
$this->setDbEncoding();
Expand Down
7 changes: 7 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2012-06-29 N. Konstantinov

* core/DB/PgSQL.class.php
test/core/DbConnectionTest.class.php:

throw DatabaseException instead of BaseException in case of connection's failure

2012-06-27 Alexey S. Denisov

* main/Base/AbstractProtoClass.class.php
Expand Down
30 changes: 30 additions & 0 deletions test/core/DbConnectionTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

final class DbConnectionTest extends TestCase
{
public function setUp()
{
DBPool::me()->
addLink(
'badLink',
DB::spawn('PinbedPgSQL', 'postgres', '', 'localhost', 'wrongDatabase')
);
}

public function testPostgresql()
{
try {
$link = DBPool::me()->getLink('badLink');
$this->fail('Unreachable code');
} catch(Exception $e) {
$this->assertInstanceOf('DatabaseException', $e);
}
}

public function tearDown()
{
DBPool::me()->dropLink('badLink');
}
}

?>

0 comments on commit d476b09

Please sign in to comment.