unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Philip Kaludercic <philipk@posteo.net>
Cc: Jonas Bernoulli <jonas@bernoul.li>, emacs-devel@gnu.org
Subject: Re: [NonGNU ELPA] New package: sqlite3
Date: Tue, 21 Mar 2023 09:51:23 +0300	[thread overview]
Message-ID: <ZBlT640a+QaOyUMF@protected.localdomain> (raw)
In-Reply-To: <87y1nzb95o.fsf@posteo.net>

* Philip Kaludercic <philipk@posteo.net> [2023-03-14 19:17]:
> Jonas Bernoulli <jonas@bernoul.li> writes:
> 
> >> Do you have a link to the package you are talking about?
> >
> > Ups, here you go: https://github.com/pekingduck/emacs-sqlite3-api
> 
> Would you happen to know if there is some rx-like, s-expression based
> language for constructing SQL queries.  I am not looking for anything
> generic, just a way to avoid writing long strings.

While such packages exists, for me I do not find them usable as then I
have to forget about the SQL and learn about the new Emacs Lisp
structure that is to correspond to SQL. I see personally no benefit in
that.

Often, I tend to do 2 steps SQL, one for preliminary query, where I
fetch only the list of items as in this example:

(defun cf-people-by-last-name (&optional last-name)
  "Return list of people by last name."
  (interactive)
  (let* ((last-name (or last-name (rcd-ask-get "Last name: ")))
	 (id-list (rcd-sql-list "SELECT people_id FROM people WHERE people_name ~* $1" cf-db last-name))
	 (title (format "People by last name `%s'" last-name)))
    (cf-people-by-id-list id-list title)))

And then the other one, more complex which is used for the final
report, and from which I only cut this one part:

(sql (format "SELECT DISTINCT ON (people_id) 
                                           people_id, get_full_contacts_name(people_id), 
                                           coalesce((SELECT get_contacts_name(peoplerelations_people2) 
                                                       FROM peoplerelations 
                                                      WHERE peoplerelations_people1 = people_id 
                                                   ORDER BY peoplerelations_default, peoplerelations_id LIMIT 1),
                                                     '>>>UNKNOWN<<<') 
                                       FROM people 
                                      WHERE people_id IN (%s) 
                                   ORDER BY people_id DESC"
                                           sql-id-list))

By using the two step approach, first selecting IDs necessary, and
then running the main report using those ID numbers, I avoid writing
larger single functions.

In general SQL is so much simpler and logically understandable as
compared to Emacs Lisp, writing SQL is beneficial as then you get the
habit and can run queries in any other programming language, or
directly on command line or in programs, first making sure you are
doing it right.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



  parent reply	other threads:[~2023-03-21  6:51 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-04 10:17 [NonGNU ELPA] New package: sqlite3 Jonas Bernoulli
2023-03-04 11:39 ` Philip Kaludercic
2023-03-06 18:43   ` Jonas Bernoulli
2023-03-14 16:16     ` Philip Kaludercic
2023-03-14 22:46       ` Jonas Bernoulli
2023-03-15  8:05         ` Philip Kaludercic
2023-03-21  6:51       ` Jean Louis [this message]
2023-03-21 10:55         ` Lynn Winebarger
2023-03-21 11:08           ` Philip Kaludercic
2023-03-21 11:56             ` Lynn Winebarger
2023-03-21 12:18               ` Philip Kaludercic
2023-03-21 13:04                 ` Lynn Winebarger
2023-03-21 16:53                   ` Philip Kaludercic
2023-03-21 21:00                     ` Tomas Hlavaty
2023-04-07  4:53                       ` Jean Louis
2023-03-21 23:58                     ` Lynn Winebarger
2023-03-22  8:10                       ` Philip Kaludercic
2023-03-22 15:05                         ` Lynn Winebarger
2023-03-23  0:07                           ` Lynn Winebarger
2023-03-21 20:42             ` Tomas Hlavaty
     [not found]               ` <875yatn70c.fsf@posteo.net>
2023-03-21 22:46                 ` Tomas Hlavaty
2023-03-22  8:00                   ` Philip Kaludercic
2023-03-21 20:36         ` Tomas Hlavaty
2023-04-07  5:17           ` Jean Louis
2023-03-06  5:08 ` Richard Stallman
2023-03-14 14:36   ` Jonas Bernoulli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZBlT640a+QaOyUMF@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=emacs-devel@gnu.org \
    --cc=jonas@bernoul.li \
    --cc=philipk@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).