Advertisement
Guest User

Untitled

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