From d1088cc188c800ea1fe10b4f70ced7c83ce51a72 Mon Sep 17 00:00:00 2001 From: Jonas Bernoulli Date: Mon, 1 Jul 2024 10:21:49 +0200 Subject: [PATCH] Speed up lookup of :closql-tables slots 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. --- closql.el | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/closql.el b/closql.el index 75a85db..513ba06 100644 --- a/closql.el +++ b/closql.el @@ -136,12 +136,7 @@ (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 @@ -149,12 +144,15 @@ 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