Combu Server  3.1.1
PHP API Documentation
DataClass.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
8 abstract class DataClass {
9 
10  public static function GetTableName($class) {
11  $name = $class::TABLE_NAME;
12  if (defined("GAME_DB_PREFIX") && GAME_DB_PREFIX) {
13  $name = GAME_DB_PREFIX . $name;
14  }
15  $quote = "`";
16  return $quote . trim(str_replace($quote, "", $name)) . $quote;
17  }
18 
26  protected function _loadFilter($tableName, $filter) {
27  global $Database;
28  $res = $Database->Query("SELECT * FROM $tableName WHERE $filter");
29  if ($res) {
30  $row = $Database->FetchAssoc($res);
31  if ($row) {
32  $this->_loadByRow($row);
33  }
34  }
35  }
36 
42  protected function _loadByRow($row, $stripSlashes = false, $callbackOnExists = false) {
43  Utils::FillObjectFromRow($this, $row, $stripSlashes, $callbackOnExists);
44  }
45 
52  protected static function _count($tableName, $filter = null) {
53  global $Database;
54  $query = "SELECT COUNT(*) FROM (SELECT * FROM " . $tableName;
55  if ($filter != null) {
56  $query .= " WHERE " . $filter;
57  }
58  $query .= ") T";
59  $res = $Database->Query($query);
60  if ($res) {
61  $row = $Database->FetchNum($res);
62  if ($row) {
63  return intval($row[0]);
64  }
65  }
66  return 0;
67  }
68 
80  protected static function _load($tableName, $returnedClass = "", $where = "", $order = null, $limit = null, $offset = null, &$count = null) {
81  return self::_loadEx(null, $tableName, $returnedClass, $where, $order, $limit, $offset, $count);
82  }
83 
84  protected static function _loadEx($select, $from, $returnedClass = "", $where = "", $order = null, $limit = null, $offset = null, &$count = null, $debug = false) {
85  if ($count !== null) {
86  // Count the total results
87  $count = self::_count($from, $where);
88  }
89 
90  $query = "SELECT " . ($select ? $select : "*") . " FROM $from";
91 
92  if ($where != "") {
93  $query .= " WHERE " . $where;
94  }
95 
96  if ($order != null) {
97  $query .= " ORDER BY $order";
98  }
99 
100  if ($limit != null) {
101  $query .= " LIMIT " . ($offset > 0 ? "$offset," : "") . $limit;
102  }
103 
104  if ($debug === true) {
105  AppLog::Info($query);
106  }
107 
108  return self::_loadQuery($query, $returnedClass);
109  }
110 
111  protected static function _loadQuery($query, $returnedClass = "") {
112  global $Database;
113  $rows = array();
114  $res = $Database->Query($query);
115  if ($res) {
116  while ($row = $Database->FetchAssoc($res)) {
117  $rows[] = ($returnedClass == "" ? $row : new $returnedClass($row));
118  }
119  }
120  return $rows;
121  }
122 
129  public static function LoadRecords($query, $returnedClass = "") {
130  return self::_loadQuery($query, $returnedClass);
131  }
132 
139  public static function CountRecords($tableName, $filter = null) {
140  return self::_count($tableName, $filter);
141  }
142 
147  public function Save() {
148  return FALSE;
149  }
150 
155  public function Delete() {
156  return FALSE;
157  }
158 
166  protected function _Delete($tableName, $filter = null) {
167  global $Database;
168  if ($filter) {
169  if ($Database->Query("DELETE FROM $tableName WHERE $filter")) {
170  return TRUE;
171  }
172  }
173  return FALSE;
174  }
175 
180  public function ToJson () {
181  return json_encode($this->ToArray());
182  }
183 
184  public function ToArray() {
185  return Utils::ObjectToArray($this);
186  }
187 
192  public static function TruncateTable ($tableName) {
193  global $Database;
194  $Database->Query("TRUNCATE " . $tableName);
195  }
196 
200  public static function TruncateClass($class) {
201  return self::TruncateTable(self::GetTableName($class));
202  }
203 }
static TruncateClass($class)
Definition: DataClass.php:200
_loadByRow($row, $stripSlashes=false, $callbackOnExists=false)
Definition: DataClass.php:42
static GetTableName($class)
Definition: DataClass.php:10
_loadFilter($tableName, $filter)
Definition: DataClass.php:26
Definition: Account.php:3
_Delete($tableName, $filter=null)
Definition: DataClass.php:166
static _load($tableName, $returnedClass="", $where="", $order=null, $limit=null, $offset=null, &$count=null)
Definition: DataClass.php:80
static CountRecords($tableName, $filter=null)
Definition: DataClass.php:139
static TruncateTable($tableName)
Definition: DataClass.php:192
static _loadEx($select, $from, $returnedClass="", $where="", $order=null, $limit=null, $offset=null, &$count=null, $debug=false)
Definition: DataClass.php:84
static LoadRecords($query, $returnedClass="")
Definition: DataClass.php:129
static _count($tableName, $filter=null)
Definition: DataClass.php:52
static _loadQuery($query, $returnedClass="")
Definition: DataClass.php:111