Combu Server  3.1.1
PHP API Documentation
DatabasePDO.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
5 use PDO;
6 use PDOException;
7 
13 class DatabasePDO extends Database {
14 
15  private $_errorCode = NULL;
16  private $_errorMessage = NULL;
17 
18  protected function _setConnection() {
19  try {
20  $dbCharset = "utf8";
21  if (defined("GAME_DB_CHARSET") && GAME_DB_CHARSET) {
22  $dbCharset = GAME_DB_CHARSET;
23  }
24  $dbOptions = NULL;
25  if (defined("GAME_DB_OPTIONS") && GAME_DB_OPTIONS) {
26  $dbOptions = GAME_DB_OPTIONS;
27  }
28  $dsn = sprintf("%s:host=%s;port=%d;dbname=%s;charset=%s", $this->_dbType, $this->_dbServer, $this->_dbPort, $this->_dbName, $dbCharset);
29  $this->_connection = new PDO($dsn, $this->_dbUser, $this->_dbPass, $dbOptions);
30  } catch (PDOException $e) {
31  $this->_connection = NULL;
32  $this->_errorCode = $e->getCode();
33  $this->_errorMessage = $e->getMessage();
34  }
35  }
36 
37  protected function _selectDatabase() {
38  }
39 
40  public function CloseConnection() {
41  $this->_connection = NULL;
42  }
43 
44  public function TestConnection() {
45  if ($this->_connection) {
46  return TRUE;
47  }
48  return FALSE;
49  }
50 
51  public function GetError() {
52  return $this->_errorMessage;
53  }
54 
55  public function GetErrorNo() {
56  return $this->_errorCode;
57  }
58 
59  public function InsertedId($name = NULL) {
60  if ($this->_connection) {
61  return $this->_connection->lastInsertId($name);
62  }
63  }
64 
65  public function AffectedRows() {
66  if ($this->_connection) {
67  return $this->_connection->rowCount();
68  }
69  return 0;
70  }
71 
72  private function _Fetch($result, $result_type) {
73  if ($result) {
74  return $result->fetch($result_type);
75  }
76  return NULL;
77  }
78 
79  public function FetchAssoc($result) {
80  if ($this->_connection) {
81  return $this->_Fetch($result, PDO::FETCH_ASSOC);
82  }
83  return NULL;
84  }
85 
86  public function FetchNum($result) {
87  if ($this->_connection) {
88  return $this->_Fetch($result, PDO::FETCH_NUM);
89  }
90  return NULL;
91  }
92 
93  public function Query($query) {
94  if ($this->_connection && $query) {
95  try {
96  if (Utils::StartsWith($query, "SELECT", TRUE)) {
97  $stmt = $this->_connection->prepare($query);
98  if ($stmt->execute()) {
99  return $stmt;
100  }
101  return FALSE;
102  }
103  if ($this->_connection->query($query)) {
104  return TRUE;
105  }
106  } catch (PDOException $e) {
107  $this->_errorNumber = $e->getCode();
108  $this->_errorMessage = $e->getMessage();
109  }
110  }
111  return FALSE;
112  }
113 
114  public function Escape($text) {
115  if ($this->_connection) {
116  $quoted = $this->_connection->quote($text);
117  return substr($quoted, 1, strlen($quoted) - 2);
118  }
119  return FALSE;
120  }
121 
122  public function TransactionStart() {
123  if ($this->_connection) {
124  return $this->_connection->beginTransaction();
125  }
126  return FALSE;
127  }
128 
129  public function TransactionCommit() {
130  if ($this->_connection) {
131  return $this->_connection->commit();
132  }
133  return FALSE;
134  }
135 
136  public function TransactionRollback() {
137  if ($this->_connection) {
138  return $this->_connection->rollBack();
139  }
140  return FALSE;
141  }
142 }
FetchAssoc($result)
Definition: DatabasePDO.php:79
InsertedId($name=NULL)
Definition: DatabasePDO.php:59
Definition: Account.php:3