Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- global $emulate_prepares;
- $emulate_prepares = false;
- class MyStatement extends \PDOStatement {
- public function BindValues(array &$values, array &$blobs, $placeholder_prefix, $columnInformation, &$max_placeholder = NULL, $blob_suffix = NULL) {
- if (empty($max_placeholder)) {
- $max_placeholder = 0;
- }
- foreach ($values as $field_name => &$field_value) {
- $placeholder = $placeholder_prefix . $max_placeholder++;
- $blob_key = $placeholder . $blob_suffix;
- if (isset($columnInformation['blobs'][$field_name])) {
- $blobs[$blob_key] = fopen('php://memory', 'a');
- fwrite($blobs[$blob_key], $field_value);
- rewind($blobs[$blob_key]);
- $this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
- }
- else {
- // Even though not a blob, make sure we retain a copy of these values.
- $blobs[$blob_key] = $field_value;
- $this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_STR);
- }
- }
- }
- }
- /**
- *
- * @param string $connection_id
- *
- * @return PDO
- */
- function connection($connection_id) {
- $host = $_SERVER['SABENTIS_DB_HOST'];
- $database = $_SERVER['SABENTIS_DB_DATABASE'];
- $username = $_SERVER['SABENTIS_DB_LOGIN'];
- $password = $_SERVER['SABENTIS_DB_PASSWORD'];
- static $connections = array();
- if (!isset($connections[$connection_id])) {
- $connection_options['pdo'] = array();
- $connection_options['pdo'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
- $cnn = new \PDO("sqlsrv:server=$host;Database=$database", $username, $password, $connection_options['pdo']);
- $cnn->setAttribute(PDO::ATTR_STATEMENT_CLASS, [MyStatement::class]);
- $connections[$connection_id] = $cnn;
- }
- return $connections[$connection_id];
- }
- /**
- * Summary of prepare
- *
- * @param mixed $connection
- * @param mixed $query
- * @return PDOStatement
- */
- function prepare($connection, $query) {
- global $emulate_prepares;
- $pdo_options = array();
- if ($emulate_prepares === TRUE) {
- $pdo_options[PDO::ATTR_EMULATE_PREPARES] = TRUE;
- }
- $pdo_options[PDO::SQLSRV_ATTR_DIRECT_QUERY] = TRUE;
- $pdo_options[PDO::ATTR_CURSOR] = PDO::CURSOR_SCROLL;
- $pdo_options[PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = PDO::SQLSRV_CURSOR_BUFFERED;
- return $connection->prepare($query, $pdo_options);
- }
- /**
- * Summary of execute
- *
- * @param PDO $connection
- * @param string $query
- *
- * @param PDOStatement;
- */
- function execute($connection, $query, array $args = array()) {
- $st = prepare($connection, $query);
- foreach ($args as $key => $value) {
- if (is_numeric($value)) {
- $st->bindValue($key, $value, PDO::PARAM_INT);
- }
- else {
- $st->bindValue($key, $value, PDO::PARAM_STR);
- }
- }
- $st->execute();
- // Bind column types properly.
- $null = array();
- $st->columnNames = array();
- for ($i = 0; $i < $st->columnCount(); $i++) {
- $meta = $st->getColumnMeta($i);
- $st->columnNames[]= $meta['name'];
- $sqlsrv_type = $meta['sqlsrv:decl_type'];
- $parts = explode(' ', $sqlsrv_type);
- $type = reset($parts);
- switch($type) {
- case 'varbinary':
- $null[$i] = NULL;
- $st->bindColumn($i + 1, $null[$i], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
- break;
- case 'int':
- case 'bit':
- case 'smallint':
- case 'tinyint':
- case 'bigint':
- $null[$i] = NULL;
- $st->bindColumn($i + 1, $null[$i], PDO::PARAM_INT);
- break;
- }
- }
- return $st;
- }
- //*******************************************************
- // TEST BEGIN
- //*******************************************************
- $connection = connection('default');
- $data = 'this is the data';
- $emulate_prepares = FALSE;
- try {
- /** @var MyStatement */
- $st = prepare($connection, 'INSERT INTO TESTTABLE VALUES(:p0)');
- $st->bindParam(':p0', $data, PDO::PARAM_STR);
- $st->bindParam(':crap_parameter', $data, PDO::PARAM_STR);
- }
- catch (\Exception $e) {
- echo '<br/>' . $e->getMessage();
- }
- $emulate_prepares = TRUE;
- try {
- /** @var MyStatement */
- $st = prepare($connection, 'INSERT INTO TESTTABLE VALUES(:p0)');
- $st->bindParam(':p0', $data, PDO::PARAM_STR);
- $st->bindParam(':crap_parameter', $data, PDO::PARAM_STR);
- echo '<br/>' . 'NO EXCEPTION?';
- }
- catch (\Exception $e) {
- echo '<br/>' . $e->getMessage();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement