d12frosted / vulpea

A collection of functions for note taking based on `org` and `org-roam`.
GNU General Public License v3.0
236 stars 12 forks source link

Doesn't vulpea-db-query work for all test / comparison ? #150

Closed Cletip closed 1 year ago

Cletip commented 1 year ago

Hello

I think I noticed a problem with the "vulpea-db-query" function. My goal : make comparison options on numbers in metadata.

Let say the following note

:PROPERTIES:
:ID: 20220617115633324937
:END:
#+title: Test Page

- user :: email
- numbers :: 12

This query works

(vulpea-select-from "texet" (vulpea-db-query
                               (lambda (note)
                                 (and
                                  (string-equal (vulpea-meta-get note "user") "email")
                                  ;; (= (vulpea-note-level note) 0)
                                  (< (vulpea-note-meta-get note "numbers" 'number) 100)
                                  )
                                 )))

But this one, no:

(vulpea-select-from "text" (vulpea-db-query
                       (lambda (note)
                         (< (vulpea-note-meta-get note "numbers" 'number) 100))))

I don't understand why, and I think this is not normal. The problem is that I have no idea where the problem could come from :/

d12frosted commented 1 year ago

Hey @Cletip

To simplify testing I would avoid clutter. E.g. you are curious about vulpea-db-query, so let's get rid of vulpea-select-from?

(vulpea-db-query
 (lambda (note)
   (and
    (string-equal (vulpea-meta-get note "user") "email")
    (< (vulpea-note-meta-get note "numbers" 'number) 100))))
(vulpea-db-query
 (lambda (note)
   (< (vulpea-note-meta-get note "numbers" 'number) 100)))

Now, please define the meaning of "doesn't work". Does it return nil or throws an error? I am pretty sure that it throws something like "Wrong type argument: number-or-marker-p, nil"

Now seeing these two use cases, let me ask - what is the difference? The first query first checks that value of "user" is "email" and only then tries to parse a number from "numbers" and compare it with 100. In the second case you parse a number unconditionally. Remember that vulpea-db-query calls predicate on every note retrieved from database. Are you sure all of your notes contain numbers?

In general, make sure the value exists before using it:

(vulpea-db-query
 (lambda (note)
   (when-let ((n (vulpea-note-meta-get note "numbers" 'number)))
     (< n 100))))

This should work.

The problem is that I have no idea where the problem could come from :/

TL;DR; I suspect that some of your notes doesn't have "numbers" inside metadata. And (vulpea-note-meta-get note "numbers" 'number) returns nil in such case. So you have (< nil 100), which errors out, because nil is not a number.

Let me know if it's not the case.

Cletip commented 1 year ago

Thank you for your answer

First of all, sorry, my question was too silly: I was looking for something, I was in a hurry and I had to do it quickly (ah, this time we would like to have)

Maybe I have too often this silly idea of "I strengthen the X conditions, so how can strengthening the conditions make the program work?". I really thought I had found something wrong... but in fact, my "(string-equal (vulpea-meta-get note "user") "email")" only brings out THE note that has the metadata number.

I'm discovering that you can nest conditions like that to do the test. Emacs is great, I have to get used to it. Your answer is, as often, the right.

Anyway, I'm really sorry to have taken your time and energy for "something so stupid". I would think longer before making this kind of irrelevant remark.

Thank you very much for your answer

d12frosted commented 1 year ago

@Cletip no worries 😸

I'm discovering that you can nest conditions like that to do the test. Emacs is great, I have to get used to it. Your answer is, as often, the right.

You can do it in other languages as well 🙃


Enjoy!