1

I'm somewhat new to emacs lisp and trying to learn the best way to do things.

My example task at hand is to generate a collection of "grant permissions on database" statements (this might be something i often do).

In order to do this most efficiently i thought i would need two lists, one of the databases and one of the permissions to apply.

I wrote one generic function to search and replace, and another to call that function and insert the needed text into my buffer.

Is this the best way to do this? Should i be looking at yasnippets, or maybe macros? Are while loops the preferred option for this? I'm just looking to be pointed in the right direction to be doing this type of work the emacs-way... in my vim days I'd probably do something like this in python or bash.

the (working, albeit not best practice?) code is below.
(Additional info is cygwin emacs 24.4, with evil via spacemacs.)

(setq database-list  
      (list 
       "[database_1]"
       "[database_2]"
       "[database_3]"))  

(setq perm-list 
      (list "EXECUTE"
            "SELECT"
            "SHOWPLAN"))  

(defun generic-string-replace-list (template search in-list )  
  "takes a string template in, a search token, and a list.  Iterates
through the search list generating an output string with the
searh/replace of each list item."
  (setq out "" )
  (while in-list
    (setq out (concat 
                    out 
                    (replace-regexp-in-string search (car in-list) template) 
                    "\n" ))  
    (setq in-list (cdr in-list)))
  out )


(defun generate-perm-list-for-db-list (perm-list database-list ) 
  (forward-line) 
  (while database-list
    (insert (concat "\nuse " (car database-list) ";\n" ))  
    (setq template (concat 
                       "grant \$perm to " 
                        (car database-list )  
                        " to [Public];" )) 
    (insert (generic-string-replace-list 
                            template 
                            "\$perm" 
                            perm-list))
    (setq database-list (cdr database-list))))   

;; Call things above with this: 
(generate-perm-list-for-db-list  perm-list database-list)

;; sample output from the functions:

use [database_1];
grant EXECUTE to [database_1] to [Public];
grant SELECT to [database_1] to [Public];
grant SHOWPLAN to [database_1] to [Public];

use [database_2];
grant EXECUTE to [database_2] to [Public];
grant SELECT to [database_2] to [Public];
grant SHOWPLAN to [database_2] to [Public];

use [database_3];
grant EXECUTE to [database_3] to [Public];
grant SELECT to [database_3] to [Public];
grant SHOWPLAN to [database_3] to [Public];
1
  • You should use dolist instead of while. You can insert multiple strings instead of concating them first. Commented Dec 2, 2014 at 21:56

1 Answer 1

1

Here's your code, simplified:

(setq database-list '("[database_1]" "[database_2]" "[database_3]"))

(setq perm-list '("EXECUTE" "SELECT" "SHOWPLAN"))

(defun generate-perm-list-for-db-list (perm-list database-list)
  (forward-line)
  (dolist (db database-list)
    (insert
     "\nuse " db ";\n"
     (mapconcat
      (lambda (x)
        (format "grant %s to %s to [Public];" x db))
      perm-list
      "\n")
     "\n")))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.