繁体   English   中英

Eloquent:查询在哪里执行

[英]Eloquent: where are queries executed

Atm 我正在为用 Laravel 编写的现有应用程序构建一个非常具体的解决方案。 该解决方案在 c++ 中执行查询修改数据,进行排序并返回结果。 这个 c++ 程序是通过 PHP 扩展加载的,并提供一个方法来处理这个逻辑。
扩展提供的方法应该使用 Eloquent 在 Laravel 中实现,我多年来一直在查看源代码以找到执行使用 Eloquensts Builder 构建的查询的特定方法。

我在哪里可以找到实际执行查询的方法?

为什么是 C++? 我听说你在想。 应该在多个线程上的多个模式(和/或数据库)上执行查询以提高性能。 Atm 正在使用 100 多个模式,每个模式在每个表中包含数千条记录。

经过大量故障排除和测试后,我找到了解决问题的方法。 Illuminate\\Database\\Query\\Builder您可以找到一个名为runSelect()的方法。 此方法针对给定的连接运行 select 语句,并将选定的行作为数组返回。

/**
 * Run the query as a "select" statement against the connection.
 *
 * @return array
 */
protected function runSelect()
{
    return $this->connection->select(
        $this->toSql(), $this->getBindings(), ! $this->useWritePdo
    );
}

我做了什么来测试我在 C++ 中的实现来运行选择,我将$this->getBindings()的返回值映射到一个新数组以对某些字符串进行一些必要的修改,并在准备好的语句上做了一个简单的str_replace_array以获得完整的查询。 最终,c++ 程序将执行准备好的语句而不是解析的查询。
适合我的情况的修改方法如下所示。 这已经快速而肮脏地完成,以测试是否可能,但您明白了。 除了 eloquent 中的count()方法外,作为一种魅力工作。

/**
 * Run the query as a "select" statement against the connection.
 *
 * @return array
 */
protected function runSelect()
{
    $bindings = [];
    foreach ($this->getBindings() as $key => $value) {
        $bindings[] = $value; // Some other logic to manipulate strings will be added.
    }

    $query = str_replace_array('?', $bindings, $this->toSql());
    $schemas = ['schema1', 'schema2', 'schema3', 'schema4']; // Will be fetched from master DB.

    return runSelectCpp($schemas, $query);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM