php final class mysql protected static oInstance null protected static

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
final class mysql {
protected static $oInstance = null;
protected static $dbh = null;
static public function getInstance() {
if (self::$oInstance == null) {
$class = __CLASS__;
self::$oInstance = new $class ( );
}
return self::$oInstance;
}
protected function __construct() {
$this->__connect ();
}
static protected function __connect() {
if (defined ( 'DB_HOST' ) && defined ( 'DB_USER' ) && defined ( 'DB_PASS' ) && defined ( 'DB_NAME' )) {
self::$dbh = mysql_connect ( DB_HOST, DB_USER, DB_PASS );
mysql_select_db ( DB_NAME, self::$dbh );
} else {
die ( 'MySQL error: connection data not found!' );
}
}
protected function clean($result) {
mysql_free_result($result);
}
public function execute($sQuery) {
if (! self::$dbh) {
self::__connect ();
}
if (! self::$dbh) {
die ( 'MySQL connection lost!' );
}
$sQuery = self::queryFixer($sQuery);
return mysql_query ( $sQuery, self::$dbh );
}
public function oneRow($sQuery) {
$result = self::execute ( $sQuery );
if($result) {
return mysql_fetch_assoc ( $result );
}
errors::trigger(mysql_error());
return false;
}
public function multiRows( $sQuery ) {
$result = self::execute($sQuery);
if($result) {
$aResults = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
$aResults[] = $row;
}
$this->clean($result);
return $aResults;
}
errors::trigger(mysql_error());
return false;
}
public function grouppedRows($sQuery, $sGroupBy) {
$result = self::execute($sQuery);
if($result) {
$aResults = array();
while ($row = mysql_fetch_assoc($result)) {
$aResults[$row[$sGroupBy]] = $row;
}
$this->clean($result);
return $aResults;
}
errors::trigger(mysql_error());
return false;
}
public function getList($sQuery, $sField) {
$result = self::execute($sQuery);
if($result) {
$aResults = array();
while($row = mysql_fetch_assoc($result)) {
$aResults[] = $row[$sField];
}
$this->clean($result);
return $aResults;
}
errors::trigger(mysql_error());
return false;
}
public function itemsCount($table, $field, $value) {
$query = sprintf('SELECT count(%s) as quanta FROM %s WHERE `%s`="%s"', $field, $table, $field, $value);
$result = self::execute($query);
return ((integer) $result['quanta']);
}
static public function lastInserted() {
return mysql_insert_id(self::$dbh);
}
static protected function queryFixer($sQuery) {
if(strpos($sQuery, '%s') !== false) {
return sprintf($sQuery, DB_PREFIX);
} else {
return $sQuery;
}
}
static public function queryBuilder($array, $table, $type = 'INSERT', $where = '1=1') {
if($type == 'INSERT') {
$arrKeys = array();
$arrVals = array();
foreach ($array as $key=>$value) {
$arrKeys[] = '`'.$key.'`';
$arrVals[] = '"'.$value.'"';
}
$arrKeys = implode(',', $arrKeys);
$arrVals = implode(',', $arrVals);
$sQuery = 'INSERT INTO `'.$table.'` ('.$arrKeys.') VALUES ('.$arrVals.')';
} elseif ($type == 'SELECT') {
if(!is_array($array) || (is_array($array) && !sizeof($array))) {
$fields = '*';
} else {
$fields = '`'.implode('`, `',$array).'`';
}
$sQuery = 'SELECT '.$fields.' FROM `'.$table.'` WHERE '.$where;
} else {
$arrKeys = array();
foreach ($array as $key=>$value) {
$arrKeys[] = '`'.$key.'`="'.$value.'"';
}
$arrKeys = implode(',', $arrKeys);
$sQuery = 'UPDATE `'.$table.'` SET '.$arrKeys.' WHERE '.$where;
}
return $sQuery;
}
}