[postmodern-devel] Help with query replacement

Vamsee Kanakala vamlists at gmail.com
Tue Mar 27 09:38:30 UTC 2007


Hi,

I'm trying add paging support to my web pages, so it's important that I
know the number of results a query will generate, so that I can create
the appropriate paging code. I'm trying to change something like this:

(query-dao 'city (:select 'id 'name :from 'cities))

to

(query (:select (:raw "count(*)") :from 'cities))

So that I can get the number of results first and then run the actual
query with limit and offset. I thought it best not to change postmodern
code itself, to avoid hassles when a new version comes out. However,
parsing the above query with a recursive function to replace 'id and
'name with (:raw "count(*)") is proving to be beyond my lisp-fu.

Any easy way to achieve this? My terrible hack to do this:

(defun query-subst (list)
  (if (null list)
      (list)
      (if (atom (car list))
	  (progn
	    (cond ((eql (car list) :select) (setf skip t))
		  ((eql (car list) :from) (setf skip nil)))
	    (if (or (null skip) (eql (car list) :select))
		(cons (car list) (query-subst (cdr list)))
		(if (null count-set)
		    (progn (setf count-set t)
			   (list :raw "count(*)"))
		    (query-subst (cddr list)))))
	  (cons (query-subst (car list)) (query-subst (cdr list))))))

But this doesn't really do my job, because my query ends up like this:

(query (:select (:raw "count(*)") nil :from 'cities))

Any way to avoid those extra nils? Probably c.l.l is a better place to
post this, but wanted to know if there's a less complicated way to do this.

Thanks,
Vamsee.



More information about the postmodern-devel mailing list