<div dir="ltr">Hello,<br><br>I have recently started using postmodern in a project. I need to store enum values (of many different types) in a class. I have declared a LISP type<br><br>(deftype season-type () '(member winter spring summer autumn))<br>
<br>and I have functions that convert the above symbols to the numbers 0..3 and vice-versa, if needed.<br><br>-- I have tried to emulate enum types with <br><br>(defclass my-type ()<br> ((id :initarg :id :col-type bigint)<br>
(season :initarg :season :col-type smallint))<br> (:metaclass dao-class)<br> (:keys id))<br><br>CREATE TABLE valid_seasons (<br> id SERIAL PRIMARY KEY NOT NULL,<br> season TEXT<br>);<br><br>INSERT INTO valid_seasons (seasons) VALUES <br>
("WINTER"), ("SPRING"), ("SUMMER"), ("AUTUMN");<br><br>and then like<br><br>(deftable season<br> (!dao-def)<br> (!foreign 'valid-seasons 'id 'season))<br><br>I would like the class my-type to have the symbols of the seasons (winter, spring, etc) stored in its slot when I am in the land of lisp and not to be forced to store integer numbers, which should only be used in the land of the database. What is the normal way to achieve that in postmodern?<br>
<br>-- PostgreSQL offers an enum type, so I tried using it in postmodern<br>(:create-enum 'seasondbtype (list "WINTER" "SPRING" "SUMMER" "AUTUMN"))<br><br>(defclass my-type ()<br>
((id :initarg :id :col-type bigint)<br> (season :initarg :season :col-type seasondbtype))<br> (:metaclass dao-class)<br> (:keys id))<br><br>But then again I have to store strings inside my-type.season, not symbols. I have added something like<br>
<br>(defmethod initialize-instance :after ((object my-type) &key)<br> (dolist (slot-name '(season))<br> (setf (slot-value object slot-name) (symbol-name (slot-value object slot-name))))),<br><br>so that I can call make-instance with a symbol of season-type as an argument. But this is only for storing objects to the database, I have not gotten around to retrieving yet, and I think initialize-instance will be called there as well, so I will have to fix it.<br>
<br>-- CL-SQL offers two slot options called :db-reader :db-writer where you can specify functions which "take one argument, the value from the database, and return the value that should be put into the slot" and "take one argument, the value of the slot, and return the value that should be put into the database" respectively. Is there something similar in postmodern? Does the macro with-column-writers or the parameter *custom-column-writers* have anything to do with that? If yes, can I have an example of usage? I think that might solve my problem.<br>
<br>In short, in what way do postmodern's users deal with enumerated types, and in particular how do they manage to store symbols in CLOS instances and the corresponding strings (or even numbers) in PostgreSQL?<br><br>
Regards,<br>Christos Gitsis<br></div>