westdc-zf1/application/module/Helpers/dbh.php

206 lines
4.2 KiB
PHP
Raw Normal View History

2013-08-22 07:59:09 +00:00
<?php
namespace Helpers;
use \Helpers\View as view;
2013-08-22 07:59:09 +00:00
class dbh
{
private $db; //传入PDO对象.
private $product = 0; //产品环境
public $debug = 0;
function __construct($db = NULL)
2013-08-22 07:59:09 +00:00
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
2013-08-22 07:59:09 +00:00
}
function insert($table,$data,$return=false)
{
$fields = array();
$datas = array();
foreach($data as $k=>$v)
{
$fields[] = '"'.$k.'"';
if(is_int($v) || is_float($v) || is_bool($v))
{
$datas[] = $v;
}else{
if(preg_match("/\'/",$v))
{
$v = preg_replace("/\'/","''",$v);
}
$datas[] = "'".$v."'";
}
}
$fields = join(",",$fields);
$datas = join(",",$datas);
if($return == false){
$sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas)";
if($this->debug == 1)
{
view::Dump($sql,false);
}
2013-08-22 07:59:09 +00:00
try{
return $this->db->exec($sql);
}catch (Exception $e) {
if($this->product)
{
return false;
}else{
echo 'Caught exception: '. $e->getMessage(). "\n";
}
}
}else{
$sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas) RETURNING id";
if($this->debug == 1)
{
view::Dump($sql,false);
}
2013-08-22 07:59:09 +00:00
try{
$sth = $this->db->prepare($sql);
if($sth->execute())
{
$temp = $sth->fetch(\PDO::FETCH_ASSOC);
2013-08-22 07:59:09 +00:00
return $temp['id'];
}else{
return false;
}
}catch (Exception $e) {
if($this->product)
{
return false;
}else{
echo 'Caught exception: '. $e->getMessage(). "\n";
}
}
}
}//insert
function update($table,$data,$condition="",$return=false)
{
$ups = array();
foreach($data as $k=>$v)
{
if(is_int($v) || is_float($v) || is_bool($v))
{
$ups[] = '"'.$k.'"='.$v;
}else{
if(preg_match("/\'/",$v))
{
$v = preg_replace("/\'/","''",$v);
}
if(preg_match("/\"/",$v))
{
$v = preg_replace("/\"/","''",$v);
}
$ups[] = '"'.$k.'"=\''.$v."'";
}
}
$fields = join(",",$ups);
if(!empty($condition))
{
$wheresql = " WHERE ".$condition;
}else{
$wheresql = "";
}
if($return == false){
try{
$sql = "UPDATE \"".$table."\" SET $fields $wheresql";
if($this->debug == 1)
{
view::Dump($sql,false);
}
2013-08-22 07:59:09 +00:00
if($this->db->exec($sql))
{
return true;
}else{
return false;
}
}catch (Exception $e) {
if($this->product)
{
return false;
}else{
echo 'Caught exception: '. $e->getMessage(). "\n";
}
}
}else{
try{
$sql = "UPDATE \"".$table."\" SET $fields $wheresql";
if($this->debug == 1)
{
view::Dump($sql,false);
}
2013-08-22 07:59:09 +00:00
return $this->db->exec($sql);
}catch (Exception $e) {
if($this->product)
{
return false;
}else{
echo "<pre>";
echo $sql."\r\n";
echo 'Caught exception: '. $e->getMessage(). "\r\n";
echo "</pre>";
}
}
}
}//update
//select
public function select($opt,$debug = false)
{
$field = (isset($opt['field']) && !empty($opt['field'])) ? $opt['field']:"*";
$tbl = (isset($opt['table']) && !empty($opt['table'])) ? $opt['table'] : "";
$join = (isset($opt['join']) && !empty($opt['join'])) ? $opt['join'] : "";
$wheresql = (isset($opt['where']) && !empty($opt['where'])) ? " WHERE ".$opt['where']:" ";
$limit = (isset($opt['limit']) && !empty($opt['limit'])) ? " LIMIT ".$opt['limit']:"";
$offset = (isset($opt['start']) && !empty($opt['start'])) ? " OFFSET ".$opt['start']:"";
$order = (isset($opt['order']) && !empty($opt['order'])) ? " ORDER BY ".$opt['order']:"";
$order .= (isset($opt['sort']) && !empty($opt['sort']) && $order !== '') ? " ".$opt['sort']:"";
$sql = "SELECT $field FROM $tbl
" . $join . "
" . $wheresql . "
" . $order . "
" . $limit . "
" . $offset . "
";
if($debug) return $sql;
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
public function chk($p)
{
if(!isset($p) || empty($p))
{
return false;
}else{
return true;
}
}
2013-08-22 07:59:09 +00:00
}