雄辩的是:查询在哪里执行

时间:2019-07-09 13:35:28

标签: php laravel eloquent

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

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

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

1 个答案:

答案 0 :(得分:0)

经过大量的故障排除和测试之后,我找到了解决问题的方法。在类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 ++程序将执行准备好的状态表,而不执行已解析的查询。
适合我的情况的修改方法如下所示。现在已经快速又肮脏地进行了测试,以测试是否可能,但是您明白了。雄辩地使用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);
}
相关问题