How to convert SelectQuery object to SQL string?(如何将 SelectQuery 对象转换为 SQL 字符串?)
问题描述
我设法使用 __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.
这篇关于如何将 SelectQuery 对象转换为 SQL 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 SelectQuery 对象转换为 SQL 字符串?
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01