個人理解: 所謂優雅編程就是喝著咖啡,摸著鍵盤,想著業務; 優雅代碼的定義是: 1、儘可能的少單雙引號; 2、不要嵌套; 3、命名全是最基本的英語單詞; 4、代碼像英文一樣,是一段一段的; 5、一個應用程式碼群組織調理清晰,最好是樹關係,網關係少用。
下邊是一個sql查詢的連貫操作實現方法,請大家指導指導!
- /**
- * DB連貫操作、sql條件構造
- * From: EQPHP FrameWork
- * Author: art_youth
- * E-mail: 258122391@qq.com
- * Pub data: 2012-11-09
- */
- class query{
- public $sql='';
- public $option=array();
- static $keyword=array('select','from','where','group','having','order','limit');
- //初始化查詢參數
- function __construct($table,$prefix=''){
- $this->option['from']=$prefix.$table;
- }
- //構造參數
- function __call($method,$param){
- if (in_array($method,self::$keyword)) {
- $this->option[$method]=$param[0];
- return $this;
- }
- }
- //輸出查詢結果
- function out($mode='sql',$rs_count=0,$now_page=1,$page_size=20){
- $this->sql='';
- foreach (self::$keyword as $key) {
- $value=($key == 'group' || $key == 'order') ? $key.' by' : $key;
- if ($key === 'where' && is_array($this->option['where'])) {
- $this->option['where']=self::condition($this->option['where']);
- }
- if (isset($this->option[$key]) && trim($this->option[$key])) {
- $this->sql.=' '.$value.' '.trim($this->option[$key]);
- }
- unset($this->option[$key]);
- }
- $this->sql=trim($this->sql);
- switch($mode){
- case 'rs':
- return db::rs($this->sql);
- case 'list':
- return db::rs_list($this->sql);
- case 'page':
- return db::page_list($this->sql,$rs_count,$now_page,$page_size);
- default:
- return $this->sql;
- }
- }
- //構造sql查詢條件
- static function condition($data){
- //處理邏輯串連符
- $logic=' and ';
- if (isset($data['logic'])) {
- $logic=' '.$data['logic'].' ';
- unset($data['logic']);
- }
- //處理字串(本生sql)
- if (isset($data['query'])) {
- $condition[]='('.$data['query'].')';
- unset($data['query']);
- }
- //處理條件資料
- foreach ($data as $key=>$value) {
- $condition[]='('.self::parse_expression($key,$value).')';
- }
- return implode($logic,$condition);
- }
- //解析運算式
- private static function parse_expression($key,$value){
- if (is_numeric($value)) return $key.'='.$value;
- if (is_string($value)) return $key.'="'.$value.'"';
- if (is_array($value)) {
- //基本條件查詢
- if (preg_match('/^(eq|neq|gt|egt|lt|elt)$/i',$value[0])) {
- is_string($value[1]) && $value[1]='"'.$value[1].'"';
- $operator=array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=',);
- return $key.$operator[$value[0]].$value[1];
- }
- //in範圍尋找
- if (in_array($value[0],array('in','not in'))) {
- is_array($value[1]) && $value[1]=implode(',',$value[1]);
- return $key.' '.$value[0].'('.$value[1].')';
- }
- //between區間尋找
- if (in_array($value[0],array('between','not between'))) {
- $param=is_string($value[1]) ? explode(',',$value[1]) : $value[1];
- return $key.' '.$value[0].' '.$param[0].' and '.$param[1];
- }
- //like模糊比對
- if (in_array($value[0],array('like','not like'))) {
- if (is_array($value[1])) {
- $buffer=array();
- foreach ($value[1] as $param) {
- $buffer[]=$key.' '.$value[0].' "'.$param.'"';
- }
- $logic=isset($value[2]) ? ' '.$value[2].' ' : ' or ';
- return implode($logic,$buffer);
- }
- if (strpos($key,'|') !== false) {
- $buffer=array();
- foreach (explode('|',$key) as $field) {
- $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
- }
- return implode(' or ',$buffer);
- }
- if (strpos($key,'&') !== false) {
- $buffer=array();
- foreach (explode('&',$key) as $field) {
- $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
- }
- return implode(' and ',$buffer);
- }
- return $key.' '.$value[0].' "'.$value[1].'"';
- }
- //數學區間查詢(1,9)/[2,3)
- if ($value[0] === 'extent') {
- $logic=isset($value[2]) ? ' '.$value[2].' ' : ' && ';
- $operator=array('('=>'>','['=>'>=',')'=>'<',']'=>'<=');
- preg_match('/^(\(|\[)(.*),(.*)(\)|\])$/',$value[1],$param);
- $result='';
- isset($param[2]) && $result.=$key.$operator[$param[1]].$param[2];
- isset($param[4]) && $result.=$logic.$key.$operator[$param[4]].$param[3];
- return $result;
- }
- return '';
- }
- }
- //資源回收
- function __destruct(){
- unset($this->option,$this->sql);
- }
- }
複製代碼 |