What is graceful programming? What is an elegant code?

Source: Internet
Author: User
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!
  1. /**
  2. * DB coherent operation, SQL conditional construction
  3. * from:eqphp FrameWork
  4. * Author:art_youth
  5. * e-mail:258122391@qq.com
  6. * Pub data:2012-11-09
  7. */
  8. Class query{
  9. Public $sql = ';
  10. Public $option =array ();
  11. Static $keyword =array (' select ', ' from ', ' where ', ' group ', ' have ', ' order ', ' limit ');
  12. Initializing query parameters
  13. function __construct ($table, $prefix = ") {
  14. $this->option[' from ']= $prefix. $table;
  15. }
  16. Construction parameters
  17. function __call ($method, $param) {
  18. if (In_array ($method, Self:: $keyword)) {
  19. $this->option[$method]= $param [0];
  20. return $this;
  21. }
  22. }
  23. Output query Results
  24. function out ($mode = ' sql ', $rs _count=0, $now _page=1, $page _size=20) {
  25. $this->sql= ';
  26. foreach (self:: $keyword as $key) {
  27. $value = ($key = = ' Group ' | | $key = = ' order ')? $key. ' By ': $key;
  28. if ($key = = = ' WHERE ' && is_array ($this->option[' where ')) {
  29. $this->option[' where ']=self::condition ($this->option[' where ');
  30. }
  31. if (Isset ($this->option[$key]) && trim ($this->option[$key])) {
  32. $this->sql.= '. $value. ' '. Trim ($this->option[$key]);
  33. }
  34. unset ($this->option[$key]);
  35. }
  36. $this->sql=trim ($this->sql);
  37. Switch ($mode) {
  38. Case ' RS ':
  39. Return Db::rs ($this->sql);
  40. Case ' list ':
  41. Return Db::rs_list ($this->sql);
  42. Case ' page ':
  43. Return DB::p age_list ($this->sql, $rs _count, $now _page, $page _size);
  44. Default
  45. return $this->sql;
  46. }
  47. }
  48. Constructing SQL query criteria
  49. static function condition ($data) {
  50. Working with logical connectors
  51. $logic = ' and ';
  52. if (Isset ($data [' logic ')]) {
  53. $logic = '. $data [' logic ']. ' ';
  54. unset ($data [' logic ']);
  55. }
  56. Working with strings (native SQL)
  57. if (Isset ($data [' query '])) {
  58. $condition []= ' ('. $data [' query ']. ') ';
  59. unset ($data [' query ']);
  60. }
  61. Processing condition data
  62. foreach ($data as $key = = $value) {
  63. $condition []= ' ('. Self::p arse_expression ($key, $value). ') ';
  64. }
  65. Return implode ($logic, $condition);
  66. }
  67. Parsing an expression
  68. private static function Parse_expression ($key, $value) {
  69. if (Is_numeric ($value)) return $key. ' = '. $value;
  70. if (is_string ($value)) return $key. ' = "'. $value. '";
  71. if (Is_array ($value)) {
  72. Basic condition Query
  73. if (Preg_match ('/^ (eq|neq|gt|egt|lt|elt) $/i ', $value [0])) {
  74. Is_string ($value [1]) && $value [1]= ' "". $value [1]. ' "';
  75. $operator =array (' eq ' = ' = ', ' neq ' = ' <> ', ' gt ' = ' > ', ' egt ' = ' >= ', ' lt ' = ' < ', ' elt ' = ' <= ',);
  76. return $key. $operator [$value [0]]. $value [1];
  77. }
  78. In range Lookup
  79. if (In_array ($value [0],array (' in ', ' not ')}) {
  80. Is_array ($value [1]) && $value [1]=implode (', ', $value [1]);
  81. return $key. ' '. $value [0]. ' ('. $value [1]. ') ';
  82. }
  83. Between interval search
  84. if (In_array ($value [0],array (' between ', ' not between ')) {
  85. $param =is_string ($value [1])? Explode (', ', $value [1]): $value [1];
  86. return $key. ' '. $value [0]. ' '. $param [0]. ' and '. $param [1];
  87. }
  88. Like fuzzy matching
  89. if (In_array ($value [0],array (' like ', ' Don't like ')}) {
  90. if (Is_array ($value [1])) {
  91. $buffer =array ();
  92. foreach ($value [1] as $param) {
  93. $buffer []= $key. ' '. $value [0]. ' "'. $param. '";
  94. }
  95. $logic =isset ($value [2])? ". $value [2]. ' ': ' or ';
  96. Return implode ($logic, $buffer);
  97. }
  98. if (Strpos ($key, ' | ')!== false) {
  99. $buffer =array ();
  100. foreach (Explode (' | ', $key) as $field) {
  101. $buffer []= ' ('. $field. ') '. $value [0]. ' "'. $value [1]. ') ';
  102. }
  103. Return implode (' or ', $buffer);
  104. }
  105. if (Strpos ($key, ' & ')!== false) {
  106. $buffer =array ();
  107. foreach (Explode (' & ', $key) as $field) {
  108. $buffer []= ' ('. $field. ') '. $value [0]. ' "'. $value [1]. ') ';
  109. }
  110. Return implode (' and ', $buffer);
  111. }
  112. return $key. ' '. $value [0]. ' "'. $value [1]. '";
  113. }
  114. Math interval Query (1,9)/[2,3)
  115. if ($value [0] = = = = ' extent ') {
  116. $logic =isset ($value [2])? ". $value [2]. ' ': ' && ';
  117. $operator =array (' = ' (' = ' > ', ' [' = ' = ' >= ', ') ' = ' < ', '] ' and ' = ' <= ');
  118. Preg_match ('/^ (\ (|\[) (. *), (. *) (\) |\]) $/', $value [1], $param);
  119. $result = ";
  120. Isset ($param [2]) && $result. = $key. $operator [$param [1]]. $param [2];
  121. Isset ($param [4]) && $result. = $logic. $key. $operator [$param [4]]. $param [3];
  122. return $result;
  123. }
  124. Return ';
  125. }
  126. }
  127. Resource Recycling
  128. function __destruct () {
  129. Unset ($this->option, $this->sql);
  130. }
  131. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.