154 lines
4.8 KiB
HTML
154 lines
4.8 KiB
HTML
<!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="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. -->
|
||
<p class="announce">
|
||
📹 <a href="https://www.udemy.com/course/common-lisp-programming/?couponCode=6926D599AA-LISP4ALL">NEW! Learn Lisp in videos and support our contributors with this 40% discount.</a>
|
||
</p>
|
||
<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–2021 the Common Lisp Cookbook Project
|
||
</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>
|