PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Extend/Driver/Db/DbSqlsrv.class.php

http://thinkphp.googlecode.com/
PHP | 359 lines | 166 code | 17 blank | 176 comment | 30 complexity | 518ca263b453796edce4d6f4be157ce2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: DbSqlsrv.class.php 2924 2012-04-20 10:09:32Z liu21st@gmail.com $
  12. /**
  13. +-------------------------------
  14. * Sqlsrv??????
  15. +-------------------------------
  16. */
  17. class DbSqlsrv extends Db{
  18. protected $selectSql = 'SELECT T1.* FROM (SELECT ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER, thinkphp.* FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 WHERE %LIMIT%';
  19. /**
  20. +----------------------------------------------------------
  21. * ???? ?????????
  22. +----------------------------------------------------------
  23. * @access public
  24. +----------------------------------------------------------
  25. * @param array $config ???????
  26. +----------------------------------------------------------
  27. */
  28. public function __construct($config='') {
  29. if ( !function_exists('sqlsrv_connect') ) {
  30. throw_exception(L('_NOT_SUPPERT_').':sqlsrv');
  31. }
  32. if(!empty($config)) {
  33. $this->config = $config;
  34. }
  35. }
  36. /**
  37. +----------------------------------------------------------
  38. * ???????
  39. +----------------------------------------------------------
  40. * @access public
  41. +----------------------------------------------------------
  42. * @throws ThinkExecption
  43. +----------------------------------------------------------
  44. */
  45. public function connect($config='',$linkNum=0) {
  46. if ( !isset($this->linkID[$linkNum]) ) {
  47. if(empty($config)) $config = $this->config;
  48. $host = $config['hostname'].($config['hostport']?",{$config['hostport']}":'');
  49. $connectInfo = array('Database'=>$config['database'],'UID'=>$config['username'],'PWD'=>$config['password'],'CharacterSet' => C('DEFAULT_CHARSET'));
  50. $this->linkID[$linkNum] = sqlsrv_connect( $host, $connectInfo);
  51. if ( !$this->linkID[$linkNum] ) throw_exception($this->error());
  52. // ??????
  53. $this->connected = true;
  54. //?????????
  55. if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
  56. }
  57. return $this->linkID[$linkNum];
  58. }
  59. /**
  60. +----------------------------------------------------------
  61. * ??????
  62. +----------------------------------------------------------
  63. * @access public
  64. +----------------------------------------------------------
  65. */
  66. public function free() {
  67. sqlsrv_free_stmt($this->queryID);
  68. $this->queryID = null;
  69. }
  70. /**
  71. +----------------------------------------------------------
  72. * ???? ?????
  73. +----------------------------------------------------------
  74. * @access public
  75. +----------------------------------------------------------
  76. * @param string $str sql??
  77. +----------------------------------------------------------
  78. * @return mixed
  79. +----------------------------------------------------------
  80. * @throws ThinkExecption
  81. +----------------------------------------------------------
  82. */
  83. public function query($str) {
  84. $this->initConnect(false);
  85. if ( !$this->_linkID ) return false;
  86. $this->queryStr = $str;
  87. //?????????
  88. if ( $this->queryID ) $this->free();
  89. N('db_query',1);
  90. // ????????
  91. G('queryStartTime');
  92. $this->queryID = sqlsrv_query($this->_linkID,$str,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
  93. $this->debug();
  94. if ( false === $this->queryID ) {
  95. $this->error();
  96. return false;
  97. } else {
  98. $this->numRows = sqlsrv_num_rows($this->queryID);
  99. return $this->getAll();
  100. }
  101. }
  102. /**
  103. +----------------------------------------------------------
  104. * ????
  105. +----------------------------------------------------------
  106. * @access public
  107. +----------------------------------------------------------
  108. * @param string $str sql??
  109. +----------------------------------------------------------
  110. * @return integer
  111. +----------------------------------------------------------
  112. * @throws ThinkExecption
  113. +----------------------------------------------------------
  114. */
  115. public function execute($str) {
  116. $this->initConnect(true);
  117. if ( !$this->_linkID ) return false;
  118. $this->queryStr = $str;
  119. //?????????
  120. if ( $this->queryID ) $this->free();
  121. N('db_write',1);
  122. // ????????
  123. G('queryStartTime');
  124. $this->queryID= sqlsrv_query($this->_linkID,$str,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
  125. $this->debug();
  126. if ( false === $this->queryID ) {
  127. $this->error();
  128. return false;
  129. } else {
  130. $this->numRows = sqlsrv_rows_affected($this->queryID);
  131. $this->lastInsID = $this->mssql_insert_id();
  132. return $this->numRows;
  133. }
  134. }
  135. /**
  136. +----------------------------------------------------------
  137. * ?????????ID
  138. +----------------------------------------------------------
  139. * @access public
  140. +----------------------------------------------------------
  141. * @return integer
  142. +----------------------------------------------------------
  143. */
  144. public function mssql_insert_id() {
  145. $query = "SELECT @@IDENTITY as last_insert_id";
  146. $result = sqlsrv_query($this->_linkID,$query);
  147. list($last_insert_id) = sqlsrv_fetch_array($result);
  148. sqlsrv_free_stmt($result);
  149. return $last_insert_id;
  150. }
  151. /**
  152. +----------------------------------------------------------
  153. * ????
  154. +----------------------------------------------------------
  155. * @access public
  156. +----------------------------------------------------------
  157. * @return void
  158. +----------------------------------------------------------
  159. */
  160. public function startTrans() {
  161. $this->initConnect(true);
  162. if ( !$this->_linkID ) return false;
  163. //??rollback ??
  164. if ($this->transTimes == 0) {
  165. sqlsrv_begin_transaction($this->_linkID);
  166. }
  167. $this->transTimes++;
  168. return ;
  169. }
  170. /**
  171. +----------------------------------------------------------
  172. * ????????????????
  173. +----------------------------------------------------------
  174. * @access public
  175. +----------------------------------------------------------
  176. * @return boolen
  177. +----------------------------------------------------------
  178. */
  179. public function commit() {
  180. if ($this->transTimes > 0) {
  181. $result = sqlsrv_commit($this->_linkID);
  182. $this->transTimes = 0;
  183. if(!$result){
  184. throw_exception($this->error());
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. +----------------------------------------------------------
  191. * ????
  192. +----------------------------------------------------------
  193. * @access public
  194. +----------------------------------------------------------
  195. * @return boolen
  196. +----------------------------------------------------------
  197. */
  198. public function rollback() {
  199. if ($this->transTimes > 0) {
  200. $result = sqlsrv_rollback($this->_linkID);
  201. $this->transTimes = 0;
  202. if(!$result){
  203. throw_exception($this->error());
  204. }
  205. }
  206. return true;
  207. }
  208. /**
  209. +----------------------------------------------------------
  210. * ?????????
  211. +----------------------------------------------------------
  212. * @access private
  213. +----------------------------------------------------------
  214. * @return array
  215. +----------------------------------------------------------
  216. * @throws ThinkExecption
  217. +----------------------------------------------------------
  218. */
  219. private function getAll() {
  220. //?????
  221. $result = array();
  222. if($this->numRows >0) {
  223. while($row = sqlsrv_fetch_array($this->queryID,SQLSRV_FETCH_ASSOC))
  224. $result[] = $row;
  225. }
  226. return $result;
  227. }
  228. /**
  229. +----------------------------------------------------------
  230. * ??????????
  231. +----------------------------------------------------------
  232. * @access public
  233. +----------------------------------------------------------
  234. * @return array
  235. +----------------------------------------------------------
  236. */
  237. public function getFields($tableName) {
  238. $result = $this->query("SELECT column_name, data_type, column_default, is_nullable
  239. FROM information_schema.tables AS t
  240. JOIN information_schema.columns AS c
  241. ON t.table_catalog = c.table_catalog
  242. AND t.table_schema = c.table_schema
  243. AND t.table_name = c.table_name
  244. WHERE t.table_name = '$tableName'");
  245. $info = array();
  246. if($result) {
  247. foreach ($result as $key => $val) {
  248. $info[$val['column_name']] = array(
  249. 'name' => $val['column_name'],
  250. 'type' => $val['data_type'],
  251. 'notnull' => (bool) ($val['is_nullable'] === ''), // not null is empty, null is yes
  252. 'default' => $val['column_default'],
  253. 'primary' => false,
  254. 'autoinc' => false,
  255. );
  256. }
  257. }
  258. return $info;
  259. }
  260. /**
  261. +----------------------------------------------------------
  262. * ??????????
  263. +----------------------------------------------------------
  264. * @access public
  265. +----------------------------------------------------------
  266. * @return array
  267. +----------------------------------------------------------
  268. */
  269. public function getTables($dbName='') {
  270. $result = $this->query("SELECT TABLE_NAME
  271. FROM INFORMATION_SCHEMA.TABLES
  272. WHERE TABLE_TYPE = 'BASE TABLE'
  273. ");
  274. $info = array();
  275. foreach ($result as $key => $val) {
  276. $info[$key] = current($val);
  277. }
  278. return $info;
  279. }
  280. /**
  281. +----------------------------------------------------------
  282. * order??
  283. +----------------------------------------------------------
  284. * @access protected
  285. +----------------------------------------------------------
  286. * @param mixed $order
  287. +----------------------------------------------------------
  288. * @return string
  289. +----------------------------------------------------------
  290. */
  291. protected function parseOrder($order) {
  292. return !empty($order)? ' ORDER BY '.$order:' ORDER BY rand()';
  293. }
  294. /**
  295. +----------------------------------------------------------
  296. * limit
  297. +----------------------------------------------------------
  298. * @access public
  299. +----------------------------------------------------------
  300. * @return string
  301. +----------------------------------------------------------
  302. */
  303. public function parseLimit($limit) {
  304. if(empty($limit)) $limit=1;
  305. $limit = explode(',',$limit);
  306. if(count($limit)>1)
  307. $limitStr = '(T1.ROW_NUMBER BETWEEN '.$limit[0].' + 1 AND '.$limit[0].' + '.$limit[1].')';
  308. else
  309. $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND '.$limit[0].")";
  310. return $limitStr;
  311. }
  312. /**
  313. +----------------------------------------------------------
  314. * ?????
  315. +----------------------------------------------------------
  316. * @access public
  317. +----------------------------------------------------------
  318. */
  319. public function close() {
  320. if ($this->_linkID){
  321. sqlsrv_close($this->_linkID);
  322. }
  323. $this->_linkID = null;
  324. }
  325. /**
  326. +----------------------------------------------------------
  327. * ???????
  328. * ??????SQL??
  329. +----------------------------------------------------------
  330. * @access public
  331. +----------------------------------------------------------
  332. * @return string
  333. +----------------------------------------------------------
  334. */
  335. public function error() {
  336. $this->error = sqlsrv_errors();
  337. if($this->debug && '' != $this->queryStr){
  338. $this->error .= "\n [ SQL?? ] : ".$this->queryStr;
  339. }
  340. return $this->error;
  341. }
  342. }