Created
April 5, 2011 07:41
Query Builder для Yii
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
public function buildQuery($query) | |
{ | |
$sql=isset($query['distinct']) && $query['distinct'] ? 'SELECT DISTINCT' : 'SELECT'; | |
$sql.=' '.(isset($query['select']) ? $query['select'] : '*'); | |
if(isset($query['from'])) | |
$sql.="\nFROM ".$query['from']; | |
else | |
throw new CDbException(Yii::t('yii','The DB query must contain the "from" portion.')); | |
if(isset($query['where'])) | |
$sql.="\nWHERE ".$query['where']; | |
if(isset($query['join'])) | |
$sql.="\n".(is_array($query['join']) ? implode("\n",$query['join']) : $query['join']); | |
if(isset($query['group'])) | |
$sql.="\nGROUP BY ".$query['group']; | |
if(isset($query['having'])) | |
$sql.="\nHAVING ".$query['having']; | |
if(isset($query['order'])) | |
$sql.="\nORDER BY ".$query['order']; | |
$limit=isset($query['limit']) ? (int)$query['limit'] : -1; | |
$offset=isset($query['offset']) ? (int)$query['offset'] : -1; | |
if($limit>=0 && $offset>=0) | |
$sql=$this->_connection->getCommandBuilder()->applyLimit($sql,$limit,$offset); | |
if(isset($query['union'])) | |
$sql.="\nUNION (\n".(is_array($query['union']) ? implode("\n) UNION (\n",$query['union']) : $query['union']) . ')'; | |
return $sql; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment