Skip to content

Commit d476b09

Browse files
committed
Merge pull request #113 from DanielPlainview/master
DatabaseException при отсутствии соединения
2 parents 5296afc + 0de68a0 commit d476b09

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

core/DB/PgSQL.class.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ public function connect()
3737
.($this->basename ? " dbname={$this->basename}" : null)
3838
.($this->port ? " port={$this->port}" : null);
3939

40-
if ($this->persistent)
41-
$this->link = pg_pconnect($conn);
42-
else
43-
$this->link = pg_connect($conn);
44-
45-
if (!$this->link)
40+
try {
41+
if ($this->persistent)
42+
$this->link = pg_pconnect($conn);
43+
else
44+
$this->link = pg_connect($conn);
45+
} catch (Exception $e) {
4646
throw new DatabaseException(
47-
'can not connect to PostgreSQL server: '.pg_errormessage()
47+
'can not connect to PostgreSQL server: '.$e->getMessage(),
48+
$e->getCode(),
49+
$e
4850
);
51+
}
4952

5053
if ($this->encoding)
5154
$this->setDbEncoding();

doc/ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2012-06-29 N. Konstantinov
2+
3+
* core/DB/PgSQL.class.php
4+
test/core/DbConnectionTest.class.php:
5+
6+
throw DatabaseException instead of BaseException in case of connection's failure
7+
18
2012-06-27 Alexey S. Denisov
29

310
* main/Base/AbstractProtoClass.class.php
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
final class DbConnectionTest extends TestCase
4+
{
5+
public function setUp()
6+
{
7+
DBPool::me()->
8+
addLink(
9+
'badLink',
10+
DB::spawn('PinbedPgSQL', 'postgres', '', 'localhost', 'wrongDatabase')
11+
);
12+
}
13+
14+
public function testPostgresql()
15+
{
16+
try {
17+
$link = DBPool::me()->getLink('badLink');
18+
$this->fail('Unreachable code');
19+
} catch(Exception $e) {
20+
$this->assertInstanceOf('DatabaseException', $e);
21+
}
22+
}
23+
24+
public function tearDown()
25+
{
26+
DBPool::me()->dropLink('badLink');
27+
}
28+
}
29+
30+
?>

0 commit comments

Comments
 (0)