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

2021-12-30 00:00:00 sql php drupal drupal-7

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

我检查了文档这个对象,也用谷歌搜索过,但找不到有效的答案.

解决方案

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

class ExportableSelectQuery {公共静态函数 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);}别的 {$_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);}别的 {$_string = str_replace(':db_condition_placeholder_' . $k, "'" . $c['value'] . "'", $_string);}}//echo('

');//var_dump($_fields);//var_dump($_conditions);//var_dump($_tables);//var_dump($_string);//echo('</pre>');//死();返回 $_string;}}

此代码的使用现在很简单(如果您在某处只有 SelectQuery 对象):

die(ExportableSelectQuery::toSql($query));

我想扩展原始的SelectQuery对象,并提供获取SQL代码的方法,但Drupal的db_select函数返回SelectQuery,所以我将不得不更改 db_select 函数或将返回的对象转换为 ExportableSelectQuery.

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

I managed to print a string using __toString() magic method, but in this string I see placeholders (for conditions params), and it doesn't work as SQL query.

I checked documentation of this object, and also looked in google, but couldn't find a working answer.

解决方案

Basing on question's comments (thanks @Scuzzy for inspiration) I wrote some simple piece of code to convert SelectQuery object:

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;
    }
}

Usage of this code is now simple (if you only have SelectQuery object somewhere):

die(ExportableSelectQuery::toSql($query));

I was thinking about extending original SelectQuery object, and provide method to get SQL code, but Drupal's db_select function returns SelectQuery, so I will have to either change db_select function or cast returned object to ExportableSelectQuery.

Also this is not probably best solution I could write, but assuming limit of time and purpose it solved my problem just fine.

相关文章