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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Postmodern reference manual</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Postmodern reference manual</h1>
<p>This is the reference manual for the component named
<code>postmodern</code>, which is part of a <a
href="index.html">library</a> of the same name.</p>
<p>Note that this package also exports the <a
href="cl-postgres.html#database-connection"><code>database-connection</code></a>
and <a
href="cl-postgres.html#database-error"><code>database-error</code></a>
types from <a href="cl-postgres.html">CL-postgres</a> and a few
operators from <a href="s-sql.html">S-SQL</a>.</p>
<p><a href="#query"><code>query</code></a>, <a
href="#execute"><code>execute</code></a>, and any other function
that would logically need to communicate with the database will
raise a condition of the type <a
href="cl-postgres.html#conditions"><code>database-error</code></a>
when something goes wrong. As a special case, errors that break
the connection (socket errors, database shutdowns) will be raised
as subtypes of <a
href="cl-postgres.html#database-connection-error"><code>database-connection-error</code></a>,
providing a <code>:reconnect</code> restart to re-try the
operation that encountered to the error.</p>
<h2>Contents</h2>
<ol>
<li><a href="#connecting">Connecting</a></li>
<li><a href="#querying">Querying</a></li>
<li><a href="#inspect">Inspecting the database</a></li>
<li><a href="#daos">Database access objects</a></li>
<li><a href="#tabledef">Table definition and creation</a></li>
<li><a href="#schemata">Schemata</a></li>
<li><a href="#index">Symbol-index</a></li>
</ol>
<h2><a name="connecting"></a>Connecting</h2>
<p class="def">
<span>class</span>
<a name="database-connection"></a>
database-connection
</p>
<p class="desc">Objects of this type represent database connections.</p>
<p class="def">
<span>function</span>
<a name="connect"></a>
connect (database user password host &key (port 5432) pooled-p use-ssl)
<br/>→ database-connection
</p>
<p class="desc">Create a new database connection for the given
<code>user</code> and the <code>database</code>. Port will default
to 5432, which is where most PostgreSQL servers are running. If
<code>pooled-p</code> is <code>T</code>, a connection will be taken
from a pool of connections of this type, if one is available there,
and when the connection is disconnected it will be put back into this
pool instead. <code>use-ssl</code> can be <code>:no</code>,
<code>:yes</code>, or <code>:try</code>, as in <a
href="cl-postgres.html#open-database"><code>open-database</code></a>,
and defaults to the value of <a
href="#*default-use-ssl*"><code>*default-use-ssl*</code></a>.</p>
<p class="def">
<span>variable</span>
<a name="*default-use-ssl*"></a>
*default-use-ssl*
</p>
<p class="desc">The default for <a
href="#connect"><code>connect</code></a>'s <code>use-ssl</code>
argument. This starts at <code>:no</code>. If you set it to
anything else, be sure to also load the <a
href="http://common-lisp.net/project/cl-plus-ssl/">CL+SSL</a>
library.</p>
<p class="def">
<span>method</span>
<a name="disconnect"></a>
disconnect (database-connection)
</p>
<p class="desc">Disconnects a normal database connection, or moves
a pooled connection into the pool.</p>
<p class="def">
<span>function</span>
<a name="connected-p"></a>
connected-p (database-connection)
<br/>→ boolean
</p>
<p class="desc">Returns a boolean indicating whether the given
connection is still connected to the server.</p>
<p class="def">
<span>method</span>
<a name="reconnect"></a>
reconnect (database-connection)
</p>
<p class="desc">Reconnect a disconnected database connection. This
is not allowed for pooled connections ― after they are
disconnected they might be in use by some other process, and
should no longer be used.</p>
<p class="def">
<span>variable</span>
<a name="*database*"></a>
*database*
</p>
<p class="desc">Special variable holding the current database.
Most functions and macros operating on a database assume this
binds to a connected database.</p>
<p class="def">
<span>macro</span>
<a name="with-connection"></a>
with-connection (spec &body body)
</p>
<p class="desc">Evaluates the <code>body</code> with <a
href="#*database*"><code>*database*</code></a> bound to a
connection as specified by <code>spec</code>, which should be list
that <a href="#connect"><code>connect</code></a> can be applied
to.</p>
<p class="def">
<span>macro</span>
<a name="call-with-connection"></a>
call-with-connection (spec thunk)
</p>
<p class="desc">The functional backend to <a
href="#with-connection"><code>with-connection</code></a>. Binds <a
href="#*database*"><code>*database*</code></a> to a new connection
as specified by <code>spec</code>, which should be a list that <a
href="#connect"><code>connect</code></a> can be applied to, and
runs the zero-argument function given as second argument in the
new environment. When the function returns or throws, the new
connection is disconnected.</p>
<p class="def">
<span>function</span>
<a name="connect-toplevel"></a>
connect-toplevel (database user password host &key (port 5432))
</p>
<p class="desc">Bind the <a
href="#*database*"><code>*database*</code></a> to a new
connection. Use this if you only need one connection, or if you
want a connection for debugging from the REPL.</p>
<p class="def">
<span>function</span>
<a name="disconnect-toplevel"></a>
disconnect-toplevel ()
</p>
<p class="desc">Disconnect the <a
href="#*database*"><code>*database*</code></a>.</p>
<p class="def">
<span>function</span>
<a name="clear-connection-pool"></a>
clear-connection-pool ()
</p>
<p class="desc">Disconnect and remove all connections from the
connection pools.</p>
<p class="def">
<span>variable</span>
<a name="*max-pool-size*"></a>
*max-pool-size*
</p>
<p class="desc">Set the maximum amount of connections kept in a
<em>single</em> connection pool, where a pool consists of all the
stored connections with the exact same connect arguments. Defaults
to <code>NIL</code>, which means there is no maximum.</p>
<h2><a name="querying"></a>Querying</h2>
<p class="def">
<span>macro</span>
<a name="query"></a>
query (query &rest args/format)
<br/>→ result
</p>
<p class="desc">Execute the given <code>query</code>, which can be
either a string or an <a href="s-sql.html">S-SQL</a> form (list starting
with a keyword). If the query contains placeholders
(<code>$1</code>, <code>$2</code>, etc) their values can be given
as extra arguments. If one of these arguments is a keyword
occurring in the table below, it will not be used as a <code>query</code>
argument, but will determine the <code>format</code> in which the results
are returned instead. Any of the following formats can be used, with
the default being <code>:rows</code>:</p>
<table class="desc">
<tr><td><code>:none</code></td><td>Ignore the result values.</td></tr>
<tr><td><code>:lists</code>, <code>:rows</code></td><td>Return a
list of lists, each list containing the values for a
row.</td></tr>
<tr><td><code>:list</code>, <code>:row</code></td><td>Return a
single row as a list.</td></tr>
<tr><td><code>:alists</code></td><td>Return a list of alists which map column
names to values, with the names represented as
keywords.</td></tr>
<tr><td><code>:alist</code></td><td>Return a single row as an alist.</td></tr>
<tr><td><code>:str-alists</code></td><td>Like
<code>:alists</code>, but use the original column
names.</td></tr>
<tr><td><code>:str-alist</code></td><td>Return a single row as an alist, with
strings for names.</td></tr>
<tr><td><code>:plists</code></td><td>Return a list of plists which map column
names to values,with the names represented as keywords.</td></tr>
<tr><td><code>:plist</code></td><td>Return a single row as a plist.</td></tr>
<tr><td><code>:column</code></td><td>Return a single column as a list.</td></tr>
<tr><td><code>:single</code></td><td>Return a single value.</td></tr>
<tr><td><code>:single!</code></td><td>Like <code>:single</code>,
but raise an error when the number of selected rows is not equal
to 1.</td></tr>
<tr><td><code>(:dao type)</code></td><td>Return a list of DAOs of the given type. The names of the fields returned by the query must match slots in the DAO class the same way as with <a href="#query-dao"><code>query-dao</code></a>.</td></tr>
<tr><td><code>(:dao type :single)</code></td><td>Return a single DAO of the given type.</td></tr>
</table>
<p class="desc">If the database returns information about the
amount rows that were affected, such as with updating or deleting
queries, this is returned as a second value.</p>
<p class="def">
<span>macro</span>
<a name="execute"></a>
execute (query &rest args)
</p>
<p class="desc">Like <a href="#query"><code>query</code></a>
called with <code>format :none</code>. Returns the amount of
affected rows as its first returned value. (Also returns this
amount as the second returned value, but use of this is
deprecated.)</p>
<p class="def">
<span>macro</span>
<a name="doquery"></a>
doquery (query (&rest names) &body body)
</p>
<p class="desc">Execute the given <code>query</code> (a string or a list
starting with a keyword), iterating over the rows in the result.
The <code>body</code> will be executed with the values in the row bound to the
symbols given in <code>names</code>. To iterate over a
parameterised query, one can specify a list whose <em>car</em> is the
query, and whose <em>cdr</em> contains the arguments. For example:</p>
<pre class="code">
(doquery (:select 'name 'score :from 'scores) (n s)
(incf (gethash n *scores*) s))
(doquery ((:select 'name :from 'scores :where (:> 'score '$1)) 100) (name)
(print name))</pre>
<p class="def">
<span>macro</span>
<a name="prepare"></a>
prepare (query &optional (format :rows))
<br/>→ function
</p>
<p class="desc">Creates a function that can be used as the
interface to a prepared statement. The given <code>query</code>
(either a string or an <a href="s-sql.html">S-SQL</a> form) may contain
placeholders, which look like <code>$1</code>, <code>$2</code>,
etc. The resulting function takes one argument for every
placeholder in the <code>query</code>, executes the prepared query,
and returns the result in the <code>format</code> specified. (Allowed
formats are the same as for <a href="#query"><code>query</code></a>.)</p>
<p class="desc">For queries that have to be run very often,
especially when they are complex, it may help performance since the
server only has to plan them once. See <a
href="http://www.postgresql.org/docs/current/static/sql-prepare.html">the
PostgreSQL manual</a> for details.</p>
<p class="desc">In some cases, the server will complain about not
being able to deduce the type of the arguments in a statement. In
that case you should add type declarations (either with the PostgreSQL's
<code>CAST</code> SQL-conforming syntax or historical <code>::</code>
syntax, or with S-SQL's <a href="s-sql.html#type"><code>:type</code></a>
construct) to help it out.</p>
<p class="def">
<span>macro</span>
<a name="defprepared"></a>
defprepared (name query &optional (format :rows))
</p>
<p class="desc">This is the <code>defun</code>-style variant of <a
href="#prepare"><code>prepare</code></a>. It will define a
top-level function for the prepared statement.</p>
<p class="def">
<span>macro</span>
<a name="defprepared-with-names"></a>
defprepared-with-names (name (&rest args) (query &rest query-args) &optional (format :rows))
</p>
<p class="desc">Like <a href="#defprepared"><code>defprepared</code></a>,
but allows to specify names of the function arguments as well as arguments
supplied to the <code>query</code>.</p>
<pre class="code">
(defprepared-with-names user-messages (user &key (limit 10))
("select * from messages
where user_id = $1
order by date desc
limit $2" (user-id user) limit)
:plists)
</pre>
<p class="def">
<span>macro</span>
<a name="with-transaction"></a>
with-transaction ((&optional name) &body body)
</p>
<p class="desc">Execute the given <code>body</code> within a database
transaction, committing it when the <code>body</code> exits normally, and
aborting otherwise. An optional <code>name</code> can be given to the
transaction, which can be used to force a commit or abort before
the <code>body</code> unwinds.</p>
<p class="def">
<span>function</span>
<a name="commit-transaction"></a>
commit-transaction (transaction)
</p>
<p class="desc">Commit the given <code>transaction</code>.</p>
<p class="def">
<span>function</span>
<a name="abort-transaction"></a>
abort-transaction (transaction)
</p>
<p class="desc">Roll back the given <code>transaction</code>.</p>
<p class="def">
<span>macro</span>
<a name="with-savepoint"></a>
with-savepoint (name &body body)
</p>
<p class="desc">Can only be used within a transaction. Establishes
a savepoint with the given <code>name</code> at the start of
<code>body</code>, and binds the same <code>name</code> to a handle
for that savepoint. At the end of <code>body</code>, the savepoint
is released, unless a condition is thrown, in which case it is rolled
back.</p>
<p class="def">
<span>function</span>
<a name="release-savepoint"></a>
release-savepoint (savepoint)
</p>
<p class="desc">Release the given <code>savepoint</code>.</p>
<p class="def">
<span>function</span>
<a name="rollback-savepoint"></a>
rollback-savepoint (savepoint)
</p>
<p class="desc">Roll back the given <code>savepoint</code>.</p>
<p class="def">
<span>function</span>
<a name="commit-hooks"></a>
commit-hooks (transaction-or-savepoint),
setf (commit-hooks transaction-or-savepoint)
</p>
<p class="desc">An accessor for the transaction or savepoint's list
of commit hooks, each of which should be a function with no required
arguments. These functions will be executed when a transaction is
committed or a savepoint released.</p>
<p class="def">
<span>function</span>
<a name="abort-hooks"></a>
abort-hooks (transaction-or-savepoint),
setf (abort-hooks transaction-or-savepoint)
</p>
<p class="desc">An accessor for the transaction or savepoint's list
of abort hooks, each of which should be a function with no required
arguments. These functions will be executed when a transaction is
aborted or a savepoint rolled back (whether via a non-local transfer
of control or explicitly by either <a href="#abort-transaction">
<code>abort-transaction</code></a> or <a href="#rollback-savepoint">
<code>rollback-savepoint</code></a>).
<p class="def">
<span>macro</span>
<a name="with-logical-transaction"></a>
with-logical-transaction ((&optional name) &body body)
</p>
<p class="desc">Executes <code>body</code> within
a <a href="#with-transaction"><code>with-transaction</code></a>
form if no transaction is currently in progress, otherwise simulates
a nested transaction by executing it within a <code>with-savepoint</code>
form. The transaction or savepoint is bound to <code>name</code> if one is
supplied.</p>
<p class="def">
<span>function</span>
<a name="abort-logical-transaction"></a>
abort-logical-transaction (transaction-or-savepoint)
</p>
<p class="desc">Roll back the given logical transaction, regardless of
whether it is an actual transaction or a savepoint.</p>
<p class="def">
<span>function</span>
<a name="commit-logical-transaction"></a>
commit-logical-transaction (transaction-or-savepoint)
</p>
<p class="desc">Commit the given logical transaction, regardless of
whether it is an actual transaction or a savepoint.</p>
<p class="def">
<a name="*current-logical-transaction*"></a>
<span>variable</span>
*current-logical-transaction*
</p>
<p class="desc">This is bound to the
current <code>transaction-handle</code>
or <code>savepoint-handle</code> instance representing the
innermost open logical transaction.</p>
<p class="def">
<span>macro</span>
<a name="ensure-transaction"></a>
ensure-transaction (&body body)
</p>
<p class="desc">Ensures that <code>body</code> is executed within
a transaction, but does not begin a new transaction if one is
already in progress.</p>
<p class="def">
<span>macro</span>
<a name="with-schema"></a>
with-schema ((namespace &key :strict t :if-not-exist
:create :drop-after) &body body)
</p>
<p class="desc">Sets the current schema to <code>namespace</code> and
executes the <code>body</code>. Before executing <code>body</code> the
PostgreSQL's session variable <code>search_path</code> is set to
the given <code>namespace</code>. After executing <code>body</code> the
<code>search_path</code> variable is restored to the original value.
If the keyword <code>:strict</code> is set to <code>T</code> then
the <code>namespace</code> is only the scheme on the search path upon
the <code>body</code> execution. Otherwise the <code>namespace</code> is
just first schema on the search path upon the the <code>body</code>
execution. If <code>:if-not-exist</code> is <code>NIL</code>,
an error is signaled. If <code>:drop-after</code> is <code>T</code>
the <code>namespace</code> is dropped from the database after the
<code>body</code> execution.
</p>
<p class="def">
<span>function</span>
<a name="sequence-next"></a>
sequence-next (sequence)
<br/>→ integer
</p>
<p class="desc">Get the next value from a <code>sequence</code>.
The sequence identifier can be either a string or a symbol, in the latter
case it will be converted to a string according to <a
href="s-sql.html">S-SQL</a> rules.</p>
<p class="def">
<span>function</span>
<a name="coalesce"></a>
coalesce (&rest arguments)
<br/>→ value
</p>
<p class="desc">Returns the first non-<code>NIL</code>, non-NULL
(as in <code>:null</code>) argument, or <code>NIL</code> if none
are present. Useful for providing a fall-back value for the result
of a query, or, when given only one argument, for transforming
<code>:null</code>s to <code>NIL</code>.</p>
<h2><a name="inspect"></a>Inspecting the database</h2>
<p class="def">
<span>function</span>
<a name="list-tables"></a>
list-tables (&optional strings-p)
<br/>→ list
</p>
<p class="desc">Returns a list of the tables in the current
database. When <code>strings-p</code> is <code>T</code>, the
names will be given as strings, otherwise as keywords.</p>
<p class="def">
<span>function</span>
<a name="table-exists-p"></a>
table-exists-p (name)
<br/>→ boolean
</p>
<p class="desc">Tests whether a table with the given <code>name</code>
exists. The <code>name</code> can be either a string or a symbol.</p>
<p class="def">
<span>function</span>
<a name="table-description"></a>
table-description (name &optional schema-name)
<br/>→ list
</p>
<p class="desc">Returns a list of the fields in the named table.
Each field is represented by a list of three elements: the field
name, the type, and a boolean indicating whether the field may be
NULL. Optionally, <code>schema-name</code> can be specified to
restrict the result to fields from the named schema. Without it,
all fields in the table are returned, regardless of their schema.</p>
<p class="def">
<span>function</span>
<a name="list-sequences"></a>
list-sequences (&optional strings-p)
<br/>→ list
</p>
<p class="desc">Returns a list of the sequences in the current
database. When <code>strings-p</code> is <code>T</code>, the names
will be given as strings, otherwise as keywords.</p>
<p class="def">
<span>function</span>
<a name="sequence-exists-p"></a>
sequence-exists-p (name)
<br/>→ boolean
</p>
<p class="desc">Tests whether a sequence with the given <code>name</code>
exists. The <code>name</code> can be either a string or a symbol.</p>
<p class="def">
<span>function</span>
<a name="list-views"></a>
list-views (&optional strings-p)
<br/>→ list
</p>
<p class="desc">Returns list of the user defined views in the current
database. When <code>strings-p</code> is <code>T</code>, the names will
be returned as strings, otherwise as keywords.</p>
<p class="def">
<span>function</span>
<a name="view-exists-p"></a>
view-exists-p (name)
<br/>→ boolean
</p>
<p class="desc">Tests whether a view with the given <code>name</code>
exists. The <code>name</code> can be either a string or a symbol.</p>
<p class="def">
<span>function</span>
<a name="list-schemata"></a>
list-schemata ()
<br/>→ list
</p>
<p class="desc">Returns list of the user defined schemata (as strings)
and the quantity of existing schemata.</p>
<p class="def">
<span>function</span>
<a name="schema-exist-p"></a>
schema-exist-p (schema) NOW DEPRECATED IN FAVOR OF schema-exists-p
which is more consistent with naming of other functions.
<br/>→ boolean
</p>
<p class="desc">Tests the existence of a given <code>schema</code>.
Returns <code>T</code> if the schema exists or <code>NIL</code>
otherwise.</p>
<p class="def">
<span>function</span>
<a name="schema-exists-p"></a>
schema-exists-p (schema)
<br/>→ boolean
</p>
<p class="desc">Tests the existence of a given <code>schema</code>.
Returns <code>T</code> if the schema exists or <code>NIL</code>
otherwise.</p>
<p class="def">
<span>function</span>
<a name="database-version"></a>
database-version ()
<br/>→ string
</p>
<p class="desc">Returns the version of the current postgresql database.</p>
<p class="def">
<span>function</span>
<a name="num-records-in-database"></a>
num-records-in-database ()
<br/>→ list
</p>
<p class="desc">Returns a list of lists with schema, table name and approximate number of records
in the currently connected database.</p>
<p class="def">
<span>function</span>
<a name="current-database"></a>
current-database ()
<br/>→ string
</p>
<p class="desc">Returns the string name of the current database.</p>
<p class="def">
<span>function</span>
<a name="database-exists-p"></a>
database-exists-p (database-name)
<br/>→ boolean
</p>
<p class="desc">Checks to see if a particular database exists.</p>
<p class="def">
<span>function</span>
<a name="database-size"></a>
database-size (&optional database-name)
<br/>→ list
</p>
<p class="desc">Given the name of a database, will return the name, a pretty-print string of
the size of the database and the size in bytes. If a database name is not provided,
it will return the result for the currently connected database.</p>
<p class="def">
<span>function</span>
<a name="list-databases"></a>
list-databases (&key (order-by-size nil) (size t))
<br/>→ list
</p>
<p class="desc">Returns a list of lists where each sub-list contains the name of the
database, a pretty-print string of the size of that database and the size in bytes.
The default order is by database name. Pass t as a parameter to :order-by-size for order by size.
Setting size to nil will return just the database names in a single list
ordered by name. This function excludes the template databases</p>
<p class="def">
<span>function</span>
<a name="list-schemas"></a>
list-schemas ()
<br/>→ list
</p>
<p class="desc">List schemas in the current database, excluding the pg_* system schemas.</p>
<p class="def">
<span>function</span>
<a name="list-tablespaces"></a>
list-tablespaces ()
<br/>→ list
</p>
<p class="desc">Lists the tablespaces in the currently connected database.</p>
<p class="def">
<span>function</span>
<a name="list-available-types"></a>
list-available-types ()
<br/>→ list
</p>
<p class="desc">List the available types in this postgresql version.</p>
<p class="def">
<span>function</span>
<a name="list-table-sizes"></a>
list-table-sizes (&key (schema "public") (order-by-size nil) (size t))
<br/>→ list
</p>
<p class="desc">Returns a list of lists (table-name, size in 8k pages) of tables in the current database.
Providing a name to the schema parameter will return just the information for tables in that schema.
It defaults to just the tables in the public schema. Setting schema to nil will return all tables, indexes etc
in the database in descending order of size. This would include system tables, so there
are a lot more than you would expect. If :size is set to nil, it returns only a flat list of table names.
Setting order-by-size to t will return the result in order of size instead of by table name.</p>
<p class="def">
<span>function</span>
<a name="table-size"></a>
table-size (table-name)
<br/>→ list
</p>
<p class="desc">Return the size of a postgresql table in k or m. Table-name can be either a string or quoted.</p>
<p class="def">
<span>function</span>
<a name="more-table-info"></a>
more-table-info (table-name)
<br/>→ list
</p>
<p class="desc">Returns more table info than table-description. Table can be either a string or quoted.</p>
<p class="def">
<span>function</span>
<a name="list-columns"></a>
list-columns (table-name)
<br/>→ list
</p>
<p class="desc">Returns a list of strings of just the column names in a table.
Pulls info from the postmodern table-description function
rather than directly.</p>
<p class="def">
<span>function</span>
<a name="list-columns-with-types"></a>
list-columns-with-types (table-name)
<br/>→ list
</p>
<p class="desc">Return a list of (name type) lists for the fields of a table. Goes directly to the pg-catalog tables.</p>
<p class="def">
<span>function</span>
<a name="column-exists-p"></a>
column-exists-p (table-name column-name)
<br/>→ boolean
</p>
<p class="desc">Determine if a particular column exists. Table name and column-name can be either strings or symbols.</p>
<p class="def">
<span>function</span>
<a name="describe-views"></a>
describe-views (&optional (schema "public")
<br/>→ list
</p>
<p class="desc">Describe the current views in the specified schema. Defaults to public schema.</p>
<p class="def">
<span>function</span>
<a name="list-database-functions"></a>
list-database-functions ()
<br/>→ list
</p>
<p class="desc">Returns a list of the functions in the database from the information_schema.</p>
<p class="def">
<span>function</span>
<a name="list-indices"></a>
list-indices (&optional strings-p)
<br/>→ list
</p>
<p class="desc">Return a list of the indexs in a database. Turn them into keywords if strings-p is not true.</p>
<p class="def">
<span>function</span>
<a name="list-table-indices"></a>
list-table-indices (table-name &optional strings-p)
<br/>→ list
</p>
<p class="desc">List the index names and the related columns in a table. </p>
<p class="def">
<span>function</span>
<a name="list-indexed-column-and-attributes"></a>
list-indexed-column-and-attributes (table-name)
<br/>→ list
</p>
<p class="desc">List the indexed columns and their attributes in a table. Includes primary key.</p>
<p class="def">
<span>function</span>
<a name="list-index-definitions"></a>
list-index-definitions (table-name)
<br/>→ list
</p>
<p class="desc">Returns a list of the definitions used to create the current indexes for the table</p>
<p class="def">
<span>function</span>
<a name="list-foreign-keys"></a>
list-foreign-keys (table-name)
<br/>→ list
</p>
<p class="desc">List the foreign keys in a table.</p>
<p class="def">
<span>function</span>
<a name="list-unique-or-primary-constraints"></a>
list-unique-or-primary-constraints (table-name)
<br/>→ list
</p>
<p class="desc">List constraints on a table.</p>
<p class="def">
<span>function</span>
<a name="list-all-constraints"></a>
list-all-constraints (table-name)
<br/>→ list
</p>
<p class="desc">Users information_schema to list all the constraints in a table. Table-name
can be either a string or quoted.</p>
<p class="def">
<span>function</span>
<a name="describe-constraint"></a>
describe-constraint (table-name constraint-name)
<br/>→ list
</p>
<p class="desc">Return a list of alists of the descriptions a particular constraint given
the table-name and the constraint name using the information_schema
table.</p>
<p class="def">
<span>function</span>
<a name="describe-foreign-key-constraints"></a>
describe-foreign-key-constraints ()
<br/>→ list
</p>
<p class="desc">Generates a list of lists of information on the foreign key constraints</p>
<p class="def">
<span>function</span>
<a name="list-triggers"></a>
list-triggers (&optional table-name)
<br/>→ list
</p>
<p class="desc">List distinct trigger names from the information_schema table. Table-name can be either quoted or string.</p>
<p class="def">
<span>function</span>
<a name="list-detailed-triggers"></a>
list-detailed-triggers ()
<br/>→ list
</p>
<p class="desc">List detailed information on the triggers from the information_schema table.</p>
<p class="def">
<span>function</span>
<a name="list-database-users"></a>
list-database-users ()
<br/>→ list
</p>
<p class="desc">List database users.</p>
<p class="def">
<span>function</span>
<a name="change-toplevel-database"></a>
change-toplevel-database (new-database user password host)
<br/>→ string
</p>
<p class="desc">Just changes the database assuming you are using a toplevel connection.
Recommended only for development work. Returns the name of the newly connected database as a string.</p>
<h2><a name="daos"></a>Database access objects</h2>
<p>Postmodern contains a simple system for defining CLOS classes
that represent rows in the database. This is not intended as a
full-fledged object-relational magic system ― while serious
ORM systems have their place, they are notoriously hard to get
right, and are outside of the scope of a humble SQL library like
this.</p>
<p class="def">
<span>metaclass</span>
<a name="dao-class"></a>
dao-class
</p>
<p class="desc">At the heart of Postmodern's DAO system is the
<code>dao-class</code> metaclass. It allows you to define classes
for your database-access objects as regular CLOS classes. Some of
the slots in these classes will refer to columns in the database.
To specify that a slot refers to a column, give it a
<code>:col-type</code> option containing
an <a href="s-sql.html">S-SQL</a> type expression (useful if you
want to be able to derive a table definition from the class
definition), or simply a <code>:column</code> option with
value <code>T</code>. Such slots can also take
a <code>:col-default</code> option, used to provide a
database-side default value as an S-SQL expression. You can use
the <code>:col-name</code> initarg (whose unevaluated value will
be passed to <code>to-sql-name</code>) to specify the slot's column's
name.</p>
<p class="desc">DAO class definitions support two extra class
options: <code>:table-name</code> to give the name of the table
that the class refers to (defaults to the class name), and
<code>:keys</code> to provide a set of primary keys for the table.
When no primary keys are defined, operations such as <a
href="#update-dao"><code>update-dao</code></a> and <a
href="#get-dao"><code>get-dao</code></a> will not work.</p>
<p class="desc">IMPORTANT: Class finalization for a dao class
instance are wrapped with a thread lock. However, any time you are using threads
and a class that inherits from other classes, you should ensure
that classes are finalized before you start generating threads
that create new instances of that class.</p>
<p class="desc">Simple example:</p>
<pre class="code">
(defclass user ()
((name :col-type string :initarg :name :accessor user-name)
(creditcard :col-type (or db-null integer) :initarg :card :col-default :null)
(score :col-type bigint :col-default 0 :accessor user-score))
(:metaclass dao-class)
(:keys name))</pre>
<p class="desc">The <code>(or db-null integer)</code> form is used
to indicate a column can have NULL values.</p>
<p class="desc">When inheriting from DAO classes, a subclass' set
of columns also contains all the columns of its superclasses. The
primary key for such a class is the union of its own keys and all
the keys from its superclasses. Classes inheriting from DAO
classes should probably always use the <code>dao-class</code>
metaclass themselves.</p>
<p class="desc">When a DAO is created with
<code>make-instance</code>, the <code>:fetch-defaults</code> keyword
argument can be passed, which, when <code>T</code>, will cause a query
to fetch the default values for all slots that refers to columns with
defaults and were not bound through initargs. In some cases, such as
<code>serial</code> columns, which have an implicit default, this will
not work. You can work around this by creating your own sequence, e.g.
<code>"my_sequence"</code>, and defining a
<code>(:nextval "my_sequence")</code> default.</p>
<p class="desc">Finally, DAO class slots can have an option
<code>:ghost t</code> to specify them as ghost slots. These are
selected when retrieving instances, but not written when updating
or inserting, or even included in the table definition. The only
known use for this to date is for creating the table with
<code>(oids=true)</code>, and specify a slot like this:</p>
<pre class="code">
(oid :col-type integer :ghost t :accessor get-oid)</pre>
<p class="def">
<span>method</span>
<a name="dao-keys"></a>
dao-keys (class)
<br/>→ list
</p>
<p class="desc">
Returns list of slot names that are the primary key of DAO
<code>class</code>.
</p>
<p class="def">
<span>method</span>
dao-keys (dao)
<br/>→ list
</p>
<p class="desc">
Returns list of values that are the primary key of <code>dao</code>.
</p>
<p class="def">
<span>method</span>
<a name="dao-exists-p"></a>
dao-exists-p (dao)
<br/>→ boolean
</p>
<p class="desc">Test whether a row with the same primary key as
the given <code>dao</code> exists in the database. Will also return
<code>NIL</code> when any of the key slots in the object are
unbound.</p>
<p class="def">
<span>method</span>
<a name="make-dao"></a>
make-dao (type &rest args &key &allow-other-keys)
<br/>→ dao
</p>
<p class="desc">Combines <code>make-instance</code> with
<a href="#insert-dao"><code>insert-dao</code></a>. Return the
created dao.</p>
<p class="def">
<span>macro</span>
<a name="define-dao-finalization"></a>
define-dao-finalization (((dao-name class) &rest keyword-args) &body body)
</p>
<p class="desc">Create an <code>:around</code>-method for <a href="#make-dao">
<code>make-dao</code></a>. The <code>body</code> is executed in
a lexical environment where <code>dao-name</code> is bound
to a freshly created and inserted DAO. The representation of the DAO in the
database is then updated to reflect changes that <code>body</code> might
have introduced. Useful for processing values of slots with the type
<code>serial</code>, which are unknown before <a href="#insert-dao">
<code>insert-dao</code></a>.</p>
<p class="def">
<span>method</span>
<a name="get-dao"></a>
get-dao (type &rest keys)
<br/>→ dao
</p>
<p class="desc">Select the DAO object from the row that has the
given primary key values, or <code>NIL</code> if no such row
exists. Objects created by this function will have
<code>initialize-instance</code> called on them (after loading in
the values from the database) without any arguments ― even
<code>:default-initargs</code> are skipped. The same goes for <a
href="#select-dao"><code>select-dao</code></a> and <a
href="#query-dao"><code>query-dao</code></a>.</p>
<p class="def">
<span>macro</span>
<a name="select-dao"></a>
select-dao (type &optional (test t) &rest sort)
<br/>→ list
</p>
<p class="desc">Select DAO objects for the rows in the associated
table for which the given <code>test</code> (either an <a
href="s-sql.html">S-SQL</a> expression or a string) holds. When
sorting arguments are given, which can also be S-SQL forms or
strings, these are used to sort the result. (Note that, if you
want to sort, you <em>have</em> to pass the <code>test</code>
argument.)</p>
<pre class="code">(select-dao 'user (:> 'score 10000) 'name)</pre>
<p class="def">
<span>macro</span>
<a name="do-select-dao"></a>
do-select-dao (((type type-var) &optional (test t) &rest sort) &body body)
</p>
<p class="desc">Like <a href="#select-dao"><code>select-dao</code></a>,
but iterates over the results rather than returning them. For each matching
DAO, <code>body</code> is evaluated with <code>type-var</code> bound to the
DAO instance.</p>
<pre class="code">(do-select-dao (('user user) (:> 'score 10000) 'name)
(pushnew user high-scorers))</pre>
<p class="def">
<span>macro</span>
<a name="query-dao"></a>
query-dao (type query &rest args)
<br/>→ list
</p>
<p class="desc">Execute the given <code>query</code> (which can be either
a string or an <a href="s-sql.html">S-SQL</a> expression) and return
the result as DAOs of the given <code>type</code>. If the <code>query</code>
contains placeholders ($1, $2, etc) their values can be given as extra
arguments. The names of the fields returned by the <code>query</code> must
either match slots in the DAO class, or be bound through <a
href="#with-column-writers"><code>with-column-writers</code></a>.</p>
<p class="def">
<span>function</span>
<a name="do-query-dao"></a>
do-query-dao (((type type-var) query &rest args) &body body)
<br/>→ list
</p>
<p class="desc">Like <a href="#query-dao"><code>query-dao</code></a>, but
iterates over the results rather than returning them. For each matching DAO,
<code>body</code> is evaluated with <code>type-var</code> bound to the
instance.</p>
<pre class="code">(do-query-dao (('user user) (:order-by (:select '* :from 'user :where (:> 'score 10000)) 'name))
(pushnew user high-scorers))</pre>
<p class="def">
<span>variable</span>
<a name="*ignore-unknown-columns*"></a>
*ignore-unknown-columns*
</p>
<p class="desc">Normally,
when <code><a href="#get-dao">get-dao</a></code>,
<code><a href="#select-dao">select-dao</a></code>,
or <code><a href="#query-dao">query-dao</a></code> finds a column
in the database that's not in the DAO class, it will raise an
error. Setting this variable to a non-<code>NIL</code> will cause it to
simply ignore the unknown column.</p>
<p class="def">
<span>method</span>
<a name="insert-dao"></a>
insert-dao (dao)
<br/>→ dao
</p>
<p class="desc">Insert the given <code>dao</code> into the database.
Column slots of the object which are unbound implies the database defaults.
Hence, if these columns has no defaults defined in the database, the
the insertion of the <code>dao</code> will be failed.
(This feature only works on PostgreSQL 8.2 and up.)</p>
<p class="def">
<span>method</span>
<a name="update-dao"></a>
update-dao (dao)
<br/>→ dao
</p>
<p class="desc">Update the representation of the given <code>dao</code>
in the database to the values in the object. This is not defined for
tables that do not have any non-primary-key columns. Raises an
error when no row matching the <code>dao</code> exists.</p>
<p class="def">
<span>function</span>
<a name="save-dao"></a>
save-dao (dao)
<br/>→ boolean
</p>
<p class="desc">Tries to insert the given <code>dao</code> using <a
href="#insert-dao"><code>insert-dao</code></a>. If this raises a
unique key violation error, it tries to update it by using <a
href="#update-dao"><code>update-dao</code></a> instead. Be aware
that there is a possible race condition here ― if some
other process deletes the row at just the right moment, the update
fails as well. Returns a boolean telling you whether a new row was
inserted.</p>
<p class="desc">This function is unsafe to use inside of a
transaction ― when a row with the given keys already
exists, the transaction will be aborted. Use <a
href="#save-dao/transaction"><code>save-dao/transaction</code></a>
instead in such a situation.</p>
<p class="desc"><b>See also:</b> <a href="#upsert-dao">
<code>upsert-dao</code></a>.</p>
<p class="def">
<span>function</span>
<a name="save-dao/transaction"></a>
save-dao/transaction (dao)
<br/>→ boolean
</p>
<p class="desc">Acts exactly like <a
href="#save-dao"><code>save-dao</code></a>, except that it
protects its attempt to insert the object with a rollback point,
so that a failure will not abort the transaction.</p>
<p class="desc"><b>See also:</b> <a href="#upsert-dao">
<code>upsert-dao</code></a>.</p>
<p class="def">
<span>method</span>
<a name="upsert-dao"></a>
upsert-dao (dao)
<br/>→ dao
</p>
<p class="desc">
Like <a href="#save-dao"><code>save-dao</code></a>
or <a href="#save-dao/transaction"><code>save-dao/transaction</code></a>
but using a different method that doesn't involve a database
exception. This is safe to use both in and outside a transaction,
though it's advisable to always do it in a transaction to prevent a
race condition. The way it works is:
</p>
<ol class="desc">
<li>If the object contains unbound slots, we
call <a href="#insert-dao"><code>insert-dao</code></a> directly, thus
the behavior is like <code>save-dao</code>.</li>
<li>Otherwise we try to update a record with the same primary key. If
the PostgreSQL returns a non-zero number of rows updated it
<em>treated</em> as the record is already exists in the database, and
we stop here.</li>
<li>If the PostgreSQL returns a zero number of rows updated, it
<em>treated</em> as the record does not exist and we call
<code>insert-dao</code>.</li>
</ol>
<p class="desc">
The race condition might occur at step 3 <em>if there's no
transaction:</em> if UPDATE returns zero number of rows updated and
another thread inserts the record at that moment, the insertion
implied by step 3 will fail.
</p>
<p class="desc">
Note, that triggers and rules may affect the number of inserted or
updated rows returned by PostgreSQL, so zero or non-zero number of
affected rows may not <em>actually</em> indicate the existence of
record in the database.
</p>
<p class="desc">
This method returns two values: the DAO object and a boolean
(<code>T</code> if the object was inserted, <code>NIL</code> if
it was updated).
</p>
<p class="def">
<span>method</span>
<a name="delete-dao"></a>
delete-dao (dao)
</p>
<p class="desc">Delete the given <code>dao</code> from the database.</p>
<p class="def">
<span>function</span>
<a name="dao-table-name"></a>
dao-table-name (class)
<br/>→ string
</p>
<p class="desc">Get the name of the <code>table</code> associated with
the given DAO <code>class</code> (or symbol naming such a class).</p>
<p class="def">
<span>function</span>
<a name="dao-table-definition"></a>
dao-table-definition (class)
<br/>→ string
</p>
<p class="desc">Given a DAO <code>class</code>, or the name of one,
this will produce an SQL query string with a definition of the table.
This is just the bare simple definition, so if you need any extra
indices or or constraints, you'll have to write your own queries
to add them.</p>
<p class="def">
<span>macro</span>
<a name="with-column-writers"></a>
with-column-writers ((&rest writers) &body body)
</p>
<p class="desc">Provides control over the way <code><a
href="#get-dao">get-dao</a></code>, <code><a
href="#select-dao">select-dao</a></code>, and <code><a
href="#query-dao">query-dao</a></code> read values from the
database. This is not commonly needed, but can be used to reduce
the amount of queries a system makes. <code>writers</code> should
be a list of alternating column names (strings or symbols) and
writers, where writers are either symbols referring to a slot in
the objects, or functions taking two arguments ― an
instance and a value ― which can be used to somehow store
the value in the new instance. When any DAO-fetching function is
called in the <code>body</code>, and columns matching the given
names are encountered in the result, the writers are used instead
of the default behaviour (try and store the value in the slot that
matches the column name).</p>
<p class="desc">An example of using this is to add some non-column
slots to a DAO class, and use <code><a
href="#query-dao">query-dao</a></code> within a
<code>with-column-writers</code> form to pull in extra information
about the objects, and immediately store it in the new
instances.</p>
<h2 id="tabledef">Table definition and creation</h2>
<p>It can be useful to have the SQL statements needed to build an
application's tables available from the source code, to do things like
automatically deploying a database. The following macro and
functions allow you to group sets of SQL statements under symbols,
with some shortcuts for common elements in table definitions.</p>
<p class="def" id="deftable">
<span>macro</span>
deftable (name &body definition)
</p>
<p class="desc">Define a table. <code>name</code> can be either a symbol
or a <code>(symbol string)</code> list. In the first case, the table
name is derived from the symbol's name by <a
href="s-sql.html">S-SQL</a>'s rules. In the second case, the
<code>name</code> is given explicitly. The body of definitions can contain
anything that evaluates to a string, as well as S-SQL expressions. The
variables <a href="#*table-name*"><code>*table-name*</code></a> and
<a href="#*table-name*"><code>*table-symbol*</code></a> are bound to
the relevant values in the body. Note that the evaluation of the
<code>definition</code> is ordered, so you'll generally want to create your
table first and then define indices on it.</p>
<p class="def">
<a name="!dao-def"></a>
<span>function</span>
!dao-def ()
</p>
<p class="desc">Should only be used inside <a
href="#deftable"><code>deftable</code></a>'s body. Adds the result
of calling <a
href="#dao-table-definition"><code>dao-table-definition</code></a>
on <a href="#*table-symbol*"><code>*table-symbol*</code></a> to
the <code>definition</code>.</p>
<p class="def">
<a name="!index"></a>
<span>function</span>
!index (&rest columns), !unique-index (&rest columns)
</p>
<p class="desc">Define an index on the table being defined. The
<code>columns</code> can be given as symbols or strings.</p>
<p class="def">
<a name="!foreign"></a>
<span>function</span>
!foreign (target-table columns &optional target-columns &key on-delete on-update deferrable initially-deferred)
</p>
<p class="desc">Add a foreign key to the table being defined.
<code>target-table</code> is the referenced table.
<code>columns</code> is a list of column names or single name in
<em>this</em> table, and, if the columns have different names in
the referenced table, <code>target-columns</code> must be
another list of column names or single column name of the
<code>target-table</code>, or <code>:primary-key</code> to denote
the column(s) of the <code>target-table</code>'s primary key
as referenced column(s).
</p>
<p class="desc">The <code>on-delete</code> and
<code>on-update</code> arguments can be used to specify ON DELETE
and ON UPDATE actions, as per the keywords allowed in <a
href="s-sql.html#create-table"><code>create-table</code></a>. In
addition, the <code>deferrable</code> and <code>initially-deferred</code>
arguments can be used to indicate whether constraint checking can be
deferred until the current transaction completed, and whether this should
be done by default. Note that none of these are really &key
arguments, but rather are picked out of a &rest arg at
runtime, so that they can be specified even when
<code>target-columns</code> is not given.</p>
<p class="def">
<a name="!unique"></a>
<span>function</span>
!unique (target-fields &key deferrable initially-deferred)
</p>
<p class="desc">Constrains one or more columns to only contain
unique (combinations of) values, with <code>deferrable</code> and
<code>initially-deferred</code> defined as in <a
href="#!foreign"><code>!foreign</code></a></p>
<p class="def" id="create-table">
<span>function</span>
create-table (symbol)
</p>
<p class="desc">Creates the table identified by
<code>symbol</code> by <a href="#execute">executing</a> all forms in
its definition.</p>
<p class="def" id="create-all-tables">
<span>function</span>
create-all-tables ()
</p>
<p class="desc">Creates all defined tables.</p>
<p class="def" id="create-package-tables">
<span>function</span>
create-package-tables (package)
</p>
<p class="desc">Creates all tables identified by symbols
interned in the given <code>package</code>.</p>
<p class="def">
<a name="*table-name*"></a>
<span>variables</span>
*table-name*, *table-symbol*
</p>
<p class="desc">These variables are bound to the relevant name and
symbol while the forms of a table definition are evaluated. Can be
used to define shorthands like the ones below.</p>
<h2 id="schemata">Schemata</h2>
Schema allow you to separate tables into differnet name spaces. In
different schemata two tables with the same name are allowed to
exists. The tables can be referred by fully qualified names or
with the macro <a href="#with-schema">with-schema</a>. You could
also set the search path
with <a href="#set-search-path">set-search-path<a/>. For listing
end checking there are also the
functions <a href="#list-schemata">list-schemata</a>
and <a href="#schema-exist-p">schema-exist-p</a>. The following
functions allow you to create, drop schemata and to set the search
path.
<p class="def">
<span>function</span>
<a name="create-schema"></a>
create-schema (schema)
</p>
<p class="desc">
Creates a new schema. Raises an error if the schema is already exists.
</p>
<p class="def">
<span>function</span>
<a name="drop-schema"></a>
drop-schema (schema)
</p>
<p class="desc">
Removes a schema. Raises an error if the schema is not empty.
</p>
<p class="def">
<span>function</span>
<a name="get-search-path"></a>
get-search-path ()
</p>
<p class="desc">
Retrieve the current search path.
</p>
<p class="def">
<span>function</span>
<a name="set-search-path"></a>
set-search-path (path)
</p>
<p class="desc">
Sets the search path to the <code>path</code>. This function is used
by <a href="#with-schema">with-schema</a>.
</p>
<h2 id="index">Symbol-index</h2>
<ul class="symbol-index">
<li><a href="#abort-transaction">abort-transaction</a></li>
<li><a href="#deftable">deftable</a></li>
<li><a href="#call-with-connection">call-with-connection</a></li>
<li><a href="#clear-connection-pool">clear-connection-pool</a></li>
<li><a href="#clear-template">clear-template</a></li>
<li><a href="#commit-transaction">commit-transaction</a></li>
<li><a href="#connect">connect</a></li>
<li><a href="#connect-toplevel">connect-toplevel</a></li>
<li><a href="#connected-p">connected-p</a></li>
<li><a href="#create-all-tables">create-all-tables</a></li>
<li><a href="#create-package-tables">create-package-tables</a></li>
<li><a href="#create-schema">create-schema</a></li>
<li><a href="#create-table">create-table</a></li>
<li><a href="#dao-class">dao-class</a></li>
<li><a href="#!dao-def">!dao-def</a></li>
<li><a href="#dao-exists-p">dao-exists-p</a></li>
<li><a href="#dao-keys">dao-keys</a></li>
<li><a href="#dao-table-definition">dao-table-definition</a></li>
<li><a href="#dao-table-name">dao-table-name</a></li>
<li><a href="#*database*">*database*</a></li>
<li><a href="#database-connection">database-connection</a></li>
<li><a href="#define-dao-finalization">define-dao-finalization</a></li>
<li><a href="cl-postgres.html#database-connection-lost">database-connection-lost</a></li>
<li><a href="cl-postgres.html#database-error">database-error</a></li>
<li><a href="cl-postgres.html#database-error-cause">database-error-cause</a></li>
<li><a href="cl-postgres.html#database-error-code">database-error-code</a></li>
<li><a href="cl-postgres.html#database-error-detail">database-error-detail</a></li>
<li><a href="cl-postgres.html#database-error-query">database-error-query</a></li>
<li><a href="cl-postgres.html#database-error-message">database-error-message</a></li>
<li><a href="#*default-use-ssl*">*default-use-ssl*</a></li>
<li><a href="#defprepared">defprepared</a></li>
<li><a href="#defprepared">defprepared-with-names</a></li>
<li><a href="#delete-dao">delete-dao</a></li>
<li><a href="#disconnect">disconnect</a></li>
<li><a href="#disconnect-toplevel">disconnect-toplevel</a></li>
<li><a href="#doquery">doquery</a></li>
<li><a href="#drop-schema">drop-schema</a></li>
<li><a href="#execute">execute</a></li>
<li><a href="#!foreign">!foreign</a></li>
<li><a href="#!unique">!unique</a></li>
<li><a href="#get-dao">get-dao</a></li>
<li><a href="#get-search-path">get-search-path</a></li>
<li><a href="#*ignore-unknown-columns*">*ignore-unknown-columns*</a></li>
<li><a href="#!index">!index</a></li>
<li><a href="#insert-dao">insert-dao</a></li>
<li><a href="#list-sequences">list-sequences</a></li>
<li><a href="#list-schemata">list-schemata</a></li>
<li><a href="#list-tables">list-tables</a></li>
<li><a href="#list-views">list-views</a></li>
<li><a href="#make-dao">make-dao</a></li>
<li><a href="#*max-pool-size*">*max-pool-size*</a></li>
<li><a href="#prepare">prepare</a></li>
<li><a href="#query">query</a></li>
<li><a href="#query-dao">query-dao</a></li>
<li><a href="#reconnect">reconnect</a></li>
<li><a href="#release-savepoint">release-savepoint</a></li>
<li><a href="#reset-table">reset-table</a></li>
<li><a href="#rollback-savepoint">rollback-savepoint</a></li>
<li><a href="#save-dao">save-dao</a></li>
<li><a href="#save-dao/transaction">save-dao/transaction</a></li>
<li><a href="#select-dao">select-dao</a></li>
<li><a href="#set-search-path">set-search-path</a></li>
<li><a href="#schema-exist-p">schema-exist-p</a></li>
<li><a href="#sequence-exists-p">sequence-exists-p</a></li>
<li><a href="#sequence-next">sequence-next</a></li>
<li><a href="#table-description">table-description</a></li>
<li><a href="#table-exists-p">table-exists-p</a></li>
<li><a href="#*table-name*">*table-name*</a></li>
<li><a href="#*table-name*">*table-symbol*</a></li>
<li><a href="#!index">!unique-index</a></li>
<li><a href="#update-dao">update-dao</a></li>
<li><a href="#view-exists-p">view-exists-p</a></li>
<li><a href="#with-column-writers">with-column-writers</a></li>
<li><a href="#with-connection">with-connection</a></li>
<li><a href="#with-savepoint">with-savepoint</a></li>
<li><a href="#with-schema">with-schema</a></li>
<li><a href="#with-transaction">with-transaction</a></li>
</ul>
</body>
</html>
|