summaryrefslogtreecommitdiff
path: root/doc/ref-fdml.xml
blob: 7113b6a98e3022056dcf050ecdc53a25520f44ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
<?xml version='1.0' ?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
<!ENTITY % myents SYSTEM "entities.inc">
%myents;
]>

<!-- Functional Data Manipulation Language --> 
<reference id="ref-fdml"> 
  <title>Functional Data Manipulation Language (FDML)</title> 
  <partintro>
    <para>
      The functional data manipulation interface provided by &clsql;
      includes functions for inserting, updating and deleting records
      in existing database tables and executing SQL queries and
      statements with the results of queries returned as Lisp types.
      SQL statements expressed as strings may be executed with the
      <link linkend="query"><function>query</function></link> and
      <link
      linkend="execute-command"><function>execute-command</function></link>
      functions. The <link
      linkend="select"><function>select</function></link> function, on
      the other hand, allows for the construction of queries in Lisp
      using the symbolic SQL syntax. Finally, iterative manipulation
      of query results is supported by <link
      linkend="do-query"><function>do-query</function></link>, <link
      linkend="map-query"><function>map-query</function></link> and an
      extended clause for the <link
      linkend="loop-tuples"><function>loop</function></link> macro.
    </para>
  </partintro>

  <!-- Caching table queries --> 

  <refentry id="cache-table-queries-default">
    <refmeta>
      <refentrytitle>*CACHE-TABLE-QUERIES-DEFAULT*</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>*CACHE-TABLE-QUERIES-DEFAULT*</refname>
      <refpurpose>Specifies the default behaviour for caching of
      attribute types.</refpurpose>
      <refclass>Variable</refclass>
    </refnamediv>
    <refsect1>
      <title>Value Type</title>
      <para>
        A valid argument to the <parameter>action</parameter>
        parameter of <function>cache-table-queries</function>,
        i.e. one of
        <simplelist type="inline">
        <member>&t;</member>
        <member>&nil;</member>
        <member><symbol>:flush</symbol></member>
        </simplelist>.
      </para>
    </refsect1>
    <refsect1>
      <title>Initial Value</title>
      <para><symbol>nil</symbol></para>
    </refsect1>
    <refsect1>
      <title>Description</title> 
      <para> 
        Specifies the default behaivour for caching of attribute
        types. Meaningful values are &t;, &nil; and
        <symbol>:flush</symbol> as described for the
        <parameter>action</parameter> argument to
        <function>cache-table-queries</function>.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Affected By</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="cache-table-queries"><function>cache-table-queries</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>None.</para>
    </refsect1>
  </refentry>

  <refentry id="cache-table-queries">
    <refmeta>
      <refentrytitle>CACHE-TABLE-QUERIES</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>CACHE-TABLE-QUERIES</refname>
      <refpurpose>Control the caching of table attribute types.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>cache-table-queries</function> <replaceable>table</replaceable> &amp;key <replaceable>action</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>table</parameter></term>
          <listitem>
            <para>
              A string representing a database table, &t; or
              <symbol>:default</symbol>.
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>action</parameter></term>
          <listitem>
            <para>
              &t;, &nil; or <symbol>:flush</symbol>. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
      </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>Controls the caching of attribute type information on the
      table specified by <parameter>table</parameter> in
      <parameter>database</parameter> which defaults to
      <symbol>*default-database*</symbol>. <parameter>action</parameter>
      specifies the caching behaviour to adopt. If its value is &t;
      then attribute type information is cached whereas if its value
      is &nil; then attribute type information is not cached. If
      <parameter>action</parameter> is <symbol>:flush</symbol> then
      all existing type information in the cache for
      <parameter>table</parameter> is removed, but caching is still
      enabled. <parameter>table</parameter> may be a string
      representing a table for which the caching action is to be taken
      while the caching action is applied to all tables if
      <parameter>table</parameter> is &t;. Alternatively, when
      <parameter>table</parameter> is <symbol>:default</symbol>, the
      default caching action specified by
      <symbol>*cache-table-queries-default*</symbol> is applied to all
      tables for which a caching action has not been explicitly set.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(setf *cache-table-queries-default* t)
=> T
(create-table [foo]
              '(([id] integer)
                ([height] float)
                ([name] (string 24))
                ([comments] varchar)))
=> 
(cache-table-queries "foo")
=> 
(list-attribute-types "foo")
=> (("id" :INT4 4 NIL 1) ("height" :FLOAT8 8 NIL 1) ("name" :BPCHAR 24 NIL 1)
    ("comments" :VARCHAR 255 NIL 1))
(drop-table "foo")
=> 
(create-table [foo]
              '(([id] integer)
                ([height] float)
                ([name] (string 36))
                ([comments] (string 100))))
=> 
(cache-table-queries "foo" :action :flush)
=> 
(list-attribute-types "foo")
=> (("id" :INT4 4 NIL 1) ("height" :FLOAT8 8 NIL 1) ("name" :BPCHAR 36 NIL 1)
    ("comments" :BPCHAR 100 NIL 1))
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
       <para>
        The internal attribute cache for
        <parameter>database</parameter> is modified.
       </para>
     </refsect1>
    <refsect1>
      <title>Affected by</title>
      <simplelist>
        <member><link linkend="cache-table-queries-default"><symbol>*cache-table-queries-default*</symbol></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="cache-table-queries-default"><symbol>*cache-table-queries-default*</symbol></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>
        None. 
      </para>
    </refsect1>
  </refentry>

  <refentry id="insert-records">
    <refmeta>
      <refentrytitle>INSERT-RECORDS</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>INSERT-RECORDS</refname>
      <refpurpose>Insert tuples of data into a database table.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>insert-records</function> &amp;key <replaceable>into</replaceable> <replaceable>attributes</replaceable> <replaceable>values</replaceable> <replaceable>av-pairs</replaceable> <replaceable>query</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>into</parameter></term>
          <listitem>
            <para>
              A string, symbol or symbolic SQL expression representing
              the name of a table existing in
              <parameter>database</parameter>.
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>attributes</parameter></term>
          <listitem>
            <para>
              A list of attribute identifiers or &nil;. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>values</parameter></term>
          <listitem>
            <para>
              A list of attribute values or &nil;.               
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>av-pairs</parameter></term>
          <listitem>
            <para>
              A list of attribute identifier/value pairs or &nil;. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>query</parameter></term>
          <listitem>
            <para>
              A query expression or &nil;. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
      </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Inserts records into the table specified by
        <parameter>into</parameter> in <parameter>database</parameter>
        which defaults to <symbol>*default-database*</symbol>. 
      </para>
      <para>
        There are five ways of specifying the values inserted into
        each row. In the first <parameter>values</parameter> contains
        a list of values to insert and
        <parameter>attributes</parameter>,
        <parameter>av-pairs</parameter> and
        <parameter>query</parameter> are &nil;. This can be used when
        values are supplied for all attributes in
        <parameter>into</parameter>. In the second,
        <parameter>attributes</parameter> is a list of column names,
        <parameter>values</parameter> is a corresponding list of
        values and <parameter>av-pairs</parameter> and
        <parameter>query</parameter> are &nil;. In the third,
        <parameter>attributes</parameter>,
        <parameter>values</parameter> and <parameter>query</parameter>
        are &nil; and <parameter>av-pairs</parameter> is an alist of
        (attribute value) pairs. In the fourth,
        <parameter>values</parameter>, <parameter>av-pairs</parameter>
        and <parameter>attributes</parameter> are &nil; and
        <parameter>query</parameter> is a symbolic SQL query
        expression in which the selected columns also exist in
        <parameter>into</parameter>. In the fifth method,
        <parameter>values</parameter> and
        <parameter>av-pairs</parameter> are nil and
        <parameter>attributes</parameter> is a list of column names
        and <parameter>query</parameter> is a symbolic SQL query
        expression which returns values for the specified columns.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 11] 
        :field-names nil)
=> NIL
(insert-records :into [employee] 
                :attributes '(emplid groupid first_name last_name email 
                              ecompanyid managerid)
                :values '(11 1 "Yuri" "Gagarin" "gagarin@soviet.org" 
                          1 1))
=> 
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 11] 
        :field-names nil)
=> (("Yuri" "Gagarin" "gagarin@soviet.org"))
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>
        Modifications are made to the underlying database. 
      </para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        An error of type <symbol>sql-database-data-error</symbol> is
        signalled if <parameter>table</parameter> is not an existing
        table in <parameter>database</parameter> or if the specified
        attributes are not found. 
      </para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="update-records"><function>update-records</function></link></member>
        <member><link linkend="delete-records"><function>delete-records</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>
        None. 
      </para>
    </refsect1>
  </refentry>

  <refentry id="update-records">
    <refmeta>
      <refentrytitle>UPDATE-RECORDS</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>UPDATE-RECORDS</refname>
      <refpurpose>Updates the values of existing records.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>update-records</function> <replaceable>table</replaceable> &amp;key  <replaceable>attributes</replaceable> <replaceable>values</replaceable> <replaceable>av-pairs</replaceable> <replaceable>where</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>table</parameter></term>
          <listitem>
            <para>
              A string, symbol or symbolic SQL expression representing
              the name of a table existing in
              <parameter>database</parameter>.
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>attributes</parameter></term>
          <listitem>
            <para>
              A list of attribute identifiers or &nil;. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>values</parameter></term>
          <listitem>
            <para>
              A list of attribute values or &nil;.               
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>av-pairs</parameter></term>
          <listitem>
            <para>
              A list of attribute identifier/value pairs or &nil;. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>where</parameter></term>
          <listitem>
            <para>
              A symbolic SQL expression. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
      </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Updates the attribute values of existing records satsifying
        the SQL expression <parameter>where</parameter> in the table
        specified by <parameter>table</parameter> in
        <parameter>database</parameter> which defaults to
        <symbol>*default-database*</symbol>.
      </para>
      <para> 
        There are three ways of specifying the values to update for
        each row. In the first, <parameter>values</parameter> contains
        a list of values to use in the update and
        <parameter>attributes</parameter> and
        <parameter>av-pairs</parameter> are &nil;. This can be used
        when values are supplied for all attributes in
        <parameter>table</parameter>. In the second,
        <parameter>attributes</parameter> is a list of column names,
        <parameter>values</parameter> is a corresponding list of
        values and <parameter>av-pairs</parameter> is &nil;. In the
        third, <parameter>attributes</parameter> and
        <parameter>values</parameter> are &nil; and
        <parameter>av-pairs</parameter> is an alist of (attribute
        value) pairs.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 1] 
        :field-names nil)
=> (("Vladimir" "Lenin" "lenin@soviet.org"))
(update-records [employee] 
                :av-pairs'((first_name "Yuri")
                           (last_name "Gagarin")
                           (email "gagarin@soviet.org"))
                :where [= [emplid] 1])
=> 
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 1] 
        :field-names nil)
=> (("Yuri" "Gagarin" "gagarin@soviet.org"))
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>
        Modifications are made to the underlying database. 
      </para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        An error of type <symbol>sql-database-data-error</symbol> is
        signalled if <parameter>table</parameter> is not an existing
        table in <parameter>database</parameter>, if the specified
        attributes are not found or if the SQL statement resulting
        from the symbolic expression <parameter>where</parameter> does
        not return a Boolean value.
      </para>
      <para>If the execution of the SQL query leads to any errors, an
      error of type <errortype>sql-database-error</errortype> is
      signalled.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <para>
        <simplelist>
          <member><link linkend="insert-records"><function>insert-records</function></link></member>
          <member><link linkend="delete-records"><function>delete-records</function></link></member>
        </simplelist>
      </para>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>
        None. 
      </para>
    </refsect1>
  </refentry>

  <refentry id="delete-records">
    <refmeta>
      <refentrytitle>DELETE-RECORDS</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>DELETE-RECORDS</refname>
      <refpurpose>Delete records from a database table.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>delete-records</function> &amp;key <replaceable>from</replaceable> <replaceable>where</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>from</parameter></term>
          <listitem>
            <para>
              A string, symbol or symbolic SQL expression representing
              the name of a table existing in
              <parameter>database</parameter>.
            </para> 
          </listitem>
        </varlistentry>
                <varlistentry>
          <term><parameter>where</parameter></term>
          <listitem>
            <para>
              A symbolic SQL expression. 
            </para> 
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>Deletes records satisfying the SQL expression
      <parameter>where</parameter> from the table specified by
      <parameter>from</parameter> in <parameter>database</parameter>
      specifies a database which defaults to
      <symbol>*default-database*</symbol>.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 11] 
        :field-names nil)
=> (("Yuri" "Gagarin" "gagarin@soviet.org"))
(delete-records :from [employee] :where [= [emplid] 11])
=> 
(select [first-name] [last-name] [email] 
        :from [employee]
        :where [= [emplid] 11] 
        :field-names nil)
=> NIL
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>
        Modifications are made to the underlying database. 
      </para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        An error of type <symbol>sql-database-data-error</symbol> is
        signalled if <parameter>from</parameter> is not an existing
        table in <parameter>database</parameter> or if the SQL
        statement resulting from the symbolic expression
        <parameter>where</parameter> does not return a Boolean value. 
      </para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <para>
        <simplelist>
          <member><link linkend="insert-records"><function>insert-records</function></link></member>
          <member><link linkend="update-records"><function>update-records</function></link></member>
        </simplelist>
      </para>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>
        None. 
      </para>
    </refsect1>
  </refentry>


  <!-- executing SQL commands and queries --> 

  <refentry id="execute-command">
    <refmeta>
      <refentrytitle>EXECUTE-COMMAND</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>EXECUTE-COMMAND</refname>
      <refpurpose>Execute an SQL command which returns no values.</refpurpose>
      <refclass>Generic Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>execute-command</function> <replaceable>sql-expression</replaceable> &amp;key <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>sql-expression</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            statement which will return no values.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
      </varlistentry></variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>Executes the SQL command
      <parameter>sql-expression</parameter>, which may be a symbolic
      SQL expression or a string representing any SQL statement apart
      from a query, on the supplied <parameter>database</parameter>
      which defaults to <symbol>*default-database*</symbol>.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
        (execute-command "create table eventlog (time char(30),event char(70))")
        => 

        (execute-command "create table eventlog (time char(30),event char(70))")
        >> 
        >> While accessing database #&lt;CLSQL-POSTGRESQL:POSTGRESQL-DATABASE {480B2B6D}>
        >>   with expression "create table eventlog (time char(30),event char(70))":
        >>   Error NIL: ERROR:  amcreate: eventlog relation already exists
        >>   has occurred.
        >> 
        >> Restarts:
        >>   0: [ABORT] Return to Top-Level.
        >> 
        >> Debug  (type H for help)
        >> 
        >> (CLSQL-POSTGRESQL::|(PCL::FAST-METHOD DATABASE-EXECUTE-COMMAND (T POSTGRESQL-DATABASE))|
        >>  #&lt;unused-arg>
        >>  #&lt;unused-arg>
        >>  #&lt;unavailable-arg>
        >>  #&lt;unavailable-arg>)
        >> Source: (ERROR 'SQL-DATABASE-ERROR :DATABASE DATABASE :EXPRESSION ...)
        >> 0] 0

        (execute-command "drop table eventlog")
        => 
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>Whatever effects the execution of the SQL statement has
      on the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>If the execution of the SQL statement leads to any errors,
      an error of type <errortype>sql-database-error</errortype> is
      signalled.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <para>
        <simplelist>
          <member><link linkend="query"><function>query</function></link></member>
        </simplelist>
      </para>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>None.</para>
    </refsect1>
  </refentry>


  <refentry id="query">
    <refmeta>
      <refentrytitle>QUERY</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>QUERY</refname>
      <refpurpose>Execute an SQL query and return the tuples as a 
      list.</refpurpose>
      <refclass>Generic Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>query</function> <replaceable>query-expression</replaceable> &amp;key <replaceable>database</replaceable> <replaceable>result-types</replaceable> <replaceable>flatp</replaceable> <replaceable>field-names</replaceable> => <returnvalue>result</returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>query-expression</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            query which is expected to return a (possibly empty)
            result set.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry> 
          <term><parameter>flatp</parameter></term>
          <listitem>
            <para>A Boolean whose default value is &nil;.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>result-types</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-field-types">field type
            specifier</glossterm>. The default is <symbol>:auto</symbol>;.
            </para>
            <para>
              The purpose of this argument is cause &clsql; to
              import SQL numeric fields into numeric Lisp objects
              rather than strings. This reduces the cost of
              allocating a temporary string and the &clsql; users'
              inconvenience of converting number strings into number
              objects.
            </para>
            <para>
              A value of <symbol>:auto</symbol> causes &clsql;
              to automatically convert SQL fields into a
              numeric format where applicable. The default value of
              &nil; causes all fields to be returned as strings
              regardless of the SQL type. Otherwise a list is expected
              which has a element for each field that specifies the
              conversion. Valid type identifiers are: 
              <simplelist type="vert">
                <member><symbol>:int</symbol> Field is imported as a
                signed integer, from 8-bits to 64-bits depending
                upon the field type.
                </member>
                <member><symbol>:double</symbol> Field is imported as a
                double-float number.
                </member>
                <member><symbol>t</symbol> Field is imported as a
                string.
                </member>
              </simplelist>
              If the list is shorter than the number of fields, the a
              value of <symbol>t</symbol> is assumed for the field.
              If the list is longer than the number of fields, the
              extra elements are ignored.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>field-names</parameter></term>
          <listitem> 
          <para>
            A boolean with a default value of &t;. When &t;, this
            function returns a second value of a list of field
            names. When &nil;, this function only returns one value -
            the list of rows.
          </para>
          </listitem>
        </varlistentry>        
        <varlistentry>
          <term><returnvalue>result</returnvalue></term>
          <listitem>
            <para>A list representing the result set obtained.  For
            each tuple in the result set, there is an element in
            this list, which is itself a list of all the attribute 
            values in the tuple.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Executes the SQL query expression
        <parameter>query-expression</parameter>, which may be an SQL
        expression or a string, on the supplied
        <parameter>database</parameter> which defaults to
        <symbol>*default-database*</symbol>. <parameter>result-types</parameter>
        is a list of symbols which specifies the lisp type for each
        field returned by <parameter>query-expression</parameter>.
      </para>
      <para> 
        If <parameter>result-types</parameter> is &nil; all results
        are returned as strings whereas the default value of
        <symbol>:auto</symbol> means that the lisp types are
        automatically computed for each field.
      </para> 
      <para> 
        <parameter>field-names</parameter> is &t; by default which
        means that the second value returned is a list of strings
        representing the columns selected by
        <parameter>query-expression</parameter>. If
        <parameter>field-names</parameter> is &nil;, the list of column
        names is not returned as a second value.
      </para>
      <para> 
        <parameter>flatp</parameter> has a default value of &nil;
        which means that the results are returned as a list of
        lists.If FLATP is &t; and only one result is returned for each
        record selected by <parameter>query-expression</parameter>,
        the results are returned as elements of a list.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(query "select emplid,first_name,last_name,height from employee where emplid = 1")
=> ((1 "Vladimir" "Lenin" 1.5564661d0)), 
   ("emplid" "first_name" "last_name" "height")

(query "select emplid,first_name,last_name,height from employee where emplid = 1" 
       :field-names nil)
=> ((1 "Vladimir" "Lenin" 1.5564661d0))

(query "select emplid,first_name,last_name,height from employee where emplid = 1" 
       :field-names nil
       :result-types nil)
=> (("1" "Vladimir" "Lenin" "1.5564661"))

(query "select emplid,first_name,last_name,height from employee where emplid = 1" 
       :field-names nil
       :result-types '(:int t t :double))
=> ((1 "Vladimir" "Lenin" 1.5564661))

(query "select last_name from employee where emplid > 5" :flatp t)
=> ("Andropov" "Chernenko" "Gorbachev" "Yeltsin" "Putin"), 
   ("last_name")

(query "select last_name from employee where emplid > 10" 
       :flatp t 
       :field-names nil)
=> NIL
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>Whatever effects the execution of the SQL query has
      on the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>If the execution of the SQL query leads to any errors, an
      error of type <errortype>sql-database-error</errortype> is
      signalled.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="execute-command"><function>execute-command</function></link></member>
        <member><link linkend="print-query"><function>print-query</function></link></member>
        <member><link linkend="do-query"><function>do-query</function></link></member>
        <member><link linkend="map-query"><function>map-query</function></link></member>
        <member><link linkend="loop-tuples"><function>loop</function></link></member>
        <member><link linkend="select"><function>select</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>The <parameter>field-names</parameter> and
      <parameter>result-types</parameter> keyword arguments are a
      &clsql; extension.</para>
    </refsect1>
  </refentry>
  
  <refentry id="print-query">
    <refmeta>
      <refentrytitle>PRINT-QUERY</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>PRINT-QUERY</refname>
      <refpurpose>Prints a tabular report of query results.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>print-query</function> <replaceable>query-expression</replaceable> &amp;key <replaceable>titles</replaceable> <replaceable>formats</replaceable> <replaceable>sizes</replaceable> <replaceable>stream</replaceable> <replaceable>database</replaceable> => <returnvalue></returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>query-expression</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            query which is expected to return a (possibly empty)
            result set.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>titles</parameter></term>
          <listitem>
            <para>
              A list of strings or &nil; which is the default value. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>formats</parameter></term>
          <listitem>
            <para>
              A list of strings, &nil; or &t; which is the default value.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>sizes</parameter></term>
          <listitem>
            <para>
              A list of numbers, &nil; or &t; which is the default value. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>stream</parameter></term>
          <listitem>
            <para>
              An output stream or &t; which is the default value. 
            </para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>Prints a tabular report of the results returned by the SQL
      query <parameter>query-expression</parameter>, which may be a
      symbolic SQL expression or a string, in
      <parameter>database</parameter> which defaults to
      <symbol>*default-database*</symbol>. The report is printed onto
      <parameter>stream</parameter> which has a default value of &t;
      which means that <symbol>*standard-output*</symbol> is used. The
      <parameter>title</parameter> argument, which defaults to &nil;,
      allows the specification of a list of strings to use as column
      titles in the tabular output. <parameter>sizes</parameter>
      accepts a list of column sizes, one for each column selected by
      <parameter>query-expression</parameter>, to use in formatting
      the tabular report. The default value of &t; means that minimum
      sizes are computed. <parameter>formats</parameter> is a list of
      format strings to be used for printing each column selected by
      <parameter>query-expression</parameter>. The default value of
      <parameter>formats</parameter> is &t; meaning that
      <symbol>~A</symbol> is used to format all columns or
      <symbol>~VA</symbol> if column sizes are used.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(print-query [select [emplid] [first-name] [last-name] [email] 
                     :from [employee] 
                     :where [&lt; [emplid] 5]] 
              :titles '("ID" "FORENAME" "SURNAME" "EMAIL"))
ID FORENAME SURNAME  EMAIL               
1  Vladimir Lenin    lenin@soviet.org    
2  Josef    Stalin   stalin@soviet.org   
3  Leon     Trotsky  trotsky@soviet.org  
4  Nikita   Kruschev kruschev@soviet.org 
=> 

(print-query "select emplid,first_name,last_name,email from employee where emplid >= 5" 
             :titles '("ID" "FORENAME" "SURNAME" "EMAIL"))
ID FORENAME   SURNAME   EMAIL                
5  Leonid     Brezhnev  brezhnev@soviet.org  
6  Yuri       Andropov  andropov@soviet.org  
7  Konstantin Chernenko chernenko@soviet.org 
8  Mikhail    Gorbachev gorbachev@soviet.org 
9  Boris      Yeltsin   yeltsin@soviet.org   
10 Vladimir   Putin     putin@soviet.org     
=> 
</screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        If the execution of the SQL query leads to any errors, an
        error of type <errortype>sql-database-error</errortype> is
        signalled.  
      </para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="query"><function>query</function></link></member>
        <member><link linkend="do-query"><function>do-query</function></link></member>
        <member><link linkend="map-query"><function>map-query</function></link></member>
        <member><link linkend="loop-tuples"><function>loop</function></link></member>
        <member><link linkend="select"><function>select</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>
        None. 
      </para>
    </refsect1>
  </refentry>

  <refentry id="select">
    <refmeta>
      <refentrytitle>SELECT</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>SELECT</refname>
      <refpurpose>Executes a query given the supplied constraints.</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>select</function> &amp;rest <replaceable>identifiers</replaceable> &amp;key <replaceable>all</replaceable> <replaceable>distinct</replaceable> <replaceable>from</replaceable> <replaceable>group-by</replaceable> <replaceable>having</replaceable> <replaceable>limit</replaceable> <replaceable>offset</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>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>identifiers</parameter></term>
          <listitem>
            <para>
              A set of <glossterm linkend="gloss-sql-expression">sql
              expressions</glossterm> each of which indicates a column
              to query.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>all</parameter></term>
          <listitem>
            <para>
              A Boolean. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>distinct</parameter></term>
          <listitem>
            <para>
              A Boolean. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>from</parameter></term>
          <listitem>
            <para>
              One or more SQL expression representing tables.  
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>group-by</parameter></term>
          <listitem>
            <para>
              An SQL expression. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>having</parameter></term>
          <listitem>
            <para>
              An SQL expression. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>limit</parameter></term>
          <listitem>
            <para>
              A non-negative integer. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>offset</parameter></term>
          <listitem>
            <para>
              A non-negative integer. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>order-by</parameter></term>
          <listitem>
            <para>
              An SQL expression. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>set-operation</parameter></term>
          <listitem>
            <para>
              An SQL expression. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>where</parameter></term>
          <listitem>
            <para>
              An SQL expression. 
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry> 
          <term><parameter>flatp</parameter></term>
          <listitem>
            <para>A Boolean whose default value is &nil;.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>result-types</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-field-types">field type
            specifier</glossterm>. The default is <symbol>:auto</symbol>.
            </para>
            <para>
              The purpose of this argument is cause &clsql; to
              import SQL numeric fields into numeric Lisp objects
              rather than strings. This reduces the cost of
              allocating a temporary string and the &clsql; users'
              inconvenience of converting number strings into number
              objects.
            </para>
            <para>
              A value of <symbol>:auto</symbol> causes &clsql;
              to automatically convert SQL fields into a
              numeric format where applicable. The default value of
              &nil; causes all fields to be returned as strings
              regardless of the SQL type. Otherwise a list is expected
              which has a element for each field that specifies the
              conversion. Valid type identifiers are: 
              <simplelist type="vert">
                <member><symbol>:int</symbol> Field is imported as a
                signed integer, from 8-bits to 64-bits depending
                upon the field type.
                </member>
                <member><symbol>:double</symbol> Field is imported as a
                double-float number.
                </member>
                <member><symbol>t</symbol> Field is imported as a
                string.
                </member>
              </simplelist>
              If the list is shorter than the number of fields, the a
              value of <symbol>t</symbol> is assumed for the field.
              If the list is longer than the number of fields, the
              extra elements are ignored.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>field-names</parameter></term>
          <listitem> 
          <para>
            A boolean with a default value of &t;. When &t;, this
            function returns a second value of a list of field
            names. When &nil;, this function only returns one value -
            the list of rows.
          </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>refresh</parameter></term>
          <listitem> 
          <para>
            This value is only considered when CLOS objects are being
            selected. A boolean with a default value of &nil;. When
            the value of the <varname>caching</varname> keyword is
            &t;, a second equivalent <function>select</function> call
            will return the same view class instance objects. When
            <varname>refresh</varname> is &t;, then slots of the
            existing instances are updated as necessary. In such
            cases, you may wish to override the hook
            <function>instance-refresh</function>.
          </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>caching</parameter></term>
          <listitem> 
          <para>
            This value is only considered when CLOS objects are being
            selected.  A boolean with a default value of
            <varname>*default-caching*</varname>. &clsql; caches
            objects in accordance with the &commonsql; interface: a
            second equivalent <function>select</function> call will
            return the same view class instance objects.
          </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>result</parameter></term>
          <listitem>
            <para>
              A list representing the result set obtained.  For each
              tuple in the result set, there is an element in this
              list, which is itself a list of all the attribute values
              in the tuple.
            </para>
          </listitem>
        </varlistentry>        
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Executes a query on <parameter>database</parameter>, which has
        a default value of <symbol>*default-database*</symbol>,
        specified by the SQL expressions supplied using the remaining
        arguments in <parameter>args</parameter>. The
        <function>select</function> function can be used to generate
        queries in both functional and object oriented contexts.
      </para>
      <para> 
        In the functional case, the required arguments specify the
        columns selected by the query and may be symbolic SQL
        expressions or strings representing attribute
        identifiers. Type modified identifiers indicate that the
        values selected from the specified column are converted to the
        specified lisp type. The keyword arguments
        <parameter>all</parameter>, <parameter>distinct</parameter>,
        <parameter>from</parameter>, <parameter>group-by</parameter>,
        <parameter>having</parameter>, <parameter>limit</parameter>, 
        <parameter>offset</parameter>, <parameter>order-by</parameter>,
        <parameter>set-operation</parameter> and
        <parameter>where</parameter> are used to specify, using the
        symbolic SQL syntax, the corresponding components of the SQL
        query generated by the call to
        <function>select</function>. 
      </para>
      <para> 
        <parameter>result-types</parameter> is a list of symbols which
        specifies the lisp type for each field returned by the
        query. If <parameter>result-types</parameter> is &nil; all
        results are returned as strings whereas the default value of
        <symbol>:auto</symbol> means that the lisp types are
        automatically computed for each
        field. <parameter>field-names</parameter> is &t; by default
        which means that the second value returned is a list of
        strings representing the columns selected by the query. If
        <parameter>field-names</parameter> is &nil;, the list of
        column names is not returned as a second value.
      </para>
      <para>
        In the object oriented case, the required arguments to
        <function>select</function> are symbols denoting View Classes
        which specify the database tables to query. In this case,
        <function>select</function> returns a list of View Class
        instances whose slots are set from the attribute values of the
        records in the specified table. <symbol>Slot-value</symbol> is
        a legal operator which can be employed as part of the symbolic
        SQL syntax used in the <parameter>where</parameter> keyword
        argument to <function>select</function>.
        <parameter>refresh</parameter> is &nil; by default which means
        that the View Class instances returned are retrieved from a
        cache if an equivalent call to <function>select</function> has
        previously been issued. If <parameter>refresh</parameter> is
        true, the View Class instances returned are updated as
        necessary from the database and the generic function
        <function>instance-refreshed</function> is called to perform
        any necessary operations on the updated instances.
      </para>
      <para> 
        In both object oriented and functional contexts,
        <parameter>flatp</parameter> has a default value of &nil;
        which means that the results are returned as a list of
        lists. If <parameter>flatp</parameter> is t and only one
        result is returned for each record selected in the query, the
        results are returned as elements of a list.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(select [first-name] :from [employee] :flatp t :distinct t
                     :field-names nil 
                     :result-types nil 
                     :order-by [first-name])
=> ("Boris" "Josef" "Konstantin" "Leon" "Leonid" "Mikhail" "Nikita" "Vladimir"
    "Yuri")

(select [first-name] [count [*]] :from [employee]
	                         :result-types nil 
			         :group-by [first-name]
			         :order-by [first-name]
			         :field-names nil)
=> (("Boris" "1") ("Josef" "1") ("Konstantin" "1") ("Leon" "1") ("Leonid" "1")
    ("Mikhail" "1") ("Nikita" "1") ("Vladimir" "2") ("Yuri" "1"))

(select [last-name] :from [employee] 
                    :where [like [email] "%org"]
		    :order-by [last-name]
		    :field-names nil 
		    :result-types nil 
		    :flatp t)
=> ("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
    "Stalin" "Trotsky" "Yeltsin")

(select [max [emplid]] :from [employee] 
                       :flatp t 
	               :field-names nil 
                       :result-types :auto)
=> (10)

(select [avg [height]] :from [employee] :flatp t :field-names nil)
=> (1.58999584d0)

(select [emplid] [last-name] :from [employee] :where [= [emplid] 1]) 
=> ((1 "Lenin")), 
   ("emplid" "last_name")

(select [emplid :string] :from [employee] 
                         :where [= 1 [emplid]] 
                         :field-names nil 
                         :flatp t)
=> ("1")

(select [emplid] :from [employee] :order-by [emplid] 
                 :where [not [between [* [emplid] 10] [* 5 10] [* 10 10]]]
                 :field-names nil 
                 :flatp t)
=> (1 2 3 4)

(select [emplid] :from [employee] 
        :where [in [emplid] '(1 2 3 4)]
        :flatp t 
        :order-by [emplid] 
        :field-names nil)
=> (1 2 3 4)

(select [emplid] :from [employee] 
        :order-by [emplid]
        :limit 5 
        :offset 3
        :field-names nil
        :flatp t)
=> (4 5 6 7 8)

(select [first-name] [last-name] :from [employee] 
        :field-names nil 
        :order-by '(([first-name] :asc) ([last-name] :desc)))
=> (("Boris" "Yeltsin") ("Josef" "Stalin") ("Konstantin" "Chernenko")
    ("Leon" "Trotsky") ("Leonid" "Brezhnev") ("Mikhail" "Gorbachev")
    ("Nikita" "Kruschev") ("Vladimir" "Putin") ("Vladimir" "Lenin")
    ("Yuri" "Andropov"))

(select [last-name] :from [employee]                   
                 :set-operation [union [select [first-name] :from [employee]
                                               :order-by [last-name]]]
                 :flatp t
                 :result-types nil 
                 :field-names nil)
=> ("Andropov" "Boris" "Brezhnev" "Chernenko" "Gorbachev" "Josef" "Konstantin"
    "Kruschev" "Lenin" "Leon" "Leonid" "Mikhail" "Nikita" "Putin" "Stalin"
    "Trotsky" "Vladimir" "Yeltsin" "Yuri")
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
        <para>Whatever effects the execution of the SQL query has on
        the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>
        None. 
      </para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>
        If the execution of the SQL query leads to any errors, an
        error of type <errortype>sql-database-error</errortype> is
        signalled.
      </para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="query"><function>query</function></link></member>
        <member><link linkend="print-query"><function>print-query</function></link></member>
        <member><link linkend="do-query"><function>do-query</function></link></member>
        <member><link linkend="map-query"><function>map-query</function></link></member>
        <member><link linkend="loop-tuples"><function>loop</function></link></member>
        <member><link linkend="instance-refreshed"><function>instance-refreshed</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para> 
        The <function>select</function> function is actually
        implemented in &clsql; with a single
        <symbol>&amp;rest</symbol> parameter (which is subsequently
        destructured) rather than the keyword parameters presented
        here for the purposes of exposition. This means that incorrect
        or missing keywords or values may not trigger errors in the
        way that they would if <function>select</function> had been
        defined using keyword arguments.
      </para>
      <para>
        The <parameter>field-names</parameter> and
        <parameter>result-types</parameter> keyword arguments are a
        &clsql; extension.
      </para>
      <para> 
        <parameter>select</parameter> is common across the functional
        and object-oriented data manipulation languages.
      </para>
    </refsect1>
  </refentry>


  <!-- iteration and mapping --> 

  <refentry id="do-query">
    <refmeta>
      <refentrytitle>DO-QUERY</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>DO-QUERY</refname>
      <refpurpose>Iterate over all the tuples of a query.</refpurpose>
      <refclass>Macro</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis>
      <function>do-query</function> ((&amp;rest <replaceable>args</replaceable>) <replaceable>query-expression</replaceable> &amp;key <replaceable>database</replaceable> <replaceable>result-types</replaceable> &amp;body <replaceable>body</replaceable> => <returnvalue>result</returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>args</parameter></term>
          <listitem>
            <para>A list of variable names.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>query-expression</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            query which is expected to return a (possibly empty)
            result set, where each tuple has as many attributes as
            <parameter>function</parameter> takes arguments.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to
            <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>result-types</parameter></term>
          <listitem>
            <para>
              A <glossterm linkend="gloss-field-types">field type
              specifier</glossterm>.  The default is &nil;. See <link
              linkend="query"><function>query</function></link> for
              the semantics of this argument.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>body</parameter></term>
          <listitem>
            <para>A body of Lisp code, like in a
            <function>destructuring-bind</function> form.</para>
          </listitem>
        </varlistentry>      
        <varlistentry>
          <term><parameter>result</parameter></term>
          <listitem>
            <para>The result of executing <parameter>body</parameter>.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Repeatedly executes <parameter>body</parameter> within a
        binding of <parameter>args</parameter> on the fields of each
        row selected by the SQL query
        <parameter>query-expression</parameter>, which may be a string
        or a symbolic SQL expression, in
        <parameter>database</parameter> which defaults to
        <symbol>*default-database*</symbol>.
      </para>
      <para>
        The body of code is executed in a block named
        <symbol>nil</symbol> which may be returned from prematurely
        via <function>return</function> or
        <function>return-from</function>.  In this case the result of
        evaluating the <function>do-query</function> form will be the
        one supplied to <function>return</function> or
        <function>return-from</function>.  Otherwise the result will
        be <symbol>nil</symbol>.
      </para>
      <para>
        The body of code appears also is if wrapped in a
        <function>destructuring-bind</function> form, thus allowing
        declarations at the start of the body, especially those
        pertaining to the bindings of the variables named in
        <parameter>args</parameter>.
      </para>
      <para> 
        <parameter>result-types</parameter> is a list of symbols which
        specifies the lisp type for each field returned by
        <parameter>query-expression</parameter>. If
        <parameter>result-types</parameter> is &nil; all results are
        returned as strings whereas the default value of
        <symbol>:auto</symbol> means that the lisp types are
        automatically computed for each field.
      </para>
      <para>
        <parameter>query-expression</parameter> may be an object query
        (i.e., the selection arguments refer to View Classes), in
        which case <parameter>args</parameter> are bound to the tuples
        of View Class instances returned by the object oriented query. 
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(do-query ((salary name) "select salary,name from simple")
  (format t "~30A gets $~2,5$~%" name (read-from-string salary)))
>> Mai, Pierre                    gets $10000.00
>> Hacker, Random J.              gets $08000.50
=> NIL

(do-query ((salary name) "select salary,name from simple")
  (return (cons salary name)))
=> ("10000.00" . "Mai, Pierre")

(let ((result '()))
  (do-query ((name) [select [last-name] :from [employee]
                            :order-by [last-name]])
    (push name result))
  result)
=> ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
    "Chernenko" "Brezhnev" "Andropov")

(let ((result '()))
  (do-query ((e) [select 'employee :order-by [last-name]])
    (push (slot-value e 'last-name) result))
  result)
=> ("Yeltsin" "Trotsky" "Stalin" "Putin" "Lenin" "Kruschev" "Gorbachev"
    "Chernenko" "Brezhnev" "Andropov")
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>Whatever effects the execution of the SQL query has
      on the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>If the execution of  the SQL query leads to any
      errors, an error of type
      <errortype>sql-database-error</errortype> is signalled.</para>
      <para>If the number of variable names in
      <parameter>args</parameter> and the number of attributes in
      the tuples in the result set don't match up, an error is
      signalled.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="query"><function>query</function></link></member>
        <member><link linkend="map-query"><function>map-query</function></link></member>
        <member><link linkend="print-query"><function>print-query</function></link></member>
        <member><link linkend="loop-tuples"><function>loop</function></link></member>
        <member><link linkend="select"><function>select</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>The <parameter>result-types</parameter> keyword argument
      is a &clsql; extension.</para>
      <para> 
        <parameter>do-query</parameter> is common across the functional
        and object-oriented data manipulation languages.
      </para>
    </refsect1>
  </refentry>

  <refentry id="loop-tuples">
    <refmeta>
      <refentrytitle>LOOP</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>LOOP</refname>
      <refpurpose>Extension to Common Lisp
      <computeroutput>Loop</computeroutput> to iterate over all the
      tuples of a query via a loop clause.</refpurpose>
      <refclass>Loop Clause</refclass>
    </refnamediv>
    <!-- refsect1>
      <title>Compatibility</title>
      <caution><para><function>loop-for-as-tuples</function> only works with &cmucl;.</para></caution>
    </refsect1 -->
    <refsect1>
      <title>Syntax</title>
      <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>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>var</parameter></term>
          <listitem>
            <para>A <literal>d-var-spec</literal>, as defined in the
            grammar for <function>loop</function>-clauses in the ANSI
            Standard for Common Lisp.  This allows for the usual
            loop-style destructuring.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>type-spec</parameter></term>
          <listitem>
            <para>An optional <literal>type-spec</literal> either
            simple or destructured, as defined in the grammar for
            <function>loop</function>-clauses in the ANSI Standard for
            Common Lisp.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>query</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            query which is expected to return a (possibly empty)
            result set, where each tuple has as many attributes as
            <parameter>function</parameter> takes arguments.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>An optional
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>This clause is an iteration driver for
      <function>loop</function>, that binds the given variable
      (possibly destructured) to the consecutive tuples (which are
      represented as lists of attribute values) in the result set
      returned by executing the SQL <parameter>query</parameter>
      expression on the <parameter>database</parameter>
      specified.</para>
      <para>
        <parameter>query</parameter> may be an object query (i.e., the
        selection arguments refer to View Classes), in which case the
        supplied variable is bound to the tuples of View Class
        instances returned by the object oriented query.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(defvar *my-db* (connect '("dent" "newesim" "dent" "dent"))
"My database"
=> *MY-DB*
(loop with time-graph = (make-hash-table :test #'equal)
   with event-graph = (make-hash-table :test #'equal)
   for (time event) being the tuples of "select time,event from log"
   from *my-db*
   do
     (incf (gethash time time-graph 0))
     (incf (gethash event event-graph 0))
   finally
     (flet ((show-graph (k v) (format t "~40A => ~5D~%" k v)))
       (format t "~&amp;Time-Graph:~%===========~%")
       (maphash #'show-graph time-graph)
       (format t "~&amp;~%Event-Graph:~%============~%")
       (maphash #'show-graph event-graph))
     (return (values time-graph event-graph)))
>> Time-Graph:
>> ===========
>> D                                        => 53000
>> X                                        =>     3
>> test-me                                  =>  3000
>> 
>> Event-Graph:
>> ============
>> CLOS Benchmark entry.                    =>  9000
>> Demo Text...                             =>     3
>> doit-text                                =>  3000
>> C    Benchmark entry.                    => 12000
>> CLOS Benchmark entry                     => 32000
=> #&lt;EQUAL hash table, 3 entries {48350A1D}>
=> #&lt;EQUAL hash table, 5 entries {48350FCD}>

(loop for (forename surname)
      being each tuple in
        [select [first-name] [last-name] :from [employee] 
                :order-by [last-name]]
      collect (concatenate 'string forename " " surname))
=> ("Yuri Andropov" "Leonid Brezhnev" "Konstantin Chernenko" "Mikhail Gorbachev"
    "Nikita Kruschev" "Vladimir Lenin" "Vladimir Putin" "Josef Stalin"
    "Leon Trotsky" "Boris Yeltsin")

(loop for (e) being the records in 
     [select 'employee :where [&lt; [emplid] 4] :order-by [emplid]]
  collect (slot-value e 'last-name))
=> ("Lenin" "Stalin" "Trotsky")
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>Whatever effects the execution of the SQL query has
      on the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>If the execution of  the SQL query leads to any
      errors, an error of type
      <errortype>sql-database-error</errortype> is signalled.</para>
      <para>Otherwise, any of the exceptional situations of
      <function>loop</function> applies.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <para>
        <simplelist>
          <member><link linkend="query"><function>query</function></link></member>
          <member><link linkend="map-query"><function>map-query</function></link></member>
          <member><link linkend="do-query"><function>do-query</function></link></member>
        <member><link linkend="print-query"><function>print-query</function></link></member>
        <member><link linkend="select"><function>select</function></link></member>
        </simplelist>
      </para>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>The <parameter>database</parameter> loop keyword is a
      &clsql; extension.</para>
      <para> 
        The extended <function>loop</function> syntax is common across
        the functional and object-oriented data manipulation
        languages.
      </para>
    </refsect1>
  </refentry>

  <refentry id="map-query">
    <refmeta>
      <refentrytitle>MAP-QUERY</refentrytitle>
    </refmeta>
    <refnamediv>
      <refname>MAP-QUERY</refname>
      <refpurpose>Map a function over all the tuples from a
      query</refpurpose>
      <refclass>Function</refclass>
    </refnamediv>
    <refsect1>
      <title>Syntax</title>
      <synopsis><function>map-query</function> <replaceable>output-type-spec</replaceable> <replaceable>function</replaceable> <replaceable>query-expression</replaceable> &amp;key <replaceable>database</replaceable> <replaceable>result-types</replaceable> => <returnvalue>result</returnvalue></synopsis>
    </refsect1>
    <refsect1>
      <title>Arguments and Values</title>
      <variablelist>
        <varlistentry>
          <term><parameter>output-type-spec</parameter></term>
          <listitem>
            <para>A sequence type specifier or <symbol>nil</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>function</parameter></term>
          <listitem>
            <para>A function designator.
            <parameter>function</parameter> takes a single argument which
            is the atom value for a query single with a single column
            or is a list of values for a multi-column query.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>query-expression</parameter></term>
          <listitem>
            <para>An <glossterm linkend="gloss-sql-expression">sql
            expression</glossterm> that represents an SQL
            query which is expected to return a (possibly empty)
            result set.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>database</parameter></term>
          <listitem>
            <para>A 
            <glossterm linkend="gloss-database-object">database
            object</glossterm>. This will default to the value
            of <symbol>*default-database*</symbol>.</para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><parameter>result-types</parameter></term>
          <listitem>
            <para>
              A <glossterm linkend="gloss-field-types">field type
              specifier</glossterm>.  The default is &nil;. See <link
              linkend="query"><function>query</function></link> for
              the semantics of this argument.
            </para>
          </listitem>
        </varlistentry>
        <varlistentry>
          <term><returnvalue>result</returnvalue></term>
          <listitem>
            <para>If <parameter>output-type-spec</parameter> is a
            type specifier other than <symbol>nil</symbol>, then a 
            sequence of the type it denotes.  Otherwise
            <symbol>nil</symbol> is returned.</para>
          </listitem>
        </varlistentry>
      </variablelist>
    </refsect1>
    <refsect1>
      <title>Description</title>
      <para>
        Applies <parameter>function</parameter> to the successive
        tuples in the result set returned by executing the SQL
        <parameter>query-expression</parameter>.  If the
        <parameter>output-type-spec</parameter> is
        <symbol>nil</symbol>, then the result of each application of
        <parameter>function</parameter> is discarded, and
        <function>map-query</function> returns <symbol>nil</symbol>.
        Otherwise the result of each successive application of
        <parameter>function</parameter> is collected in a sequence of
        type <parameter>output-type-spec</parameter>, where the jths
        element is the result of applying
        <parameter>function</parameter> to the attributes of the jths
        tuple in the result set.  The collected sequence is the result
        of the call to <function>map-query</function>.
      </para>
      <para>
        If the <parameter>output-type-spec</parameter> is a subtype of
        <type>list</type>, the result will be a <type>list</type>.
      </para>
      <para>
        If the <parameter>result-type</parameter> is a subtype of
        <type>vector</type>, then if the implementation can determine
        the element type specified for the
        <parameter>result-type</parameter>, the element type of the
        resulting array is the result of
        <emphasis>upgrading</emphasis> that element type; or, if the
        implementation can determine that the element type is
        unspecified (or <symbol>*</symbol>), the element type of the
        resulting array is <type>t</type>; otherwise, an error is
        signaled.
      </para>
      <para>
        If <parameter>result-types</parameter> is &nil; all results
        are returned as strings whereas the default value of
        <symbol>:auto</symbol> means that the lisp types are
        automatically computed for each field.</para>
      <para>
        <parameter>query-expression</parameter> may be an object query
        (i.e., the selection arguments refer to View Classes), in
        which case the supplied function is applied to the tuples of
        View Class instances returned by the object oriented query.
      </para>
    </refsect1>
    <refsect1>
      <title>Examples</title>
      <screen>
(map-query 'list #'(lambda (tuple) 
                     (multiple-value-bind (salary name) tuple
                        (declare (ignorable name))
                        (read-from-string salary)))
            "select salary,name from simple where salary > 8000")
=> (10000.0 8000.5)

(map-query '(vector double-float)
           #'(lambda (tuple)
               (multiple-value-bind (salary name) tuple
                  (declare (ignorable name))
                  (let ((*read-default-float-format* 'double-float))
                    (coerce (read-from-string salary) 'double-float))
           "select salary,name from simple where salary > 8000")))
=> #(10000.0d0 8000.5d0)
(type-of *)
=> (SIMPLE-ARRAY DOUBLE-FLOAT (2))

(let (list)
  (values (map-query nil #'(lambda (tuple) 
                             (multiple-value-bind (salary name) tuple
                               (push (cons name (read-from-string salary)) list))
                         "select salary,name from simple where salary > 8000"))
          list))
=> NIL
=> (("Hacker, Random J." . 8000.5) ("Mai, Pierre" . 10000.0))

(map-query 'vector #'identity
           [select [last-name] :from [employee] :flatp t
                   :order-by [last-name]])
=> #("Andropov" "Brezhnev" "Chernenko" "Gorbachev" "Kruschev" "Lenin" "Putin"
     "Stalin" "Trotsky" "Yeltsin")

(map-query 'list #'identity
           [select [first-name] [last-name] :from [employee] 
                   :order-by [last-name]])
=> (("Yuri" "Andropov") ("Leonid" "Brezhnev") ("Konstantin" "Chernenko")
    ("Mikhail" "Gorbachev") ("Nikita" "Kruschev") ("Vladimir" "Lenin")
    ("Vladimir" "Putin") ("Josef" "Stalin") ("Leon" "Trotsky") 
    ("Boris" "Yeltsin"))

(map-query 'list #'last-name [select 'employee :order-by [emplid]])
=> ("Lenin" "Stalin" "Trotsky" "Kruschev" "Brezhnev" "Andropov" "Chernenko"
    "Gorbachev" "Yeltsin" "Putin")
      </screen>
    </refsect1>
    <refsect1>
      <title>Side Effects</title>
      <para>Whatever effects the execution of the SQL query has
      on the underlying database, if any.</para>
    </refsect1>
    <refsect1>
      <title>Affected by</title>
      <para>None.</para>
    </refsect1>
    <refsect1>
      <title>Exceptional Situations</title>
      <para>If the execution of  the SQL query leads to any
      errors, an error of type
      <errortype>sql-database-error</errortype> is signalled.</para>
      <para>An error of type <errortype>type-error</errortype> must
      be signaled if the <parameter>output-type-spec</parameter> is
      not a recognizable subtype of <type>list</type>, not a
      recognizable subtype of <type>vector</type>, and not
      <symbol>nil</symbol>.</para>
      <para>An error of type <errortype>type-error</errortype>
      should be signaled if
      <parameter>output-type-spec</parameter> specifies the number
      of elements and the size of the result set is different from
      that number.</para>
    </refsect1>
    <refsect1>
      <title>See Also</title>
      <simplelist>
        <member><link linkend="query"><function>query</function></link></member>
        <member><link linkend="do-query"><function>do-query</function></link></member>
        <member><link linkend="print-query"><function>print-query</function></link></member>
        <member><link linkend="loop-tuples"><function>loop</function></link></member>
        <member><link linkend="select"><function>select</function></link></member>
      </simplelist>
    </refsect1>
    <refsect1>
      <title>Notes</title>
      <para>The <parameter>result-types</parameter> keyword argument
      is a &clsql; extension.</para>
      <para> 
        <parameter>map-query</parameter> is common across the
        functional and object-oriented data manipulation languages.
      </para>
    </refsect1>
  </refentry>

</reference>