<?php
/**
* The implementation of MySQL Database.
* MyISAM and ISAM dont support transaction and foreign key, they shouldn't be used.
*
* @package com.freeorm
* @author Yide Zou
* @link http://www.freeorm.com
* @copyright Copyright (c) 2010 Yide Zou <hide@address.com>.
* All Rights Reserved.
* @license This software is released under the terms of the GNU Lesser General Public License
* A copy of which is available from http://www.gnu.org/copyleft/lesser.html
*/
require_once ('Database.php');
class DBMySQL extends Database
{
// php database object
private $db;
public $insert_id = -1;
public function __construct($host, $port, $username, $password, $dbname, Logger $logger)
{
parent::__construct($logger);
$this->db = new mysqli($host, $username, $password, $dbname, $port);
if ( mysqli_connect_errno() )
{
$this->logger->err("Connect failed: ".mysqli_connect_error());
die("Connect failed!");
}
$this->db->autocommit(false);
}
public function query($sql)
{
$this->logger->debug("Executing SQL: $sql");
$result = $this->db->query($sql);
if ($result === false)
{
$this->logger->err($this->db->error."\n SQL: $sql");
die("DBError!");
}
else
$this->insert_id = $this->db->insert_id;
return $result;
}
public function loadObject($sql)
{
$this->logger->debug("Executing SQL: $sql");
$result = $this->db->query($sql);
if ($result === false)
{
$this->logger->err($this->db->error."\n SQL: $sql");
die("DBError!");
}
$object = $result->fetch_object();
$result->close();
return $object;
}
public function loadObjectList($sql)
{
$this->logger->debug("Executing SQL: $sql");
$result = $this->db->query($sql);
if ($result === false)
{
$this->logger->err($this->db->error."\n SQL: $sql");
die("DBError!");
}
$array = array();
while ($row = $result->fetch_object())
{
$array[] = $row;
}
$result->close();
return $array;
}
public function commit()
{
return $this->db->commit();
}
public function close()
{
$this->db->close();
}
public function getInsertId()
{
return $this->insert_id;
}
}