Classes

File io/db/driver/mysql/MySQLDBConnection.class.php

File io/db/driver/mysql/MySQLDBConnection.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 
<?php
/**
 * @package     IO
 * @subpackage  DB\driver\mysql
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 09 09
 * @since       PHPBoost 3.0 - 2009 10 01
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class MySQLDBConnection implements DBConnection
{
    /**
     * @var MysqlResource
     */
    private $link = null;

    public function __destruct()
    {
        $this->disconnect();
    }

    public function connect(array $db_connection_data)
    {
        if (!extension_loaded('mysqli'))
        {
            throw new DBConnectionException('Unable to load mysqli extension');
        }

        $mysqli_link = @mysqli_connect(
            $db_connection_data['host'],
            $db_connection_data['login'],
            $db_connection_data['password'],
            "",
            $db_connection_data['port']
        );

        if ($mysqli_link)
        {
            $this->link = $mysqli_link;
            $this->select_database($db_connection_data['database']);
            $this->execute("SET NAMES UTF8");
        }
        else
        {
            throw new MySQLDBConnectionException('can\'t connect to database!');
        }
    }

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

    public function disconnect()
    {
        if ($this->link !== null)
        {
            if (!@mysqli_close($this->link))
            {
                throw new MySQLDBConnectionException('can\'t close database connection');
            }
            else
            {
                $this->link = null;
            }
        }
    }

    public function start_transaction()
    {
        $this->execute("START TRANSACTION;");
    }

    public function commit()
    {
        $this->execute("COMMIT;");
    }

    public function rollback()
    {
        $this->execute("ROLLBACK;");
    }

    private function execute($command)
    {
        $resource = mysqli_query($this->link, $command);
        if ($resource === false)
        {
            throw new MySQLQuerierException('invalid mysql command', $command);
        }
    }

    private function select_database($database)
    {
        if (!@mysqli_select_db($this->link, $database))
        {
            throw new MySQLUnexistingDatabaseException();
        }
    }
}
?>