|
36 | 36 | (require 'cl) |
37 | 37 | (require 'php-mode) |
38 | 38 | (require 'format-spec) |
| 39 | +(require 'json) |
| 40 | +(require 'sql) |
39 | 41 |
|
40 | 42 | ;; Silence byte compiler. |
41 | 43 | (defvar css-indent-level) |
@@ -239,7 +241,8 @@ get better filling in Doxygen comments." |
239 | 241 | (?f . drupal-insert-function) |
240 | 242 | (?m . drupal-module-name) |
241 | 243 | (?e . drupal-drush-php-eval) |
242 | | - (?t . drupal-wrap-string-in-t-function)) |
| 244 | + (?t . drupal-wrap-string-in-t-function) |
| 245 | + (?s . drupal-drush-sql-cli)) |
243 | 246 | "Map of mnemonic keys and functions for keyboard shortcuts. |
244 | 247 | See `drupal-mode-map'.") |
245 | 248 |
|
@@ -428,6 +431,10 @@ of the project)." |
428 | 431 | [menu-bar drupal cache-clear] |
429 | 432 | '(menu-item "Clear all caches" drupal-drush-cache-clear |
430 | 433 | :enable (and drupal-rootdir drupal-drush-program))) |
| 434 | +(define-key drupal-mode-map |
| 435 | + [menu-bar drupal sql-cli] |
| 436 | + '(menu-item "Open SQL shell" drupal-drush-sql-cli |
| 437 | + :enable (and drupal-rootdir drupal-drush-program))) |
431 | 438 |
|
432 | 439 | (define-key drupal-mode-map |
433 | 440 | [menu-bar drupal drupal-project drupal-project-bugs] |
@@ -519,6 +526,48 @@ buffer." |
519 | 526 | (search-forward-regexp "\\(\"\\|'\\)") |
520 | 527 | (insert ")"))))) |
521 | 528 |
|
| 529 | +(defun drupal-drush-sql-cli () |
| 530 | + "Run a SQL shell using \"drush sql-cli\" in a SQL-mode comint buffer." |
| 531 | + (interactive) |
| 532 | + (let* ((json-object-type 'plist) |
| 533 | + (config |
| 534 | + (json-read-from-string |
| 535 | + (with-temp-buffer |
| 536 | + (call-process drupal-drush-program nil t nil |
| 537 | + "sql-conf" "--format=json") |
| 538 | + (buffer-string))))) |
| 539 | + (when (not config) |
| 540 | + (error "No Drupal SQL configuration found.")) |
| 541 | + (destructuring-bind (&key database driver &allow-other-keys) config |
| 542 | + (let ((sql-interactive-product |
| 543 | + (drupal--db-driver-to-sql-product driver)) |
| 544 | + (start-buffer (current-buffer)) |
| 545 | + (sqli-buffer |
| 546 | + (make-comint (format "SQL (%s)" database) |
| 547 | + drupal-drush-program nil "sql-cli"))) |
| 548 | + (with-current-buffer sqli-buffer |
| 549 | + (sql-interactive-mode) |
| 550 | + (set (make-local-variable 'sql-buffer) |
| 551 | + (buffer-name (current-buffer))) |
| 552 | + |
| 553 | + ;; Set `sql-buffer' in the start buffer |
| 554 | + (with-current-buffer start-buffer |
| 555 | + (when (derived-mode-p 'sql-mode) |
| 556 | + (setq sql-buffer (buffer-name sqli-buffer)) |
| 557 | + (run-hooks 'sql-set-sqli-hook))) |
| 558 | + |
| 559 | + ;; All done. |
| 560 | + (run-hooks 'sql-login-hook) |
| 561 | + (pop-to-buffer sqli-buffer)))))) |
| 562 | + |
| 563 | +(defun drupal--db-driver-to-sql-product (driver) |
| 564 | + "Translate a Drupal DB driver name into a sql-mode symbol." |
| 565 | + (let ((driver (intern driver))) |
| 566 | + (cond |
| 567 | + ((eq driver 'pgsql) 'postgres) |
| 568 | + ((assq driver sql-product-alist) driver) |
| 569 | + (t 'ansi)))) |
| 570 | + |
522 | 571 |
|
523 | 572 |
|
524 | 573 | (defvar drupal-form-id-history nil |
|
0 commit comments