Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do you recommend using handle in each function using database? #15

Open
gnusupport opened this issue Nov 12, 2020 · 5 comments
Open

Do you recommend using handle in each function using database? #15

gnusupport opened this issue Nov 12, 2020 · 5 comments

Comments

@gnusupport
Copy link

At first I have made libraries that connect and fetch and handle was &optional. If there was no handle standard *db* handle was used. Programs could access one database.

Then I have developed separate Emacs package that uses the same database but due to its nature could also use separate database and other databases outside of my local computer.

As then I had 2 programs connecting to local database and complexities arrive in future when using multiple databases in the same time, I am of opinion that each call to function that uses database should also transfer the handle, so that handle is not just global variable.

Do you agree on this?

Handle was here optional and it worked well until I started making two connections to database:

(defun rcd-sql (sql &optional handle)
  "Sends SQL queries to PostgreSQL database and returns results"
  (let ((handle (if handle handle *cf*)))
    (condition-case err
	(pq:query handle sql)
      (error
       (if (string-match "^ERROR:  syntax error" (cdr err))
	   (progn
	     (if (fboundp 'speak) (speak (cdr err)))
	     (message (cdr err)))
	 ;; re-throw
	 (signal (car err) (cdr err)))))))

(defun rcd-sql-list (sql &optional handle)
  "Returns list of lists instead of vectors"
  (let ((list '()))
    (dolist (i (apply 'rcd-sql (list sql handle)) (reverse list))
      (cond ((eq (type-of i) 'vector) (push (append i '()) list))
	    (t (push i list))))))

(defun rcd-sql-first (sql &optional handle)
  "Returns first entry from SQL query"
  (car (apply 'rcd-sql (list sql handle))))

My opinion is that I should not make it optional but require handle each time. Is that what you also recommend?

@anse1
Copy link
Owner

anse1 commented Nov 12, 2020 via email

@gnusupport
Copy link
Author

gnusupport commented Nov 17, 2020 via email

@gnusupport
Copy link
Author

I use handles as global variables, per package, that works well. I have only the problem when there is server interruption, for example server stopped and started. So I have to improve generic function to recognize that and to re-connect again. That may need passing a symbol to handle so that generic function recognizes it.

My generic functions are here:

RCD Database Basics
https://hyperscope.link/3/7/4/9/3/RCD-Database-Basics-37493.html

@anse1
Copy link
Owner

anse1 commented May 18, 2021 via email

@gnusupport
Copy link
Author

gnusupport commented May 29, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants