Skip to content

Commit

Permalink
Re-applied an optimization that existed in the previous implementation.
Browse files Browse the repository at this point in the history
String results will not be "casted" using the identity function found
in the StringType class, as a way of sparing some function calls.
  • Loading branch information
lorenzo committed Nov 22, 2015
1 parent 3bffd01 commit bff580d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Database/FieldTypeConverter.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\Database;

use Cake\Database\Type\OptionalConvertInterface;

/**
* A callable class to be used for processing each of the rows in a statement
* result, so that the values are converted to the right PHP types.
Expand Down Expand Up @@ -49,6 +51,12 @@ public function __construct(TypeMap $typeMap, Driver $driver)
$types = array_map(['Cake\Database\Type', 'build'], array_combine($types, $types));
$result = [];

foreach ($types as $k => $type) {
if ($type instanceof OptionalConvertInterface && !$type->requiresToPHPCast()) {
unset($types[$k]);
}
}

foreach ($map as $field => $type) {
if (isset($types[$type])) {
$result[$field] = $types[$type];
Expand Down
31 changes: 31 additions & 0 deletions src/Database/Type/OptionalConvertInterface.php
@@ -0,0 +1,31 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Type;

/**
* An interface used by Type objects to signal whether the casting
* is actually required.
*/
interface OptionalConvertInterface
{

/**
* Returns whehter the cast to PHP is required to be invoked, since
* it is not a indentity function.
*
* @return boolean
*/
public function requiresToPHPCast();
}
12 changes: 11 additions & 1 deletion src/Database/Type/StringType.php
Expand Up @@ -24,7 +24,7 @@
*
* Use to convert string data between PHP and the database types.
*/
class StringType extends Type
class StringType extends Type implements OptionalConvertInterface
{

/**
Expand Down Expand Up @@ -94,4 +94,14 @@ public function marshal($value)
}
return (string)$value;
}

/**
* {@inheritDoc}
*
* @return boolean False as databse results are returned already as strings
*/
public function requiresToPHPCast()
{
return false;
}
}

0 comments on commit bff580d

Please sign in to comment.