Guest User

Untitled

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