Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring some code in Connection and Statement, to make it easier to
use statements on their own and to cleanly share code between both classes
- Loading branch information
Showing
3 changed files
with
87 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Cake\Model\Datasource\Database; | ||
|
||
trait TypeConverter { | ||
|
||
/** | ||
* Converts a give value to a suitable database value based on type | ||
* and return relevant internal statement type | ||
* | ||
* @param mixed value | ||
* @param string $type | ||
* @return array list containing converted value and internal type | ||
**/ | ||
public function cast($value, $type) { | ||
if (is_string($type)) { | ||
$type = Type::build($type); | ||
} | ||
if ($type instanceof Type) { | ||
$value = $type->toDatabase($value, $this->_driver); | ||
$type = $type->toStatement($value, $this->_driver); | ||
} | ||
return [$value, $type]; | ||
} | ||
|
||
/** | ||
* Matches columns to corresponding types | ||
* | ||
* Both $columns and $types should either be numeric based or string key based at | ||
* the same time. | ||
* | ||
* @param array $columns list or associative array of columns and parameters to be bound with types | ||
* @param array $types list or associative array of types | ||
* @return array | ||
**/ | ||
public function matchTypes($columns, $types) { | ||
if (!is_int(key($types))) { | ||
$positons = array_intersect_key(array_flip($columns), $types); | ||
$types = array_intersect_key($types, $positons); | ||
$types = array_combine($positons, $types); | ||
} | ||
return $types; | ||
} | ||
|
||
} |