Skip to content

Commit

Permalink
Speed up lookup of :closql-tables slots
Browse files Browse the repository at this point in the history
1) Enforce the use of `id' as the name of the rows used for joining.
2) Use the name of the row used in the `where' clause matches the
   value of the `closql-primary-key' slot.
3) Order by `id'.

Do this because determining the slots again every time `oref' is used
on such a slot, was very expensive.

This is less flexible than what we had before, but that is okay because:

a) This matches how Forge actually uses these slots, expect that it
   uses a different order for some of the slots.
b) In an earlier commit we made lookup of indirect slots more flexible.
   The behavior can now be overridden completely for any indirect slot,
   by implementing a `closql-dref' method.

I've considered these two alternatives (amongst others):

A) Specify the names of the rows using the value of the `closql-tables'
   slot property.  This became very complicated because one had to think
   around three corners, which gave raise to the idea that...
B) ... if we have specify so many details anyway, we might as well use
   an appropriate dsl, i.e., SQL.  Or in other words, do not provide
   any default behavior whatsoever, and instead require a dedicated
   `closql-dref' method for each `closql-tables' slot.

Instead this commit implements the reasonable compromise.
  • Loading branch information
tarsius committed Jul 1, 2024
1 parent dae0256 commit d1088cc
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions closql.el
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,23 @@
(closql--oref obj (closql--oref-default obj 'closql-primary-key))
(cadr columns))))))
((setq tables (closql--slot-tables obj slot))
(pcase-let*
((`(,slot-table ,data-table) tables)
(`(,where ,slot-join) (closql--table-columns db slot-table))
(`(,_ . ,select) (closql--table-columns db data-table))
(data-join (car select))
(object-id (closql--oref obj (oref-default obj closql-primary-key))))
(pcase-let ((`(,slot-table ,data-table) tables))
(aset obj c
(mapcar
#'cdr
(emacsql
db [:select $i1 :from $i2
:join $i3 :on (= $i4 $i5)
:where (= $i6 $s7)
:order-by [(asc $i8)]]
:order-by [(asc id)]]
(intern (format "%s:*" data-table))
data-table slot-table
(intern (format "%s:%s" slot-table slot-join))
(intern (format "%s:%s" data-table data-join))
where object-id (car select))))))
(intern (format "%s:id" slot-table))
(intern (format "%s:id" data-table))
(intern (format "%s:%s" slot-table
(closql--oref-default obj 'closql-table)))
(closql--oref obj (closql--oref-default obj 'closql-primary-key))
(closql--oref obj 'id))))))
((slot-unbound obj (eieio--object-class obj) slot 'oref)))))

;;;; Oset
Expand Down

0 comments on commit d1088cc

Please sign in to comment.