+ -
当前位置:首页 → 问答吧 → 贴一段哥写的代码..一个简单的封装select查询

贴一段哥写的代码..一个简单的封装select查询

时间:2010-07-15

来源:互联网


  1. <?php
  2. /************************************************************
  3. * 封装数据库查询语句
  4. *
  5. * @filename: Select.Class.php 2010-03-23 13:22
  6. * @copyright: Copyright by 2007 - 2010
  7. * @author: <[email protected]> yjj
  8. * @link:
  9. ***********************************************************/
  10. class Select {
  11. /**
  12. * 当前数据库连接实例
  13. * @var db_resurce
  14. */
  15. protected $_db = null;
  16. /**
  17. * SQL 语句
  18. * @var string
  19. */
  20. protected $_sql = 'SELECT';

  21. /**
  22. * 所要查询的字段
  23. * @var string
  24. */
  25. protected $_filed = '*';

  26. /**
  27. * 所要查询的表
  28. * @var string
  29. */
  30. protected $_from = '';

  31. /**
  32. * 查询条件 WHERE 子句
  33. * @var string
  34. */
  35. protected $_where = '';

  36. /**
  37. * limit 子句
  38. * @var string
  39. */
  40. protected $_limit = '';

  41. /**
  42. * ORDER 子句
  43. * @var string
  44. */
  45. protected $_order = '';

  46. /**
  47. * 构造函数
  48. * @param array $filed //需要查询的字段
  49. * @return void
  50. */
  51. public function __construct($db_object = null) {
  52. if (!empty($db_object)) {
  53. $this->_db = $db_object;
  54. }
  55. }

  56. /**
  57. * SQL FROM 语句 支持多表查询
  58. *
  59. * <CODE>
  60. * $db->select()->from(
  61. * array('alias' => table1, //数组的KEY为表的别名
  62. * 'alias1' => table2,
  63. * ... ...
  64. * ),
  65. * array('alias' => fileds,
  66. * 'alias1' => fileds1
  67. * ... ...
  68. * )
  69. * )->query();
  70. * </code>
  71. * <CODE1>
  72. * $db->select()->from(
  73. * array(table1,
  74. * table2,
  75. * ... ...
  76. * ),
  77. * array('feild',
  78. * 'feild1',
  79. * ... ...
  80. * )
  81. * )->query();
  82. * </CODE>
  83. * <CODE1>
  84. * $db->select()->from(
  85. * 'table1,
  86. * table2,
  87. * ... ...
  88. * ',
  89. * 'feild,
  90. * feild1,
  91. * ... ...'
  92. * )
  93. * )->query();
  94. * </CODE>
  95. *
  96. * @param string|array $tablename //表名
  97. * @param array|string $fileds //字段
  98. * @return Object_Select
  99. */
  100. public function from($tablename = null, $fileds = null) {
  101. if ($tablename === null) throw new Db_Select_Exception('This DB tablename is null');

  102. //支持多表联合查询
  103. if (is_array($tablename)) {
  104. $ar_table_name = array();
  105. foreach ($tablename as $k => $v) {
  106. if (is_int($k)) {
  107. $ar_table_name[] = sprintf('`%s`', $v);
  108. } else {
  109. $ar_table_name[] = sprintf('`%s` AS %s', $v, $k);
  110. }
  111. }
  112. $this->_from = sprintf('FROM %s', implode(',', $ar_table_name));
  113. } else {
  114. $this->_from = sprintf('FROM `%s`', $tablename);
  115. }

  116. //支持自定义多字段查询
  117. if (!empty($fileds)) {
  118. if (is_array($fileds)) {
  119. $ar_fileds_name = array();
  120. foreach ($fileds as $k => $v) {
  121. if (is_int($k)) {
  122. $ar_fileds_name[] = sprintf('`%s`', $v);
  123. } else {
  124. $ar_fileds_name[] = sprintf('`%s` AS %s', $v, $k);
  125. }
  126. }
  127. $this->_filed = implode(',', $ar_fileds_name);
  128. } else {
  129. $this->_filed = $fileds;
  130. }
  131. }
  132. return $this;
  133. }

  134. /**
  135. * 封装WHERE 子句
  136. * <code>
  137. * $db->select()->from('table1')->where(
  138. * array(
  139. * 'field' => condition,
  140. * 'field1' => condition,
  141. * 'field2' => condition
  142. * )
  143. * )->query();
  144. * </code>
  145. *
  146. * <CODE1>
  147. * $db->select()->from('table1')->where(
  148. * 'a > b and c < d'
  149. * )->query();
  150. * </CODE1>
  151. *
  152. * @param array|string $where
  153. * @return Object_Select
  154. */
  155. public function where($where = null) {
  156. if (!empty($where)) {
  157. if (empty($this->_where)) $this->_where = 'WHERE ';
  158. if (is_array($where)) {
  159. $where_link = array();
  160. foreach ($where as $k => $v) {
  161. $where_link[] = sprintf('`%s` = \'%s\'', $k, $v);
  162. }
  163. $this->_where .= implode(' AND ', $where_link);
  164. } else {
  165. $this->_where .= $where;
  166. }
  167. }
  168. return $this;
  169. }

  170. /**
  171. * 封装LIMIT子句
  172. * @param int $start
  173. * @param int $end
  174. * @return Object_Select
  175. */
  176. public function limit($start = 0, $end = 30) {
  177. $this->_limit = sprintf('LIMIT %d,%d', $start, $end);
  178. return $this;
  179. }

  180. /**
  181. * 封装 ORDER 子句
  182. * <code>
  183. * $db->select()->from()->order(
  184. * array('fields1 desc', 'fields2 ASC', ... ...)
  185. * )->query();
  186. * </code>
  187. *
  188. * <code>
  189. * $db->select()->from()->order(
  190. * 'fields desc'
  191. * )->query();
  192. * </code>
  193. *
  194. * @param string|array $order
  195. * @return Object_Select
  196. */
  197. public function order($order = null) {
  198. if (!empty($order)) {
  199. $this->_order = 'ORDER BY ';
  200. if (is_array($order)) {
  201. $this->_order .= implode(',', $order);
  202. } else {
  203. $this->_order .= $order;
  204. }
  205. }
  206. return $this;
  207. }

  208. /**
  209. * 封装 SQL COUNT()
  210. *
  211. * @param string $filed
  212. * @return Object_Select
  213. */
  214. public function count($filed = '*', $row = 'row_result') {
  215. if ('*' === $filed) {
  216. $this->_filed = sprintf('COUNT(%s) AS %s', $filed, $row);
  217. } else {
  218. $this->_filed = sprintf('COUNT(`%s`) AS %s', $filed, $row);
  219. }
  220. return $this;
  221. }

  222. /**
  223. * 封装 SQL SUM()
  224. *
  225. * @param string $filed
  226. * @return Object_Select
  227. */
  228. public function sum($filed = null) {
  229. if (!empty($filed)) {
  230. $this->_filed = sprintf('SUM(`%s`)', $filed);
  231. }
  232. return $this;
  233. }

  234. /**
  235. * 构造SQL 语句
  236. */
  237. protected function _setSqlString() {
  238. $sql_array = array();
  239. $sql_array[] = $this->_filed;
  240. if (!empty($this->_from)) {
  241. $sql_array[] = $this->_from;
  242. }
  243. if (!empty($this->_where)) {
  244. $sql_array[] = $this->_where;
  245. }
  246. if (!empty($this->_order)) {
  247. $sql_array[] = $this->_order;
  248. }
  249. if (!empty($this->_limit)) {
  250. $sql_array[] = $this->_limit;
  251. }
  252. $this->_sql .= ' ' . implode(' ', $sql_array);
  253. }

  254. /**
  255. * 获得当前SQL语句
  256. * @return string
  257. */
  258. public function __toString() {
  259. $this->_setSqlString();
  260. return $this->_sql;
  261. }

  262. /**
  263. * 返回查询结果
  264. * @return array
  265. */
  266. public function query() {
  267. $this->_setSqlString();
  268. return $this->_db->execute($this->_sql);
  269. }

  270. /**
  271. * 重新聚合 查询结果
  272. * @param string $fileds
  273. * @return array
  274. */
  275. public function fetchAssoc($fileds = null) {
  276. $result = array();
  277. $rs = $this->query();
  278. foreach($rs as $v) {
  279. if (null === $fileds) {
  280. if (isset($v['id'])) {
  281. $fileds = 'id';
  282. }
  283. }
  284. $result[$v[$fileds]] = $v;
  285. }
  286. return $result;
  287. }
  288. }

  289. class Select_Db_Exception extends Exception {
  290. public function __construct($info) {
  291. parent::_construct($info);
  292. }
  293. }
  294. ?>
复制代码


=========================================================
欢迎各位砖家和砖头们拍砖,哥脑壳比较硬......

作者: jiaoyin   发布时间: 2010-07-15

何必如此复杂

作者: fkj   发布时间: 2010-07-15

回复 fkj


    谢谢支持!
这样写肯定是有原因的。。。

作者: jiaoyin   发布时间: 2010-07-15

回复 jiaoyin


    我以前用一个叫ClipBucket的视频程序的时候见过这样类似的代码,但个人觉得小项目还是自己写查询语句好

作者: fkj   发布时间: 2010-07-16

哥永远崇拜手写SQL

作者: yafeikf   发布时间: 2010-07-16

回复 jiaoyin


   膜拜楼主,围观楼主,代问家豪童鞋好

作者: qxhy123   发布时间: 2010-07-16


哥永远崇拜手写SQL
yafeikf 发表于 2010-7-16 11:49


以上的写法,只是我个人爱好而已

谢谢WG哥的帖子..

作者: jiaoyin   发布时间: 2010-07-16

回复  jiaoyin


   膜拜楼主,围观楼主,代问家豪童鞋好
qxhy123 发表于 2010-7-16 12:19



    受宠若惊,竟然有超级斑竹认识哥,看来哥得换马甲了...嘎嘎

作者: jiaoyin   发布时间: 2010-07-16

回复 fkj


    不瞒您说,哥这也是模仿别人的写法,谢谢

作者: jiaoyin   发布时间: 2010-07-16

回复 jiaoyin


   换MJ也木有用滴

作者: qxhy123   发布时间: 2010-07-16

回复 qxhy123


   

作者: jiaoyin   发布时间: 2010-07-16

搞不懂为什么喜欢以哥自称。哎~~~~

作者: 寂寞流星   发布时间: 2010-07-16

回复 寂寞流星


    因为本来就是哥么...

作者: jiaoyin   发布时间: 2010-07-16

那也要低调点撒。

学学我,多低调。


作者: 寂寞流星   发布时间: 2010-07-16

回复 寂寞流星
哥就是因为太DD了 所也才要提高一点DD。

作者: jiaoyin   发布时间: 2010-07-17