Advertisement
Guest User

Untitled

a guest
May 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. <?php
  2.  
  3. if (! extension_loaded('mssql') && extension_loaded('pdo_dblib')) {
  4. //Return an associative array. Used on mssql_fetch_array()'s result_type parameter.
  5. define('MSSQL_ASSOC', '1');
  6.  
  7. //Return an array with numeric keys. Used on mssql_fetch_array()'s result_type parameter.
  8. define('MSSQL_NUM', '2');
  9.  
  10. //Return an array with both numeric keys and keys with their field name. This is the default value for mssql_fetch_array()'s result_type parameter.
  11. define('MSSQL_BOTH', '3');
  12.  
  13. //Indicates the 'TEXT' type in MSSQL, used by mssql_bind()'s type parameter.
  14. define('SQLTEXT', '35');
  15.  
  16. //Indicates the 'VARCHAR' type in MSSQL, used by mssql_bind()'s type parameter.
  17. define('SQLVARCHAR', '39');
  18.  
  19. //Indicates the 'CHAR' type in MSSQL, used by mssql_bind()'s type parameter.
  20. define('SQLCHAR', '47');
  21.  
  22. //Represents one byte, with a range of -128 to 127.
  23. define('SQLINT1', '48');
  24.  
  25. //Represents two bytes, with a range of -32768 to 32767.
  26. define('SQLINT2', '52');
  27.  
  28. //Represents four bytes, with a range of -2147483648 to 2147483647.
  29. define('SQLINT4', '56');
  30.  
  31. //Indicates the 'BIT' type in MSSQL, used by mssql_bind()'s type parameter.
  32. define('SQLBIT', '50');
  33.  
  34. //Represents an four byte float.
  35. define('SQLFLT4', '59');
  36.  
  37. //Represents an eight byte float.
  38. define('SQLFLT8', '62');
  39.  
  40. class MSSQL_PDO extends PDO {
  41. public function __construct($dsn, $username="", $password="", $driver_options=array()) {
  42. parent::__construct($dsn,$username,$password, $driver_options);
  43. if (empty($driver_options[PDO::ATTR_STATEMENT_CLASS])) {
  44. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MSSQL_PDOStatement', array($this)));
  45. }
  46. }
  47. }
  48. class MSSQL_PDOStatement extends PDOStatement {
  49. //public $_all = null;
  50. public $dbh;
  51. protected function __construct($dbh) {
  52. $this->dbh = $dbh;
  53. }
  54.  
  55. }
  56.  
  57. function mssql_last_link($link_identifier = null) {
  58. static $last = null;
  59. if ($link_identifier) {
  60. $last = $link_identifier;
  61. }
  62. return $last;
  63. }
  64.  
  65. function mssql_bind($stmt, $param_name, &$var, $type, $is_output = false, $is_null = false, $maxlen = -1) {
  66. $mssql_type_map = array(
  67. SQLTEXT => PDO::PARAM_LOB,
  68. SQLVARCHAR => PDO::PARAM_STR,
  69. SQLCHAR => PDO::PARAM_STR,
  70. SQLINT1 => PDO::PARAM_INT,
  71. SQLINT2 => PDO::PARAM_INT,
  72. SQLINT4 => PDO::PARAM_INT,
  73. SQLBIT => PDO::PARAM_BOOL,
  74. SQLFLT4 => PDO::PARAM_INT,
  75. SQLFLT8 => PDO::PARAM_INT,
  76. );
  77. $var = $is_null?null:$var;
  78. $ret = $stmt->bindParam($param_name, $var, $mssql_type_map[$type], $is_output?$maxlen:($maxlen<0?null:$maxlen));
  79. return $ret;
  80. }
  81.  
  82.  
  83. function mssql_close() {
  84.  
  85. }
  86.  
  87. function mssql_connect($servername, $username, $password, $new_link = false) {
  88. $pdo = new MSSQL_PDO('sqlsrv:Server='.$servername, $username, $password);
  89. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  90. $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MSSQL_PDOStatement', array($pdo)));
  91. mssql_last_link($pdo);
  92.  
  93. return $pdo;
  94. }
  95.  
  96. function mssql_pconnect($servername, $username, $password, $new_link = false) {
  97. $pdo = mssql_connect($servername, $username, $password, $new_link);
  98.  
  99. // @todo this may not work except at connection time.
  100. // this wont work in fast-cgi or pdo_sqlsrv in anycase.
  101. // left here as a woulda-shoulda-cant
  102. //$pdo->setAttribute(PDO::ATTR_PERSISTENT, true);
  103. return $pdo;
  104. }
  105.  
  106. function mssql_execute($stmt, $skip_results = false) {
  107. return $stmt->execute();
  108. }
  109.  
  110.  
  111. function mssql_fetch_array($result, $result_type = MSSQL_BOTH) {
  112. $mssql_result_type = array(
  113. MSSQL_ASSOC => PDO::FETCH_ASSOC,
  114. MSSQL_NUM =>PDO::FETCH_NUM,
  115. MSSQL_BOTH => PDO::FETCH_BOTH
  116. );
  117. return $result->fetch($mssql_result_type[$result_type]);
  118. }
  119.  
  120. function mssql_fetch_assoc($result) {
  121. return $result->fetch(PDO::FETCH_ASSOC);
  122. }
  123.  
  124. function mssql_fetch_object($result) {
  125. return $result->fetch(PDO::FETCH_OBJ);
  126. }
  127.  
  128. function mssql_fetch_row($result) {
  129. return $result->fetch();
  130. }
  131.  
  132. function mssql_free_result($result) {
  133. return $stmt->closeCursor();
  134. }
  135.  
  136. function mssql_free_statement($stmt) {
  137. return $stmt->closeCursor();
  138. }
  139.  
  140. function mssql_get_last_message() {
  141. $link_identifier = mssql_last_link();
  142.  
  143. $errors = $link_identifier->errorInfo();
  144. return $errors[2];
  145. }
  146.  
  147. function mssql_init($sp_name, $link_identifier = null) {
  148. if (is_null($link_identifier)) {
  149. $link_identifier = mssql_last_link();
  150. }
  151.  
  152. return $link_identifier->prepare('exec '.$sp_name);
  153.  
  154. }
  155.  
  156. function mssql_next_result($result_id) {
  157. return $result_id->nextRowset();
  158. }
  159.  
  160. function mssql_num_fields($result) {
  161. return $result->columnCount();
  162. }
  163.  
  164. function mssql_num_rows($result) {
  165. return $result->rowCount();
  166. }
  167.  
  168. function mssql_query($query, $link_identifier = null, $batch_size = 0) {
  169. if (is_null($link_identifier)) {
  170. $link_identifier = mssql_last_link();
  171. }
  172.  
  173. // $stmt = $link_identifier->query($query);
  174. $driver_options = array(
  175. PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL
  176. #PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED
  177. );
  178. $stmt = $link_identifier->prepare($query, $driver_options);
  179. $return = $stmt->execute();
  180.  
  181. // Match mssql_query behavior
  182. if ($return) {
  183. $rows = $stmt->rowCount();
  184. $link_identifier->lastAffected = $rows;
  185.  
  186. if (is_null($rows)) {
  187. $return = true;
  188. } else {
  189. $return = $stmt;
  190. }
  191. }
  192. return $return;
  193.  
  194. }
  195.  
  196. function mssql_data_seek($result_identifier, $row_number) {
  197. /*
  198. if (is_null($result_identifier->_all)) {
  199. $result_identifier->_all = $result_identifier->fetchAll(PDO::FETCH_BOTH);
  200. }
  201. $row = $result_identifier->_all[$row_number];
  202. */
  203. $row = $result_identifier->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, $row_number);
  204.  
  205. // Rewind - CI always does a data_seek(0) as a rewind, but that offsets the cursor,
  206. // we have to move to -1 so the next fetch will return the first piece of data
  207. // @todo rather than resetting, it would be better to restore the old position
  208. // if we can't fetch that data somehow then track the position in the statement class
  209. $row = $result_identifier->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, -1);
  210.  
  211. return $row;
  212. }
  213.  
  214. function mssql_result($resource, $row, $field) {
  215. $data_row = mssql_data_seek($resource, $row);
  216. return $data_row[$field];
  217. }
  218.  
  219.  
  220. function mssql_rows_affected($link_identifier = null) {
  221. if (is_null($link_identifier)) {
  222. $link_identifier = mssql_last_link();
  223. }
  224. return $link_identifier->lastAffected;
  225.  
  226. }
  227.  
  228. // @todo try/catch return false on failure
  229. function mssql_select_db($database_name, $link_identifier = null) {
  230. if (is_null($link_identifier)) {
  231. $link_identifier = mssql_last_link();
  232. }
  233. $affected = $link_identifier->exec('USE '.$database_name);
  234. $link_identifier->lastAffected = $affected;
  235.  
  236. return true;
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement