Personal Understanding: The so-called elegant programming is to drink coffee, touch the keyboard, thinking about business; The elegant code is defined as: 1, as far as possible less single double quotation marks; 2, do not nest; 3, the name is all the most basic English words; 4, the code, like English, is a paragraph; 5, an application code organization clear, preferably a tree relationship, network relationship is less useful.
Below is a coherent operation of SQL query implementation method, please guide the guidance!
-
- /**
- * DB coherent operation, SQL conditional construction
- * 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 ', ' have ', ' order ', ' limit ');
- Initializing query parameters
- function __construct ($table, $prefix = ") {
- $this->option[' from ']= $prefix. $table;
- }
- Construction parameters
- function __call ($method, $param) {
- if (In_array ($method, Self:: $keyword)) {
- $this->option[$method]= $param [0];
- return $this;
- }
- }
- Output query Results
- 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::p age_list ($this->sql, $rs _count, $now _page, $page _size);
- Default
- return $this->sql;
- }
- }
- Constructing SQL query criteria
- static function condition ($data) {
- Working with logical connectors
- $logic = ' and ';
- if (Isset ($data [' logic ')]) {
- $logic = '. $data [' logic ']. ' ';
- unset ($data [' logic ']);
- }
- Working with strings (native SQL)
- if (Isset ($data [' query '])) {
- $condition []= ' ('. $data [' query ']. ') ';
- unset ($data [' query ']);
- }
- Processing condition data
- foreach ($data as $key = = $value) {
- $condition []= ' ('. Self::p arse_expression ($key, $value). ') ';
- }
- Return implode ($logic, $condition);
- }
- Parsing an expression
- 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)) {
- Basic condition Query
- 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 range Lookup
- if (In_array ($value [0],array (' in ', ' not ')}) {
- Is_array ($value [1]) && $value [1]=implode (', ', $value [1]);
- return $key. ' '. $value [0]. ' ('. $value [1]. ') ';
- }
- Between interval search
- 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 fuzzy matching
- if (In_array ($value [0],array (' like ', ' Don't 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]. '";
- }
- Math interval Query (1,9)/[2,3)
- if ($value [0] = = = = ' extent ') {
- $logic =isset ($value [2])? ". $value [2]. ' ': ' && ';
- $operator =array (' = ' (' = ' > ', ' [' = ' = ' >= ', ') ' = ' < ', '] ' and ' = ' <= ');
- 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 ';
- }
- }
- Resource Recycling
- function __destruct () {
- Unset ($this->option, $this->sql);
- }
- }
Copy Code |