Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将SelectQuery对象转换为SQL字符串?_Php_Sql_Drupal_Drupal 7 - Fatal编程技术网

Php 如何将SelectQuery对象转换为SQL字符串?

Php 如何将SelectQuery对象转换为SQL字符串?,php,sql,drupal,drupal-7,Php,Sql,Drupal,Drupal 7,我使用_toString()魔术方法打印了一个字符串,但在这个字符串中我看到了占位符(用于条件参数),它不能作为SQL查询 我检查了这个对象的名称,也在谷歌上搜索了一下,但找不到有效的答案。根据问题的评论(感谢@Scuzzy的启发),我编写了一些简单的代码来转换SelectQuery对象: class ExportableSelectQuery { public static function toSql(SelectQuery $obj) { $_string =

我使用_toString()魔术方法打印了一个字符串,但在这个字符串中我看到了占位符(用于条件参数),它不能作为SQL查询


我检查了这个对象的名称,也在谷歌上搜索了一下,但找不到有效的答案。

根据问题的评论(感谢@Scuzzy的启发),我编写了一些简单的代码来转换
SelectQuery
对象:

class ExportableSelectQuery {

    public static function toSql(SelectQuery $obj) {

        $_string = $obj->__toString();
        $_conditions = $obj->conditions();
        $_tables = $obj->getTables();
        $_fields = $obj->getFields();

        foreach($_tables as $k => $t) {
            if(!empty($t['alias'])) {
                $_string = str_replace('{' . $t['table'] . '}', $t['table'] . ' as', $_string);
            }
            else {
                $_string = str_replace('{' . $t['table'] . '}', $t['table'], $_string);
            }
        }

        foreach($_conditions as $k => $c) {
            if(is_int($c['value'])) {
                $_string = str_replace(':db_condition_placeholder_' . $k, $c['value'], $_string);
            }
            else {
                $_string = str_replace(':db_condition_placeholder_' . $k, "'" . $c['value'] . "'", $_string);
            }
        }

        //echo('<pre>');
        //var_dump($_fields);
        //var_dump($_conditions);
        //var_dump($_tables);
        //var_dump($_string);
        //echo('</pre>');
        //die();

        return $_string;
    }
}
我曾考虑扩展原始的
SelectQuery
对象,并提供获取SQL代码的方法,但Drupal的
db\u select
函数返回
SelectQuery
,因此我必须更改
db\u select
函数或将返回的对象强制转换为
ExportableSelectQuery


此外,这可能不是我能写的最好的解决方案,但假设时间和目的有限,它很好地解决了我的问题。

如果您希望从例如“EntityFieldQyery”中获取SQL,您可以使用类似的方法

  • 向查询添加标记

    function YOURMODULE_query_EFQDumper_alter(QueryAlterableInterface $query)
    {
     //echo ExportableSelectQuery::toSql($query);
     //die();
    }
    

  • 基于Carlos comment的解决方案

    如果它是一个准备好的SQL语句,那么就没有查询的“编译”版本,所有查询都由数据库服务器内部处理。有没有办法在phpmyadmin中手动执行它?或者只有使用prepare、set和execute命令才能实现?您基本上需要找到一种方法来模拟存储过程的工作方式,方法是使用查询参数并相应地替换语句中的占位符。但这并不是drupal解决方案所需要的,只是概念而已。
    $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'page')
          ->addTag('EFQDumper'); //<=== TAG
    
    function YOURMODULE_query_EFQDumper_alter(QueryAlterableInterface $query)
    {
     //echo ExportableSelectQuery::toSql($query);
     //die();
    }