Location: PHPKode > projects > Gnew > Gnew-2013.1/includes/pgsql.php
<?php
// -----------------------------------------------------------------------------
// $Id: pgsql.php 703 2013-04-01 12:10:36Z raoul $
//
// Copyright (C) 2013 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
// -----------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

class pgsql
{
    // ------------------------------
    private $host = SQL_HOST;
    private $user = SQL_USER;
    private $password = SQL_PASSWORD;
    private $database = SQL_DATABASE;
    private $port = SQL_PORT;
    // ------------------------------
    private $link;
    private $num_queries = 0;
    private $result;
    // ------------------------------

    public function close()
    {
        return $this->link ? @pg_close($this->link) : false;
    }

    private function connect()
    {
        $str = 'host=' . $this->host . ' user=' . $this->user . ' password=' . $this->password . ' dbname=' . $this->database . ' port=' . $this->port;
        $this->link = @pg_connect($str);
        return $this->link ? $this->link : $this->error('');
    }

    private function error($query)
    {
        if (!$this->link)
        {
            exit('Connection to PostgreSQL server has failed.');
        }
        if (!$this->result)
        {
            printf('Error in SQL query "%s".', $query);
            exit();
        }
    }

    public function fetch()
    {
        return $this->result ? @pg_fetch_assoc($this->result) : false;
    }

    public function fetch_all()
    {
        return $this->result ? @pg_fetch_all($this->result) : false;
    }

    public function insert_id()
    {
        if ($this->result && !empty($this->last_query))
        {
            if (preg_match('`^INSERT[\n ]INTO[\n ]([a-z_]+)`', $this->last_query, $table))
            {
                $seq = array(TABLE_ANSWERS => TABLE_ANSWERS . '_answer_id_seq',
                             TABLE_ARTICLES => TABLE_ARTICLES . '_article_id_seq',
                             TABLE_CATEGORIES => TABLE_CATEGORIES . '_category_id_seq',
                             TABLE_COMMENTS => TABLE_COMMENTS . '_comment_id_seq',
                             TABLE_EMOTICONS => TABLE_EMOTICONS . '_emoticon_id_seq',
                             TABLE_NEWS => TABLE_NEWS . '_news_id_seq',
                             TABLE_POSTS => TABLE_POSTS . '_post_id_seq',
                             TABLE_QUESTIONS => TABLE_QUESTIONS . '_question_id_seq',
                             TABLE_USERS => TABLE_USERS . '_user_id_seq',
                             TABLE_VOTES => TABLE_VOTES . '_vote_id_seq');
                $last_id = @pg_query($this->link, 'SELECT currval(\'' . $seq[$table[1]] . '\') AS last_id');
                if (!$last_id)
                {
                    return false;
                }
                else
                {
                    $insert_id = @pg_fetch_array($last_id, null, PGSQL_ASSOC);
                    return $insert_id ? $insert_id['last_id'] : false;
                }
            }
        }
        else
        {
            return false;
        }
    }

    public function num_queries()
    {
        return $this->num_queries;
    }

    public function num_rows()
    {
        return $this->result ? @pg_num_rows($this->result) : false;
    }

    public function query($query)
    {
        // Check and open a new connection to the PostgreSQL server
        if (!$this->connect())
        {
            return false;
        }
        elseif (!empty($query))
        {
            $this->num_queries++;
            $this->last_query = $query;
            $this->result = @pg_query($this->link, $query);
            if ($this->result)
            {
                return $this->result;
            }
            else
            {
                return $this->error($query);
            }
        }
        else
        {
            return false;
        }
    }
}

?>
Return current item: Gnew