[postmodern-devel] Dash in Postgresql Column name - "query-dao"

Maciej Pasternacki maciej at pasternacki.net
Wed Dec 3 16:16:57 UTC 2008


On 2008-12-03, at 16:28, Phil wrote:

>>> query-dao does not seem to like column names with a dash in them  
>>> like
>>> column-name and I suspect it would have the same problem with  
>>> spaces.
>>>
>>> I know that the preffered for postgresql would be column_name but I
>>> though that being able to use the dash would simplify going from  
>>> db to
>>> dao object to html and back, well at least for me.
>>>
>>> Is column names with dashes just not supported, a bug or on the wish
>>> list?
>>
>> Everything is supported with :RAW sql-op:  (select-dao 'foo (:= (:raw
>> "\"foo-bar\"") 23)) calls query (SELECT * FROM foo WHERE ("foo-bar" =
>> 23)) (SELECT-DAO is a shortcut for QUERY-DAO, where you supply only
>> the WHERE clause).  There doesn't seem to exist any other way now,  
>> but
>> it may be possible to add a bit of macro sugar.
>
> Ok maybe I should have been a bit more precise in my description. I  
> was
> not using the columns names in the query.
>
> Here follows a quick repeatable test:
>
> CREATE TABLE test
> (
>  id integer NOT NULL,
>  "some" character varying NOT NULL,
>  "test-dash" character varying
> )
> WITH (OIDS=FALSE);
> ALTER TABLE test OWNER TO postgres;
>
> insert into test (id,"some","test-dash") values(1, 'some','dash-1');
>
> (defclass test ()
>  ((id :col-type string :initarg :id
>   :accessor biographical-id)
>   (some :col-type  (or db-null string) :initarg :some
>     :accessor biographical-some)
>   (test-dash :col-type  (or db-null string) :initarg :test-dash
>    :accessor biographical-test-dash))
>  (:metaclass postmodern:dao-class)
>  (:keys id))
>
> (postmodern:query-dao 'test (:select '* :from 'test))
>
> That last bit produces the following error:
>
> No slot named test-dash in class TEST. DAO out of sync with table, or
> incorrect query used.
>   [Condition of type SIMPLE-ERROR]


I see.  It was SELECT-DAO that made me think there's problem in the  
query.  What you write about is not supported at the moment, and while  
it's easy to add this, it's not trivial.

I just looked into postmodern/table.lisp and I see that in class  
DIRECT-COLUMN-SLOT there is a slot SQL-NAME.  It would seem trivial to  
add an initarg, e.g. :COL-SQL-NAME, for this slot, and modify SHARED- 
INITIALIZE of DIRECT-COLUMN-SLOT to set this slot only if it's not  
already bound by an initarg.  This way it would be possible to  
add :COL-SQL-NAME initarg to the slot to explicitly set column name,  
with upside being it wouldn't need to be a symbol.

However, this slot seems to be rarely used, and there are many cases  
where simple TO-SQL-NAME is called on slot name, which would need to  
be fixed.  Also, column name would need to be properly double-quoted  
everywhere.

Your problem seems interesting and it looks like it would be useful to  
support your case.  If all that was needed would be adding the initarg  
to :COL-SQL-NAME, the patch would be probably already attached to this  
post ;) but the way it is, I can only give these starting points so  
that you can fix it yourself.  It shouldn't be hard, but testing the  
patch may be a bit time-consuming.

Regards,
Maciej.

-- 
Maciej Pasternacki -><- http://www.pasternacki.net/ -><- http://www.3ofcoins.net/





More information about the postmodern-devel mailing list