PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/output/connections/MSSQLWinConnection.php

https://gitlab.com/Lidbary/PHPRunner
PHP | 315 lines | 201 code | 41 blank | 73 comment | 26 complexity | b2e9b9267f39cdffbb0e0590a24fe849 MD5 | raw file
  1. <?php
  2. class MSSQLWinConnection extends Connection
  3. {
  4. protected $host;
  5. protected $dbname;
  6. protected $user = "";
  7. protected $pwd = "";
  8. protected $options = "";
  9. protected $ODBCString;
  10. /**
  11. * A date format string
  12. * @type String
  13. */
  14. protected $mssql_dmy;
  15. function MSSQLWinConnection( $params )
  16. {
  17. parent::Connection( $params );
  18. }
  19. /**
  20. * Set db connection's properties
  21. * @param Array params
  22. */
  23. protected function assignConnectionParams( $params )
  24. {
  25. parent::assignConnectionParams( $params );
  26. $this->ODBCString = $params["ODBCString"];
  27. $this->host = $params["connInfo"][0]; //strConnectInfo1
  28. if( $params["connInfo"][2] == "SSPI" )
  29. {
  30. $this->dbname = $params["connInfo"][1]; //strConnectInfo2
  31. $this->options = $params["connInfo"][2]; //strConnectInfo3
  32. }
  33. else
  34. {
  35. $this->user = $params["connInfo"][1]; //strConnectInfo2
  36. $this->pwd = $params["connInfo"][2]; //strConnectInfo3
  37. $this->dbname = $params["connInfo"][3]; //strConnectInfo4
  38. }
  39. }
  40. /**
  41. * Open a connection to db
  42. */
  43. public function connect()
  44. {
  45. global $cCodepage;
  46. $connStrings = array();
  47. if( $_SESSION["MSSQLConnString"] )
  48. $connStrings[] = $_SESSION["SQLConnString"];
  49. $connStrings[] = $this->ODBCString;
  50. // SQLOLEDB provider
  51. if( $options == "SSPI" )
  52. $connStrings[] = "PROVIDER=SQLOLEDB;SERVER=".$this->host.";DATABASE=".$this->dbname.";Integrated Security=SSPI";
  53. else
  54. $connStrings[] = "PROVIDER=SQLOLEDB;SERVER=".$this->host.";UID=".$this->user.";PWD=".$this->pwd.";DATABASE=".$this->dbname;
  55. // SQLNCLI provider
  56. if( $options=="SSPI" )
  57. $connStrings[] = "PROVIDER=SQLNCLI;SERVER=".$this->host.";DATABASE=".$this->dbname.";Integrated Security=SSPI";
  58. else
  59. $connStrings[] = "PROVIDER=SQLNCLI;SERVER=".$this->host.";UID=".$this->user.";PWD=".$this->pwd.";DATABASE=".$this->dbname;
  60. // go thru connection strings one by one
  61. $errorString = "";
  62. foreach($connStrings as $connStr)
  63. {
  64. try
  65. {
  66. $this->conn = new COM("ADODB.Connection", NULL, $cCodepage);
  67. $this->conn->Open( $connStr );
  68. $rs = $this->conn->Execute("select convert(datetime,'2000-11-22',121)");
  69. $str = $rs->Fields[0]->Value;
  70. $y = strpos($str, "2000");
  71. $m = strpos($str, "11");
  72. $d = strpos($str, "22");
  73. $this->mssql_dmy = "mdy";
  74. if( $y < $m && $m < $d )
  75. $this->mssql_dmy = "ymd";
  76. if( $d < $m && $m < $y )
  77. $this->mssql_dmy = "dmy";
  78. $_SESSION["MSSQLConnString"] = $connStr;
  79. return $this->conn;
  80. }
  81. catch(com_exception $e)
  82. {
  83. $errorString .= "<br>".$e->getMessage();
  84. }
  85. }
  86. trigger_error($errorString, E_USER_ERROR);
  87. }
  88. /**
  89. * Close the db connection
  90. */
  91. public function close()
  92. {
  93. $this->conn->Close();
  94. }
  95. /**
  96. * Send an SQL query
  97. * @param String sql
  98. * @return Mixed
  99. */
  100. public function query( $sql )
  101. {
  102. $this->debugInfo($sql);
  103. try
  104. {
  105. $qHandle = $this->conn->Execute( $sql );
  106. return new QueryResult( $this, $qHandle );
  107. }
  108. catch(com_exception $e)
  109. {
  110. trigger_error($e->getMessage(), E_USER_ERROR);
  111. return FALSE;
  112. }
  113. }
  114. /**
  115. * Execute an SQL query
  116. * @param String sql
  117. */
  118. public function exec( $sql )
  119. {
  120. $qResult = $this->query( $sql );
  121. if( $qResult )
  122. return $qResult->getQueryHandle();
  123. return FALSE;
  124. }
  125. /**
  126. * Get a description of the last error
  127. * @return String
  128. */
  129. public function lastError()
  130. {
  131. if( $this->conn->Errors->Count )
  132. return $this->conn->Errors[ $this->conn->Errors->Count - 1 ]->Description;
  133. return '';
  134. }
  135. /**
  136. * Fetch a result row as an array
  137. * @param Mixed qHanle The query handle
  138. * @param Number assoc (optional)
  139. * @return Array
  140. */
  141. protected function _fetch_array($qHanle, $assoc = 1)
  142. {
  143. $ret = array();
  144. if( $qHanle->EOF() )
  145. return $ret;
  146. try {
  147. for( $i = 0; $i < $this->num_fields($qHanle); $i++ )
  148. {
  149. if( IsBinaryType( $qHanle->Fields[$i]->Type ) && $qHanle->Fields[$i]->Type != 128 )
  150. {
  151. $str = "";
  152. if( $qHanle->Fields[$i]->ActualSize )
  153. {
  154. $val = $qHanle->Fields[$i]->GetChunk( $qHanle->Fields[$i]->ActualSize );
  155. $str = str_pad("", count($val));
  156. $j = 0;
  157. foreach($val as $byte)
  158. {
  159. $str[ $j++ ] = chr($byte);
  160. }
  161. }
  162. if( $assoc )
  163. $ret[ $qHanle->Fields[$i]->Name ] = $str;
  164. else
  165. $ret[ $i ] = $str;
  166. }
  167. else
  168. {
  169. $value = $qHanle->Fields[$i]->Value;
  170. if( is_null($value) )
  171. $val = NULL;
  172. else
  173. {
  174. if( isdatefieldtype($qHanle->Fields[$i]->Type) )
  175. $value = localdatetime2db( (string)$qHanle->Fields[$i]->Value, $this->mssql_dmy );
  176. if( IsNumberType($qHanle->Fields[$i]->Type) )
  177. $val = floatval($value);
  178. else
  179. $val = strval($value);
  180. }
  181. if( $assoc )
  182. $ret[ $qHanle->Fields[$i]->Name ] = $val;
  183. else
  184. $ret[ $i ] = $val;
  185. }
  186. }
  187. $qHanle->MoveNext();
  188. }
  189. catch(com_exception $e)
  190. {
  191. trigger_error($e->getMessage(), E_USER_ERROR);
  192. }
  193. return $ret;
  194. }
  195. /**
  196. * Fetch a result row as an associative array
  197. * @param Mixed qHanle The query handle
  198. * @return Array
  199. */
  200. public function fetch_array( $qHanle )
  201. {
  202. return $this->_fetch_array( $qHanle );
  203. }
  204. /**
  205. * Fetch a result row as a numeric array
  206. * @param Mixed qHanle The query handle
  207. * @return Array
  208. */
  209. public function fetch_numarray( $qHanle )
  210. {
  211. return $this->_fetch_array( $qHanle, 0 );
  212. }
  213. /**
  214. * Free resources associated with a query result set
  215. * @param Mixed qHanle The query handle
  216. */
  217. public function closeQuery( $qHanle )
  218. {
  219. $qHanle->Close();
  220. }
  221. /**
  222. * Get number of fields in a result
  223. * @param Mixed qHanle The query handle
  224. * @return Number
  225. */
  226. public function num_fields( $qHandle )
  227. {
  228. return $qHandle->Fields->Count;
  229. }
  230. /**
  231. * Get the name of the specified field in a result
  232. * @param Mixed qHanle The query handle
  233. * @param Number offset
  234. * @return String
  235. */
  236. public function field_name( $qHanle, $offset )
  237. {
  238. return $qHanle->Fields($offset)->Name;
  239. }
  240. /**
  241. * @param Mixed qHandle
  242. * @param Number pageSize
  243. * @param Number page
  244. */
  245. public function seekPage($qHandle, $pageSize, $page)
  246. {
  247. if( $page == 1 )
  248. return;
  249. if( $qHandle->EOF() )
  250. return;
  251. $qHandle->Move($pageSize * ($page - 1));
  252. }
  253. /**
  254. * Execute an SQL query with blob fields processing
  255. * @param String sql
  256. * @param Array blobs
  257. * @param Array blobTypes
  258. * @return Boolean
  259. */
  260. public function execWithBlobProcessing( $sql, $blobs, $blobTypes = array() )
  261. {
  262. $this->debugInfo($sql);
  263. try
  264. {
  265. $this->conn->Execute( $sql );
  266. return true;
  267. }
  268. catch(com_exception $e)
  269. {
  270. return false;
  271. }
  272. }
  273. }
  274. ?>