74 lines
1.8 KiB
PHP
74 lines
1.8 KiB
PHP
<?php
|
||
|
||
require_once 'core/drivers/db.php';
|
||
|
||
/**
|
||
* Простой класс для работы с базами данных
|
||
*/
|
||
class Database_PGSQL extends DB implements IDatabase
|
||
{
|
||
public function connect(array $dsn)
|
||
{
|
||
if (isset($dsn['port'])) {
|
||
$port = "port={$dsn['port']}";
|
||
} else {
|
||
$port = "port=5432";
|
||
}
|
||
$str = "host={$dsn['hostspec']} $port dbname={$dsn['database']} user={$dsn['username']} password={$dsn['password']}";
|
||
$db = @pg_connect($str)
|
||
or die("Unable connect to database!");
|
||
|
||
return ($this->db = $db);
|
||
}
|
||
|
||
public function close()
|
||
{
|
||
return pg_close($this->db);
|
||
}
|
||
|
||
public function query($query)
|
||
{
|
||
$res = pg_query($this->db, $query)
|
||
or die("Error: wrong SQL query #$query#");
|
||
return $res;
|
||
}
|
||
|
||
public function fetchAllArray($query, $type = PGSQL_ASSOC)
|
||
{
|
||
$res = $this->query($query);
|
||
|
||
$rows = array();
|
||
while ($row = pg_fetch_array($res, NULL, $type)) {
|
||
$rows[] = $this->clean($row);
|
||
}
|
||
pg_free_result($res);
|
||
return ($rows) ? $rows : array();
|
||
}
|
||
|
||
public function affectedRows()
|
||
{
|
||
return pg_affected_rows($this->db);
|
||
}
|
||
|
||
private function clean($row)
|
||
{
|
||
foreach ($row as $key => $value) {
|
||
$row[$key] = trim($value);
|
||
}
|
||
return $row;
|
||
}
|
||
|
||
public function fetchOneArray($query, $type = PGSQL_ASSOC)
|
||
{
|
||
$res = $this->query($query);
|
||
$row = pg_fetch_array($res, NULL, $type);
|
||
pg_free_result($res);
|
||
return ($row) ? $this->clean($row) : array();
|
||
}
|
||
|
||
function getNextId($seq)
|
||
{
|
||
$result = $this->fetchOneArray("SELECT nextval('$seq')");
|
||
return $result['nextval'];
|
||
}
|
||
}
|