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