php - 在 Emacs 中使用 SQL 查询编写 PHP

标签 php mysql emacs

我应该在 Emacs 中使用什么来开发带有 SQL 查询的 PHP 文件?

缩进缓冲区时,代码应如下所示:

<?php
$query = "
    SELECT
        id,
        name
    FROM 
        products
    WHERE 
        id > 12"
?>

在 web 模式和 php 模式下,它看起来像这样:

<?php 
$query = "
SELECT
id,
name
FROM
products
WHERE
id > 12"
?>

如果这不可能,一种替代方法是让它启用手动缩进(使用 TABShiftTAB,例如Sublime 和其他编辑器)在 PHP 代码中使用多行字符串时。我该怎么做?

最佳答案

当缓冲区缩进时自动执行此操作将非常困难,您可以尝试多种主要模式,但这并不理想。

一个解决方案是编写一个函数来格式化光标下的 sql,然后您可以在完成查询字符串的编写后手动运行此命令。

此示例需要两个包:expand-regionsql-indent,它们都可以在 MELPA 上下载。

如果您安装了软件包,此函数将在指定位置格式化 SQL,与注释中的解决方案不同,这还可以根据代码周围的深度缩进整个 SQL 字符串。

(defun sql-indent-string ()
  "Indents the string under the cursor as SQL."
  (interactive)
  (save-excursion
    (er/mark-inside-quotes)
    (let* ((text (buffer-substring-no-properties (region-beginning) (region-end)))
           (pos (region-beginning))
           (column (progn (goto-char pos) (current-column)))
           (formatted-text (with-temp-buffer
                             (insert text)
                             (delete-trailing-whitespace)
                             (sql-indent-buffer)
                             (replace-string "\n" (concat "\n" (make-string column (string-to-char " "))) nil (point-min) (point-max))
                             (buffer-string))))
      (delete-region (region-beginning) (region-end))
      (goto-char pos)
      (insert formatted-text))))

该函数的工作原理是复制光标下的字符串并将其移动到一个临时缓冲区,sql-indent 将在其中格式化它,然后返回到原始缓冲区并将旧字符串替换为新字符串。它很实用但不漂亮。

enter image description here

关于php - 在 Emacs 中使用 SQL 查询编写 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397274/

相关文章:

php - 快速计算每种产品过去 6、3、1 个月的平均月销售额

php - 如何使用 mysqli 在事务中仅创建部分查询?

PHP缓存sql结果

c++ - 如何防止 emacs C/C++ 模式自动更正?

debugging - Emacs中如何实现错误回溯?

emacs - 如何在 Emacs 23 中获得底部而不是右侧的编译缓冲区?

php - 如何处理抽象类 PDO 中的错误

php - 根据id显示一条记录

cs-cart中的mysql查询

mysql - 为什么 if 语句的 'begin' 关键字在存储过程中显示错误。 (mysql)?