Location: PHPKode > projects > MJGUEST > mjguest/database/pgsql/driver.php
<?php
	
	/*
	+----------------------------------------------------------------
	|
	|	MJGUEST
	|	=============================================================
	|	Copyright (c) 2002-2008 Giacomo "mdsjack" Menni
	|	Terms of agreement and support at www.mdsjack.bo.it
	|
	+----------------------------------------------------------------
	|	[ DATABASE MODULE ]
	|	Database driver (PostgreSQL 8)
	+----------------------------------------------------------------
	*/
	
	class db_driver
	{
		var $__id;
		var $__query;
		var $lastquestion;
		
		var $driver			= 'PgSQL'; # const
		var $answer			= '';
		var $tot_queries	= 0;
		var $error			= false;
		
		var $tables			= array();
		
		var $questions		= array
		(	'truncate'			=> 'TRUNCATE %s'
		,	'optimize'			=> 'VACUUM %s'
		,	'row_delete'		=> 'DELETE FROM %s WHERE %s=\'%s\''
		,	'row_count'			=> 'SELECT COUNT(*) FROM %s'
		,	'row_add'			=> 'INSERT INTO %s DEFAULT VALUES'
		,	'settings_load'		=> 'SELECT * FROM [TBL=settings]'
		,	'settings_update'	=> 'UPDATE [TBL=settings] SET adminpass=\'%s\', adminmail=\'%s\', timezone=%d, dateformat=\'%s\', theme=\'%s\', entriesxpage=\'%u\', wordfilter=B\'%b\', title=\'%s\', namelen=\'%u\', msglen=\'%u\', emoticons=\'%u\', avatars_maxkilobytes=\'%u\', redirectscreen=B\'%b\', notifynewentry=B\'%b\', captcha=B\'%b\', messenger=B\'%b\', entry_approval=B\'%b\', mod_account=B\'%b\', modpass=\'%s\', modmail=\'%s\', rating=B\'%b\', banned=\'%s\', name_admin=\'%s\', name_mod=\'%s\' '
		,	'settings_restore'	=> 'UPDATE [TBL=settings] SET adminpass=DEFAULT, adminmail=DEFAULT, timezone=DEFAULT, dateformat=DEFAULT, theme=DEFAULT, entriesxpage=DEFAULT, wordfilter=DEFAULT, title=DEFAULT, namelen=DEFAULT, msglen=DEFAULT, emoticons=DEFAULT, avatars_maxkilobytes=DEFAULT, redirectscreen=DEFAULT, notifynewentry=DEFAULT, captcha=DEFAULT, messenger=DEFAULT, entry_approval=DEFAULT, mod_account=DEFAULT, modpass=DEFAULT, modmail=DEFAULT, rating=DEFAULT, banned=DEFAULT, name_admin=DEFAULT, name_mod=DEFAULT '
		,	'entry_add'			=> 'INSERT INTO [TBL=entries] VALUES(DEFAULT, \'%s\', B\'%b\', \'%s\',\'%s\', %u, \'%s\', \'%s\', \'%s\', \'%s\',\'%s\',\'%s\',\'%s\', \'\', B\'%b\', \'%u\')'
		,	'entry_get_set'		=> 'SELECT id, ip, pvt, name, country, datetime, message, dir, email, web, chat, uin, CHAR_LENGTH(avatar) AS avatar, reply, approved, rating FROM [TBL=entries] %§ ORDER BY id DESC OFFSET %u LIMIT %u'
		,	'entry_get_one'		=> 'SELECT pvt, name, country, message, dir, email, web, chat, uin, CHAR_LENGTH(avatar) AS avatar, reply, rating FROM [TBL=entries] WHERE id=%u'
		,	'entry_edit'		=> 'UPDATE [TBL=entries] SET pvt=B\'%b\', name=\'%s\', country=\'%s\', message=\'%s\', dir=\'%s\', email=\'%s\', web=\'%s\', chat=\'%s\', uin=\'%s\', rating=\'%u\' %§ WHERE id=%u'
		,	'get_avatar'		=> 'SELECT avatar FROM [TBL=entries] WHERE id=%u'
		,	'check_flood'		=> 'SELECT MAX(datetime) FROM [TBL=entries] WHERE ip=\'%s\''
		,	'reply_save'		=> 'UPDATE [TBL=entries] SET reply=\'%s\' WHERE id=%u'
		,	'approve'			=> 'UPDATE [TBL=entries] SET approved=B\'1\' WHERE id=%u'
		,	'ban_guest'			=> 'UPDATE [TBL=settings] SET banned=\'%s\''
		,	'rating_stats'		=> 'SELECT AVG(rating) AS avg, COUNT(rating) AS tot FROM [TBL=entries] WHERE rating != 0'
		);
		
		function db_driver()
		{
			$this->tables = array
			(	'[TBL=entries]'		=> db_flag.'entries'
			,	'[TBL=settings]'	=> db_flag.'settings'
			);
			
			$this->open();
		}
		
		function ask($question, $sql = null, $params = array())
		{
			$this->lastquestion = $question;
			
			$this->query(strtr(vsprintf(str_replace('%§', $sql, $this->questions[$this->lastquestion]), $params), $this->tables));
			
			return true;
		}
		
		function get_field($num = 0)
		{
			$this->answer = @pg_fetch_row($this->__query);
			
			return ($this->answer ? $this->answer[$num] : $this->__error());
		}
		
		function get_row()
		{
			$this->answer = @pg_fetch_array($this->__query, null, PGSQL_ASSOC);
			
			return ($this->answer ? true : $this->__error());
		}
		
		function open($persistance = 1)
		{
			$string = sprintf('host=%s port=%s dbname=%s user=%s password=%s', (!db_host ? 'localhost' : db_host), (!db_port ? '5432' : db_port), db_name, db_user, db_pass);
			
			switch ($persistance):
				
				case 0:
					$this->__id = @pg_connect($string);
				break;
				
				case 1:
					$this->__id = @pg_pconnect($string);
				break;
				
			endswitch;
			
			unset($string);
			
			return ($this->__id ? true : $this->__error());
		}
		
		function __error()
		{
			if($this->error == true) return false;
			
			$errno = @pg_last_error($this->__id);
			
			if ($errno or !$this->__id):
				
				echo "
				<h3>Database Error!</h3>
				<strong>Module:</strong> ".db_type."<br />
				<strong>Error (".$this->lastquestion."):</strong> ".$errno;
				
				$this->error = true;
				return false;
			endif;
			
			return true;
		}
		
		function close()
		{
			@pg_close($this->__id);
			
			unset($this->__id);
		}
		
		function query($sql)
		{
			$this->__query = @pg_query($this->__id, $sql);
			
			$this->tot_queries++;
			
			return ($this->__query != 0 ? true : $this->__error() );
		}
		
		function escape($raw_data)
		{
			return pg_escape_string(stripslashes($raw_data));
		}
		
		function sql($sqlfile)
		{
			$this->query('BEGIN');
			
			$this->query(str_replace(array('mjguest_', "\r\n"), array(db_flag, ' '), file_get_contents($sqlfile)));
			
			if ($this->error) $this->query('ROLLBACK');
			else $this->query('COMMIT');
		}
		
		function free_memory()
		{
			@pg_free_result($this->__query);
			
			unset($this->__query);
		}
	}
?>
Return current item: MJGUEST