Advertisement
Guest User

Untitled

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