Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQLi update #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions ManiaLive/Database/MYSQLI/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
/**
* ManiaLive - TrackMania dedicated server manager in PHP
*
* @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
* @version $Revision$:
* @author $Author$:
* @date $Date$:
*/

namespace ManiaLive\Database\MySQLi;

use ManiaLive\Database\SelectionException;
use ManiaLive\Database\NotSupportedException;
use ManiaLive\Database\QueryException;
use ManiaLive\Database\DisconnectionException;
use ManiaLive\Database\NotConnectedException;
use ManiaLive\Database\Exception;
use ManiaLive\Database\ConnectionException;
use ManiaLive\Event\Dispatcher;
use ManiaLive\Features\Tick\Event as TickEvent;
use ManiaLive\Features\Tick\Listener as TickListener;

class Connection extends \ManiaLive\Database\Connection implements TickListener
{
protected $connection;
protected $host;
protected $user;
protected $password;
protected $database;

protected $tick = 0;

function __construct($host, $username, $password, $database, $port)
{
// Init
$this->host = $host.($port ? ':'.$port : '');
$this->user = $username;
$this->password = $password;
$this->connect($database);
}

function onTick()
{
if(++$this->tick % 3600 == 0)
{
$this->execute('SELECT 1');
$this->tick = 0;
}
}

protected function connect($database)
{
// Connection
try
{
$this->connection = mysqli_connect(
$this->host,
$this->user,
$this->password
);
}
catch(\ErrorException $err)
{
throw new ConnectionException($err->getMessage(), $err->getCode());
}

// Success ?
if(!$this->connection)
{
throw new ConnectionException;
}

$this->select($database);

// Default Charset : UTF8
$this->setCharset('utf8');
Dispatcher::register(TickEvent::getClass(), $this);
}

/**
* @see ManiaLive\Database.Connection::getHandle()
* @return resource
*/
function getHandle()
{
return $this->connection;
}

function setCharset($charset)
{
if(function_exists('mysqli_set_charset'))
{
if(!$this->isConnected())
{
$this->connect($this->database);
}

if(!mysqli_set_charset($this->connection, $charset))
{
throw new Exception;
}
}
else
{
$charset = $this->quote($charset);
$this->execute('SET NAMES '.$charset);
}
}

function select($database)
{
$this->database = $database;
if(!mysqli_select_db($this->connection, $this->database))
{
throw new SelectionException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
}

function quote($string)
{
if(!$this->isConnected())
{
$this->connect($this->database);
}
return '\''.mysqli_real_escape_string($this->connection, $string).'\'';
}

function execute($query)
{
if(!$this->isConnected())
{
$this->connect($this->database);
}

Connection::startMeasuring($this);
if(func_num_args() > 1)
{
$query = call_user_func_array('sprintf', func_get_args());
}
$result = mysqli_query($this->connection, $query);
Connection::endMeasuring($this);

if(!$result)
{
throw new QueryException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
return new RecordSet($result);
}

function affectedRows()
{
if(!$this->isConnected())
{
$this->connect($this->database);
}
return mysqli_affected_rows($this->connection);
}

function insertID()
{
if(!$this->isConnected())
{
$this->connect($this->database);
}
return mysqli_insert_id($this->connection);
}

function isConnected()
{
return (bool)$this->connection;
}

function disconnect()
{
if(!mysqli_close($this->connection))
{
throw new DisconnectionException;
}
$this->connection = null;
Dispatcher::unregister(TickEvent::getClass(), $this);
}

function getDatabase()
{
return $this->database;
}

function tableExists($tableName)
{
$table = $this->execute('SHOW TABLES LIKE '.$this->quote($tableName));
return ($table->recordCount() > 0);
}
}
?>
80 changes: 80 additions & 0 deletions ManiaLive/Database/MYSQLI/RecordSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* ManiaLive - TrackMania dedicated server manager in PHP
*
* @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
* @version $Revision$:
* @author $Author$:
* @date $Date$:
*/

namespace ManiaLive\Database\MySQLi;

class RecordSet extends \ManiaLive\Database\RecordSet
{
const FETCH_ASSOC = MYSQLI_ASSOC;
const FETCH_NUM = MYSQLI_NUM;
const FETCH_BOTH = MYSQLI_BOTH;

protected $result;

function __construct($result)
{
$this->result = $result;
}

function fetchRow()
{
return mysqli_fetch_row($this->result);
}

function fetchAssoc()
{
return mysqli_fetch_assoc($this->result);
}

function fetchArray($resultType = self::FETCH_ASSOC)
{
return mysqli_fetch_array($this->result, $resultType);
}

/**
* @deprecated
*/
function fetchStdObject()
{
return $this->fetchObject(null);
}

function fetchObject($className='\\stdClass', array $params=array())
{
if($className)
{
if($params)
{
return mysqli_fetch_object($this->result, $className, $params);
}
else
{
return mysqli_fetch_object($this->result, $className);
}
}
else
{
return mysqli_fetch_object($this->result);
}
}

function recordCount()
{
return mysqli_num_rows($this->result);
}

function recordAvailable()
{
return mysqli_num_rows($this->result) > 0;
}
}

?>