PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/nova/core/database/mysql.class.php

http://xklog.googlecode.com/
PHP | 393 lines | 195 code | 18 blank | 180 comment | 41 complexity | 4ce5555f11ff0bed6c59dc4415b265ac MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. class Mysql extends Database {
  3. /**
  4. +----------------------------------------------------------
  5. * ???? ?????????
  6. +----------------------------------------------------------
  7. * @access public
  8. +----------------------------------------------------------
  9. * @param array $config ???????
  10. +----------------------------------------------------------
  11. */
  12. public function __construct($config=''){
  13. if ( !extension_loaded('mysql') ) {
  14. throw_exception(L('_NOT_SUPPERT_').':mysql');
  15. }
  16. if(!empty($config)) {
  17. $this->config = $config;
  18. }
  19. }
  20. /**
  21. +----------------------------------------------------------
  22. * ???????
  23. +----------------------------------------------------------
  24. * @access public
  25. +----------------------------------------------------------
  26. * @throws ThinkExecption
  27. +----------------------------------------------------------
  28. */
  29. public function connect($config='',$linkNum=0) {
  30. if ( !isset($this->linkID[$linkNum]) ) {
  31. if(empty($config)) $config = $this->config;
  32. // ????????socket????
  33. $host = $config['hostname'].($config['hostport']?":{$config['hostport']}":'');
  34. if($this->pconnect) {
  35. $this->linkID[$linkNum] = mysql_pconnect( $host, $config['username'], $config['password'],CLIENT_MULTI_RESULTS);
  36. }else{
  37. $this->linkID[$linkNum] = mysql_connect( $host, $config['username'], $config['password'],true,CLIENT_MULTI_RESULTS);
  38. }
  39. if ( !$this->linkID[$linkNum] || (!empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) ) {
  40. throw_exception(mysql_error());
  41. }
  42. $dbVersion = mysql_get_server_info($this->linkID[$linkNum]);
  43. if ($dbVersion >= "4.1") {
  44. //??UTF8????? ??mysql 4.1.0????
  45. mysql_query("SET NAMES '".C('DB_CHARSET')."'", $this->linkID[$linkNum]);
  46. }
  47. //?? sql_model
  48. if($dbVersion >'5.0.1'){
  49. mysql_query("SET sql_mode=''",$this->linkID[$linkNum]);
  50. }
  51. // ??????
  52. $this->connected = true;
  53. // ???????????
  54. if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
  55. }
  56. return $this->linkID[$linkNum];
  57. }
  58. /**
  59. +----------------------------------------------------------
  60. * ??????
  61. +----------------------------------------------------------
  62. * @access public
  63. +----------------------------------------------------------
  64. */
  65. public function free() {
  66. @mysql_free_result($this->queryID);
  67. $this->queryID = 0;
  68. }
  69. /**
  70. +----------------------------------------------------------
  71. * ???? ?????
  72. +----------------------------------------------------------
  73. * @access public
  74. +----------------------------------------------------------
  75. * @param string $str sql??
  76. +----------------------------------------------------------
  77. * @return mixed
  78. +----------------------------------------------------------
  79. * @throws ThinkExecption
  80. +----------------------------------------------------------
  81. */
  82. public function query($str) {
  83. $this->initConnect(false);
  84. if ( !$this->_linkID ) return false;
  85. $this->queryStr = $str;
  86. //?????????
  87. if ( $this->queryID ) { $this->free(); }
  88. $this->Q(1);
  89. $this->queryID = mysql_query($str, $this->_linkID);
  90. $this->debug();
  91. if ( false === $this->queryID ) {
  92. $this->error();
  93. return false;
  94. } else {
  95. $this->numRows = mysql_num_rows($this->queryID);
  96. return $this->getAll();
  97. }
  98. }
  99. /**
  100. +----------------------------------------------------------
  101. * ????
  102. +----------------------------------------------------------
  103. * @access public
  104. +----------------------------------------------------------
  105. * @param string $str sql??
  106. +----------------------------------------------------------
  107. * @return integer
  108. +----------------------------------------------------------
  109. * @throws ThinkExecption
  110. +----------------------------------------------------------
  111. */
  112. public function execute($str) {
  113. $this->initConnect(true);
  114. if ( !$this->_linkID ) return false;
  115. $this->queryStr = $str;
  116. //?????????
  117. if ( $this->queryID ) { $this->free(); }
  118. $this->W(1);
  119. $result = mysql_query($str, $this->_linkID) ;
  120. $this->debug();
  121. if ( false === $result) {
  122. $this->error();
  123. return false;
  124. } else {
  125. $this->numRows = mysql_affected_rows($this->_linkID);
  126. $this->lastInsID = mysql_insert_id($this->_linkID);
  127. return $this->numRows;
  128. }
  129. }
  130. /**
  131. +----------------------------------------------------------
  132. * ????
  133. +----------------------------------------------------------
  134. * @access public
  135. +----------------------------------------------------------
  136. * @return void
  137. +----------------------------------------------------------
  138. * @throws ThinkExecption
  139. +----------------------------------------------------------
  140. */
  141. public function startTrans() {
  142. $this->initConnect(true);
  143. if ( !$this->_linkID ) return false;
  144. //??rollback ??
  145. if ($this->transTimes == 0) {
  146. mysql_query('START TRANSACTION', $this->_linkID);
  147. }
  148. $this->transTimes++;
  149. return ;
  150. }
  151. /**
  152. +----------------------------------------------------------
  153. * ????????????????
  154. +----------------------------------------------------------
  155. * @access public
  156. +----------------------------------------------------------
  157. * @return boolen
  158. +----------------------------------------------------------
  159. * @throws ThinkExecption
  160. +----------------------------------------------------------
  161. */
  162. public function commit()
  163. {
  164. if ($this->transTimes > 0) {
  165. $result = mysql_query('COMMIT', $this->_linkID);
  166. $this->transTimes = 0;
  167. if(!$result){
  168. throw_exception($this->error());
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. +----------------------------------------------------------
  175. * ????
  176. +----------------------------------------------------------
  177. * @access public
  178. +----------------------------------------------------------
  179. * @return boolen
  180. +----------------------------------------------------------
  181. * @throws ThinkExecption
  182. +----------------------------------------------------------
  183. */
  184. public function rollback()
  185. {
  186. if ($this->transTimes > 0) {
  187. $result = mysql_query('ROLLBACK', $this->_linkID);
  188. $this->transTimes = 0;
  189. if(!$result){
  190. throw_exception($this->error());
  191. }
  192. }
  193. return true;
  194. }
  195. /**
  196. +----------------------------------------------------------
  197. * ?????????
  198. +----------------------------------------------------------
  199. * @access private
  200. +----------------------------------------------------------
  201. * @return array
  202. +----------------------------------------------------------
  203. * @throws ThinkExecption
  204. +----------------------------------------------------------
  205. */
  206. private function getAll() {
  207. //?????
  208. $result = array();
  209. if($this->numRows >0) {
  210. while($row = mysql_fetch_assoc($this->queryID)){
  211. $result[] = $row;
  212. }
  213. mysql_data_seek($this->queryID,0);
  214. }
  215. return $result;
  216. }
  217. /**
  218. +----------------------------------------------------------
  219. * ??????????
  220. +----------------------------------------------------------
  221. * @access public
  222. +----------------------------------------------------------
  223. */
  224. public function getFields($tableName) {
  225. $result = $this->query('SHOW COLUMNS FROM '.$tableName);
  226. $info = array();
  227. if($result) {
  228. foreach ($result as $key => $val) {
  229. $info[$val['Field']] = array(
  230. 'name' => $val['Field'],
  231. 'type' => $val['Type'],
  232. 'notnull' => (bool) ($val['Null'] === ''), // not null is empty, null is yes
  233. 'default' => $val['Default'],
  234. 'primary' => (strtolower($val['Key']) == 'pri'),
  235. 'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
  236. );
  237. }
  238. }
  239. return $info;
  240. }
  241. /**
  242. +----------------------------------------------------------
  243. * ?????????
  244. +----------------------------------------------------------
  245. * @access public
  246. +----------------------------------------------------------
  247. */
  248. public function getTables($dbName='') {
  249. if(!empty($dbName)) {
  250. $sql = 'SHOW TABLES FROM '.$dbName;
  251. }else{
  252. $sql = 'SHOW TABLES ';
  253. }
  254. $result = $this->query($sql);
  255. $info = array();
  256. foreach ($result as $key => $val) {
  257. $info[$key] = current($val);
  258. }
  259. return $info;
  260. }
  261. /**
  262. +----------------------------------------------------------
  263. * ????
  264. +----------------------------------------------------------
  265. * @access public
  266. +----------------------------------------------------------
  267. * @param mixed $data ??
  268. * @param array $options ?????
  269. +----------------------------------------------------------
  270. * @return false | integer
  271. +----------------------------------------------------------
  272. */
  273. public function replace($data,$options=array()) {
  274. foreach ($data as $key=>$val){
  275. $value = $this->parseValue($val);
  276. if(is_scalar($value)) { // ???????
  277. $values[] = $value;
  278. $fields[] = $this->addSpecialChar($key);
  279. }
  280. }
  281. $sql = 'REPLACE INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')';
  282. return $this->execute($sql);
  283. }
  284. /**
  285. +----------------------------------------------------------
  286. * ????
  287. +----------------------------------------------------------
  288. * @access public
  289. +----------------------------------------------------------
  290. * @param mixed $datas ??
  291. * @param array $options ?????
  292. +----------------------------------------------------------
  293. * @return false | integer
  294. +----------------------------------------------------------
  295. */
  296. public function insertAll($datas,$options=array()) {
  297. if(!is_array($datas[0])) return false;
  298. $fields = array_keys($datas[0]);
  299. array_walk($fields, array($this, 'addSpecialChar'));
  300. $values = array();
  301. foreach ($datas as $data){
  302. $value = array();
  303. foreach ($data as $key=>$val){
  304. $val = $this->parseValue($val);
  305. if(is_scalar($val)) { // ???????
  306. $value[] = $val;
  307. }
  308. }
  309. $values[] = '('.implode(',', $value).')';
  310. }
  311. $sql = 'INSERT INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values);
  312. return $this->execute($sql);
  313. }
  314. /**
  315. +----------------------------------------------------------
  316. * ?????
  317. +----------------------------------------------------------
  318. * @access public
  319. +----------------------------------------------------------
  320. * @throws ThinkExecption
  321. +----------------------------------------------------------
  322. */
  323. public function close() {
  324. if (!empty($this->queryID))
  325. mysql_free_result($this->queryID);
  326. if ($this->_linkID && !mysql_close($this->_linkID)){
  327. throw_exception($this->error());
  328. }
  329. $this->_linkID = 0;
  330. }
  331. /**
  332. +----------------------------------------------------------
  333. * ???????
  334. * ??????SQL??
  335. +----------------------------------------------------------
  336. * @access public
  337. +----------------------------------------------------------
  338. * @return string
  339. +----------------------------------------------------------
  340. */
  341. public function error() {
  342. $this->error = mysql_error($this->_linkID);
  343. if($this->debug && '' != $this->queryStr){
  344. $this->error .= "\n [ SQL?? ] : ".$this->queryStr;
  345. }
  346. return $this->error;
  347. }
  348. /**
  349. +----------------------------------------------------------
  350. * SQL??????
  351. +----------------------------------------------------------
  352. * @access public
  353. +----------------------------------------------------------
  354. * @param string $str SQL???
  355. +----------------------------------------------------------
  356. * @return string
  357. +----------------------------------------------------------
  358. */
  359. public function escape_string($str) {
  360. return mysql_escape_string($str);
  361. }
  362. /**
  363. +----------------------------------------------------------
  364. * ????
  365. +----------------------------------------------------------
  366. * @access public
  367. +----------------------------------------------------------
  368. */
  369. public function __destruct()
  370. {
  371. // ????
  372. $this->close();
  373. }
  374. }//?????
  375. ?>