2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4 <!ENTITY % myents SYSTEM "entities.inc">
8 <!-- Functional Data Manipulation Language -->
9 <reference id="ref-fdml">
10 <title>Functional Data Manipulation Language (FDML)</title>
13 The functional data manipulation interface provided by &clsql;
14 includes functions for inserting, updating and deleting records
15 in existing database tables and executing SQL queries and
16 statements with the results of queries returned as Lisp types.
17 SQL statements expressed as strings may be executed with the
18 <link linkend="query"><function>query</function></link> and
20 linkend="execute-command"><function>execute-command</function></link>
22 linkend="select"><function>select</function></link> function, on
23 the other hand, allows for the construction of queries in Lisp
24 using the symbolic SQL syntax. Finally, iterative manipulation
25 of query results is supported by <link
26 linkend="do-query"><function>do-query</function></link>, <link
27 linkend="map-query"><function>map-query</function></link> and an
28 extended clause for the <link
29 linkend="loop-tuples"><function>loop</function></link> macro.
33 <!-- Caching table queries -->
35 <refentry id="cache-table-queries-default">
37 <refentrytitle>*CACHE-TABLE-QUERIES-DEFAULT*</refentrytitle>
40 <refname>*CACHE-TABLE-QUERIES-DEFAULT*</refname>
41 <refpurpose>Specifies the default behaviour for caching of
42 attribute types.</refpurpose>
43 <refclass>Variable</refclass>
46 <title>Value Type</title>
48 A valid argument to the <parameter>action</parameter>
49 parameter of <function>cache-table-queries</function>,
51 <simplelist type="inline">
53 <member>&nil;</member>
54 <member><symbol>:flush</symbol></member>
59 <title>Initial Value</title>
60 <para><symbol>nil</symbol></para>
63 <title>Description</title>
65 Specifies the default behaivour for caching of attribute
66 types. Meaningful values are &t;, &nil; and
67 <symbol>:flush</symbol> as described for the
68 <parameter>action</parameter> argument to
69 <function>cache-table-queries</function>.
73 <title>Examples</title>
77 <title>Affected By</title>
81 <title>See Also</title>
83 <member><link linkend="cache-table-queries"><function>cache-table-queries</function></link></member>
92 <refentry id="cache-table-queries">
94 <refentrytitle>CACHE-TABLE-QUERIES</refentrytitle>
97 <refname>CACHE-TABLE-QUERIES</refname>
98 <refpurpose>Control the caching of table attribute types.</refpurpose>
99 <refclass>Function</refclass>
102 <title>Syntax</title>
104 <function>cache-table-queries</function> <replaceable>table</replaceable> &key <replaceable>action</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
107 <title>Arguments and Values</title>
110 <term><parameter>table</parameter></term>
113 A string representing a database table, &t; or
114 <symbol>:default</symbol>.
119 <term><parameter>action</parameter></term>
122 &t;, &nil; or <symbol>:flush</symbol>.
127 <term><parameter>database</parameter></term>
130 <glossterm linkend="gloss-database-object">database
131 object</glossterm>. This will default to the value
132 of <symbol>*default-database*</symbol>.</para>
138 <title>Description</title>
139 <para>Controls the caching of attribute type information on the
140 table specified by <parameter>table</parameter> in
141 <parameter>database</parameter> which defaults to
142 <symbol>*default-database*</symbol>. <parameter>action</parameter>
143 specifies the caching behaviour to adopt. If its value is &t;
144 then attribute type information is cached whereas if its value
145 is &nil; then attribute type information is not cached. If
146 <parameter>action</parameter> is <symbol>:flush</symbol> then
147 all existing type information in the cache for
148 <parameter>table</parameter> is removed, but caching is still
149 enabled. <parameter>table</parameter> may be a string
150 representing a table for which the caching action is to be taken
151 while the caching action is applied to all tables if
152 <parameter>table</parameter> is &t;. Alternatively, when
153 <parameter>table</parameter> is <symbol>:default</symbol>, the
154 default caching action specified by
155 <symbol>*cache-table-queries-default*</symbol> is applied to all
156 tables for which a caching action has not been explicitly set.
160 <title>Examples</title>
162 (setf *cache-table-queries-default* t)
168 ([comments] varchar)))
170 (cache-table-queries "foo")
172 (list-attribute-types "foo")
173 => (("id" :INT4 4 NIL 1) ("height" :FLOAT8 8 NIL 1) ("name" :BPCHAR 24 NIL 1)
174 ("comments" :VARCHAR 255 NIL 1))
181 ([comments] (string 100))))
183 (cache-table-queries "foo" :action :flush)
185 (list-attribute-types "foo")
186 => (("id" :INT4 4 NIL 1) ("height" :FLOAT8 8 NIL 1) ("name" :BPCHAR 36 NIL 1)
187 ("comments" :BPCHAR 100 NIL 1))
191 <title>Side Effects</title>
193 The internal attribute cache for
194 <parameter>database</parameter> is modified.
198 <title>Affected by</title>
200 <member><link linkend="cache-table-queries-default"><symbol>*cache-table-queries-default*</symbol></link></member>
204 <title>Exceptional Situations</title>
210 <title>See Also</title>
212 <member><link linkend="cache-table-queries-default"><symbol>*cache-table-queries-default*</symbol></link></member>
223 <refentry id="insert-records">
225 <refentrytitle>INSERT-RECORDS</refentrytitle>
228 <refname>INSERT-RECORDS</refname>
229 <refpurpose>Insert tuples of data into a database table.</refpurpose>
230 <refclass>Function</refclass>
233 <title>Syntax</title>
235 <function>insert-records</function> &key <replaceable>into</replaceable> <replaceable>attributes</replaceable> <replaceable>values</replaceable> <replaceable>av-pairs</replaceable> <replaceable>query</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
238 <title>Arguments and Values</title>
241 <term><parameter>into</parameter></term>
244 A string, symbol or symbolic SQL expression representing
245 the name of a table existing in
246 <parameter>database</parameter>.
251 <term><parameter>attributes</parameter></term>
254 A list of attribute identifiers or &nil;.
259 <term><parameter>values</parameter></term>
262 A list of attribute values or &nil;.
267 <term><parameter>av-pairs</parameter></term>
270 A list of attribute identifier/value pairs or &nil;.
275 <term><parameter>query</parameter></term>
278 A query expression or &nil;.
283 <term><parameter>database</parameter></term>
286 <glossterm linkend="gloss-database-object">database
287 object</glossterm>. This will default to the value
288 of <symbol>*default-database*</symbol>.</para>
294 <title>Description</title>
296 Inserts records into the table specified by
297 <parameter>into</parameter> in <parameter>database</parameter>
298 which defaults to <symbol>*default-database*</symbol>.
301 There are five ways of specifying the values inserted into
302 each row. In the first <parameter>values</parameter> contains
303 a list of values to insert and
304 <parameter>attributes</parameter>,
305 <parameter>av-pairs</parameter> and
306 <parameter>query</parameter> are &nil;. This can be used when
307 values are supplied for all attributes in
308 <parameter>into</parameter>. In the second,
309 <parameter>attributes</parameter> is a list of column names,
310 <parameter>values</parameter> is a corresponding list of
311 values and <parameter>av-pairs</parameter> and
312 <parameter>query</parameter> are &nil;. In the third,
313 <parameter>attributes</parameter>,
314 <parameter>values</parameter> and <parameter>query</parameter>
315 are &nil; and <parameter>av-pairs</parameter> is an alist of
316 (attribute value) pairs. In the fourth,
317 <parameter>values</parameter>, <parameter>av-pairs</parameter>
318 and <parameter>attributes</parameter> are &nil; and
319 <parameter>query</parameter> is a symbolic SQL query
320 expression in which the selected columns also exist in
321 <parameter>into</parameter>. In the fifth method,
322 <parameter>values</parameter> and
323 <parameter>av-pairs</parameter> are nil and
324 <parameter>attributes</parameter> is a list of column names
325 and <parameter>query</parameter> is a symbolic SQL query
326 expression which returns values for the specified columns.
330 <title>Examples</title>
332 (select [first-name] [last-name] [email]
334 :where [= [emplid] 11]
337 (insert-records :into [employee]
338 :attributes '(emplid groupid first_name last_name email
339 ecompanyid managerid)
340 :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org"
343 (select [first-name] [last-name] [email]
345 :where [= [emplid] 11]
347 => (("Yuri" "Gagarin" "gagarin@soviet.org"))
351 <title>Side Effects</title>
353 Modifications are made to the underlying database.
357 <title>Affected by</title>
363 <title>Exceptional Situations</title>
365 An error of type <symbol>sql-database-data-error</symbol> is
366 signalled if <parameter>table</parameter> is not an existing
367 table in <parameter>database</parameter> or if the specified
368 attributes are not found.
372 <title>See Also</title>
374 <member><link linkend="update-records"><function>update-records</function></link></member>
375 <member><link linkend="delete-records"><function>delete-records</function></link></member>
386 <refentry id="update-records">
388 <refentrytitle>UPDATE-RECORDS</refentrytitle>
391 <refname>UPDATE-RECORDS</refname>
392 <refpurpose>Updates the values of existing records.</refpurpose>
393 <refclass>Function</refclass>
396 <title>Syntax</title>
398 <function>update-records</function> <replaceable>table</replaceable> &key <replaceable>attributes</replaceable> <replaceable>values</replaceable> <replaceable>av-pairs</replaceable> <replaceable>where</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
401 <title>Arguments and Values</title>
404 <term><parameter>table</parameter></term>
407 A string, symbol or symbolic SQL expression representing
408 the name of a table existing in
409 <parameter>database</parameter>.
414 <term><parameter>attributes</parameter></term>
417 A list of attribute identifiers or &nil;.
422 <term><parameter>values</parameter></term>
425 A list of attribute values or &nil;.
430 <term><parameter>av-pairs</parameter></term>
433 A list of attribute identifier/value pairs or &nil;.
438 <term><parameter>where</parameter></term>
441 A symbolic SQL expression.
446 <term><parameter>database</parameter></term>
449 <glossterm linkend="gloss-database-object">database
450 object</glossterm>. This will default to the value
451 of <symbol>*default-database*</symbol>.</para>
457 <title>Description</title>
459 Updates the attribute values of existing records satsifying
460 the SQL expression <parameter>where</parameter> in the table
461 specified by <parameter>table</parameter> in
462 <parameter>database</parameter> which defaults to
463 <symbol>*default-database*</symbol>.
466 There are three ways of specifying the values to update for
467 each row. In the first, <parameter>values</parameter> contains
468 a list of values to use in the update and
469 <parameter>attributes</parameter> and
470 <parameter>av-pairs</parameter> are &nil;. This can be used
471 when values are supplied for all attributes in
472 <parameter>table</parameter>. In the second,
473 <parameter>attributes</parameter> is a list of column names,
474 <parameter>values</parameter> is a corresponding list of
475 values and <parameter>av-pairs</parameter> is &nil;. In the
476 third, <parameter>attributes</parameter> and
477 <parameter>values</parameter> are &nil; and
478 <parameter>av-pairs</parameter> is an alist of (attribute
483 <title>Examples</title>
485 (select [first-name] [last-name] [email]
487 :where [= [emplid] 1]
489 => (("Vladamir" "Lenin" "lenin@soviet.org"))
490 (update-records [employee]
491 :av-pairs'((first_name "Yuri")
492 (last_name "Gagarin")
493 (email "gagarin@soviet.org"))
494 :where [= [emplid] 1])
496 (select [first-name] [last-name] [email]
498 :where [= [emplid] 1]
500 => (("Yuri" "Gagarin" "gagarin@soviet.org"))
504 <title>Side Effects</title>
506 Modifications are made to the underlying database.
510 <title>Affected by</title>
516 <title>Exceptional Situations</title>
518 An error of type <symbol>sql-database-data-error</symbol> is
519 signalled if <parameter>table</parameter> is not an existing
520 table in <parameter>database</parameter>, if the specified
521 attributes are not found or if the SQL statement resulting
522 from the symbolic expression <parameter>where</parameter> does
523 not return a Boolean value.
525 <para>If the execution of the SQL query leads to any errors, an
526 error of type <errortype>sql-database-error</errortype> is
530 <title>See Also</title>
533 <member><link linkend="insert-records"><function>insert-records</function></link></member>
534 <member><link linkend="delete-records"><function>delete-records</function></link></member>
546 <refentry id="delete-records">
548 <refentrytitle>DELETE-RECORDS</refentrytitle>
551 <refname>DELETE-RECORDS</refname>
552 <refpurpose>Delete records from a database table.</refpurpose>
553 <refclass>Function</refclass>
556 <title>Syntax</title>
558 <function>delete-records</function> &key <replaceable>from</replaceable> <replaceable>where</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
561 <title>Arguments and Values</title>
564 <term><parameter>from</parameter></term>
567 A string, symbol or symbolic SQL expression representing
568 the name of a table existing in
569 <parameter>database</parameter>.
574 <term><parameter>where</parameter></term>
577 A symbolic SQL expression.
582 <term><parameter>database</parameter></term>
585 <glossterm linkend="gloss-database-object">database
586 object</glossterm>. This will default to the value
587 of <symbol>*default-database*</symbol>.</para>
593 <title>Description</title>
594 <para>Deletes records satisfying the SQL expression
595 <parameter>where</parameter> from the table specified by
596 <parameter>from</parameter> in <parameter>database</parameter>
597 specifies a database which defaults to
598 <symbol>*default-database*</symbol>.
602 <title>Examples</title>
604 (select [first-name] [last-name] [email]
606 :where [= [emplid] 11]
608 => (("Yuri" "Gagarin" "gagarin@soviet.org"))
609 (delete-records :from [employee] :where [= [emplid] 11])
611 (select [first-name] [last-name] [email]
613 :where [= [emplid] 11]
619 <title>Side Effects</title>
621 Modifications are made to the underlying database.
625 <title>Affected by</title>
631 <title>Exceptional Situations</title>
633 An error of type <symbol>sql-database-data-error</symbol> is
634 signalled if <parameter>from</parameter> is not an existing
635 table in <parameter>database</parameter> or if the SQL
636 statement resulting from the symbolic expression
637 <parameter>where</parameter> does not return a Boolean value.
641 <title>See Also</title>
644 <member><link linkend="insert-records"><function>insert-records</function></link></member>
645 <member><link linkend="update-records"><function>update-records</function></link></member>
658 <!-- executing SQL commands and queries -->
660 <refentry id="execute-command">
662 <refentrytitle>EXECUTE-COMMAND</refentrytitle>
665 <refname>EXECUTE-COMMAND</refname>
666 <refpurpose>Execute an SQL command which returns no values.</refpurpose>
667 <refclass>Generic Function</refclass>
670 <title>Syntax</title>
672 <function>execute-command</function> <replaceable>sql-expression</replaceable> &key <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
675 <title>Arguments and Values</title>
678 <term><parameter>sql-expression</parameter></term>
680 <para>An <glossterm linkend="gloss-sql-expression">sql
681 expression</glossterm> that represents an SQL
682 statement which will return no values.</para>
686 <term><parameter>database</parameter></term>
689 <glossterm linkend="gloss-database-object">database
690 object</glossterm>. This will default to the value
691 of <symbol>*default-database*</symbol>.</para>
693 </varlistentry></variablelist>
696 <title>Description</title>
697 <para>Executes the SQL command
698 <parameter>sql-expression</parameter>, which may be a symbolic
699 SQL expression or a string representing any SQL statement apart
700 from a query, on the supplied <parameter>database</parameter>
701 which defaults to <symbol>*default-database*</symbol>.
705 <title>Examples</title>
707 (execute-command "create table eventlog (time char(30),event char(70))")
710 (execute-command "create table eventlog (time char(30),event char(70))")
712 >> While accessing database #<CLSQL-POSTGRESQL:POSTGRESQL-DATABASE {480B2B6D}>
713 >> with expression "create table eventlog (time char(30),event char(70))":
714 >> Error NIL: ERROR: amcreate: eventlog relation already exists
718 >> 0: [ABORT] Return to Top-Level.
720 >> Debug (type H for help)
722 >> (CLSQL-POSTGRESQL::|(PCL::FAST-METHOD DATABASE-EXECUTE-COMMAND (T POSTGRESQL-DATABASE))|
725 >> #<unavailable-arg>
726 >> #<unavailable-arg>)
727 >> Source: (ERROR 'SQL-DATABASE-ERROR :DATABASE DATABASE :EXPRESSION ...)
730 (execute-command "drop table eventlog")
735 <title>Side Effects</title>
736 <para>Whatever effects the execution of the SQL statement has
737 on the underlying database, if any.</para>
740 <title>Affected by</title>
744 <title>Exceptional Situations</title>
745 <para>If the execution of the SQL statement leads to any errors,
746 an error of type <errortype>sql-database-error</errortype> is
750 <title>See Also</title>
753 <member><link linkend="query"><function>query</function></link></member>
764 <refentry id="query">
766 <refentrytitle>QUERY</refentrytitle>
769 <refname>QUERY</refname>
770 <refpurpose>Execute an SQL query and return the tuples as a
772 <refclass>Generic Function</refclass>
775 <title>Syntax</title>
777 <function>query</function> <replaceable>query-expression</replaceable> &key <replaceable>database</replaceable> <replaceable>result-types</replaceable> <replaceable>flatp</replaceable> <replaceable>field-names</replaceable> => <returnvalue>result</returnvalue></synopsis>
780 <title>Arguments and Values</title>
783 <term><parameter>query-expression</parameter></term>
785 <para>An <glossterm linkend="gloss-sql-expression">sql
786 expression</glossterm> that represents an SQL
787 query which is expected to return a (possibly empty)
792 <term><parameter>database</parameter></term>
795 <glossterm linkend="gloss-database-object">database
796 object</glossterm>. This will default to the value
797 of <symbol>*default-database*</symbol>.</para>
801 <term><parameter>flatp</parameter></term>
803 <para>A Boolean whose default value is &nil;.</para>
807 <term><parameter>result-types</parameter></term>
810 <glossterm linkend="gloss-field-types">field type
811 specifier</glossterm>. The default is &nil;.
814 The purpose of this argument is cause &clsql; to
815 import SQL numeric fields into numeric Lisp objects
816 rather than strings. This reduces the cost of
817 allocating a temporary string and the &clsql; users'
818 inconvenience of converting number strings into number
822 A value of <symbol>:auto</symbol> causes &clsql;
823 to automatically convert SQL fields into a
824 numeric format where applicable. The default value of
825 &nil; causes all fields to be returned as strings
826 regardless of the SQL type. Otherwise a list is expected
827 which has a element for each field that specifies the
828 conversion. Valid type identifiers are:
829 <simplelist type="vert">
830 <member><symbol>:int</symbol> Field is imported as a
831 signed integer, from 8-bits to 64-bits depending
834 <member><symbol>:double</symbol> Field is imported as a
837 <member><symbol>t</symbol> Field is imported as a
841 If the list is shorter than the number of fields, the a
842 value of <symbol>t</symbol> is assumed for the field.
843 If the list is longer than the number of fields, the
844 extra elements are ignored.
849 <term><parameter>field-names</parameter></term>
852 A boolean with a default value of &t;. When &t;, this
853 function returns a second value of a list of field
854 names. When &nil;, this function only returns one value -
860 <term><returnvalue>result</returnvalue></term>
862 <para>A list representing the result set obtained. For
863 each tuple in the result set, there is an element in
864 this list, which is itself a list of all the attribute
865 values in the tuple.</para>
871 <title>Description</title>
873 Executes the SQL query expression
874 <parameter>query-expression</parameter>, which may be an SQL
875 expression or a string, on the supplied
876 <parameter>database</parameter> which defaults to
877 <symbol>*default-database*</symbol>. <parameter>result-types</parameter>
878 is a list of symbols which specifies the lisp type for each
879 field returned by <parameter>query-expression</parameter>.
882 If <parameter>result-types</parameter> is &nil; all results
883 are returned as strings whereas the default value of
884 <symbol>:auto</symbol> means that the lisp types are
885 automatically computed for each field.
888 <parameter>field-names</parameter> is &t; by default which
889 means that the second value returned is a list of strings
890 representing the columns selected by
891 <parameter>query-expression</parameter>. If
892 <parameter>field-names</parameter> is &nil;, the list of column
893 names is not returned as a second value.
896 <parameter>flatp</parameter> has a default value of &nil;
897 which means that the results are returned as a list of
898 lists.If FLATP is &t; and only one result is returned for each
899 record selected by <parameter>query-expression</parameter>,
900 the results are returned as elements of a list.
904 <title>Examples</title>
906 (query "select emplid,first_name,last_name,height from employee where emplid = 1")
907 => ((1 "Vladamir" "Lenin" 1.5564661d0)),
908 ("emplid" "first_name" "last_name" "height")
910 (query "select emplid,first_name,last_name,height from employee where emplid = 1"
912 => ((1 "Vladamir" "Lenin" 1.5564661d0))
914 (query "select emplid,first_name,last_name,height from employee where emplid = 1"
917 => (("1" "Vladamir" "Lenin" "1.5564661"))
919 (query "select emplid,first_name,last_name,height from employee where emplid = 1"
921 :result-types '(:int t t :double))
922 => ((1 "Vladamir" "Lenin" 1.5564661))
924 (query "select last_name from employee where emplid > 5" :flatp t)
925 => ("Andropov" "Chernenko" "Gorbachev" "Yeltsin" "Putin"),
928 (query "select last_name from employee where emplid > 10"
935 <title>Side Effects</title>
936 <para>Whatever effects the execution of the SQL query has
937 on the underlying database, if any.</para>
940 <title>Affected by</title>
944 <title>Exceptional Situations</title>
945 <para>If the execution of the SQL query leads to any errors, an
946 error of type <errortype>sql-database-error</errortype> is
950 <title>See Also</title>
952 <member><link linkend="execute-command"><function>execute-command</function></link></member>
953 <member><link linkend="print-query"><function>print-query</function></link></member>
954 <member><link linkend="do-query"><function>do-query</function></link></member>
955 <member><link linkend="map-query"><function>map-query</function></link></member>
956 <member><link linkend="loop-tuples"><function>loop</function></link></member>
957 <member><link linkend="select"><function>select</function></link></member>
962 <para>The <parameter>field-names</parameter> and
963 <parameter>result-types</parameter> keyword arguments are a
964 &clsql; extension.</para>
968 <refentry id="print-query">
970 <refentrytitle>PRINT-QUERY</refentrytitle>
973 <refname>PRINT-QUERY</refname>
974 <refpurpose>Prints a tabular report of query results.</refpurpose>
975 <refclass>Function</refclass>
978 <title>Syntax</title>
980 <function>print-query</function> <replaceable>query-expression</replaceable> &key <replaceable>titles</replaceable> <replaceable>formats</replaceable> <replaceable>sizes</replaceable> <replaceable>stream</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
983 <title>Arguments and Values</title>
986 <term><parameter>query-expression</parameter></term>
988 <para>An <glossterm linkend="gloss-sql-expression">sql
989 expression</glossterm> that represents an SQL
990 query which is expected to return a (possibly empty)
995 <term><parameter>database</parameter></term>
998 <glossterm linkend="gloss-database-object">database
999 object</glossterm>. This will default to the value
1000 of <symbol>*default-database*</symbol>.</para>
1004 <term><parameter>titles</parameter></term>
1007 A list of strings or &nil; which is the default value.
1012 <term><parameter>formats</parameter></term>
1015 A list of strings, &nil; or &t; which is the default value.
1020 <term><parameter>sizes</parameter></term>
1023 A list of numbers, &nil; or &t; which is the default value.
1028 <term><parameter>stream</parameter></term>
1031 An output stream or &t; which is the default value.
1038 <title>Description</title>
1039 <para>Prints a tabular report of the results returned by the SQL
1040 query <parameter>query-expression</parameter>, which may be a
1041 symbolic SQL expression or a string, in
1042 <parameter>database</parameter> which defaults to
1043 <symbol>*default-database*</symbol>. The report is printed onto
1044 <parameter>stream</parameter> which has a default value of &t;
1045 which means that <symbol>*standard-output*</symbol> is used. The
1046 <parameter>title</parameter> argument, which defaults to &nil;,
1047 allows the specification of a list of strings to use as column
1048 titles in the tabular output. <parameter>sizes</parameter>
1049 accepts a list of column sizes, one for each column selected by
1050 <parameter>query-expression</parameter>, to use in formatting
1051 the tabular report. The default value of &t; means that minimum
1052 sizes are computed. <parameter>formats</parameter> is a list of
1053 format strings to be used for printing each column selected by
1054 <parameter>query-expression</parameter>. The default value of
1055 <parameter>formats</parameter> is &t; meaning that
1056 <symbol>~A</symbol> is used to format all columns or
1057 <symbol>~VA</symbol> if column sizes are used.
1061 <title>Examples</title>
1063 (print-query [select [emplid] [first-name] [last-name] [email]
1065 :where [< [emplid] 5]]
1066 :titles '("ID" "FORENAME" "SURNAME" "EMAIL"))
1067 ID FORENAME SURNAME EMAIL
1068 1 Vladamir Lenin lenin@soviet.org
1069 2 Josef Stalin stalin@soviet.org
1070 3 Leon Trotsky trotsky@soviet.org
1071 4 Nikita Kruschev kruschev@soviet.org
1074 (print-query "select emplid,first_name,last_name,email from employee where emplid >= 5"
1075 :titles '("ID" "FORENAME" "SURNAME" "EMAIL"))
1076 ID FORENAME SURNAME EMAIL
1077 5 Leonid Brezhnev brezhnev@soviet.org
1078 6 Yuri Andropov andropov@soviet.org
1079 7 Konstantin Chernenko chernenko@soviet.org
1080 8 Mikhail Gorbachev gorbachev@soviet.org
1081 9 Boris Yeltsin yeltsin@soviet.org
1082 10 Vladamir Putin putin@soviet.org
1087 <title>Side Effects</title>
1093 <title>Affected by</title>
1099 <title>Exceptional Situations</title>
1101 If the execution of the SQL query leads to any errors, an
1102 error of type <errortype>sql-database-error</errortype> is
1107 <title>See Also</title>
1109 <member><link linkend="query"><function>query</function></link></member>
1110 <member><link linkend="do-query"><function>do-query</function></link></member>
1111 <member><link linkend="map-query"><function>map-query</function></link></member>
1112 <member><link linkend="loop-tuples"><function>loop</function></link></member>
1113 <member><link linkend="select"><function>select</function></link></member>
1117 <title>Notes</title>
1124 <refentry id="select">
1126 <refentrytitle>SELECT</refentrytitle>
1129 <refname>SELECT</refname>
1130 <refpurpose>Executes a query given the supplied constraints.</refpurpose>
1131 <refclass>Function</refclass>
1134 <title>Syntax</title>
1136 <function>select</function> &rest <replaceable>identifiers</replaceable> &key <replaceable>all</replaceable> <replaceable>distinct</replaceable> <replaceable>from</replaceable> <replaceable>group-by</replaceable> <replaceable>having</replaceable> <replaceable>order-by</replaceable> <replaceable>set-operation</replaceable> <replaceable>where</replaceable> <replaceable>result-types</replaceable> <replaceable>field-names</replaceable> <replaceable>flatp</replaceable> <replaceable>refresh</replaceable> <replaceable>caching</replaceable> <replaceable>database</replaceable> => <returnvalue>result</returnvalue></synopsis>
1139 <title>Arguments and Values</title>
1142 <term><parameter>identifiers</parameter></term>
1145 A set of <glossterm linkend="gloss-sql-expression">sql
1146 expressions</glossterm> each of which indicates a column
1152 <term><parameter>all</parameter></term>
1160 <term><parameter>distinct</parameter></term>
1168 <term><parameter>from</parameter></term>
1171 One or more SQL expression representing tables.
1176 <term><parameter>group-by</parameter></term>
1184 <term><parameter>having</parameter></term>
1192 <term><parameter>order-by</parameter></term>
1200 <term><parameter>set-operation</parameter></term>
1208 <term><parameter>where</parameter></term>
1216 <term><parameter>database</parameter></term>
1219 <glossterm linkend="gloss-database-object">database
1220 object</glossterm>. This will default to the value
1221 of <symbol>*default-database*</symbol>.</para>
1225 <term><parameter>flatp</parameter></term>
1227 <para>A Boolean whose default value is &nil;.</para>
1231 <term><parameter>result-types</parameter></term>
1234 <glossterm linkend="gloss-field-types">field type
1235 specifier</glossterm>. The default is &nil;.
1238 The purpose of this argument is cause &clsql; to
1239 import SQL numeric fields into numeric Lisp objects
1240 rather than strings. This reduces the cost of
1241 allocating a temporary string and the &clsql; users'
1242 inconvenience of converting number strings into number
1246 A value of <symbol>:auto</symbol> causes &clsql;
1247 to automatically convert SQL fields into a
1248 numeric format where applicable. The default value of
1249 &nil; causes all fields to be returned as strings
1250 regardless of the SQL type. Otherwise a list is expected
1251 which has a element for each field that specifies the
1252 conversion. Valid type identifiers are:
1253 <simplelist type="vert">
1254 <member><symbol>:int</symbol> Field is imported as a
1255 signed integer, from 8-bits to 64-bits depending
1256 upon the field type.
1258 <member><symbol>:double</symbol> Field is imported as a
1259 double-float number.
1261 <member><symbol>t</symbol> Field is imported as a
1265 If the list is shorter than the number of fields, the a
1266 value of <symbol>t</symbol> is assumed for the field.
1267 If the list is longer than the number of fields, the
1268 extra elements are ignored.
1273 <term><parameter>field-names</parameter></term>
1276 A boolean with a default value of &t;. When &t;, this
1277 function returns a second value of a list of field
1278 names. When &nil;, this function only returns one value -
1284 <term><parameter>refresh</parameter></term>
1287 This value is only considered when CLOS objects are being
1288 selected. A boolean with a default value of &nil;. When
1289 the value of the <variable>caching</variable> keyword is
1290 &t;, a second equivalent <function>select</function> call
1291 will return the same view class instance objects. When
1292 <variable>refresh</variable> is &t;, then slots of the
1293 existing instances are updated as necessary. In such
1294 cases, you may wish to override the hook
1295 <function>instance-refresh</function>.
1300 <term><parameter>caching</parameter></term>
1303 This value is only considered when CLOS objects are being
1304 selected. A boolean with a default value of
1305 <variable>*default-caching*</variable>. &clsql; caches
1306 objects in accordance with the &commonsql; interface: a
1307 second equivalent <function>select</function> call will
1308 return the same view class instance objects.
1313 <term><parameter>result</parameter></term>
1316 A list representing the result set obtained. For each
1317 tuple in the result set, there is an element in this
1318 list, which is itself a list of all the attribute values
1326 <title>Description</title>
1328 Executes a query on <parameter>database</parameter>, which has
1329 a default value of <symbol>*default-database*</symbol>,
1330 specified by the SQL expressions supplied using the remaining
1331 arguments in <parameter>args</parameter>. The
1332 <function>select</function> function can be used to generate
1333 queries in both functional and object oriented contexts.
1336 In the functional case, the required arguments specify the
1337 columns selected by the query and may be symbolic SQL
1338 expressions or strings representing attribute
1339 identifiers. Type modified identifiers indicate that the
1340 values selected from the specified column are converted to the
1341 specified lisp type. The keyword arguments
1342 <parameter>all</parameter>, <parameter>distinct</parameter>,
1343 <parameter>from</parameter>, <parameter>group-by</parameter>,
1344 <parameter>having</parameter>,
1345 <parameter>order-by</parameter>,
1346 <parameter>set-operation</parameter> and
1347 <parameter>where</parameter> are used to specify, using the
1348 symbolic SQL syntax, the corresponding components of the SQL
1349 query generated by the call to
1350 <function>select</function>.
1353 <parameter>result-types</parameter> is a list of symbols which
1354 specifies the lisp type for each field returned by the
1355 query. If <parameter>result-types</parameter> is &nil; all
1356 results are returned as strings whereas the default value of
1357 <symbol>:auto</symbol> means that the lisp types are
1358 automatically computed for each
1359 field. <parameter>field-names</parameter> is &t; by default
1360 which means that the second value returned is a list of
1361 strings representing the columns selected by the query. If
1362 <parameter>field-names</parameter> is &nil;, the list of
1363 column names is not returned as a second value.
1366 In the object oriented case, the required arguments to
1367 <function>select</function> are symbols denoting View Classes
1368 which specify the database tables to query. In this case,
1369 <function>select</function> returns a list of View Class
1370 instances whose slots are set from the attribute values of the
1371 records in the specified table. <symbol>Slot-value</symbol> is
1372 a legal operator which can be employed as part of the symbolic
1373 SQL syntax used in the <parameter>where</parameter> keyword
1374 argument to <function>select</function>.
1375 <parameter>refresh</parameter> is &nil; by default which means
1376 that the View Class instances returned are retrieved from a
1377 cache if an equivalent call to <function>select</function> has
1378 previously been issued. If <parameter>refresh</parameter> is
1379 true, the View Class instances returned are updated as
1380 necessary from the database and the generic function
1381 <function>instance-refreshed</function> is called to perform
1382 any necessary operations on the updated instances.
1385 In both object oriented and functional contexts,
1386 <parameter>flatp</parameter> has a default value of &nil;
1387 which means that the results are returned as a list of
1388 lists. If <parameter>flatp</parameter> is t and only one
1389 result is returned for each record selected in the query, the
1390 results are returned as elements of a list.
1394 <title>Examples</title>
1396 (select [first-name] :from [employee] :flatp t :distinct t
1399 :order-by [first-name])
1400 => ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladamir"
1403 (select [first-name] [count [*]] :from [employee]
1405 :group-by [first-name]
1406 :order-by [first-name]
1408 => (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
1409 ("Mikhail" "1") ("Nikita" "1") ("Vladamir" "2") ("Yuri" "1"))
1411 (select [last-name] :from [employee]
1412 :where [like [email] "%org"]
1413 :order-by [last-name]
1417 => ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
1418 "Stalin" "Trotsky" "Yeltsin")
1420 (select [max [emplid]] :from [employee]
1423 :result-types :auto)
1426 (clsql:select [avg [height]] :from [employee] :flatp t :field-names nil)
1429 (select [emplid] [last-name] :from [employee] :where [= [emplid] 1])
1431 ("emplid" "last_name")
1433 (select [emplid :string] :from [employee]
1434 :where [= 1 [emplid]]
1439 (select [emplid] :from [employee] :order-by [emplid]
1440 :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]]
1445 (clsql:select [emplid] :from [employee]
1446 :where [in [emplid] '(1 2 3 4)]
1452 (select [first-name] [last-name] :from [employee]
1454 :order-by '(([first-name] :asc) ([last-name] :desc)))
1455 => (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
1456 ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
1457 ("Nikita" "Kruschev") ("Vladamir" "Putin") ("Vladamir" "Lenin")
1458 ("Yuri" "Andropov"))
1460 (select [last-name] :from [employee]
1461 :set-operation [union [select [first-name] :from [employee]
1462 :order-by [last-name]]]
1466 => ("Andropov" "Boris" "Brezhnev" "Chernenko" "Gorbachev" "Josef" "Konstantin"
1467 "Kruschev" "Lenin" "Leon" "Leonid" "Mikhail" "Nikita" "Putin" "Stalin"
1468 "Trotsky" "Vladamir" "Yeltsin" "Yuri")
1472 <title>Side Effects</title>
1473 <para>Whatever effects the execution of the SQL query has on
1474 the underlying database, if any.</para>
1477 <title>Affected by</title>
1483 <title>Exceptional Situations</title>
1485 If the execution of the SQL query leads to any errors, an
1486 error of type <errortype>sql-database-error</errortype> is
1491 <title>See Also</title>
1493 <member><link linkend="query"><function>query</function></link></member>
1494 <member><link linkend="print-query"><function>print-query</function></link></member>
1495 <member><link linkend="do-query"><function>do-query</function></link></member>
1496 <member><link linkend="map-query"><function>map-query</function></link></member>
1497 <member><link linkend="loop-tuples"><function>loop</function></link></member>
1498 <member><link linkend="instance-refreshed"><function>instance-refreshed</function></link></member>
1502 <title>Notes</title>
1504 The <function>select</function> function is actually
1505 implemented in &clsql; with a single
1506 <symbol>&rest</symbol> parameter (which is subsequently
1507 destructured) rather than the keyword parameters presented
1508 here for the purposes of exposition. This means that incorrect
1509 or missing keywords or values may not trigger errors in the
1510 way that they would if <function>select</function> had been
1511 defined using keyword arguments.
1514 The <parameter>field-names</parameter> and
1515 <parameter>result-types</parameter> keyword arguments are a
1519 <parameter>select</parameter> is common across the functional
1520 and object-oriented data manipulation languages.
1526 <!-- iteration and mapping -->
1528 <refentry id="do-query">
1530 <refentrytitle>DO-QUERY</refentrytitle>
1533 <refname>DO-QUERY</refname>
1534 <refpurpose>Iterate over all the tuples of a query.</refpurpose>
1535 <refclass>Macro</refclass>
1538 <title>Syntax</title>
1540 <function>do-query</function> ((&rest <replaceable>args</replaceable>) <replaceable>query-expression</replaceable> &key <replaceable>database</replaceable> <replaceable>result-types</replaceable> &body <replaceable>body</replaceable> => <returnvalue>result</returnvalue></synopsis>
1543 <title>Arguments and Values</title>
1546 <term><parameter>args</parameter></term>
1548 <para>A list of variable names.</para>
1552 <term><parameter>query-expression</parameter></term>
1554 <para>An <glossterm linkend="gloss-sql-expression">sql
1555 expression</glossterm> that represents an SQL
1556 query which is expected to return a (possibly empty)
1557 result set, where each tuple has as many attributes as
1558 <parameter>function</parameter> takes arguments.</para>
1562 <term><parameter>database</parameter></term>
1565 <glossterm linkend="gloss-database-object">database
1566 object</glossterm>. This will default to
1567 <symbol>*default-database*</symbol>.</para>
1571 <term><parameter>result-types</parameter></term>
1574 A <glossterm linkend="gloss-field-types">field type
1575 specifier</glossterm>. The default is &nil;. See <link
1576 linkend="query"><function>query</function></link> for
1577 the semantics of this argument.
1582 <term><parameter>body</parameter></term>
1584 <para>A body of Lisp code, like in a
1585 <function>destructuring-bind</function> form.</para>
1589 <term><parameter>result</parameter></term>
1591 <para>The result of executing <parameter>body</parameter>.</para>
1597 <title>Description</title>
1599 Repeatedly executes <parameter>body</parameter> within a
1600 binding of <parameter>args</parameter> on the fields of each
1601 row selected by the SQL query
1602 <parameter>query-expression</parameter>, which may be a string
1603 or a symbolic SQL expression, in
1604 <parameter>database</parameter> which defaults to
1605 <symbol>*default-database*</symbol>.
1608 The body of code is executed in a block named
1609 <symbol>nil</symbol> which may be returned from prematurely
1610 via <function>return</function> or
1611 <function>return-from</function>. In this case the result of
1612 evaluating the <function>do-query</function> form will be the
1613 one supplied to <function>return</function> or
1614 <function>return-from</function>. Otherwise the result will
1615 be <symbol>nil</symbol>.
1618 The body of code appears also is if wrapped in a
1619 <function>destructuring-bind</function> form, thus allowing
1620 declarations at the start of the body, especially those
1621 pertaining to the bindings of the variables named in
1622 <parameter>args</parameter>.
1625 <parameter>result-types</parameter> is a list of symbols which
1626 specifies the lisp type for each field returned by
1627 <parameter>query-expression</parameter>. If
1628 <parameter>result-types</parameter> is &nil; all results are
1629 returned as strings whereas the default value of
1630 <symbol>:auto</symbol> means that the lisp types are
1631 automatically computed for each field.
1634 <parameter>query-expression</parameter> may be an object query
1635 (i.e., the selection arguments refer to View Classes), in
1636 which case <parameter>args</parameter> are bound to the tuples
1637 of View Class instances returned by the object oriented query.
1641 <title>Examples</title>
1643 (do-query ((salary name) "select salary,name from simple")
1644 (format t "~30A gets $~2,5$~%" name (read-from-string salary)))
1645 >> Mai, Pierre gets $10000.00
1646 >> Hacker, Random J. gets $08000.50
1649 (do-query ((salary name) "select salary,name from simple")
1650 (return (cons salary name)))
1651 => ("10000.00" . "Mai, Pierre")
1654 (do-query ((name) [select [last-name] :from [employee]
1655 :order-by [last-name]])
1658 => ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
1659 "Chernenko" "Brezhnev" "Andropov")
1662 (do-query ((e) [select 'employee :order-by [last-name]])
1663 (push (slot-value e 'last-name) result))
1665 => ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
1666 "Chernenko" "Brezhnev" "Andropov")
1670 <title>Side Effects</title>
1671 <para>Whatever effects the execution of the SQL query has
1672 on the underlying database, if any.</para>
1675 <title>Affected by</title>
1679 <title>Exceptional Situations</title>
1680 <para>If the execution of the SQL query leads to any
1681 errors, an error of type
1682 <errortype>sql-database-error</errortype> is signalled.</para>
1683 <para>If the number of variable names in
1684 <parameter>args</parameter> and the number of attributes in
1685 the tuples in the result set don't match up, an error is
1689 <title>See Also</title>
1691 <member><link linkend="query"><function>query</function></link></member>
1692 <member><link linkend="map-query"><function>map-query</function></link></member>
1693 <member><link linkend="print-query"><function>print-query</function></link></member>
1694 <member><link linkend="loop-tuples"><function>loop</function></link></member>
1695 <member><link linkend="select"><function>select</function></link></member>
1699 <title>Notes</title>
1700 <para>The <parameter>result-types</parameter> keyword argument
1701 is a &clsql; extension.</para>
1703 <parameter>do-query</parameter> is common across the functional
1704 and object-oriented data manipulation languages.
1709 <refentry id="loop-tuples">
1711 <refentrytitle>LOOP</refentrytitle>
1714 <refname>LOOP</refname>
1715 <refpurpose>Extension to Common Lisp
1716 <computeroutput>Loop</computeroutput> to iterate over all the
1717 tuples of a query via a loop clause.</refpurpose>
1718 <refclass>Loop Clause</refclass>
1721 <title>Compatibility</title>
1722 <caution><para><function>loop-for-as-tuples</function> only works with &cmucl;.</para></caution>
1725 <title>Syntax</title>
1726 <synopsis>{as | for} <replaceable>var</replaceable> [<replaceable>type-spec</replaceable>] being {each | the} {record | records | tuple | tuples} {in | of} <replaceable>query</replaceable> [from <replaceable>database</replaceable>]</synopsis>
1729 <title>Arguments and Values</title>
1732 <term><parameter>var</parameter></term>
1734 <para>A <literal>d-var-spec</literal>, as defined in the
1735 grammar for <function>loop</function>-clauses in the ANSI
1736 Standard for Common Lisp. This allows for the usual
1737 loop-style destructuring.</para>
1741 <term><parameter>type-spec</parameter></term>
1743 <para>An optional <literal>type-spec</literal> either
1744 simple or destructured, as defined in the grammar for
1745 <function>loop</function>-clauses in the ANSI Standard for
1750 <term><parameter>query</parameter></term>
1752 <para>An <glossterm linkend="gloss-sql-expression">sql
1753 expression</glossterm> that represents an SQL
1754 query which is expected to return a (possibly empty)
1755 result set, where each tuple has as many attributes as
1756 <parameter>function</parameter> takes arguments.</para>
1760 <term><parameter>database</parameter></term>
1763 <glossterm linkend="gloss-database-object">database
1764 object</glossterm>. This will default to the value
1765 of <symbol>*default-database*</symbol>.</para>
1771 <title>Description</title>
1772 <para>This clause is an iteration driver for
1773 <function>loop</function>, that binds the given variable
1774 (possibly destructured) to the consecutive tuples (which are
1775 represented as lists of attribute values) in the result set
1776 returned by executing the SQL <parameter>query</parameter>
1777 expression on the <parameter>database</parameter>
1780 <parameter>query</parameter> may be an object query (i.e., the
1781 selection arguments refer to View Classes), in which case the
1782 supplied variable is bound to the tuples of View Class
1783 instances returned by the object oriented query.
1787 <title>Examples</title>
1789 (defvar *my-db* (connect '("dent" "newesim" "dent" "dent"))
1792 (loop with time-graph = (make-hash-table :test #'equal)
1793 with event-graph = (make-hash-table :test #'equal)
1794 for (time event) being the tuples of "select time,event from log"
1797 (incf (gethash time time-graph 0))
1798 (incf (gethash event event-graph 0))
1800 (flet ((show-graph (k v) (format t "~40A => ~5D~%" k v)))
1801 (format t "~&Time-Graph:~%===========~%")
1802 (maphash #'show-graph time-graph)
1803 (format t "~&~%Event-Graph:~%============~%")
1804 (maphash #'show-graph event-graph))
1805 (return (values time-graph event-graph)))
1814 >> CLOS Benchmark entry. => 9000
1815 >> Demo Text... => 3
1816 >> doit-text => 3000
1817 >> C Benchmark entry. => 12000
1818 >> CLOS Benchmark entry => 32000
1819 => #<EQUAL hash table, 3 entries {48350A1D}>
1820 => #<EQUAL hash table, 5 entries {48350FCD}>
1822 (loop for (forename surname)
1824 [select [first-name] [last-name] :from [employee]
1825 :order-by [last-name]]
1826 collect (concatenate 'string forename " " surname))
1827 => ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
1828 "Nikita Kruschev" "Vladamir Lenin" "Vladamir Putin" "Josef Stalin"
1829 "Leon Trotsky" "Boris Yeltsin")
1831 (loop for (e) being the records in
1832 [select 'employee :where [< [emplid] 4] :order-by [emplid]]
1833 collect (slot-value e 'last-name))
1834 => ("Lenin" "Stalin" "Trotsky")
1838 <title>Side Effects</title>
1839 <para>Whatever effects the execution of the SQL query has
1840 on the underlying database, if any.</para>
1843 <title>Affected by</title>
1847 <title>Exceptional Situations</title>
1848 <para>If the execution of the SQL query leads to any
1849 errors, an error of type
1850 <errortype>sql-database-error</errortype> is signalled.</para>
1851 <para>Otherwise, any of the exceptional situations of
1852 <function>loop</function> applies.</para>
1855 <title>See Also</title>
1858 <member><link linkend="query"><function>query</function></link></member>
1859 <member><link linkend="map-query"><function>map-query</function></link></member>
1860 <member><link linkend="do-query"><function>do-query</function></link></member>
1861 <member><link linkend="print-query"><function>print-query</function></link></member>
1862 <member><link linkend="select"><function>select</function></link></member>
1867 <title>Notes</title>
1868 <para>The <parameter>database</parameter> loop keyword is a
1869 &clsql; extension.</para>
1871 The extended <function>loop</function> syntax is common across
1872 the functional and object-oriented data manipulation
1878 <refentry id="map-query">
1880 <refentrytitle>MAP-QUERY</refentrytitle>
1883 <refname>MAP-QUERY</refname>
1884 <refpurpose>Map a function over all the tuples from a
1886 <refclass>Function</refclass>
1889 <title>Syntax</title>
1890 <synopsis><function>map-query</function> <replaceable>output-type-spec</replaceable> <replaceable>function</replaceable> <replaceable>query-expression</replaceable> &key <replaceable>database</replaceable> <replaceable>result-types</replaceable> => <returnvalue>result</returnvalue></synopsis>
1893 <title>Arguments and Values</title>
1896 <term><parameter>output-type-spec</parameter></term>
1898 <para>A sequence type specifier or <symbol>nil</symbol>.</para>
1902 <term><parameter>function</parameter></term>
1904 <para>A function designator.
1905 <parameter>function</parameter> takes a single argument which
1906 is the atom value for a query single with a single column
1907 or is a list of values for a multi-column query.</para>
1911 <term><parameter>query-expression</parameter></term>
1913 <para>An <glossterm linkend="gloss-sql-expression">sql
1914 expression</glossterm> that represents an SQL
1915 query which is expected to return a (possibly empty)
1920 <term><parameter>database</parameter></term>
1923 <glossterm linkend="gloss-database-object">database
1924 object</glossterm>. This will default to the value
1925 of <symbol>*default-database*</symbol>.</para>
1929 <term><parameter>result-types</parameter></term>
1932 A <glossterm linkend="gloss-field-types">field type
1933 specifier</glossterm>. The default is &nil;. See <link
1934 linkend="query"><function>query</function></link> for
1935 the semantics of this argument.
1940 <term><returnvalue>result</returnvalue></term>
1942 <para>If <parameter>output-type-spec</parameter> is a
1943 type specifier other than <symbol>nil</symbol>, then a
1944 sequence of the type it denotes. Otherwise
1945 <symbol>nil</symbol> is returned.</para>
1951 <title>Description</title>
1953 Applies <parameter>function</parameter> to the successive
1954 tuples in the result set returned by executing the SQL
1955 <parameter>query-expression</parameter>. If the
1956 <parameter>output-type-spec</parameter> is
1957 <symbol>nil</symbol>, then the result of each application of
1958 <parameter>function</parameter> is discarded, and
1959 <function>map-query</function> returns <symbol>nil</symbol>.
1960 Otherwise the result of each successive application of
1961 <parameter>function</parameter> is collected in a sequence of
1962 type <parameter>output-type-spec</parameter>, where the jths
1963 element is the result of applying
1964 <parameter>function</parameter> to the attributes of the jths
1965 tuple in the result set. The collected sequence is the result
1966 of the call to <function>map-query</function>.
1969 If the <parameter>output-type-spec</parameter> is a subtype of
1970 <type>list</type>, the result will be a <type>list</type>.
1973 If the <parameter>result-type</parameter> is a subtype of
1974 <type>vector</type>, then if the implementation can determine
1975 the element type specified for the
1976 <parameter>result-type</parameter>, the element type of the
1977 resulting array is the result of
1978 <emphasis>upgrading</emphasis> that element type; or, if the
1979 implementation can determine that the element type is
1980 unspecified (or <symbol>*</symbol>), the element type of the
1981 resulting array is <type>t</type>; otherwise, an error is
1985 If <parameter>result-types</parameter> is &nil; all results
1986 are returned as strings whereas the default value of
1987 <symbol>:auto</symbol> means that the lisp types are
1988 automatically computed for each field.</para>
1990 <parameter>query-expression</parameter> may be an object query
1991 (i.e., the selection arguments refer to View Classes), in
1992 which case the supplied function is applied to the tuples of
1993 View Class instances returned by the object oriented query.
1997 <title>Examples</title>
1999 (map-query 'list #'(lambda (tuple)
2000 (multiple-value-bind (salary name) tuple
2001 (declare (ignorable name))
2002 (read-from-string salary)))
2003 "select salary,name from simple where salary > 8000")
2006 (map-query '(vector double-float)
2008 (multiple-value-bind (salary name) tuple
2009 (declare (ignorable name))
2010 (let ((*read-default-float-format* 'double-float))
2011 (coerce (read-from-string salary) 'double-float))
2012 "select salary,name from simple where salary > 8000")))
2013 => #(10000.0d0 8000.5d0)
2015 => (SIMPLE-ARRAY DOUBLE-FLOAT (2))
2018 (values (map-query nil #'(lambda (tuple)
2019 (multiple-value-bind (salary name) tuple
2020 (push (cons name (read-from-string salary)) list))
2021 "select salary,name from simple where salary > 8000"))
2024 => (("Hacker, Random J." . 8000.5) ("Mai, Pierre" . 10000.0))
2026 (map-query 'vector #'identity
2027 [select [last-name] :from [employee] :flatp t
2028 :order-by [last-name]])
2029 => #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
2030 "Stalin" "Trotsky" "Yeltsin")
2032 (map-query 'list #'identity
2033 [select [first-name] [last-name] :from [employee]
2034 :order-by [last-name]])
2035 => (("Yuri" "Andropov") ("Leonid" "Brezhnev") ("Konstantin" "Chernenko")
2036 ("Mikhail" "Gorbachev") ("Nikita" "Kruschev") ("Vladamir" "Lenin")
2037 ("Vladamir" "Putin") ("Josef" "Stalin") ("Leon" "Trotsky")
2038 ("Boris" "Yeltsin"))
2040 (map-query 'list #'last-name [select 'employee :order-by [emplid]])
2041 => ("Lenin" "Stalin" "Trotsky" "Kruschev" "Brezhnev" "Andropov" "Chernenko"
2042 "Gorbachev" "Yeltsin" "Putin")
2046 <title>Side Effects</title>
2047 <para>Whatever effects the execution of the SQL query has
2048 on the underlying database, if any.</para>
2051 <title>Affected by</title>
2055 <title>Exceptional Situations</title>
2056 <para>If the execution of the SQL query leads to any
2057 errors, an error of type
2058 <errortype>sql-database-error</errortype> is signalled.</para>
2059 <para>An error of type <errortype>type-error</errortype> must
2060 be signaled if the <parameter>output-type-spec</parameter> is
2061 not a recognizable subtype of <type>list</type>, not a
2062 recognizable subtype of <type>vector</type>, and not
2063 <symbol>nil</symbol>.</para>
2064 <para>An error of type <errortype>type-error</errortype>
2065 should be signaled if
2066 <parameter>output-type-spec</parameter> specifies the number
2067 of elements and the size of the result set is different from
2071 <title>See Also</title>
2073 <member><link linkend="query"><function>query</function></link></member>
2074 <member><link linkend="do-query"><function>do-query</function></link></member>
2075 <member><link linkend="print-query"><function>print-query</function></link></member>
2076 <member><link linkend="loop-tuples"><function>loop</function></link></member>
2077 <member><link linkend="select"><function>select</function></link></member>
2081 <title>Notes</title>
2082 <para>The <parameter>result-types</parameter> keyword argument
2083 is a &clsql; extension.</para>
2085 <parameter>map-query</parameter> is common across the
2086 functional and object-oriented data manipulation languages.