Creates a DB command that can be used to execute this query.
@param AbstractConnection $db the DB connection used to create the DB command.
If null, the DB connection returned by [[modelClass]] will be used.
@return Command the created DB command instance
329121
public function createCommand($db = null)
    {
        if ($this->primaryModel !== null) {
            // lazy loading
            if (is_array($this->via)) {
                // via relation
                /** @var $viaQuery ActiveQuery */
                list($viaName, $viaQuery) = $this->via;
                if ($viaQuery->multiple) {
                    $viaModels = $viaQuery->all();
                    $this->primaryModel->populateRelation($viaName, $viaModels);
                } else {
                    $model = $viaQuery->one();
                    $this->primaryModel->populateRelation($viaName, $model);
                    $viaModels = $model === null ? [] : [$model];
                }
                $this->filterByModels($viaModels);
            } else {
                $this->filterByModels([$this->primaryModel]);
            }
        }

        /* @var $modelClass ActiveRecord */
        $modelClass = $this->modelClass;

        if ($db === null) {
            $db = $modelClass::getDb();
        }
        if ($this->from === null) {
            $this->from = $modelClass::tableName();
        }

        return parent::createCommand($db);
    }
1026152
public static function getInstance($db = null) {

        if (empty(self::$instance)) {

            if (is_null($db)) {
                throw new \RuntimeException('Empty params');
            }

            self::$instance = new Acl($db);
        }

        return self::$instance;
    }
111542
public static function db()
    {
        if (self::$db === null) {
            $rc = rcube::get_instance();
            $db = rcube_db::factory($rc->config->get('db_dsnw'));

            $db->set_debug((bool)$rc->config->get('sql_debug'));

            // Connect to database
            $db->db_connect('w');

            if (!$db->is_connected()) {
                rcube::raise_error("Error connecting to database: " . $db->is_error(), false, true);
            }

            self::$db = $db;
        }

        return self::$db;
    }
223457
public static void command(final ODatabaseObject db, final String command, final Object... args) {
        db.command(new OCommandSQL(String.format(command, args))).execute();
    }
822936
private function createBuilderFromConnection(Connection $connection, string $modelClass): ModelQueryBuilder
    {
        return $this->getFactory()->createModelQueryBuilder($connection, $modelClass, $this->getModelSchemas());
    }
438364
public function set_connection($db)
	{
		if ( ! $db instanceof \Database_Connection)
		{
			// Get the database instance
			$db = \Database_Connection::instance($db);
		}
		$this->_connection = $db;

		return $this;
	}
252650
public function compile($db = null)
	{
		if ( ! $db instanceof \Database_Connection)
		{
			// Get the database instance
			$db = $this->_connection ?: \Database_Connection::instance($db);
		}

		// Callback to quote identifiers
		$quote_ident = array($db, 'quote_identifier');

		// Callback to quote tables
		$quote_table = array($db, 'quote_table');

		// Start a selection query
		$query = 'SELECT ';

		if ($this->_distinct === TRUE)
		{
			// Select only unique results
			$query .= 'DISTINCT ';
		}

		if (empty($this->_select))
		{
			// Select all columns
			$query .= '*';
		}
		else
		{
			// Select all columns
			$query .= implode(', ', array_unique(array_map($quote_ident, $this->_select)));
		}

		if ( ! empty($this->_from))
		{
			// Set tables to select from
			$query .= ' FROM '.implode(', ', array_unique(array_map($quote_table, $this->_from)));
		}

		if ( ! empty($this->_join))
		{
			// Add tables to join
			$query .= ' '.$this->_compile_join($db, $this->_join);
		}

		if ( ! empty($this->_where))
		{
			// Add selection conditions
			$query .= ' WHERE '.$this->_compile_conditions($db, $this->_where);
		}

		if ( ! empty($this->_group_by))
		{
			// Add sorting
			$query .= ' GROUP BY '.implode(', ', array_map($quote_ident, $this->_group_by));
		}

		if ( ! empty($this->_having))
		{
			// Add filtering conditions
			$query .= ' HAVING '.$this->_compile_conditions($db, $this->_having);
		}

		if ( ! empty($this->_order_by))
		{
			// Add sorting
			$query .= ' '.$this->_compile_order_by($db, $this->_order_by);
		}

		if ($this->_limit !== NULL)
		{
			// Add limiting
			$query .= ' LIMIT '.$this->_limit;
		}

		if ($this->_offset !== NULL)
		{
			// Add offsets
			$query .= ' OFFSET '.$this->_offset;
		}

		return $query;
	}
326021
public function buildCursor($db = null)
    {
        $this->prepare();

        $options = $this->options;
        if (!empty($this->orderBy)) {
            $options['sort'] = $this->orderBy;
        }
        $options['limit'] = $this->limit;
        $options['skip'] = $this->offset;

        $cursor = $this->getCollection($db)->find($this->composeCondition(), $this->select, $options);

        return $cursor;
    }
439588
final public function db() {
        if (!$this->connector) {
            $this->connector = $this->getWorker()->getConnector();
        }
        return $this->connector;
    }
908217
public function connect(): DatabaseConnection
    {
        if (null !== $this->pdo) {
            return $this;
        }

        try {
            $pdoCreator = $this->getPdoCreator();
            $this->pdo  = $pdoCreator($this->configuration);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            if ($this->configuration->hasInitialQuery()) {
                $this->pdo->query($this->configuration->getInitialQuery());
            }
        } catch (PDOException $pdoe) {
            throw new DatabaseException($pdoe->getMessage(), $pdoe);
        }

        return $this;
    }
709870
public function setDBO(&$db)
	{
		// Make sure the new database object is a Database.
		if (!($db instanceof \Hubzero\Database\Driver))
		{
			return false;
		}

		$this->_db = &$db;

		return true;
	}
577075
public function createSqlCommand($sql,$params=array())
	{
		$command=$this->_connection->createCommand($sql);
		$this->bindValues($command,$params);
		return $command;
	}
574722
public function create(array $data, $databaseConnectionName = null)
  {
    if(empty($databaseConnectionName))
    {
      $databaseConnectionName = $this->databaseConnectionName;
    }

    $Apportionment = new Apportionment();
    $Apportionment->setConnection($databaseConnectionName);
    $Apportionment->fill($data)->save();

    return $Apportionment;
  }
841465
public function executeModelChanges($db)
    {
        $count = 0;
        $models = Bootstrap::getSingleton()->getModels();
        $instanceModels = [];
        foreach ($models as $model) {
            if (class_exists($model)) {
                $instance = new $model(null);
                $instance->db = $db;
                if ($instance instanceof Model) {
                    $instanceModels[] = $instance;
                }
            }
        }
        $this->checkForDuplicatedTables($instanceModels);
        foreach ($instanceModels as $model) {
            $this->executeModel($db, $model);
        }
        $this->setDatabaseUpdate();
        return $count;
    }
730026
public function command($command, $db)
    {
        $start_time = microtime(true);
        if ($start_time - $this->_last_heartbeat > 1.0) {
            $this->_last_heartbeat = null;
        }
        try {
            $cursor = $this->_getManager()->executeCommand($db, new Command($command));
            $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
            return $cursor->toArray();
        } catch (\Exception $exception) {
            throw new MongodbException($exception->getMessage(), $exception->getCode(), $exception);
        }
    }
1206264
public function command($command, $db = null)
    {
        if (!$db) {
            $db = $this->_default_db;
        }

        $this->eventsManager->fireEvent('mongodb:beforeCommand', $this, compact('db', 'command'));

        /** @var \ManaPHP\Mongodb\ConnectionInterface $connection */
        $connection = $this->poolManager->pop($this);
        try {
            $start_time = microtime(true);
            $result = $connection->command($command, $db);
            $elapsed = round(microtime(true) - $start_time, 3);
        } finally {
            $this->poolManager->push($this, $connection);
        }
        $this->eventsManager->fireEvent('mongodb:afterCommand', $this, compact('db', 'command', 'result', 'elapsed'));

        $count = count($result);
        $command_name = key($command);
        if (strpos('ping,aggregate,count,distinct,group,mapReduce,geoNear,geoSearch,find,' .
                'authenticate,listDatabases,listCollections,listIndexes', $command_name) !== false) {
            $this->logger->debug(compact('db', 'command', 'count', 'elapsed'), 'mongodb.command.' . $command_name);
        } else {
            $this->logger->info(compact('db', 'command', 'count', 'elapsed'), 'mongodb.command.' . $command_name);
        }

        return $result;
    }
714222
public function delete($db = null)
    {
        $this->getDAO($this->getDB($db))->delete($this);
        return $this;
    }