• Forum
  • Doc
  • Screenshots
  • Download
  • Donate
  • Contributors
  • Contact
  • Follow @phpfreechat
  • DEMO
  • Board index ‹ Version 1.x branch ‹ Contributions (v1.x)
  • Change font size
  • FAQ
  • Register
  • Login

Postgresql Container

Post a bug fix, a new feature, a theme ...

Moderators: OldWolf, re*s.t.a.r.s.*2

Post a reply
5 posts • Page 1 of 1

Postby dhalsim » Wed Dec 20, 2006 12:14 pm

Hi !

I've written a postgresql container from the mysql one given by HenkBB. I've only modified the way the container is connecting the server and sending query. All queries are the same.

Code: Select all
<?php
/**
 * postgres.class.php
 *
 * Copyright © 2006 Erwan Richard <erwan.richard@laposte.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 
 * This class is a modification of the mysql container written by stephane gully
 
 * MYSQL DATABASE INFO
 * Because of the new storage functions (setMeta, getMeta, rmMeta)
 * everything can be stored in just one single table.
 * Using type "HEAP" or "MEMORY" mysql loads this table into memory making it very fast
 * There is no routine to create the table if it does not exists so you have to create it by hand
 * Replace the database login info at the top of pfcContainer_mysql class with your own
 * You also need a container line in your chat index file:
 * $params["container_type"] = "mysql";
 
 
 
 */

require_once dirname(__FILE__)."/../pfccontainer.class.php";

/**
 * pfcContainer_mysql is a concret container which stock data into mysql
 *
 * @author Stephane Gully <stephane.gully@gmail.com>
 */
class pfcContainer_postgres extends pfcContainer{

  var $dbServer = 'localhost';
  var $dbPort = 5432;
  var $dbName = '';
  var $dbUser = '';
  var $dbPass = '';
  var $dbTable = 'phpfreechat';
  var $nConnID;
  var $bCheck;
 
  function OpenDatabase(){
        $sConnString = "host=" . $this->dbServer . " port=" . $this->dbPort . " dbname=" . $this->dbName . " user=" . $this->dbUser;   
      //$nConnID = pg_Connect("localhost", "5432", "", "",$this->dbName)
       $nConnID = pg_Connect($sConnString);

    //$bCheck = mysql_select_db($this->dbName)
     //   or die ("No database selected.");

    return $nConnID;
  }
 
  function todb($sql){
    global $nConnID;
    $result = pg_query($sql);
   
    if (!$result) {
        echo "Psql error ...";
       
            echo pg_last_error() ."$sql";
     
        exit();
    };
    return $result;
  }
 
  var $_users = array("nickid"    => array(),
                      "timestamp" => array());
  var $_meta = array();
 
  function pfcContainer_File(&$config)
  {
    pfcContainer::pfcContainer($config);
  }

  function getDefaultConfig()
  {
    $c =& $this->c;
   
    $cfg = pfcContainer::getDefaultConfig();
    return $cfg;
  }
 

  function setMeta($group, $subgroup, $leaf, $leafvalue = NULL)
  {
    $c =& $this->c;

    if ($c->debug)
      file_put_contents("tmp/debug.txt", "nsetMeta(".$group.",".$subgroup.",".$leaf.",".$leafvalue.")", FILE_APPEND);
   
    $server = $c->serverid;   
    $nConnID=$this->OpenDatabase();

    if ($leafvalue == NULL){$leafvalue="";};
   
    $sql_select="SELECT * FROM ".$this->dbTable." WHERE "server"='$server' AND "group"='$group' AND "subgroup"='$subgroup' AND "leaf"='$leaf'";
    $sql_insert="INSERT INTO `.$this->dbTable.` ("server", "group", "subgroup", "leaf", "leafvalue", "timestamp") VALUES('$server', '$group', '$subgroup', '$leaf', '".addslashes($leafvalue)."', '".time()."')";
    $sql_update="UPDATE ".$this->dbTable." SET "leafvalue"='".addslashes($leafvalue)."', "timestamp"='".time()."' WHERE  "server"='$server' AND "group"='$group' AND "subgroup"='$subgroup' AND "leaf"='$leaf'";
   
    $thisresult = $this->todb($sql_select);
   
    if(!pg_num_rows($thisresult)){
        if ($c->debug)
              file_put_contents("tmp/debug.txt", "nsetSQL(".$sql_insert.")", FILE_APPEND);
       
        $this->todb($sql_insert);
        return 0; // value created
    }else{
        if($sql_update!=""){
            if ($c->debug)
              file_put_contents("tmp/debug.txt", "nsetSQL(".$sql_update.")", FILE_APPEND);
           
            $this->todb($sql_update);
        }
        return 1; // value overwritten
    } 
  }

 
  function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false)
  {
    $c =& $this->c;
    if ($c->debug)
      file_put_contents("tmp/debug.txt", "ngetMeta(".$group.",".$subgroup.",".$leaf.",".$withleafvalue.")", FILE_APPEND);
   
    $ret = array();
    $ret["timestamp"] = array();
    $ret["value"]     = array();
   
    $server = $c->serverid;   
    $nConnID=$this->OpenDatabase();
   
    $sql_where="";
    $value="leafvalue";
   
    if ($group != NULL){
        $sql_where.=" AND "group"='$group'";
        $value="subgroup";       
    }   
   
    if ($subgroup != NULL){
        $sql_where.=" AND "subgroup"='$subgroup'";
        $value="leaf";       
    }
   
    if ($leaf != NULL){
        $sql_where.=" AND "leaf"='$leaf'";
        $value="leafvalue";   
    };
   
    $sql_select="SELECT "".$value."", "timestamp" FROM ".$this->dbTable." WHERE "server"='$server'".$sql_where." ORDER BY timestamp";
   
    if ($c->debug)
      file_put_contents("tmp/debug.txt", "ngetSQL(".$sql_select.")", FILE_APPEND);
   
    if($sql_select!=""){
        $thisresult = $this->todb($sql_select);
        if(pg_num_rows($thisresult)){       
            while ($regel = pg_fetch_array($thisresult)) {
                 $ret["timestamp"][] = $regel["timestamp"];
                if($value=="leafvalue"){
                    if($withleafvalue){
                          $ret["value"][]     = $regel[$value];
                    }else{
                        $ret["value"][]     = NULL;
                    };
                }else{
                    $ret["value"][] = $regel[$value];
                };       
            };//end while   
        }else{
            return $ret;
        }
    }   
    return $ret;
  }

  function rmMeta($group, $subgroup = null, $leaf = null)
  {
    $c =& $this->c;
    if ($c->debug)
      file_put_contents("tmp/debug.txt", "nrmMeta(".$group.",".$subgroup.",".$leaf.")", FILE_APPEND);
   
    $server = $c->serverid;   
    $nConnID=$this->OpenDatabase();
   
    $sql_delete="DELETE FROM ".$this->dbTable." WHERE "server"='$server'";
   
    if($group != NULL){
        $sql_delete.=" AND "group"='$group'";
    }   
   
    if($subgroup != NULL){
        $sql_delete.=" AND "subgroup"='$subgroup'";
    }

    if ($leaf != NULL) {
        $sql_delete.=" AND "leaf"='$leaf'";
    }
   
    if ($c->debug)
      file_put_contents("tmp/debug.txt", "nrmSQL(".$sql_delete.")", FILE_APPEND);
   
    $this->todb($sql_delete);
    return true;
  }


  /**
   * Used to encode UTF8 strings to ASCII filenames
   */ 
  function encode($str)
  {
    return urlencode($str);
  }
 
  /**
   * Used to decode ASCII filenames to UTF8 strings
   */ 
  function decode($str)
  {
    return urldecode($str);
  }

}

?>

I've also written an /history x command which open a new History tab and show messages sent since x days. But i'll send the code later since i need to clean and optimize the code.

Bye
dhalsim
New member
 
Posts: 4
Joined: Wed Dec 20, 2006 11:50 am
Top

Postby phpfreechat » Wed Dec 20, 2006 1:26 pm

Thank you for your work, but I have a few comments about it :
1. I cleaned a lot the HenkBB original code so it would be better to base your container on the cleaned code that you can found in the official 1.0-beta8 release (in src/containers/mysql.class.php)
2. It's of course possible to create a postgres container but as the code between mysql and postgres is almost the same, it will be difficult to maintain the code in the future. A more intelligent way is to factorize the code : for example using the PEAR::DB (or MDB2) package.
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby dhalsim » Wed Dec 20, 2006 2:40 pm

kerphi wrote:Thank you for your work, but I have a few comments about it :
2. It's of course possible to create a postgres container but as the code between mysql and postgres is almost the same, it will be difficult to maintain the code in the future. A more intelligent way is to factorize the code : for example using the PEAR::DB (or MDB2) package.

+1

I'll write a DB.class.php with MDB2 which provide support for many SGBDR. SQL queries are simples enough to permit it.
Where do I have to put PEAR librairies ?
Last edited by dhalsim on Wed Dec 20, 2006 2:44 pm, edited 1 time in total.
dhalsim
New member
 
Posts: 4
Joined: Wed Dec 20, 2006 11:50 am
Top

Postby phpfreechat » Wed Dec 20, 2006 3:27 pm

in lib/pear/
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby mikez » Wed Dec 20, 2006 6:30 pm

One way to gain some performance is to try to either compile your PHP installation with the required extensions, or configure them to load at startup. Loading extensions dynamically is not efficient, but of course, fine if there are no alternatives. Check with your web host provider to see if they will allow you to edit your php.ini file.

The only reason I'm posting this general suggestion is because anything to help make the chat even a little more efficient would be useful. :-)
Last edited by mikez on Wed Dec 20, 2006 6:37 pm, edited 1 time in total.
mikez
Member
 
Posts: 29
Joined: Wed Dec 13, 2006 1:24 am
Top


Post a reply
5 posts • Page 1 of 1

Return to Contributions (v1.x)

Who is online

Users browsing this forum: No registered users and 8 guests

  • Board index
  • The team • Delete all board cookies • All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
cron
Sign in
Wrong credentials
Sign up I forgot my password
.
jeu-gratuit.net | more partners
Fork me on GitHub