fukamachi / mito

An ORM for Common Lisp with migrations, relationships and PostgreSQL support
292 stars 31 forks source link

Question: How can I deal with the `mito:retrieve-dao` return? #142

Closed kevinmarquesp closed 4 months ago

kevinmarquesp commented 4 months ago

I started learning Common Lisp yesterday, and I found this library to help me out on a little project that I'm building, just to practice.

The problem is, I didn't see any examples, nor blog articles, that explains how to use the mito:retrieve-dao. On my code, I'm trying to create a function that returns all names and ages of a registered person, but I don't know how to pull out values from the model's properties.

Here's what I'm trying to build:

;;;; In my main code I have those functions, just for testing...

(connect +default-path+)  ; This works.
(migrate)                 ; This works.

(register "Fulano" 19)    ; This works.
(register "Ciclano" 18)   ; This works.
(register "Beltrano" 19)  ; This works.
(print (fetch))           ; This doesn't...

;;;; The functions below, in my code it's in its own Lisp file.

(mito:deftable user ()
  ((name :col-type (:varchar 128))
   (age :col-type :integer)))

(eval-when (:compile-toplevel)
  (defconstant +default-path+ "database.sqlite3"
    "Path to the SQLite3 database file, where the persons will be registered."))

(defun connect (path)
  "Connects to the SQLite3 database file."
  (mito:connect-toplevel :sqlite3 :database-name path))

(defun migrate ()
  "Creates the user table on the database, it has to be connect before that."
  (mito:ensure-table-exists 'user))

(defun register (name age)
  "Register a new entry on the user table. Each user IS NOT unique!"
  (mito:insert-dao
    (make-instance 'user :name name :age age)))

;;;; That's the function that I need to fix, when I do an `(inspect dao)` it
;;;; shows the `user` properties well, with all values and all, but I can't
;;;; get the value of those values individually...

(defun fetch ()
  "Returns a matrix of tuples with all the registered names and ages."
  (let ((daos (mito:retrieve-dao 'user)))
    (mapcar (lambda (dao)
      (getf dao 'name)) daos)))

Thank you for reading my issue, sorry if it doesn't fit with the other issues in any way.

kevinmarquesp commented 4 months ago

Never mind... skill issues on my part. Sorry. I still need learn more about how classes and objects work in Lisp. The solution was simple, I just needed to get the property value with (user-name dao), where user-* is the name of the model.

Again, sorry...