Skip to content

Commit

Permalink
moved Command::getPdoType() to Schema
Browse files Browse the repository at this point in the history
this allows different implementation in different DBMS

CUBRID does not supprt PDO::TYPE_BOOL, so we use STRING here which will
be casted by the DBMS
  • Loading branch information
cebe committed Sep 6, 2013
1 parent e446a11 commit c6ef7ec
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
25 changes: 3 additions & 22 deletions framework/yii/db/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function bindParam($name, &$value, $dataType = null, $length = null, $dri
{
$this->prepare();
if ($dataType === null) {
$this->pdoStatement->bindParam($name, $value, $this->getPdoType($value));
$this->pdoStatement->bindParam($name, $value, $this->db->schema->getPdoType($value));
} elseif ($length === null) {
$this->pdoStatement->bindParam($name, $value, $dataType);
} elseif ($driverOptions === null) {
Expand All @@ -208,7 +208,7 @@ public function bindValue($name, $value, $dataType = null)
{
$this->prepare();
if ($dataType === null) {
$this->pdoStatement->bindValue($name, $value, $this->getPdoType($value));
$this->pdoStatement->bindValue($name, $value, $this->db->schema->getPdoType($value));
} else {
$this->pdoStatement->bindValue($name, $value, $dataType);
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public function bindValues($values)
$type = $value[1];
$value = $value[0];
} else {
$type = $this->getPdoType($value);
$type = $this->db->schema->getPdoType($value);
}
$this->pdoStatement->bindValue($name, $value, $type);
$this->_params[$name] = $value;
Expand All @@ -245,25 +245,6 @@ public function bindValues($values)
return $this;
}

/**
* Determines the PDO type for the give PHP data value.
* @param mixed $data the data whose PDO type is to be determined
* @return integer the PDO type
* @see http://www.php.net/manual/en/pdo.constants.php
*/
private function getPdoType($data)
{
static $typeMap = array(
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
);
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}

/**
* Executes the SQL statement.
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs.
Expand Down
19 changes: 19 additions & 0 deletions framework/yii/db/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,23 @@ protected function getColumnPhpType($column)
return 'string';
}
}

/**
* Determines the PDO type for the give PHP data value.
* @param mixed $data the data whose PDO type is to be determined
* @return integer the PDO type
* @see http://www.php.net/manual/en/pdo.constants.php
*/
public function getPdoType($data)
{
static $typeMap = array( // php type => PDO type
'boolean' => \PDO::PARAM_BOOL,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
);
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
}
2 changes: 1 addition & 1 deletion framework/yii/db/cubrid/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use yii\base\InvalidParamException;

/**
* QueryBuilder is the query builder for CUBRID databases.
* QueryBuilder is the query builder for CUBRID databases (version 9.1.x and higher).
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
Expand Down
19 changes: 19 additions & 0 deletions framework/yii/db/cubrid/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,23 @@ protected function findTableNames($schema = '')
}
return $tableNames;
}

/**
* Determines the PDO type for the give PHP data value.
* @param mixed $data the data whose PDO type is to be determined
* @return integer the PDO type
* @see http://www.php.net/manual/en/pdo.constants.php
*/
public function getPdoType($data)
{
static $typeMap = array(
'boolean' => \PDO::PARAM_STR, // CUBRID PDO does not support PARAM_BOOL
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
);
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
}

0 comments on commit c6ef7ec

Please sign in to comment.