Guest User

Untitled

a guest
Jul 19th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. <?php
  2.  
  3. if (! extension_loaded('mssql') && (extension_loaded('pdo_dblib') || extension_loaded('pdo_sqlsrv'))) {
  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(extension_loaded('pdo_sqlsrv') ? 'sqlsrv:Server='.$servername' :
  89. 'dblib:host='.$servername, $username, $password); // prefix can be 'mssql:'
  90. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  91. $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MSSQL_PDOStatement', array($pdo)));
  92. mssql_last_link($pdo);
  93.  
  94. return $pdo;
  95. }
  96.  
  97. function mssql_pconnect($servername, $username, $password, $new_link = false) {
  98. $pdo = mssql_connect($servername, $username, $password, $new_link);
  99.  
  100. // @todo this may not work except at connection time.
  101. // this wont work in fast-cgi or pdo_sqlsrv in anycase.
  102. // left here as a woulda-shoulda-cant
  103. //$pdo->setAttribute(PDO::ATTR_PERSISTENT, true);
  104. return $pdo;
  105. }
  106.  
  107. function mssql_execute($stmt, $skip_results = false) {
  108. return $stmt->execute();
  109. }
  110.  
  111.  
  112. function mssql_fetch_array($result, $result_type = MSSQL_BOTH) {
  113. $mssql_result_type = array(
  114. MSSQL_ASSOC => PDO::FETCH_ASSOC,
  115. MSSQL_NUM =>PDO::FETCH_NUM,
  116. MSSQL_BOTH => PDO::FETCH_BOTH
  117. );
  118. return $result->fetch($mssql_result_type[$result_type]);
  119. }
  120.  
  121. function mssql_fetch_assoc($result) {
  122. return $result->fetch(PDO::FETCH_ASSOC);
  123. }
  124.  
  125. function mssql_fetch_object($result) {
  126. return $result->fetch(PDO::FETCH_OBJ);
  127. }
  128.  
  129. function mssql_fetch_row($result) {
  130. return $result->fetch();
  131. }
  132.  
  133. function mssql_free_result($result) {
  134. return $stmt->closeCursor();
  135. }
  136.  
  137. function mssql_free_statement($stmt) {
  138. return $stmt->closeCursor();
  139. }
  140.  
  141. function mssql_get_last_message() {
  142. $link_identifier = mssql_last_link();
  143.  
  144. $errors = $link_identifier->errorInfo();
  145. return $errors[2];
  146. }
  147.  
  148. function mssql_init($sp_name, $link_identifier = null) {
  149. if (is_null($link_identifier)) {
  150. $link_identifier = mssql_last_link();
  151. }
  152.  
  153. return $link_identifier->prepare('exec '.$sp_name);
  154.  
  155. }
  156.  
  157. function mssql_next_result($result_id) {
  158. return $result_id->nextRowset();
  159. }
  160.  
  161. function mssql_num_fields($result) {
  162. return $result->columnCount();
  163. }
  164.  
  165. function mssql_num_rows($result) {
  166. return $result->rowCount();
  167. }
  168.  
  169. function mssql_query($query, $link_identifier = null, $batch_size = 0) {
  170. if (is_null($link_identifier)) {
  171. $link_identifier = mssql_last_link();
  172. }
  173.  
  174. // $stmt = $link_identifier->query($query);
  175. $driver_options = array(
  176. PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL
  177. );
  178. if (extension_loaded('pdo_sqlsrv')) {
  179. $driver_options[PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = PDO::SQLSRV_CURSOR_BUFFERED;
  180. }
  181. $stmt = $link_identifier->prepare($query, $driver_options);
  182. $return = $stmt->execute();
  183.  
  184. // Match mssql_query behavior
  185. if ($return) {
  186. $rows = $stmt->rowCount();
  187. $link_identifier->lastAffected = $rows;
  188.  
  189. if (is_null($rows)) {
  190. $return = true;
  191. } else {
  192. $return = $stmt;
  193. }
  194. }
  195. return $return;
  196.  
  197. }
  198.  
  199. function mssql_data_seek($result_identifier, $row_number) {
  200. /*
  201. if (is_null($result_identifier->_all)) {
  202. $result_identifier->_all = $result_identifier->fetchAll(PDO::FETCH_BOTH);
  203. }
  204. $row = $result_identifier->_all[$row_number];
  205. */
  206. $row = $result_identifier->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, $row_number);
  207.  
  208. // Rewind - CI always does a data_seek(0) as a rewind, but that offsets the cursor,
  209. // we have to move to -1 so the next fetch will return the first piece of data
  210. // @todo rather than resetting, it would be better to restore the old position
  211. // if we can't fetch that data somehow then track the position in the statement class
  212. $row = $result_identifier->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, -1);
  213.  
  214. return $row;
  215. }
  216.  
  217. function mssql_result($resource, $row, $field) {
  218. $data_row = mssql_data_seek($resource, $row);
  219. return $data_row[$field];
  220. }
  221.  
  222.  
  223. function mssql_rows_affected($link_identifier = null) {
  224. if (is_null($link_identifier)) {
  225. $link_identifier = mssql_last_link();
  226. }
  227. return $link_identifier->lastAffected;
  228.  
  229. }
  230.  
  231. // @todo try/catch return false on failure
  232. function mssql_select_db($database_name, $link_identifier = null) {
  233. if (is_null($link_identifier)) {
  234. $link_identifier = mssql_last_link();
  235. }
  236. $affected = $link_identifier->exec('USE '.$database_name);
  237. $link_identifier->lastAffected = $affected;
  238.  
  239. return true;
  240. }
  241. }
Add Comment
Please, Sign In to add comment