r9336: 12 May 2004 Kevin Rosenberg (kevin@rosenberg.net)
[clsql.git] / sql / basic-sql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; $Id$
5 ;;;;
6 ;;;; Base SQL functions
7 ;;;;
8 ;;;; This file is part of CLSQL.
9 ;;;;
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
14
15 (in-package #:clsql-sys)
16
17 ;;; Query
18
19 (defgeneric query (query-expression &key database result-types flatp)
20   (:documentation
21    "Execute the SQL query expression QUERY-EXPRESSION on the given
22 DATABASE which defaults to *default-database*. RESULT-TYPES is a list
23 of symbols such as :string and :integer, one for each field in the
24 query, which are used to specify the types to return. The FLATP
25 argument, which has a default value of nil, specifies if full
26 bracketed results should be returned for each matched entry. If FLATP
27 is nil, the results are returned as a list of lists. If FLATP is t,
28 the results are returned as elements of a list, only if there is only
29 one result per row. Returns a list of lists of values of the result of
30 that expression and a list of field names selected in sql-exp."))
31
32 (defmethod query ((query-expression string) &key (database *default-database*)
33                   (result-types :auto) (flatp nil) (field-names t))
34   (record-sql-command query-expression database)
35   (multiple-value-bind (rows names) (database-query query-expression database result-types
36                                                     field-names)
37     (let ((result (if (and flatp (= 1 (length (car rows))))
38                       (mapcar #'car rows)
39                     rows)))
40       (record-sql-result result database)
41       (if field-names
42           (values result names)
43         result))))
44
45 ;;; Execute
46
47 (defgeneric execute-command (expression &key database)
48   (:documentation
49    "Executes the SQL command specified by EXPRESSION for the database
50 specified by DATABASE, which has a default value of
51 *DEFAULT-DATABASE*. The argument EXPRESSION may be any SQL statement
52 other than a query. To run a stored procedure, pass an appropriate
53 string. The call to the procedure needs to be wrapped in a BEGIN END
54 pair."))
55
56 (defmethod execute-command ((sql-expression string)
57                             &key (database *default-database*))
58   (record-sql-command sql-expression database)
59   (let ((res (database-execute-command sql-expression database)))
60     (record-sql-result res database))
61   (values))
62
63 ;;; Large objects support
64
65 (defun create-large-object (&key (database *default-database*))
66   "Creates a new large object in the database and returns the object identifier"
67   (database-create-large-object database))
68
69 (defun write-large-object (object-id data &key (database *default-database*))
70   "Writes data to the large object"
71   (database-write-large-object object-id data database))
72
73 (defun read-large-object (object-id &key (database *default-database*))
74   "Reads the large object content"
75   (database-read-large-object object-id database))
76
77 (defun delete-large-object (object-id &key (database *default-database*))
78   "Deletes the large object in the database"
79   (database-delete-large-object object-id database))
80