[plt-scheme] more possible planet functions

From: Corey Sweeney (corey.sweeney at gmail.com)
Date: Wed Feb 7 12:31:09 EST 2007

I have another function that i'm thinking of adding to my plant
package, but I'm not confidant that the API is as clean as it could
be, so I'm looking for any more comments...


;;;This is a function that is designed to take away the task of
writing a seperate recursive function evey time you want to do a
recursive sql call.  I.E. any time you have a table that has parents
and children and want to know all the ancestors of a record, or all
the children of a record.

;If effiency is a serious issue, you should probably re-layout your
tables so that it is less readable, but the recursive call is not
necicary

(define (sql-recurse-on sql-function start-list)
  (define (sql-recurse-on-loop list-to-do list-done-so-far)
    (if (null? list-to-do)
        list-done-so-far
        (begin
          (let ([currently-known-records (append list-done-so-far list-to-do)])
            (sql-recurse-on-loop
             (remove
              (lambda (element) (member element currently-known-records))
              (apply append
                     (map
                      (lambda (field)
                        (sqli-query s (sql-function field))
                        (sqli-fetchall s))
                      list-to-do)))
             currently-known-records)))))
    (sql-recurse-on-loop start-list `()))





;;here's a example of how to use it for the following table:
            Table "public.groups"
    Column    |       Type        | Modifiers
--------------+-------------------+-----------
 groupname    | character varying | not null
 parent_group | character varying |
Indexes:
    "groups_pkey" PRIMARY KEY, btree (groupname)
Foreign-key constraints:
    "groups_parent_group_fkey" FOREIGN KEY (parent_group) REFERENCES
groups(groupname)


(define (get-all-group-children list-of-groups-to-get-children-of)
  (sql-recurse-on
   (lambda (list-of-groupname)
     (string-append
      "select all groupname from groups where parent_group = '" (first
list-of-groupname) "'"))
   (map list list-of-groups-to-get-children-of)))

(get-all-group-children (list "root" "group1" "group3"))




Corey

-- 
((lambda (y) (y y)) (lambda (y) (y y)))


Posted on the users mailing list.