1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: High-level SQL interface
7 ;;;; Authors: Kevin M. Rosenberg based on code by Pierre R. Mai
8 ;;;; Date Started: Feb 2002
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
13 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
15 ;;;; CLSQL users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (in-package #:clsql-sys)
23 ;;; Row processing macro
27 (defun lisp->sql-name (field)
30 (symbol (string-upcase (symbol-name field)))
32 (t (format nil "~A" field))))
34 (defun field-names (field-forms)
35 "Return a list of field name strings from a fields form"
36 (loop for field-form in field-forms
43 (defun from-names (from)
44 "Return a list of field name strings from a fields form"
45 (loop for table in (if (atom from) (list from) from)
46 collect (lisp->sql-name table)))
49 (defun where-strings (where)
50 (loop for w in (if (atom (car where)) (list where) where)
53 (format nil "~A ~A ~A" (second w) (first w) (third w))
54 (format nil "~A" w))))
56 (defun order-by-strings (order-by)
57 (loop for o in order-by
61 (format nil "~A ~A" (lisp->sql-name (car o))
62 (lisp->sql-name (cadr o))))))
64 (defun query-string (fields from where distinct order-by limit)
67 (format nil "select ~A~{~A~^,~} from ~{~A~^ and ~}"
68 (if distinct "distinct " "") (field-names fields)
70 (if where (format nil " where ~{~A~^ ~}"
71 (where-strings where)) "")
72 (if order-by (format nil " order by ~{~A~^, ~}"
73 (order-by-strings order-by)))
74 (if limit (format nil " limit ~D" limit) "")))
76 (defmacro for-each-row (((&rest fields) &key from order-by where distinct limit) &body body)
77 (let ((d (gensym "DISTINCT-"))
78 (bind-fields (loop for f in fields collect (car f)))
80 (o (gensym "ORDER-BY-"))
81 (frm (gensym "FROM-"))
83 (q (gensym "QUERY-")))
89 (let ((,q (query-string ',fields ,frm ,w ,d ,o ,l)))
90 (loop for tuple in (query ,q)
91 collect (destructuring-bind ,bind-fields tuple
94 ;;; Marc Battyani : Large objects support
96 (defun create-large-object (&key (database *default-database*))
97 "Creates a new large object in the database and returns the object identifier"
98 (database-create-large-object database))
100 (defun write-large-object (object-id data &key (database *default-database*))
101 "Writes data to the large object"
102 (database-write-large-object object-id data database))
104 (defun read-large-object (object-id &key (database *default-database*))
105 "Reads the large object content"
106 (database-read-large-object object-id database))
108 (defun delete-large-object (object-id &key (database *default-database*))
109 "Deletes the large object in the database"
110 (database-delete-large-object object-id database))