php thinkphpを模したsqlクラスライブラリ


thinkphpパッケージを模倣したクラスライブラリ
php
/**
 * MySql   2015 
 *   :       [email protected]
 *     :
 *         //    
 *         inclode 'Mysql.class.php';
 *         //             
 *         function newMySQL($table){
 *             //DSN  ,         
 *             $dsn = array(
 *                     'DB_HOST'        =>    'loaclhost',    //    
 *                    'DB_NAME'        =>    'mydb',        //    
 *                    'DB_PORT'        =>    3306,            //  
 *                    'DB_PREFIX'        =>    'kf',            //   
 *                    'DB_CHARSET'    =>    'utf8',            //     
 *                    'DB_USER'        =>    'root',            //   
 *                    'DB_PWD'        =>    '123456',    //  
 *             );
 *             //  
 *             $dsn['DB_TABLE'] = $table;
 *             //       
 *             return Mysql::start($dsn);
 *         }
 *         //    ,       
 *         $Sql = newMySQL('user');
 *         //       ,    table      
 *         $Sql->table = 'product';
 *         //    
 *         $data['name'] = '  ';
 *         $data['pwd'] = md5('123456');
 *         $data['age'] = 25;
 *         $data['sex'] = ' ';
 *         $Sql->add($data);
 *         
 *         //    
 *         $data['name'] = '  ';
 *         $where = "id=1";
 *         $Sql->where($where)->save($data);
 *         //     1
 *         $Sql->where($where)->sum('age');
 *         //     N
 *         $Sql->where($where)->sum('age',10);
 * 
 *         //    
 *         $where = "sex=' ' and age>20";
 *         //           
 *         $arr = $Sql->where($where)->select();
 *         //  field        
 *         $arr = $Sql->field('name,sex')->where($where)->select();
 *         //        limit           
 *         $arr = $Sql->where($where)->limit(   ,    )->select();
 *         //      desc  or  asc         , ","  
 *         $arr = $Sql->where($where)->order("sex desc")->select();
 *         //     
 *         $find = $Sql->where($where)->find();
 *         //        ,         >1 ,     
 *         $field = $Sql->where('id=1')->getField('name');
 *         //         
 *         $count = $Sql->where($where)->getCount();
 *         
 *         //    
 *         $Sql->where($where)->delete();
 */
class Mysql
{
    private $host;   //    
    private $dbname; //    
    private $port;  //     
    private $username; //   
    private $password; //  
    private $charset; //     
    static $SQL; //     
    static $obj = null;
    public $table; //   
    private $pre = null;
    private $opt;  //  
    
    /**
     *       
     * @param Array $dsn        
     */
    private function __construct($dsn){
        $this->host = $dsn['DB_HOST'];
        $this->dbname = $dsn['DB_NAME'];
        $this->username = $dsn['DB_USER'];
        $this->password = $dsn['DB_PWD'];
        $this->port = $dsn['DB_PORT'];
        $this->charset = $dsn['DB_CHARSET'];
        //$this->table = $dsn['DB_PREFIX'].$dsn['DB_TABLE'];
        $this->opt['where'] = '';
        $this->opt['order'] = '';
        $this->opt['limit'] = '';
        $this->opt['field'] = null;
        $this->connect();
    }
    
    /*
     * Lee:
     *            
     * */
    public function reset()
    {
        $this->opt['where'] = '';
        $this->opt['order'] = '';
        $this->opt['limit'] = '';
        $this->opt['field'] = null;
    }
    
    /**
     *      
     * @return Bool
     */
    private function connect(){
        try{
             $dsn  =  'mysql:host='.$this->host.';dbname='.$this->dbname.';port='.$this->port;
             $options  = array(
                 PDO :: MYSQL_ATTR_INIT_COMMAND  =>  'SET NAMES '.$this->charset,
            ); 
             self::$SQL = new  PDO ($dsn,$this->username,$this->password,$options);
            //      
            self::$SQL->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            echo "       :".$e->getMessage();
            exit;
        }
        return true;
    }
    
    /**
     * @param Array $dsn        (DB_HOST[  ],DB_NAME[    ],DB_PORT[  ],DB_CHARSET[  ],DB_USER[  ],DB_PWD[  ],DB_TABLE[  ])
     * @return Object
     */
    static function start($dsn){
        //        ,            
        //if(is_null(self::$obj)){
      
        self::$obj = null;
        self::$obj = new self($dsn);
        //}
        return self::$obj;
    }
    
    /**
     *     (               ,    0)
     * @param Array $data       ,        
     * @param Int 0      ,1     ,2(  )        
     * @return Int 
     */
    public function add($data,$type = 2){
        try{
            //  SQL  
            $count = 0;
            $a = '';
            foreach($data as $k=>$v){
                if($count != 0){
                    $a = ',';
                }
                @$key .= $a.$k;
                @$bind .=$a.":$k";
                $count++;
            }
            $s = "insert into {
     $this->table}({
     $key}) values({
     $bind})";
         
            //    
            if($type != 1){
                $this->pre = self::$SQL->prepare($s);
                if($type == 0 && is_object($this->pre) == true){
                    return 1;
                }
            }
            //    
            if($type != 0){
                $this->pre->execute($data);
            }
            //      
            $this->reset();
            //      
            return self::$SQL->lastinsertid();
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
        
    }
    /**
     *     (      )
     * @param void $data       
     * @return Int
     */
    public function save($data){
        try{
            //  SQL  
            $count = 0;
            $a = '';
            foreach($data as $k=>$v)
            {
                if($count != 0){
                    $a = ',';
                }
                @$bind .=$a."$k='$v'";
                $count++;
            }
            $s = "update {
     $this->table} set {
     $bind} {
     $this->opt['where']}";  
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute($data);
            //      
            $this->reset();
            //      
            return $this->pre->rowCount();
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
    }
    
    /**
     *      N,   1 
     * @param String $key   
     * @param Int $num [  ]      ,  1
     * @return Int
     */
    public function sum($key,$num){
        try{
            $s = "update {
     $this->table} set {
     $key}={
     $key}+{
     $num} {
     $this->opt['where']}";    
            
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //      
            return $this->pre->rowCount();
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
    }
    
    
    
    
    
    
    
    /**
     *     (      )
     * @return Int
     */
    public function delete(){
        try{
            $s = "delete from {
     $this->table} {
     $this->opt['where']}";
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //      
            return $this->pre->rowCount();
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
    }
    
    /*
     * Lee  :
     *            sql       
     * */
    public  function query($str)
    {
        try 
        {
            $s = $str;
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //        
            $this->pre->setFetchMode(PDO::FETCH_ASSOC);
            //      
            $this->reset();
            return $this->pre->fetchAll();
        }
        catch(PDOException $e)
        {
           exit("  :".$e->getMessage());
        }
    }
    
    /*
     * Lee  :
     *            sql       
     * */
    public  function send($str)
    {
        try
        {
            $s = $str;
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //      
            $this->reset();     
        }
        catch(PDOException $e)
        {            
            exit("  :".$e->getMessage());
        }
    }
    
    
    /**
     *     (           )
     * @return array 
     */
    public function select(){
        try{
            $s = "select ".($this->opt['field']? $this->opt['field']:'*')." from {
     $this->table} {
     $this->opt['where']} {
     $this->opt['order']} {
     $this->opt['limit']}";
            
            //    
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //        
            $this->pre->setFetchMode(PDO::FETCH_ASSOC);
            //      
            $this->reset();
            return $this->pre->fetchAll();
        }
        catch(PDOException $e)
        {
            exit("  :".$e->getMessage());
        }
    }
    /**
     *      (        )
     * @return array  
     */
    public function find(){
        try{
            $s = "select ".($this->opt['field']? $this->opt['field']:'*')." from {
     $this->table} {
     $this->opt['where']} {
     $this->opt['order']} limit 1";
            
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            //        
            $this->pre->setFetchMode(PDO::FETCH_ASSOC);
            $arr = $this->pre->fetchAll();
            //      
            $this->reset();
            return @$arr[0];
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
    }
    /**
     *       (        ,      ,      ,    false)
     * @param String $key      
     * @return mixed
     */
    public function getField($key){
        $opt['field'] = $key;
        $selArr = $this->select();
        foreach($selArr as $number=>$value){
            $retuls[$number] = $value[$key];
        }
        
        if(@$retuls == null) $retuls=false;
        //           
        if(count($retuls) == 1 and $retuls != false){
            $retuls = $retuls[0];
        }
        return $retuls;
    }
    
    
    /**
     *        
     * @param String $key      
     * @param mixed $value    
     * @return mixed
     */
    public function setField($key,$value){
        return $this->save(array($key=>$value));
    }
    
    /**
     *            
     */
    public function getCount(){
        try{
            $s = "select count(*) from {
     $this->table} {
     $this->opt['where']}";
            $this->pre = self::$SQL->prepare($s);
            //    
            $this->pre->execute();
            $count = $this->pre->fetch();
            return Intval($count[0]);
        }catch(PDOException $e){
            exit("  :".$e->getMessage());
        }
    }
    /**
     *     
     * @param String $where     
     * @return this
     */
    public function where($where){
        $this->opt['where'] = $where? "where ".$where:'';
        return $this;
    }
    
    /**
     *     
     * @param String $order       desc.      asc.  
     * @return this
     */
    public function order($order){
        $this->opt['order'] = $order? "order by ".$order:'';
        return $this;
    }
    
    /**
     *          
     * @param Int $min        ,         ;         , 0  
     * @param Int $max         
     * @return this
     */
    public function limit($min,$max=null){
        $this->opt['limit'] = "limit ".intval($min).($max? ','.intval($max):'');
        return $this;
    }
    
    /**
     *        
     * @param String $field       ,    (,)    
     * @param this
     */
    public function field($field){
        $this->opt['field'] = $field;
        return $this;
    }

}

?>

 
転載先:https://www.cnblogs.com/CyLee/p/5606137.html