r9456: relax type for server-version
[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 field-names)
20   (:documentation
21    "Executes the SQL query expression QUERY-EXPRESSION, which may
22 be an SQL expression or a string, on the supplied DATABASE which
23 defaults to *DEFAULT-DATABASE*. RESULT-TYPES is a list of symbols
24 which specifies the lisp type for each field returned by
25 QUERY-EXPRESSION. If RESULT-TYPES is nil all results are returned
26 as strings whereas the default value of :auto means that the lisp
27 types are automatically computed for each field. FIELD-NAMES is t
28 by default which means that the second value returned is a list
29 of strings representing the columns selected by
30 QUERY-EXPRESSION. If FIELD-NAMES is nil, the list of column names
31 is not returned as a second value. FLATP has a default value of
32 nil which means that the results are returned as a list of
33 lists. If FLATP is t and only one result is returned for each
34 record selected by QUERY-EXPRESSION, the results are returned as
35 elements of a list."))
36
37 (defmethod query ((query-expression string) &key (database *default-database*)
38                   (result-types :auto) (flatp nil) (field-names t))
39   (record-sql-command query-expression database)
40   (multiple-value-bind (rows names) 
41       (database-query query-expression database result-types field-names)
42     (let ((result (if (and flatp (= 1 (length (car rows))))
43                       (mapcar #'car rows)
44                     rows)))
45       (record-sql-result result database)
46       (if field-names
47           (values result names)
48         result))))
49
50 ;;; Execute
51
52 (defgeneric execute-command (expression &key database)
53   (:documentation
54    "Executes the SQL command EXPRESSION, which may be an SQL
55 expression or a string representing any SQL statement apart from
56 a query, on the supplied DATABASE which defaults to
57 *DEFAULT-DATABASE*."))
58
59 (defmethod execute-command ((sql-expression string)
60                             &key (database *default-database*))
61   (record-sql-command sql-expression database)
62   (let ((res (database-execute-command sql-expression database)))
63     (record-sql-result res database))
64   (values))
65
66 ;;; Large objects support
67
68 (defun create-large-object (&key (database *default-database*))
69   "Creates a new large object in the database and returns the object identifier"
70   (database-create-large-object database))
71
72 (defun write-large-object (object-id data &key (database *default-database*))
73   "Writes data to the large object"
74   (database-write-large-object object-id data database))
75
76 (defun read-large-object (object-id &key (database *default-database*))
77   "Reads the large object content"
78   (database-read-large-object object-id database))
79
80 (defun delete-large-object (object-id &key (database *default-database*))
81   "Deletes the large object in the database"
82   (database-delete-large-object object-id database))
83