PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/db/Db_Mysql.php

http://slightphp.googlecode.com/
PHP | 470 lines | 264 code | 17 blank | 189 comment | 46 complexity | b32f05675fa725b8d952383d95b56f4d MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*{{{LICENSE
  3. +-----------------------------------------------------------------------+
  4. | SlightPHP Framework |
  5. +-----------------------------------------------------------------------+
  6. | This program is free software; you can redistribute it and/or modify |
  7. | it under the terms of the GNU General Public License as published by |
  8. | the Free Software Foundation. You should have received a copy of the |
  9. | GNU General Public License along with this program. If not, see |
  10. | http://www.gnu.org/licenses/. |
  11. | Copyright (C) 2008-2009. All Rights Reserved. |
  12. +-----------------------------------------------------------------------+
  13. | Supports: http://www.slightphp.com |
  14. +-----------------------------------------------------------------------+
  15. }}}*/
  16. /**
  17. * @package SlightPHP
  18. * @subpackage SDb
  19. */
  20. class Db_Mysql extends DbObject{
  21. /**
  22. *
  23. */
  24. private $mysql;
  25. /**
  26. *
  27. */
  28. public $host;
  29. /**
  30. *
  31. */
  32. public $port=3306;
  33. /**
  34. *
  35. */
  36. public $user;
  37. /**
  38. *
  39. */
  40. public $password;
  41. /**
  42. *
  43. */
  44. public $database;
  45. /**
  46. *
  47. */
  48. public $charset;
  49. /**
  50. *
  51. */
  52. public $orderby;
  53. /**
  54. *
  55. */
  56. public $groupby;
  57. /**
  58. *
  59. */
  60. public $sql;
  61. /**
  62. *
  63. */
  64. public $count=true;
  65. /**
  66. *
  67. */
  68. public $limit=0;
  69. /**
  70. *
  71. */
  72. public $page=1;
  73. /**
  74. *
  75. */
  76. private $countsql;
  77. /**
  78. *
  79. */
  80. public $error=array('code'=>0,'msg'=>"");
  81. /**
  82. * @var array $globals
  83. */
  84. static $globals;
  85. function __construct(){
  86. }
  87. /**
  88. * construct
  89. *
  90. * @param string host
  91. * @param string user
  92. * @param string password
  93. * @param string database
  94. * @param int port=3306
  95. */
  96. function init($params=array()){
  97. foreach($params as $key=>$value){
  98. $this->$key = $value;
  99. }
  100. $this->key = "mysql:".$this->host.":".$this->user.":".$this->password.":".$this->database;
  101. if(!isset(Db_Mysql::$globals[$this->key])) Db_Mysql::$globals[$this->key] = "";
  102. }
  103. /**
  104. * is count
  105. *
  106. * @param boolean count
  107. */
  108. function setCount($count)
  109. {
  110. if($count==true){
  111. $this->count=true;
  112. }else{
  113. $this->count=false;
  114. }
  115. }
  116. /**
  117. * page number
  118. *
  119. * @param int page
  120. */
  121. function setPage($page)
  122. {
  123. if(!is_numeric($page) || $page<1){$page=1;}
  124. $this->page=$page;
  125. }
  126. /**
  127. * page size
  128. *
  129. * @param int limit ,0 is all
  130. */
  131. function setLimit($limit)
  132. {
  133. if(!is_numeric($limit) || $limit<0){$limit=0;}
  134. $this->limit=$limit;
  135. }
  136. /**
  137. * group by sql
  138. *
  139. * @param string groupby
  140. * eg: setGroupby("groupby MusicID");
  141. * setGroupby("groupby MusicID,MusicName");
  142. */
  143. function setGroupby($groupby)
  144. {
  145. $this->groupby=$groupby;
  146. }
  147. /**
  148. * order by sql
  149. *
  150. * @param string orderby
  151. * eg: setOrderby("order by MusicID Desc");
  152. */
  153. function setOrderby($orderby)
  154. {
  155. $this->orderby=$orderby;
  156. }
  157. /**
  158. * select data from db
  159. *
  160. * @param mixed $table
  161. * @param array $condition
  162. * @param array $item
  163. * @param string $groupby
  164. * @param string $orderby
  165. * @param string $leftjoin
  166. * @return DbData object
  167. */
  168. function select($table,$condition="",$item="*",$groupby="",$orderby="",$leftjoin=""){
  169. if($item==""){$item="*";}
  170. if(is_array($table)){
  171. for($i=0;$i<count($table);$i++)
  172. {
  173. $tmp[]=trim($table[$i]);
  174. }
  175. $table=@implode(" , ",$tmp);
  176. }else{
  177. $table=trim($table);
  178. }
  179. if(is_array($item)&&!empty($item)){
  180. $item ="`".@implode("` , `",$item)."`";
  181. }
  182. $condiStr = $this->__quote($condition);
  183. if($condiStr!=""){
  184. $condiStr=" WHERE ".$condiStr;
  185. }
  186. $join="";
  187. if(is_array($leftjoin)){
  188. foreach ($leftjoin as $key=>$value){
  189. $join.=" LEFT JOIN $key ON $value ";
  190. }
  191. }
  192. $this->groupby =$groupby!=""?$groupby:$this->groupby;
  193. $this->orderby =$orderby!=""?$orderby:$this->orderby;
  194. $orderby_sql="";
  195. $orderby_sql_tmp = array();
  196. if(is_array($orderby)){
  197. foreach($orderby as $key=>$value){
  198. if(!is_numeric($key)){
  199. $orderby_sql_tmp[]=$key." ".$value;
  200. }
  201. }
  202. }else{
  203. $orderby_sql=$this->orderby;
  204. }
  205. if(count($orderby_sql_tmp)>0){
  206. $orderby_sql=" ORDER BY ".implode(",",$orderby_sql_tmp);
  207. }
  208. $limit="";
  209. if($this->limit!=0){
  210. $limit =($this->page-1)*$this->limit;
  211. $limit ="LIMIT $limit,$this->limit";
  212. }
  213. $this->sql="SELECT $item FROM $table $join $condiStr $groupby $orderby_sql $limit";
  214. $this->countsql="SELECT count(1) totalSize FROM $table $join $condiStr $groupby";
  215. return $this->query($this->sql,$this->countsql);
  216. }
  217. /**
  218. *
  219. *
  220. * @param mixed $table
  221. * @param array $condition
  222. * @param array $item
  223. * @param string $groupby
  224. * @param string $orderby
  225. * @param string $leftjoin
  226. * @return array item
  227. */
  228. function selectOne($table,$condition="",$item="*",$groupby="",$orderby="",$leftjoin="")
  229. {
  230. $this->setLimit(1);
  231. $this->setCount(false);
  232. $data=$this->select($table,$condition,$item,$groupby,$orderby,$leftjoin);
  233. if(isset($data->items[0]))
  234. return $data->items[0];
  235. else return false;
  236. }
  237. /**
  238. * update data
  239. *
  240. * @param mixed $table
  241. * @param string,array $condition
  242. * @param array $item
  243. * @param int $limit
  244. * @return int
  245. * update("table",array('name'=>'myName','password'=>'myPass'),array('id'=>1));
  246. * update("table",array('name'=>'myName','password'=>'myPass'),array("password=$myPass"));
  247. */
  248. function update($table,$condition="",$item=""){
  249. $value = $this->__quote($item,",");
  250. $condiStr = $this->__quote($condition);
  251. if($condiStr!=""){
  252. $condiStr=" WHERE ".$condiStr;
  253. }
  254. $this->sql="UPDATE $table SET $value $condiStr";
  255. $this->execute($this->sql);
  256. return $this->rowCount();
  257. }
  258. /**
  259. * delete
  260. *
  261. * @param mixed table
  262. * @param string,array $condition
  263. * @param int $limit
  264. * @return int
  265. * delete("table",array('name'=>'myName','password'=>'myPass'),array('id'=>1));
  266. * delete("table",array('name'=>'myName','password'=>'myPass'),array("password=$myPass"));
  267. */
  268. function delete($table,$condition=""){
  269. $condiStr = $this->__quote($condition);
  270. if($condiStr!=""){
  271. $condiStr=" WHERE ".$condiStr;
  272. }
  273. $this->sql="DELETE FROM $table $condiStr";
  274. $this->execute($this->sql);
  275. return $this->rowCount();
  276. }
  277. /**
  278. * insert
  279. *
  280. * @param $table
  281. * @param array $item
  282. * @param array $update ,egarray("key"=>value,"key2"=>value2")
  283. * this param may be BUG
  284. insert into zone_user_online values(2,'','','','',now(),now()) on duplicate key update onlineactivetime=CURRENT_TIMESTAMP;
  285. * @return int InsertID
  286. */
  287. function insert($table,$item="",$isreplace=false,$isdelayed=false,$update=array())
  288. {
  289. if($isreplace==true){
  290. $command="REPLACE";
  291. }else{
  292. $command="INSERT";
  293. }
  294. if($isdelayed==true){
  295. $command.=" DELAYED ";
  296. }
  297. $f = $this->__quote($item,",");
  298. $this->sql="$command INTO $table SET $f ";
  299. $v = $this->__quote($update);
  300. if(!empty($v)){
  301. $this->sql.="ON DUPLICATE KEY UPDATE $v";
  302. }
  303. $r=$this->execute($this->sql);
  304. if($r!==false){
  305. if($this->lastInsertId ()>0){
  306. return $this->lastInsertId ();
  307. }
  308. }
  309. return $r;
  310. }
  311. /**
  312. * query
  313. *
  314. * @param string $sql
  315. * @return DbData object
  316. */
  317. function query($sql,$countsql="")
  318. {
  319. $data = new DbData;
  320. $data->limit = $this->limit;
  321. $start = microtime(true);
  322. $result = $this->execute($sql);
  323. $end = microtime(true);
  324. $data->totalSecond = $end-$start;
  325. if($result){
  326. while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
  327. $tmp = array();
  328. foreach($row as $key=>$value){
  329. $tmp[$key]=($value);
  330. }
  331. $data->items[]=$tmp;
  332. $data->pageSize++;
  333. }
  334. }
  335. if($this->limit !=0 and $this->count==true and $countsql!=""){
  336. $result = $this->execute($countsql);
  337. if($result){
  338. $row = mysql_fetch_array($result,MYSQL_NUM );
  339. $data->totalSize = $row[0];
  340. }
  341. $data->totalPage = ceil($data->totalSize/$data->limit);
  342. }
  343. //{{{reset
  344. $this->setPage(1);
  345. $this->setLimit(0);
  346. $this->setCount(false);
  347. $this->setGroupby("");
  348. $this->setOrderby("");
  349. //}}}
  350. return $data;
  351. }
  352. function lastInsertId(){
  353. return mysql_insert_id(Db_Mysql::$globals[$this->key]);
  354. }
  355. function rowCount(){
  356. return mysql_affected_rows(Db_Mysql::$globals[$this->key]);
  357. }
  358. function __connect($forceReconnect=false){
  359. if(empty(Db_Mysql::$globals[$this->key]) || $forceReconnect){
  360. if(!empty(Db_Mysql::$globals[$this->key])){
  361. mysql_close(Db_Mysql::$globals[$this->key]);
  362. unset(Db_Mysql::$globals[$this->key]);
  363. }
  364. Db_Mysql::$globals[$this->key] = mysql_connect($this->host.":".$this->port,$this->user,$this->password,true);
  365. }
  366. if(!Db_Mysql::$globals[$this->key]){
  367. if(defined("DEBUG")){
  368. die("connect database error:\n".var_export($this,true));
  369. }else{
  370. die("connect database error:");
  371. }
  372. }
  373. if($this->database!=""){
  374. mysql_select_db($this->database,Db_Mysql::$globals[$this->key]);
  375. if(!empty($this->charset)){
  376. mysql_query("SET NAMES ".$this->charset);
  377. }
  378. }
  379. }
  380. function execute($sql){
  381. if(empty(Db_Mysql::$globals[$this->key])){
  382. $this->__connect($forceReconnect=true);
  383. }
  384. if(defined("DEBUG")){
  385. echo "SQL:$sql\n";
  386. }
  387. $result = mysql_query($sql,Db_Mysql::$globals[$this->key]);
  388. if(!$result){
  389. $this->error['code']=mysql_errno();
  390. $this->error['msg']=mysql_error();
  391. return false;
  392. }else{
  393. return $result;
  394. }
  395. }
  396. private function __quote($condition,$split="AND"){
  397. $condiStr = "";
  398. if(is_array($condition) || is_object($condition)){
  399. $v1=array();
  400. $i=1;
  401. foreach($condition as $k=>$v){
  402. if(!is_numeric($k)){
  403. if(strpos($k,".")===false){
  404. $k = $this->__addsqlslashes($k);
  405. }
  406. $v = addslashes($v);
  407. $v1[]="$k = \"$v\"";
  408. }else{
  409. $v1[]=($v);
  410. }
  411. }
  412. if(count($v1)>0){
  413. $condiStr=implode(" ".$split." ",$v1);
  414. }
  415. }else{
  416. $condiStr=$condition;
  417. }
  418. return $condiStr;
  419. }
  420. private function __addsqlslashes($k){
  421. if(strpos($k,"(")!==false || strpos($k,")")!==false || strpos($k,".")!==false){
  422. return $k;
  423. }else{
  424. return "`$k`";
  425. }
  426. }
  427. /*
  428. function __quote($condition,$split="AND"){
  429. $condiStr = "";
  430. if(is_array($condition)){
  431. $v1=array();
  432. foreach($condition as $k=>$v)
  433. {
  434. if(!is_numeric($k))
  435. {
  436. $v1[]="`".$k."`"." = '".mysql_escape_string($v)."'";
  437. }else{
  438. $v1[]=($v);
  439. }
  440. }
  441. if(count($v1)>0)
  442. {
  443. $condiStr=implode(" ".$split." ",$v1);
  444. }
  445. }else{
  446. $condiStr=$condition;
  447. }
  448. return $condiStr;
  449. }
  450. */
  451. }
  452. ?>