Commit 05c7915d by Qiang Xue

w

parent 8f15fd41
<?php
/**
* CDbColumnSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbColumnSchema class describes the column meta data of a database table.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CDbColumnSchema.php 3099 2011-03-19 01:26:47Z qiang.xue $
* @package system.db.schema
* @since 1.0
*/
class CDbColumnSchema extends CComponent
{
/**
* @var string name of this column (without quotes).
*/
public $name;
/**
* @var string raw name of this column. This is the quoted name that can be used in SQL queries.
*/
public $rawName;
/**
* @var boolean whether this column can be null.
*/
public $allowNull;
/**
* @var string the DB type of this column.
*/
public $dbType;
/**
* @var string the PHP type of this column.
*/
public $type;
/**
* @var mixed default value of this column
*/
public $defaultValue;
/**
* @var integer size of the column.
*/
public $size;
/**
* @var integer precision of the column data, if it is numeric.
*/
public $precision;
/**
* @var integer scale of the column data, if it is numeric.
*/
public $scale;
/**
* @var boolean whether this column is a primary key
*/
public $isPrimaryKey;
/**
* @var boolean whether this column is a foreign key
*/
public $isForeignKey;
/**
* @var boolean whether this column is auto-incremental
* @since 1.1.7
*/
public $autoIncrement = false;
/**
* Initializes the column with its DB type and default value.
* This sets up the column's PHP type, size, precision, scale as well as default value.
* @param string $dbType the column's DB type
* @param mixed $defaultValue the default value
*/
public function init($dbType, $defaultValue)
{
$this->dbType = $dbType;
$this->extractType($dbType);
$this->extractLimit($dbType);
if ($defaultValue !== null)
$this->extractDefault($defaultValue);
}
/**
* Extracts the PHP type from DB type.
* @param string $dbType DB type
*/
protected function extractType($dbType)
{
if (stripos($dbType, 'int') !== false && stripos($dbType, 'unsigned int') === false)
$this->type = 'integer';
elseif (stripos($dbType, 'bool') !== false)
$this->type = 'boolean';
elseif (preg_match('/(real|floa|doub)/i', $dbType))
$this->type = 'double';
else
$this->type = 'string';
}
/**
* Extracts size, precision and scale information from column's DB type.
* @param string $dbType the column's DB type
*/
protected function extractLimit($dbType)
{
if (strpos($dbType, '(') && preg_match('/\((.*)\)/', $dbType, $matches))
{
$values = explode(',', $matches[1]);
$this->size = $this->precision = (int)$values[0];
if (isset($values[1]))
$this->scale = (int)$values[1];
}
}
/**
* Extracts the default value for the column.
* The value is typecasted to correct PHP type.
* @param mixed $defaultValue the default value obtained from metadata
*/
protected function extractDefault($defaultValue)
{
$this->defaultValue = $this->typecast($defaultValue);
}
/**
* Converts the input value to the type that this column is of.
* @param mixed $value input value
* @return mixed converted value
*/
public function typecast($value)
{
if (gettype($value) === $this->type || $value === null || $value instanceof CDbExpression)
return $value;
if ($value === '')
return $this->type === 'string' ? '' : null;
switch ($this->type)
{
case 'string': return (string)$value;
case 'integer': return (integer)$value;
case 'boolean': return (boolean)$value;
case 'double':
default: return $value;
}
}
}
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\db\dao;
/** /**
* Command represents a SQL statement to be executed against a database. * Command represents a SQL statement to be executed against a database.
* *
...@@ -40,7 +42,7 @@ ...@@ -40,7 +42,7 @@
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Command extends CComponent class Command extends \yii\base\Component
{ {
/** /**
* @var array the parameters (name=>value) to be bound to the current query. * @var array the parameters (name=>value) to be bound to the current query.
......
<?php
/**
* Expression class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\dao;
/**
* Expression represents a DB expression that does not need escaping or quoting.
* When an Expression object is embedded within a SQL statement or fragment,
* it will be replaced with the [[expression]] property value without any
* DB escaping or quoting. For example,
*
* ~~~
* $expression = new Expression('NOW()');
* $sql = 'SELECT ' . $expression; // SELECT NOW()
* ~~~
*
* An expression can also be bound with parameters specified via [[params]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Expression
{
/**
* @var string the DB expression
*/
public $expression;
/**
* @var array list of parameters that should be bound for this expression.
* The keys are placeholders appearing in [[expression]] and the values
* are the corresponding parameter values.
*/
public $params = array();
/**
* Constructor.
* @param string $expression the DB expression
* @param array $params parameters
*/
public function __construct($expression, $params = array())
{
$this->expression = $expression;
$this->params = $params;
}
/**
* String magic method
* @return string the DB expression
*/
public function __toString()
{
return $this->expression;
}
}
\ No newline at end of file
<?php
/**
* CDbTableSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbTableSchema is the base class for representing the metadata of a database table.
*
* It may be extended by different DBMS driver to provide DBMS-specific table metadata.
*
* CDbTableSchema provides the following information about a table:
* <ul>
* <li>{@link name}</li>
* <li>{@link rawName}</li>
* <li>{@link columns}</li>
* <li>{@link primaryKey}</li>
* <li>{@link foreignKeys}</li>
* <li>{@link sequenceName}</li>
* </ul>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CDbTableSchema.php 2799 2011-01-01 19:31:13Z qiang.xue $
* @package system.db.schema
* @since 1.0
*/
class CDbTableSchema extends CComponent
{
/**
* @var string name of this table.
*/
public $name;
/**
* @var string raw name of this table. This is the quoted version of table name with optional schema name. It can be directly used in SQLs.
*/
public $rawName;
/**
* @var string|array primary key name of this table. If composite key, an array of key names is returned.
*/
public $primaryKey;
/**
* @var string sequence name for the primary key. Null if no sequence.
*/
public $sequenceName;
/**
* @var array foreign keys of this table. The array is indexed by column name. Each value is an array of foreign table name and foreign column name.
*/
public $foreignKeys = array();
/**
* @var array column metadata of this table. Each array element is a CDbColumnSchema object, indexed by column names.
*/
public $columns = array();
/**
* Gets the named column metadata.
* This is a convenient method for retrieving a named column even if it does not exist.
* @param string $name column name
* @return CDbColumnSchema metadata of the named column. Null if the named column does not exist.
*/
public function getColumn($name)
{
return isset($this->columns[$name]) ? $this->columns[$name] : null;
}
/**
* @return array list of column names
*/
public function getColumnNames()
{
return array_keys($this->columns);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment