DB.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. $DBCONF = "";
  3. /**
  4. * Db extension
  5. */
  6. class DB {
  7. private $COND = ""; // 条件
  8. private $SQL = ""; // SQL语句
  9. private $TABLE_NAME = ""; // 表名称
  10. private $DBH = ""; // 数据库驱动
  11. private $S = ""; // 字段筛选
  12. private $LIMIT = ""; // 数据条数筛选
  13. private $GROUP = ""; // 数据分组
  14. private $SORT = ""; // 数据排序
  15. /**
  16. * DB Extension 构造方法
  17. * @param string $M [数据表]
  18. */
  19. function __construct($M = "") {
  20. $this->TABLE_NAME = $M;
  21. $DBConf = require "Config/DBConfig.php";
  22. global $DBCONF;
  23. $DBCONF = $DBConf;
  24. $DBDriver = strtoupper($DBCONF["driver"]) . "Driver";
  25. require_once "Library/DataBaseExtension/{$DBDriver}.php";
  26. $Driver = new $DBDriver();
  27. $this->DBH = $Driver->DBInit();
  28. }
  29. /**
  30. * 获取SQL
  31. * @param [type] $A [description]
  32. * @return [type] [description]
  33. */
  34. public function getSQL() {
  35. return $this->SQL;
  36. }
  37. /**
  38. * 设置表名
  39. * @param [String] $table 表名
  40. */
  41. public function setTable($table) {
  42. $this->TABLE_NAME = $table;
  43. return $this;
  44. }
  45. /**
  46. * 数据查询
  47. * @return [Array] [返回数据结果]
  48. */
  49. public function get() {
  50. if ($this->S == "") {
  51. $this->S = " * ";
  52. }
  53. $this->SQL = "SELECT $this->S FROM $this->TABLE_NAME $this->COND $this->GROUP $this->SORT $this->LIMIT";
  54. $DB = $this->DBH;
  55. $DBI = $DB->prepare($this->SQL);
  56. $DBI->execute();
  57. return $DBI->fetchAll(PDO::FETCH_ASSOC);
  58. }
  59. /**
  60. * 查询一条数据
  61. * @return Sting 返回数据结果
  62. */
  63. public function find() {
  64. if ($this->S == "") {
  65. $this->S = " * ";
  66. }
  67. $this->SQL = "SELECT $this->S FROM $this->TABLE_NAME $this->COND $this->GROUP $this->SORT $this->LIMIT";
  68. $DB = $this->DBH;
  69. $DBI = $DB->prepare($this->SQL);
  70. $DBI->execute();
  71. return $DBI->fetch(PDO::FETCH_ASSOC);
  72. }
  73. /**
  74. * 增加一条数据
  75. * @param array $A 需要添加的数据
  76. * @return int [返回受影响的行数]
  77. */
  78. public function insert($A = "") {
  79. $A_value = "";
  80. if (is_array($A)) {
  81. $this->S = "";
  82. foreach ($A as $K => $V) {
  83. if ($this->S == "") {
  84. $this->S = $K;
  85. $A_value = "'{$V}'";
  86. } else {
  87. $this->S .= ",{$K}";
  88. $A_value .= ",'{$V}'";
  89. }
  90. }
  91. } else {
  92. echo "Wanning:: Data not is array";
  93. }
  94. $this->SQL = "INSERT INTO $this->TABLE_NAME ($this->S) VALUES($A_value)";
  95. $DB = $this->DBH;
  96. $DBI = $DB->prepare($this->SQL);
  97. $DBI->execute();
  98. return $DBI->rowCount();
  99. }
  100. /**
  101. * 数据更新
  102. * @param array $A [需要修改的数据]
  103. * @return [int] [返回受影响的行数 ]
  104. */
  105. public function update($A = "") {
  106. if ($this->COND == "") {
  107. echo "Wanning:: Condition is null";
  108. }
  109. $this->S = "";
  110. if (is_array($A)) {
  111. foreach ($A as $K => $V) {
  112. if ($this->S == "") {
  113. $this->S = "$K = '$V'";
  114. } else {
  115. $this->S .= ",$K = '$V'";
  116. }
  117. }
  118. }
  119. $this->SQL = "UPDATE $this->TABLE_NAME SET $this->S $this->COND";
  120. $DB = $this->DBH;
  121. $DBI = $DB->prepare($this->SQL);
  122. $DBI->execute();
  123. return $DBI->rowCount();
  124. }
  125. /**
  126. * 删除数据
  127. * @return int 返回受影响的行数
  128. */
  129. public function delete() {
  130. if ($this->COND == "") {
  131. echo "Wanning:: condtion is null";
  132. }
  133. $this->SQL = "DELETE FROM $this->TABLE_NAME $this->COND";
  134. $DB = $this->DBH;
  135. $DBI = $DB->prepare($this->SQL);
  136. $DBI->execute();
  137. return $DBI->rowCount();
  138. }
  139. /**
  140. * 获取数据条数
  141. * @return int 返回数据条数
  142. */
  143. public function count() {
  144. $this->SQL = "SELECT COUNT(*) FROM $this->TABLE_NAME $this->COND $this->GROUP $this->SORT";
  145. $DB = $this->DBH;
  146. $DBI = $DB->prepare($this->SQL);
  147. $DBI->execute();
  148. $res = $DBI->fetch();
  149. return $res[0];
  150. }
  151. /**
  152. * 数据排序
  153. * @param string $A 字段
  154. * @param string $B 排序规则
  155. */
  156. public function sort($A, $B) {
  157. $this->SORT = "ORDER BY $A $B";
  158. return $this;
  159. }
  160. /**
  161. * 条件生成
  162. * @param string $A 字段2
  163. * @param string $B 参数为2时为值 参数为3时为判断符
  164. * @param string $C 值
  165. */
  166. public function where($A, $B, $C = "") {
  167. $this->COND = "";
  168. if ($C == "") {
  169. $this->COND = "WHERE $A = '$B'";
  170. } else {
  171. $this->COND = "WHERE $A $B '$C'";
  172. }
  173. return $this;
  174. }
  175. /**
  176. * 条件生成
  177. * @param array $ArrayData 条件 数组
  178. */
  179. public function AW($ArrayData) {
  180. foreach ($ArrayData as $K => $V) {
  181. if ($this->COND == "") {
  182. $this->COND = "WHERE $K = '$V'";
  183. } else {
  184. $this->COND .= " AND $k = '$V'";
  185. }
  186. }
  187. return $this;
  188. }
  189. /**
  190. * 条件生成
  191. * @param string $A 字段2
  192. * @param string $B 参数为2时为值 参数为3时为判断符
  193. * @param string $C 值
  194. */
  195. public function SW($A, $B, $C = "") {
  196. if ($C == "") {
  197. if ($this->COND == "") {
  198. $this->COND = "WHERE $A = '$B'";
  199. } else {
  200. $this->COND .= " AND $A = '$B'";
  201. }
  202. } else {
  203. if ($this->COND == "") {
  204. $this->COND = "WHERE $A $B '$C'";
  205. } else {
  206. $this->COND .= " AND $A $B '$C'";
  207. }
  208. }
  209. return $this;
  210. }
  211. /**
  212. * 选择列
  213. * @param string $sel 字段名称 格式为 a,b,c
  214. * @return [type] [description]
  215. */
  216. public function select($sel) {
  217. $this->S = "";
  218. $this->S = " " . $sel . " ";
  219. return $this;
  220. }
  221. /**
  222. * 数据选择
  223. * @param int $A 开始
  224. * @param int $B 条数
  225. */
  226. public function limit($A, $B) {
  227. $this->LIMIT = "OFFSET $A limit $B";
  228. return $this;
  229. }
  230. /**
  231. * 分组
  232. * @param string $A 字段
  233. */
  234. public function group($A) {
  235. $this->GROUP = "GROUP BY $A";
  236. return $this;
  237. }
  238. public function join($sql) {
  239. $this->SQL = "{$sql}";
  240. $DB = $this->DBH;
  241. $DBI = $DB->prepare($this->SQL, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  242. $DBI->execute();
  243. return $DBI->fetchAll(PDO::FETCH_ASSOC);
  244. }
  245. public function exec($sql) {
  246. $this->SQL = "{$sql}";
  247. $DB = $this->DBH;
  248. return $DB->query($this->SQL);
  249. }
  250. }