コード例 #1
0
ファイル: SelectBuilder.php プロジェクト: romeoz/rock-db
 /**
  * @param array      $params
  * @throws DbException
  * @return array
  */
 public function build(&$params = [])
 {
     /** @var Connection $connection */
     $connection = Instance::ensure($this->connection, Connection::className());
     $result = [];
     foreach ($this->selects as $key => $select) {
         $alias = false;
         $aliasSeparator = null;
         if (is_array($select) && !is_string($key) && !is_string(key($select))) {
             $select[1] = Helper::getValue($select[1], false, true);
             $select[2] = Helper::getValue($select[2], null, true);
             list($select, $alias, $aliasSeparator) = $select;
         }
         if ($select instanceof ActiveQuery) {
             if (!isset($select->modelClass)) {
                 continue;
             }
             /** @var ActiveRecord $class */
             $class = $select->modelClass;
             $table = $class::tableAlias() ?: $class::tableName();
             $tableAlias = $table;
             if (is_string($alias)) {
                 $tableAlias = $alias;
                 $alias = true;
             }
             $connection = $class::getConnection();
             if (!($columns = $select->select)) {
                 continue;
             }
         } elseif (is_array($select)) {
             if (!is_string($key)) {
                 $table = key($select);
                 $select[0] = Helper::getValue($select[0], false, true);
                 $select[1] = Helper::getValue($select[1], null, true);
                 list($alias, $aliasSeparator) = $select;
                 $columns = current($select);
             } else {
                 $table = $key;
                 $columns = $select;
             }
             $tableAlias = $table;
             if (is_string($alias)) {
                 $tableAlias = $alias;
                 $alias = true;
             }
         } else {
             throw new DbException(DbException::WRONG_TYPE, ['name' => json_encode($select)]);
         }
         $aliasSeparator = Helper::getValue($aliasSeparator, $connection->aliasSeparator);
         foreach ($columns as $i => $column) {
             if ($column instanceof Expression) {
                 $columns[$i] = $column->expression;
                 $params = array_merge($params, $column->params);
             } elseif (is_string($i)) {
                 if (strpos($column, '(') === false) {
                     $column = $this->connection->quoteColumnName($column);
                 }
                 $columns[$i] = "{$column} AS " . $connection->quoteSimpleColumnName($i);
             } elseif (strpos($column, '(') === false) {
                 if (preg_match('/^(.*?)(?i:\\s+as\\s+|\\s+)([\\w\\-_\\.]+)$/', $column, $matches)) {
                     $matches[2] = $alias === true ? $tableAlias . $aliasSeparator . $matches[2] : $matches[2];
                     $columns[$i] = "{{{$table}}}." . $this->connection->quoteColumnName($matches[1]) . ' AS ' . $connection->quoteSimpleColumnName($matches[2]);
                 } else {
                     $columns[$i] = "{{{$table}}}." . $this->connection->quoteColumnName($column) . ($alias === true ? ' AS ' . $connection->quoteSimpleColumnName($tableAlias . $aliasSeparator . $column) : null);
                 }
             } elseif (strpos($column, '(') !== false) {
                 if (preg_match('/^(.*?)(?i:\\s+as\\s+|\\s+)([\\w\\-_\\.]+)$/', $column, $matches)) {
                     $matches[2] = $alias === true ? $tableAlias . $aliasSeparator . $matches[2] : $matches[2];
                     $columns[$i] = "{$matches['1']} AS " . $connection->quoteSimpleColumnName($matches[2]);
                 } else {
                     $columns[$i] = "{{{$table}}}." . $this->connection->quoteColumnName($column) . ($alias === true ? ' AS ' . $connection->quoteSimpleColumnName($tableAlias . $aliasSeparator . $column) : null);
                 }
             }
         }
         $result = array_merge($result, $columns);
     }
     return implode(', ', $result);
 }
コード例 #2
0
ファイル: classes.php プロジェクト: romeoz/rock
<?php

use rock\base\Alias;
use rock\db\BatchQueryResult;
use rock\rbac\Permission;
use rock\rbac\Role;
use rock\Rock;
use rock\security\Security;
use rock\template\Template;
return array_merge(['route' => ['class' => \rock\route\Route::className()], 'access' => ['class' => \rock\access\Access::className()], 'behavior' => ['class' => \rock\components\Behavior::className()], 'db' => ['class' => \rock\db\Connection::className(), 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'dsn' => 'mysql:host=localhost;dbname=rockdemo;charset=utf8', 'tablePrefix' => 'spt_', 'aliasSeparator' => '__'], 'BatchQueryResult' => ['class' => BatchQueryResult::className()], 'template' => ['class' => Template::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale'], 'autoEscape' => Template::ESCAPE | Template::TO_TYPE, 'handlerLink' => function ($link, Template $template, array $params = []) {
    if (!($link = Alias::getAlias("@{$link}", [], false))) {
        return '#';
    }
    return $template->autoEscape(\rock\template\filters\BaseFilter::modifyUrl($link, $params));
}, 'extensions' => ['cfg' => function (array $keys) {
    return \rock\helpers\ArrayHelper::getValue(Rock::$config, $keys);
}, 'user' => function (array $keys) {
    if (current($keys) === 'isGuest') {
        return Rock::$app->user->isGuest();
    } elseif (in_array(current($keys), ['isLogged', 'isAuthenticated'], true)) {
        return !Rock::$app->user->isGuest();
    }
    return \rock\helpers\ArrayHelper::getValue(Rock::$app->user->getAll(), $keys);
}, 'call' => function (array $call, array $params = [], Template $template) {
    if (!isset($call[1])) {
        $call[1] = null;
    }
    list($class, $method) = $call;
    if ($class === 'context') {
        $object = $template->context;
        $function = [$object, $method];
コード例 #3
0
ファイル: Query.php プロジェクト: romeoz/rock-db
 /**
  * @return Connection connection instance
  */
 public function getConnection()
 {
     $this->connection = Instance::ensure($this->connection, Connection::className());
     return $this->calculateCacheParams($this->connection);
 }
コード例 #4
0
ファイル: ActiveRecord.php プロジェクト: romeoz/rock-db
 /**
  * Returns the database connection used by this AR class.
  * By default, the "db" application component is used as the database connection.
  * You may override this method if you want to use a different database connection.
  * @return Connection the database connection used by this AR class.
  */
 public static function getConnection()
 {
     return Instance::ensure('db', Connection::className());
 }
コード例 #5
0
ファイル: Migration.php プロジェクト: romeoz/rock-db
 /**
  * Initializes the migration.
  * This method will set {@see \rock\db\Migration::$connection} to be the 'db' application component, if it is null.
  */
 public function init()
 {
     $this->connection = Instance::ensure($this->connection, Connection::className());
     $this->connection->getSchema()->refresh();
 }