Advertisement
Guest User

MSSQL PDO ISSUE

a guest
Aug 3rd, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.35 KB | None | 0 0
  1. <?php
  2.  
  3. global $emulate_prepares;
  4.  
  5. $emulate_prepares = false;
  6.  
  7. class MyStatement extends \PDOStatement {
  8.   public function BindValues(array &$values, array &$blobs, $placeholder_prefix, $columnInformation, &$max_placeholder = NULL, $blob_suffix = NULL) {
  9.     if (empty($max_placeholder)) {
  10.       $max_placeholder = 0;
  11.     }
  12.     foreach ($values as $field_name => &$field_value) {
  13.       $placeholder = $placeholder_prefix . $max_placeholder++;
  14.       $blob_key = $placeholder . $blob_suffix;
  15.       if (isset($columnInformation['blobs'][$field_name])) {
  16.         $blobs[$blob_key] = fopen('php://memory', 'a');
  17.         fwrite($blobs[$blob_key], $field_value);
  18.         rewind($blobs[$blob_key]);
  19.         $this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
  20.       }
  21.       else {
  22.         // Even though not a blob, make sure we retain a copy of these values.
  23.         $blobs[$blob_key] = $field_value;
  24.         $this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_STR);
  25.       }
  26.     }
  27.   }
  28. }
  29.  
  30.  
  31. /**
  32.  *
  33.  * @param string $connection_id
  34.  *
  35.  * @return PDO
  36.  */
  37. function connection($connection_id) {
  38.   $host = $_SERVER['SABENTIS_DB_HOST'];
  39.   $database = $_SERVER['SABENTIS_DB_DATABASE'];
  40.   $username =  $_SERVER['SABENTIS_DB_LOGIN'];
  41.   $password =  $_SERVER['SABENTIS_DB_PASSWORD'];
  42.  
  43.   static $connections = array();
  44.   if (!isset($connections[$connection_id])) {
  45.     $connection_options['pdo'] = array();
  46.     $connection_options['pdo'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  47.  
  48.     $cnn = new \PDO("sqlsrv:server=$host;Database=$database",  $username, $password, $connection_options['pdo']);
  49.     $cnn->setAttribute(PDO::ATTR_STATEMENT_CLASS, [MyStatement::class]);
  50.     $connections[$connection_id] = $cnn;
  51.  
  52.   }
  53.   return $connections[$connection_id];
  54. }
  55.  
  56. /**
  57.  * Summary of prepare
  58.  *
  59.  * @param mixed $connection
  60.  * @param mixed $query
  61.  * @return PDOStatement
  62.  */
  63. function prepare($connection, $query) {
  64.   global $emulate_prepares;
  65.   $pdo_options = array();
  66.   if ($emulate_prepares === TRUE) {
  67.     $pdo_options[PDO::ATTR_EMULATE_PREPARES] = TRUE;
  68.   }
  69.   $pdo_options[PDO::SQLSRV_ATTR_DIRECT_QUERY] = TRUE;
  70.   $pdo_options[PDO::ATTR_CURSOR] = PDO::CURSOR_SCROLL;
  71.   $pdo_options[PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = PDO::SQLSRV_CURSOR_BUFFERED;
  72.   return $connection->prepare($query, $pdo_options);
  73. }
  74.  
  75. /**
  76.  * Summary of execute
  77.  *
  78.  * @param PDO $connection
  79.  * @param string $query
  80.  *
  81.  * @param PDOStatement;
  82.  */
  83. function execute($connection, $query, array $args = array()) {
  84.   $st = prepare($connection, $query);
  85.   foreach ($args as $key => $value) {
  86.     if (is_numeric($value)) {
  87.       $st->bindValue($key, $value, PDO::PARAM_INT);
  88.     }
  89.     else {
  90.       $st->bindValue($key, $value, PDO::PARAM_STR);
  91.     }
  92.   }
  93.  
  94.   $st->execute();
  95.  
  96.   // Bind column types properly.
  97.   $null = array();
  98.   $st->columnNames = array();
  99.   for ($i = 0; $i < $st->columnCount(); $i++) {
  100.     $meta = $st->getColumnMeta($i);
  101.     $st->columnNames[]= $meta['name'];
  102.     $sqlsrv_type = $meta['sqlsrv:decl_type'];
  103.     $parts = explode(' ', $sqlsrv_type);
  104.     $type = reset($parts);
  105.     switch($type) {
  106.       case 'varbinary':
  107.         $null[$i] = NULL;
  108.         $st->bindColumn($i + 1, $null[$i], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
  109.         break;
  110.       case 'int':
  111.       case 'bit':
  112.       case 'smallint':
  113.       case 'tinyint':
  114.       case 'bigint':
  115.         $null[$i] = NULL;
  116.         $st->bindColumn($i + 1, $null[$i], PDO::PARAM_INT);
  117.         break;
  118.     }
  119.   }
  120.  
  121.   return $st;
  122. }
  123.  
  124.  
  125. //*******************************************************
  126. // TEST BEGIN
  127. //*******************************************************
  128.  
  129. $connection = connection('default');
  130.  
  131. $data = 'this is the data';
  132.  
  133. $emulate_prepares = FALSE;
  134.  
  135. try {
  136.   /** @var MyStatement */
  137.   $st = prepare($connection, 'INSERT INTO TESTTABLE VALUES(:p0)');
  138.   $st->bindParam(':p0', $data, PDO::PARAM_STR);
  139.   $st->bindParam(':crap_parameter', $data, PDO::PARAM_STR);
  140. }
  141. catch (\Exception $e) {
  142.   echo '<br/>' . $e->getMessage();
  143. }
  144.  
  145. $emulate_prepares = TRUE;
  146.  
  147. try {
  148.   /** @var MyStatement */
  149.   $st = prepare($connection, 'INSERT INTO TESTTABLE VALUES(:p0)');
  150.   $st->bindParam(':p0', $data, PDO::PARAM_STR);
  151.   $st->bindParam(':crap_parameter');
  152.   echo '<br/>' . 'NO EXCEPTION?';
  153. }
  154. catch (\Exception $e) {
  155.   echo '<br/>' . $e->getMessage();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement