diff options
Diffstat (limited to 'framework/Data/Common')
| -rw-r--r-- | framework/Data/Common/Mysql/TMysqlMetaData.php | 14 | ||||
| -rw-r--r-- | framework/Data/Common/TDbCommandBuilder.php | 154 | ||||
| -rw-r--r-- | framework/Data/Common/TDbTableInfo.php | 24 | 
3 files changed, 169 insertions, 23 deletions
diff --git a/framework/Data/Common/Mysql/TMysqlMetaData.php b/framework/Data/Common/Mysql/TMysqlMetaData.php index ae552cf3..fad33cea 100644 --- a/framework/Data/Common/Mysql/TMysqlMetaData.php +++ b/framework/Data/Common/Mysql/TMysqlMetaData.php @@ -4,7 +4,7 @@   *
   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
   * @link http://www.pradosoft.com/
 - * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft
   * @license http://www.pradosoft.com/license/
   * @version $Id$
   * @package System.Data.Common.Mysql
 @@ -110,7 +110,7 @@ class TMysqlMetaData extends TDbMetaData  			//find SET/ENUM values
  			if($this->isEnumSetType($info['DbType']))
 -				$info['DbTypeValues'] = preg_split('/\s*,\s*|\s+/', preg_replace('/\'|"/', '', $match[1]));
 +				$info['DbTypeValues'] = preg_split("/[',]/S", $match[1], -1, PREG_SPLIT_NO_EMPTY);
  			//find column size, precision and scale
  			$pscale = array();
 @@ -212,9 +212,9 @@ class TMysqlMetaData extends TDbMetaData  		if($this->getServerVersion()<5.01)
  			return false;
  		if($schemaName!==null)
 -			$sql = "SHOW FULL TABLES FROM `{$schemaName}` LIKE ':table'";
 +			$sql = "SHOW FULL TABLES FROM `{$schemaName}` LIKE :table";
  		else
 -			$sql = "SHOW FULL TABLES LIKE ':table'";
 +			$sql = "SHOW FULL TABLES LIKE :table";
  		$command = $this->getDbConnection()->createCommand($sql);
  		$command->bindValue(':table', $tableName);
 @@ -246,8 +246,8 @@ class TMysqlMetaData extends TDbMetaData  			if($row['Key_name']==='PRIMARY')
  				$primary[] = $row['Column_name'];
  		}
 -        // MySQL version was increased to >=5.1.21 instead of 5.x -        // due to a MySQL bug (http://bugs.mysql.com/bug.php?id=19588) +        // MySQL version was increased to >=5.1.21 instead of 5.x
 +        // due to a MySQL bug (http://bugs.mysql.com/bug.php?id=19588)
  		if($this->getServerVersion() >= 5.121)
  			$foreign = $this->getForeignConstraints($schemaName,$tableName);
  		else
 @@ -353,4 +353,4 @@ EOD;  		return false;
  	}
  }
 - +
 diff --git a/framework/Data/Common/TDbCommandBuilder.php b/framework/Data/Common/TDbCommandBuilder.php index 155a62f8..0dc13e7e 100644 --- a/framework/Data/Common/TDbCommandBuilder.php +++ b/framework/Data/Common/TDbCommandBuilder.php @@ -157,16 +157,158 @@ class TDbCommandBuilder extends TComponent  	}
  	/**
 +	 *
 +	 * Different behavior depends on type of passed data
 +	 * string
 +	 * 	usage without modification
 +	 *
 +	 * null
 +	 * 	will be expanded to full list of quoted table column names (quoting depends on database)
 +	 *
 +	 * array
 +	 * - Column names will be quoted if used as key or value of array
 +	 * 	<code>
 +	 * 	array('col1', 'col2', 'col2')
 +	 * 	// SELECT `col1`, `col2`, `col3` FROM...
 +	 * 	</code>
 +	 *
 +	 * - Column aliasing
 +	 * <code>
 +	 * array('mycol1' => 'col1', 'mycol2' => 'COUNT(*)')
 +	 * // SELECT `col1` AS mycol1, COUNT(*) AS mycol2 FROM...
 +	 * </code>
 +	 *
 +	 * - NULL and scalar values (strings will be quoted depending on database)
 +	 * <code>
 +	 * array('col1' => 'my custom string', 'col2' => 1.0, 'col3' => 'NULL')
 +	 * // SELECT "my custom string" AS `col1`, 1.0 AS `col2`, NULL AS `col3` FROM...
 +	 * </code>
 +	 *
 +	 * - If the *-wildcard char is used as key or value, add the full list of quoted table column names
 +	 * <code>
 +	 * array('col1' => 'NULL', '*')
 +	 * // SELECT `col1`, `col2`, `col3`, NULL AS `col1` FROM...
 +	 * </code>
 +	 * @param mixed $value
 +	 * @return array of generated fields - use implode(', ', $selectfieldlist) to collapse field list for usage
 +	 * @since 3.1.7
 +	 * @todo add support for table aliasing
 +	 * @todo add support for quoting of column aliasing
 +	 */
 +	public function getSelectFieldList($data='*') {
 +		if(is_scalar($data)) {
 +			$tmp = explode(',', $data);
 +			$result = array();
 +			foreach($tmp as $v)
 +				$result[] = trim($v);
 +			return $result;
 +		}
 +
 +		$bHasWildcard = false;
 +		$result = array();
 +		if(is_array($data) || $data instanceof Traversable) {
 +			$columns = $this->getTableInfo()->getColumns();
 +			foreach($data as $key=>$value) {
 +				if($key==='*' || $value==='*') {
 +					$bHasWildcard = true;
 +					continue;
 +				}
 +
 +				if(strToUpper($key)==='NULL') {
 +					$result[] = 'NULL';
 +					continue;
 +				}
 +
 +				if(strpos($key, '(')!==false && strpos($key, ')')!==false) {
 +					$result[] = $key;
 +					continue;
 +				}
 +
 +				if(stripos($key, 'AS')!==false) {
 +					$result[] = $key;
 +					continue;
 +				}
 +
 +				if(stripos($value, 'AS')!==false) {
 +					$result[] = $value;
 +					continue;
 +				}
 +
 +				$v = isset($columns[$value]);
 +				$k = isset($columns[$key]);
 +				if(is_integer($key) && $v) {
 +					$key = $value;
 +					$k = $v;
 +				}
 +
 +				if(strToUpper($value)==='NULL') {
 +					if($k)
 +						$result[] = 'NULL AS ' . $columns[$key]->getColumnName();
 +					else
 +						$result[] = 'NULL' . (is_string($key) ? (' AS ' . (string)$key) : '');
 +					continue;
 +				}
 +
 +				if(strpos($value, '(')!==false && strpos($value, ')')!==false) {
 +					if($k)
 +						$result[] = $value . ' AS ' . $columns[$key]->getColumnName();
 +					else
 +						$result[] = $value . (is_string($key) ? (' AS ' . (string)$key) : '');
 +					continue;
 +				}
 +
 +				if($v && $key==$value) {
 +					$result[] = $columns[$value]->getColumnName();
 +					continue;
 +				}
 +
 +				if($k && $value==null) {
 +					$result[] = $columns[$key]->getColumnName();
 +					continue;
 +				}
 +
 +				if(is_string($key) && $v) {
 +					$result[] = $columns[$value]->getColumnName() . ' AS ' . $key;
 +					continue;
 +				}
 +
 +				if(is_numeric($value) && $k) {
 +					$result[] = $value . ' AS ' . $columns[$key]->getColumnName();
 +					continue;
 +				}
 +
 +				if(is_string($value) && $k) {
 +					$result[] = $this->getDbConnection()->quoteString($value) . ' AS ' . $columns[$key]->getColumnName();
 +					continue;
 +				}
 +
 +				if(!$v && !$k && is_integer($key)) {
 +					$result[] = is_numeric($value) ? $value : $this->getDbConnection()->quoteString((string)$value);
 +					continue;
 +				}
 +
 +				$result[] = (is_numeric($value) ? $value : $this->getDbConnection()->quoteString((string)$value)) . ' AS ' . $key;
 +			}
 +		}
 +
 +		if($data===null || count($result) == 0 || $bHasWildcard)
 +			$result = $result = array_merge($this->getTableInfo()->getColumnNames(), $result);
 +
 +		return $result;
 +	}
 +
 +	/**
  	 * Appends the $where condition to the string "SELECT * FROM tableName WHERE ".
  	 * The tableName is obtained from the {@link setTableInfo TableInfo} property.
  	 * @param string query condition
  	 * @param array condition parameters.
  	 * @return TDbCommand query command.
  	 */
 -	public function createFindCommand($where='1=1', $parameters=array(), $ordering=array(), $limit=-1, $offset=-1)
 +	public function createFindCommand($where='1=1', $parameters=array(), $ordering=array(), $limit=-1, $offset=-1, $select='*')
  	{
  		$table = $this->getTableInfo()->getTableFullName();
 -		$sql = "SELECT * FROM {$table}";
 +		$fields = implode(', ', $this -> getSelectFieldList($select));
 +		$sql = "SELECT {$fields} FROM {$table}";
  		if(!empty($where))
  			$sql .= " WHERE {$where}";
  		return $this->applyCriterias($sql, $parameters, $ordering, $limit, $offset);
 @@ -191,11 +333,7 @@ class TDbCommandBuilder extends TComponent  	 */
  	public function createCountCommand($where='1=1', $parameters=array(),$ordering=array(), $limit=-1, $offset=-1)
  	{
 -		$table = $this->getTableInfo()->getTableFullName();
 -		$sql = "SELECT COUNT(*) FROM {$table}";
 -		if(!empty($where))
 -			$sql .= " WHERE {$where}";
 -		return $this->applyCriterias($sql, $parameters, $ordering, $limit, $offset);
 +		return $this->createFindCommand($where, $parameters, $ordering, $limit, $offset, 'COUNT(*)');
  	}
  	/**
 @@ -368,4 +506,4 @@ class TDbCommandBuilder extends TComponent  	}
  }
 -?>
 +?>
\ No newline at end of file diff --git a/framework/Data/Common/TDbTableInfo.php b/framework/Data/Common/TDbTableInfo.php index e2aae3d0..455dbc33 100644 --- a/framework/Data/Common/TDbTableInfo.php +++ b/framework/Data/Common/TDbTableInfo.php @@ -27,7 +27,13 @@ class TDbTableInfo extends TComponent  	private $_columns;
 -	private $_lowercase;
 +	private $_lowercase; + +	/** +	 * @var null|array +	 * @since 3.1.7 +	 */
 +	private $_names = null;
  	/**
  	 * Sets the database table meta data information.
 @@ -118,11 +124,14 @@ class TDbTableInfo extends TComponent  	 * @return array table column names (identifier quoted)
  	 */
  	public function getColumnNames()
 -	{
 -		$names=array();
 -		foreach($this->getColumns() as $column)
 -			$names[] = $column->getColumnName();
 -		return $names;
 +	{ +		if($this->_names===null) +		{
 +			$this->_names=array();
 +			foreach($this->getColumns() as $column)
 +				$this->_names[] = $column->getColumnName(); +		}
 +		return $this->_names;
  	}
  	/**
 @@ -154,5 +163,4 @@ class TDbTableInfo extends TComponent  		}
  		return $this->_lowercase;
  	}
 -}
 - +}
\ No newline at end of file  | 
