Snippets
Revised by
Zaid Bin Khalid
67397ac
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | <?php
class Database{
/**
* database connection object
* @var \PDO
*/
protected $pdo;
/**
* Connect to the database
*/
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
/**
* Return the pdo connection
*/
public function getPdo()
{
return $this->pdo;
}
/**
* Changes a camelCase table or field name to lowercase,
* underscore spaced name
*
* @param string $string camelCase string
* @return string underscore_space string
*/
protected function camelCaseToUnderscore($string)
{
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string));
}
/**
* Returns the ID of the last inserted row or sequence value
*
* @param string $param Name of the sequence object from which the ID should be returned.
* @return string representing the row ID of the last row that was inserted into the database.
*/
public function lastInsertId($param = null)
{
return $this->pdo->lastInsertId($param);
}
/**
* handler for dynamic CRUD methods
*
* Format for dynamic methods names -
* Create: insertTableName($arrData)
* Retrieve: getTableNameByFieldName($value)
* Update: updateTableNameByFieldName($value, $arrUpdate)
* Delete: deleteTableNameByFieldName($value)
*
* @param string $function
* @param array $arrParams
* @return array|bool
*/
public function __call($function, array $params = array())
{
if (! preg_match('/^(get|update|insert|delete)(.*)$/', $function, $matches)) {
throw new \BadMethodCallException($function.' is an invalid method Call');
}
if ('insert' == $matches[1]) {
if (! is_array($params[0]) || count($params[0]) < 1) {
throw new \InvalidArgumentException('insert values must be an array');
}
return $this->insert($this->camelCaseToUnderscore($matches[2]), $params[0]);
}
list($tableName, $fieldName) = explode('By', $matches[2], 2);
if (! isset($tableName, $fieldName)) {
throw new \BadMethodCallException($function.' is an invalid method Call');
}
if ('update' == $matches[1]) {
if (! is_array($params[1]) || count($params[1]) < 1) {
throw new \InvalidArgumentException('update fields must be an array');
}
return $this->update(
$this->camelCaseToUnderscore($tableName),
$params[1],
array($this->camelCaseToUnderscore($fieldName) => $params[0])
);
}
//select and delete method
return $this->{$matches[1]}(
$this->camelCaseToUnderscore($tableName),
array($this->camelCaseToUnderscore($fieldName) => $params[0])
);
}
/**
* Record retrieval method
*
* @param string $tableName name of the table
* @param array $where (key is field name)
* @return array|bool (associative array for single records, multidim array for multiple records)
*/
public function get($tableName, $whereAnd = array(), $whereOr = array(), $whereLike = array())
{
$cond = '';
$s=1;
$params = array();
foreach($whereAnd as $key => $val)
{
$cond .= " And ".$key." = :a".$s;
$params['a'.$s] = $val;
$s++;
}
foreach($whereOr as $key => $val)
{
$cond .= " OR ".$key." = :a".$s;
$params['a'.$s] = $val;
$s++;
}
foreach($whereLike as $key => $val)
{
$cond .= " OR ".$key." like '% :a".$s."%'";
$params['a'.$s] = $val;
$s++;
}
$stmt = $this->pdo->prepare("SELECT $tableName.* FROM $tableName WHERE 1 ".$cond);
try {
$stmt->execute($params);
$res = $stmt->fetchAll();
if (! $res || count($res) != 1) {
return $res;
}
return $res;
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
public function getAllRecords($tableName, $fields='*', $cond='', $orderBy='', $limit='')
{
//echo "SELECT $tableName.$fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
//print "<br>SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
$stmt = $this->pdo->prepare("SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit);
//print "SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." " ;
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
public function getRecFrmQry($query)
{
//echo $query;
$stmt = $this->pdo->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
public function getRecFrmQryStr($query)
{
//echo $query;
$stmt = $this->pdo->prepare($query);
$stmt->execute();
return array();
}
public function getQueryCount($tableName, $field, $cond='')
{
$stmt = $this->pdo->prepare("SELECT count($field) as total FROM $tableName WHERE 1 ".$cond);
try {
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (! $res || count($res) != 1) {
return $res;
}
return $res;
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
/**
* Update Method
*
* @param string $tableName
* @param array $set (associative where key is field name)
* @param array $where (associative where key is field name)
* @return int number of affected rows
*/
public function update($tableName, array $set, array $where)
{
$arrSet = array_map(
function($value) {
return $value . '=:' . $value;
},
array_keys($set)
);
$stmt = $this->pdo->prepare(
"UPDATE $tableName SET ". implode(',', $arrSet).' WHERE '. key($where). '=:'. key($where) . 'Field'
);
foreach ($set as $field => $value) {
$stmt->bindValue(':'.$field, $value);
}
$stmt->bindValue(':'.key($where) . 'Field', current($where));
try {
$stmt->execute();
return $stmt->rowCount();
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
/**
* Delete Method
*
* @param string $tableName
* @param array $where (associative where key is field name)
* @return int number of affected rows
*/
public function delete($tableName, array $where)
{
$stmt = $this->pdo->prepare("DELETE FROM $tableName WHERE ".key($where) . ' = ?');
try {
$stmt->execute(array(current($where)));
return $stmt->rowCount();
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
public function delete2($query)
{
$stmt = $this->pdo->prepare($query);
$stmt->execute();
}
/**
* Insert Method
*
* @param string $tableName
* @param array $arrData (data to insert, associative where key is field name)
* @return int number of affected rows
*/
public function insert($tableName, array $data)
{
$stmt = $this->pdo->prepare("INSERT INTO $tableName (".implode(',', array_keys($data)).")
VALUES (".implode(',', array_fill(0, count($data), '?')).")"
);
try{
$stmt->execute(array_values($data));
return $stmt->rowCount();
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
/**
* Print array Method
*
* @param array
*/
public function arprint($array){
print"<pre>";
print_r($array);
print"</pre>";
}
/**
* Maker Model Name Method
*
* @param Int make id
* @param Int name id
*/
public function getModelMake($makeID,$nameID){
$vehMakeData = self::getRecFrmQry('SELECT veh_make_id,veh_make_name FROM tb_vehicle_make WHERE veh_make_id="'.$makeID.'"');
$vehNameData = self::getRecFrmQry('SELECT veh_name_id,veh_name FROM tb_vehicle_name WHERE veh_name_id="'.$nameID.'"');
return $vehMakeData[0]['veh_make_name'].' '.$vehNameData[0]['veh_name'];
}
/**
* Cache Method
*
* @param string QUERY
* @param Int Time default 0 set
*/
public function getCache($sql,$cache_min=0) {
$f = HOME_PATH.'aj_cache/'.md5($sql);
if ( $cache_min!=0 && file_exists($f) && ( (time()-filemtime($f))/60 < $cache_min ) ) {
$arr = unserialize(file_get_contents($f));
}
else {
unlink($f);
$arr = self::getRecFrmQry($sql);
if ($cache_min!=0) {
$fp = fopen($f,'w');
fwrite($fp,serialize($arr));
fclose($fp);
}
}
return $arr;
}
}
|
You can clone a snippet to your computer for local editing. Learn more.