Skip to content

Commit

Permalink
Make the $value a string-only property again.
Browse files Browse the repository at this point in the history
It's a BC break that this might sometimes be a stream now. Instead make $value a setter/getter and add $stream property too. Convert between these two on the fly.
Also make Horde_Db_Value_Text extend Horde_Db_Value_Binary because they share most of the code. This requires to change some class tests though, because "instanceof Horde_Db_Value_Binary" is true now for Value_Text objects too.

Request: 10805
  • Loading branch information
yunosh committed Feb 3, 2017
1 parent c5eb4ce commit ea32d50
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 52 deletions.
2 changes: 1 addition & 1 deletion framework/Db/lib/Horde/Db/Adapter/Base.php
Expand Up @@ -793,7 +793,7 @@ protected function _replaceParameters($sql, array $args, $no_binary = false)
$sql = array_shift($sqlPieces);
while (count($sqlPieces)) {
$value = array_shift($args);
if ($no_binary && $value instanceof Horde_Db_Value_Binary) {
if ($no_binary && get_class($value) == 'Horde_Db_Value_Binary') {
$sql_value = '<binary_data>';
} else {
$sql_value = $this->quote($value);
Expand Down
8 changes: 4 additions & 4 deletions framework/Db/lib/Horde/Db/Adapter/Oci8.php
Expand Up @@ -297,7 +297,7 @@ public function execute($sql, $arg1 = null, $arg2 = null, $lobs = array())
$descriptors = array();
foreach ($lobs as $name => $lob) {
$descriptors[$name] = oci_new_descriptor($this->_connection, OCI_DTYPE_LOB);
oci_bind_by_name($stmt, ':' . $name, $descriptors[$name], -1, $lob instanceof Horde_Db_Value_Binary ? OCI_B_BLOB : OCI_B_CLOB);
oci_bind_by_name($stmt, ':' . $name, $descriptors[$name], -1, $lob instanceof Horde_Db_Value_Text ? OCI_B_CLOB : OCI_B_BLOB);
}

$flags = $lobs
Expand Down Expand Up @@ -465,9 +465,9 @@ protected function _prepareBlobs($fields)
$field instanceof Horde_Db_Value_Text) {
$blobs[$this->quoteColumnName($column)] = $field;
$locators[] = ':' . $this->quoteColumnName($column);
$field = $field instanceof Horde_Db_Value_Binary
? 'EMPTY_BLOB()'
: 'EMPTY_CLOB()';
$field = $field instanceof Horde_Db_Value_Text
? 'EMPTY_CLOB()'
: 'EMPTY_BLOB()';
} else {
$field = $this->quote($field);
}
Expand Down
8 changes: 4 additions & 4 deletions framework/Db/lib/Horde/Db/Adapter/Pdo/Base.php
Expand Up @@ -311,9 +311,9 @@ public function insertBlob($table, $fields, $pk = null, $idValue = null)
$placeholders = $values = $binary = array();
$binary_cnt = 0;
foreach ($fields as $name => $value) {
if ($value instanceof Horde_Db_Value && is_resource($value->value)) {
if ($value instanceof Horde_Db_Value) {
$placeholders[] = ':binary' . $binary_cnt++;
$binary[] = $value->value;
$binary[] = $value->stream;
} else {
$placeholders[] = '?';
$values[] = $value;
Expand Down Expand Up @@ -363,9 +363,9 @@ public function updateBlob($table, $fields, $where = null)
$binary_cnt = 0;

foreach ($fields as $field => $value) {
if ($value instanceof Horde_Db_Value && is_resource($value->value)) {
if ($value instanceof Horde_Db_Value) {
$fnames[] = $this->quoteColumnName($field) . ' = :binary' . $binary_cnt++;
$binary_values[] = $value->value;
$binary_values[] = $value->stream;
} else {
$fnames[] = $this->quoteColumnName($field) . ' = ?';
$values[] = $value;
Expand Down
89 changes: 70 additions & 19 deletions framework/Db/lib/Horde/Db/Value/Binary.php
Expand Up @@ -2,52 +2,103 @@
/**
* Copyright 2006-2017 Horde LLC (http://www.horde.org/)
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
*/

/**
* Encapsulation object for binary values to be used in SQL statements to
* ensure proper quoting, escaping, retrieval, etc.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
* @property $value The binary value as a string. @since Horde_Db 2.1.0
* @property $stream The binary value as a stream. @since Horde_Db 2.4.0
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package Db
*/
class Horde_Db_Value_Binary implements Horde_Db_Value
{
/**
* Binary value to be quoted
* Binary scalar value to be quoted
*
* @var string
* @since Horde_Db 2.1.0
*/
public $value;
protected $_value;

/**
* Binary stream value to be quoted
*
* @var stream
*/
protected $_stream;

/**
* Constructor
*
* @param string|stream resource $binaryValue The binary value in either
* a string or a stream resource.
* @param string|stream $binaryValue The binary value in a string or
* stream resource.
*/
public function __construct($binaryValue)
{
$this->value = $binaryValue;
if (is_resource($binaryValue)) {
$this->stream = $binaryValue;
} else {
$this->value = $binaryValue;
}
}

/**
* @param Horde_Db_Adapter $db
* Getter for $value and $stream properties.
*/
public function quote(Horde_Db_Adapter $db)
public function __get($name)
{
if (is_resource($this->value)) {
rewind($this->value);
return $db->quoteBinary(stream_get_contents($this->value));
switch ($name) {
case 'value':
if (isset($this->_value)) {
return $this->_value;
}
if (isset($this->_stream)) {
rewind($this->_stream);
return stream_get_contents($this->_stream);
}
break;

case 'stream':
if (isset($this->_stream)) {
return $this->_stream;
}
if (isset($this->_value)) {
$stream = @fopen('php://temp', 'r+');
fwrite($stream, $this->_value);
rewind($stream);
return $stream;
}
break;
}
}

/**
* Setter for $value and $stream properties.
*/
public function __set($name, $value)
{
switch ($name) {
case 'value':
case 'stream':
$this->{'_' . $name} = $value;
break;
}
}

/**
* @param Horde_Db_Adapter $db
*/
public function quote(Horde_Db_Adapter $db)
{
return $db->quoteBinary($this->value);
}
}
25 changes: 1 addition & 24 deletions framework/Db/lib/Horde/Db/Value/Text.php
Expand Up @@ -17,36 +17,13 @@
* @category Horde
* @package Db
*/
class Horde_Db_Value_Text implements Horde_Db_Value
class Horde_Db_Value_Text extends Horde_Db_Value_Binary
{
/**
* Text value to be quoted
*
* @var string
* @since Horde_Db 2.1.0
*/
public $value;

/**
* Constructor
*
* @param string|stream $textValue The text value in a string or stream
* resource.
*/
public function __construct($textValue)
{
$this->value = $textValue;
}

/**
* @param Horde_Db_Adapter $db
*/
public function quote(Horde_Db_Adapter $db)
{
if (is_resource($this->value)) {
rewind($this->value);
return $db->quoteString(stream_get_contents($this->value));
}
return $db->quoteString($this->value);
}
}

0 comments on commit ea32d50

Please sign in to comment.