2023-10-25 11:23:21 +02:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta name = "generator" content =
"HTML Tidy for HTML5 for Linux version 5.2.0">
< title > A "defmodel" macro< / title >
< meta charset = "utf-8" >
< meta name = "description" content = "A collection of examples of using Common Lisp" >
< meta name = "viewport" content =
"width=device-width, initial-scale=1">
< link rel = "icon" href =
"../assets/cl-logo-blue.png"/>
< link rel = "stylesheet" href =
"../assets/style.css">
< script type = "text/javascript" src =
"../assets/highlight-lisp.js">
< / script >
< script type = "text/javascript" src =
"../assets/jquery-3.2.1.min.js">
< / script >
< script type = "text/javascript" src =
"../assets/jquery.toc/jquery.toc.min.js">
< / script >
< script type = "text/javascript" src =
"../assets/toggle-toc.js">
< / script >
< link rel = "stylesheet" href =
"../assets/github.css">
< link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity = "sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin = "anonymous" >
< / head >
< body >
< h1 id = "title-xs" > < a href = "../index.html" > The Common Lisp Cookbook< / a > – A "defmodel" macro< / h1 >
< div id = "logo-container" >
< a href = "../index.html" >
< img id = "logo" src = "../assets/cl-logo-blue.png" / >
< / a >
< div id = "searchform-container" >
< form onsubmit = "duckSearch()" action = "javascript:void(0)" >
< input id = "searchField" type = "text" value = "" placeholder = "Search..." >
< / form >
< / div >
< div id = "toc-container" class = "toc-close" >
< div id = "toc-title" > Table of Contents< / div >
< ul id = "toc" class = "list-unstyled" > < / ul >
< / div >
< / div >
< div id = "content-container" >
< h1 id = "title-non-xs" > < a href = "../index.html" > The Common Lisp Cookbook< / a > – A "defmodel" macro< / h1 >
<!-- Announcement we can keep for 1 month or more. I remove it and re - add it from time to time. -->
2024-05-15 18:18:38 +02:00
<!-- <p class="announce"> -->
<!-- 📢 🤶 ⭐ -->
<!-- <a style="font - size: 120%" href="https://www.udemy.com/course/common - lisp - programming/?couponCode=LISPY - XMAS2023" title="This course is under a paywall on the Udemy platform. Several videos are freely available so you can judge before diving in. vindarel is (I am) the main contributor to this Cookbook."> Discover our contributor's Lisp course with this Christmas coupon.</a> -->
<!-- <strong> -->
<!-- Recently added: 18 videos on MACROS. -->
<!-- </strong> -->
<!-- <a style="font - size: 90%" href="https://github.com/vindarel/common - lisp - course - in - videos/">Learn more</a>. -->
<!-- </p> -->
< p class = "announce" >
📢 New videos: < a href = "https://www.youtube.com/watch?v=h_noB1sI_e8" > web dev demo part 1< / a > , < a href = "https://www.youtube.com/watch?v=xnwc7irnc8k" > dynamic page with HTMX< / a > , < a href = "https://www.youtube.com/watch?v=Zpn86AQRVN8" > Weblocks demo< / a >
< / p >
2023-10-25 11:23:21 +02:00
< p class = "announce-neutral" >
📕 < a href = "../index.html#download-in-epub" > Get the EPUB and PDF< / a >
< / p >
< div id = "content"
< p > < / p >
< pre > < code class = "language-lisp" > (defmacro defmodel (name slot-definitions)
`(progn
(defclass ,name ()
((id :col-type serial :reader ,(symb name 'id))
,@slot-definitions)
(:metaclass dao-class)
(:keys id))
(with-connection (db-params)
(unless (table-exists-p ',name)
(execute (dao-table-definition ',name))))
;; Create
(defmacro ,(symb name 'create) (& rest args)
`(with-connection (db-params)
(make-dao ',',name ,@args)))
;; Read
(defun ,(symb name 'get-all) ()
(with-connection (db-params)
(select-dao ',name)))
(defun ,(symb name 'get) (id)
(with-connection (db-params)
(get-dao ',name id)))
(defmacro ,(symb name 'select) (sql-test & optional sort)
`(with-connection (db-params)
(select-dao ',',name ,sql-test ,sort)))
;; Update
(defun ,(symb name 'update) (,name)
(with-connection (db-params)
(update-dao ,name)))
;; Delete
(defun ,(symb name 'delete) (,name)
(with-connection (db-params)
(delete-dao ,name)))))
< / code > < / pre >
< p class = "page-source" >
Page source: < a href = "https://github.com/LispCookbook/cl-cookbook/blob/master/drafts/defmodel.lisp.md" > drafts/defmodel.lisp.md< / a >
< / p >
< / div >
< script type = "text/javascript" >
// Don't write the TOC on the index.
if (window.location.pathname != "/cl-cookbook/") {
$("#toc").toc({
content: "#content", // will ignore the first h1 with the site+page title.
headings: "h1,h2,h3,h4"});
}
$("#two-cols + ul").css({
"column-count": "2",
});
$("#contributors + ul").css({
"column-count": "4",
});
< / script >
< div >
< footer class = "footer" >
< hr / >
© 2002– 2023 the Common Lisp Cookbook Project
< div >
2024-05-15 18:18:38 +02:00
📹 Discover < a style = "color: darkgrey; text-decoration: underline" , href = "https://www.udemy.com/course/common-lisp-programming/?referralCode=2F3D698BBC4326F94358" > our contributor's Common Lisp video course on Udemy< / a >
2023-10-25 11:23:21 +02:00
< / div >
< / footer >
< / div >
< div id = "toc-btn" > T< br > O< br > C< / div >
< / div >
< script text = "javascript" >
HighlightLisp.highlight_auto({className: null});
< / script >
< script type = "text/javascript" >
function duckSearch() {
var searchField = document.getElementById("searchField");
if (searchField & & searchField.value) {
var query = escape("site:lispcookbook.github.io/cl-cookbook/ " + searchField.value);
window.location.href = "https://duckduckgo.com/?kj=b2& kf=-1& ko=1& q=" + query;
// https://duckduckgo.com/params
// kj=b2: blue header in results page
// kf=-1: no favicons
}
}
< / script >
< script async defer data-domain = "lispcookbook.github.io/cl-cookbook" src = "https://plausible.io/js/plausible.js" > < / script >
< / body >
< / html >