| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
final class Database { |
|---|
| 4 |
|
|---|
| 5 |
protected static $_instances = array(); |
|---|
| 6 |
protected $_db = null; |
|---|
| 7 |
public $nameQuote = '`'; |
|---|
| 8 |
|
|---|
| 9 |
private function __construct($user, $pass, $type, $host, $name) { |
|---|
| 10 |
|
|---|
| 11 |
try { |
|---|
| 12 |
$this->_db = new PDO($type . ':host=' . $host . ';dbname=' . $name, $user, $pass); |
|---|
| 13 |
} catch (PDOException $e) { |
|---|
| 14 |
echo 'Connection failed: ' . $e->getMessage(); |
|---|
| 15 |
} |
|---|
| 16 |
$this->_db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
public static function getInstance($connection = 'default') { |
|---|
| 20 |
if (!isset(self::$_instances[$connection])) |
|---|
| 21 |
throw new Exception('Database error: Not connected to ' . $connection); |
|---|
| 22 |
|
|---|
| 23 |
return self::$_instances[$connection]; |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
public static function connect($user, $pass, $type, $host, $name, $connection = 'default') { |
|---|
| 27 |
if (isset(self::$_instances[$connection])) |
|---|
| 28 |
$_instances[$connection]->close(); |
|---|
| 29 |
|
|---|
| 30 |
return self::$_instances[$connection] = new Database($user, $pass, $type, $host, $name); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
public function ErrorMsg() { |
|---|
| 34 |
return implode(' - ', $this->_db->errorInfo()); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
public function errorInfo() { |
|---|
| 38 |
return $this->_db->errorInfo(); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
public function Execute($sql, $param = null) { |
|---|
| 42 |
if ($sql instanceof PDOStatement) { |
|---|
| 43 |
if (is_array($param)) |
|---|
| 44 |
$ret = $sql->execute($param); |
|---|
| 45 |
elseif (is_string($param) || is_int($param)) |
|---|
| 46 |
$ret = $sql->execute(array($param)); |
|---|
| 47 |
else |
|---|
| 48 |
$ret = $sql->execute(); |
|---|
| 49 |
|
|---|
| 50 |
if ($ret) return new DatabaseResult($sql); |
|---|
| 51 |
} else { |
|---|
| 52 |
$ret = $this->_db->query($sql); |
|---|
| 53 |
if ($ret instanceof PDOStatement) return new DatabaseResult($ret); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
return $ret; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
public function Prepare($sql) { |
|---|
| 60 |
if (version_compare(PHP_VERSION, '5.2.5', '<')) { |
|---|
| 61 |
if (preg_match("/(ALTER |CREATE |DROP |GRANT |REVOKE |FLUSH )/i", $sql, $matches) > 0) { |
|---|
| 62 |
$this->_db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, true); |
|---|
| 63 |
} else { |
|---|
| 64 |
$this->_db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, false); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
return $this->_db->prepare($sql); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
public function MetaTables() { |
|---|
| 71 |
$tables = array(); |
|---|
| 72 |
|
|---|
| 73 |
$result = $this->_db->query('SHOW TABLES'); |
|---|
| 74 |
while ($result instanceof PDOStatement && $row = $result->fetch(PDO::FETCH_NUM)) |
|---|
| 75 |
$tables[] = $row[0]; |
|---|
| 76 |
|
|---|
| 77 |
return $tables; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
public function Insert_ID() { |
|---|
| 81 |
return $this->_db->lastInsertId(); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
public function getAttribute($attribute) { |
|---|
| 85 |
return $this->_db->getAttribute($attribute); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
public function StartTrans() { |
|---|
| 89 |
$this->_db->beginTransaction(); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
public function CompleteTrans() { |
|---|
| 93 |
$this->_db->commit(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
public function HasFailedTrans() { |
|---|
| 97 |
return false; |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
final class DatabaseResult { |
|---|
| 102 |
|
|---|
| 103 |
protected $_result = null; |
|---|
| 104 |
protected $_fields = null; |
|---|
| 105 |
|
|---|
| 106 |
public function __construct($result) { |
|---|
| 107 |
if (!$result instanceof PDOStatement) return false; |
|---|
| 108 |
|
|---|
| 109 |
$this->_result = $result; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
public function __get($param) { |
|---|
| 113 |
if ($param == 'fields') { |
|---|
| 114 |
if ($this->_fields === null) { |
|---|
| 115 |
$this->_fields = $this->_result->fetch(); |
|---|
| 116 |
} |
|---|
| 117 |
return $this->_fields; |
|---|
| 118 |
} |
|---|
| 119 |
if ($param == 'EOF') { |
|---|
| 120 |
if ($this->_result->rowCount() == 0) { |
|---|
| 121 |
return true; |
|---|
| 122 |
} |
|---|
| 123 |
return !is_null($this->_fields) && !is_array($this->_fields); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
throw new Exception('Unknown parameter: ' . $param); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
public function fields($param) { |
|---|
| 130 |
return $this->fields[$param]; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
public function RowCount() |
|---|
| 134 |
{ |
|---|
| 135 |
return $this->_result->rowCount(); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
public function RecordCount() { |
|---|
| 139 |
return $this->_result->rowCount(); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
public function FetchRow() { |
|---|
| 143 |
return $this->_result->fetch(); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
public function MoveNext() { |
|---|
| 147 |
$this->_fields = $this->_result->fetch(); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
?> |
|---|