r8821: integrate usql support
[clsql.git] / base / basic-sql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;  $Id: $
4
5 (in-package #:clsql-base-sys)
6
7 ;;; Query
8
9 (defgeneric query (query-expression &key database result-types flatp)
10   (:documentation
11    "Execute the SQL query expression QUERY-EXPRESSION on the given
12 DATABASE which defaults to *default-database*. RESULT-TYPES is a list
13 of symbols such as :string and :integer, one for each field in the
14 query, which are used to specify the types to return. The FLATP
15 argument, which has a default value of nil, specifies if full
16 bracketed results should be returned for each matched entry. If FLATP
17 is nil, the results are returned as a list of lists. If FLATP is t,
18 the results are returned as elements of a list, only if there is only
19 one result per row. Returns a list of lists of values of the result of
20 that expression and a list of field names selected in sql-exp."))
21
22 (defmethod query ((query-expression string) &key (database *default-database*)
23                   (result-types nil) (flatp nil))
24   (record-sql-command query-expression database)
25   (let* ((res (database-query query-expression database result-types))
26          (res (if (and flatp (= (length
27                                  (slot-value query-expression 'selections))
28                                 1))
29                   (mapcar #'car res)
30                   res)))
31     (record-sql-result res database)
32     res))
33
34 ;;; Execute
35
36 (defgeneric execute-command (expression &key database)
37   (:documentation
38    "Executes the SQL command specified by EXPRESSION for the database
39 specified by DATABASE, which has a default value of
40 *DEFAULT-DATABASE*. The argument EXPRESSION may be any SQL statement
41 other than a query. To run a stored procedure, pass an appropriate
42 string. The call to the procedure needs to be wrapped in a BEGIN END
43 pair."))
44
45 (defmethod execute-command ((sql-expression string)
46                             &key (database *default-database*))
47   (record-sql-command sql-expression database)
48   (let ((res (database-execute-command sql-expression database)))
49     (record-sql-result res database))
50   (values))
51
52