<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Artful Code &#187; clsql</title>
	<atom:link href="http://www.artfulcode.net/tags/clsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create/update function for CLSQL</title>
		<link>http://www.artfulcode.net/articles/create-update-function-clsql/</link>
		<comments>http://www.artfulcode.net/articles/create-update-function-clsql/#comments</comments>
		<pubDate>Fri, 09 May 2008 19:51:32 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clsql]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/create-update-function-clsql/</guid>
		<description><![CDATA[Django has a very handy model method in its database library called get_or_create, which either creates a new record using the keyword arguments passed as field values or gets a record using the arguments passed. from models import Person person = Person.objects.get_or_create&#40;first_name=&#34;John&#34;, last_name=&#34;Johnson&#34;&#41; It adds syntactic sugar to the common case of checking if an [...]]]></description>
			<content:encoded><![CDATA[<p>Django has a very handy model method in its database library called get_or_create, which either creates a new record using the keyword arguments passed as field values or gets a record using the arguments passed.<span id="more-25"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> models <span style="color: #ff7700;font-weight:bold;">import</span> Person
person = Person.<span style="color: black;">objects</span>.<span style="color: black;">get_or_create</span><span style="color: black;">&#40;</span>first_name=<span style="color: #483d8b;">&quot;John&quot;</span>, last_name=<span style="color: #483d8b;">&quot;Johnson&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It adds syntactic sugar to the common case of checking if an entry exists, creating it if it does not, then updating (or creating with new) values.  The problem is that its <em>slow</em>.</p>
<p>I offload our most expensive database work on our server to a Lisp-based XML RPC server using <a href="http://clsql.b9.com/">CLSQL</a>.  I wrote an analog of get_or_create for CLSQL.  It does not have the advantage of Django&#8217;s keyword searches; all searches use <code>=</code> and pull the first row that matches the value.</p>
<p>The first function, <code>get-instance</code> is a helper to get a row in a view-class using a field/value pair.<br />
<code>create/update</code> takes the first three arguments and passes them to <code>get-instance</code> to get any existing match.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> get-instance <span style="color: #66cc66;">&#40;</span>cls pk val<span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">locally-enable-sql-reader-syntax</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>inst <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">select</span> cls <span style="color: #66cc66;">:</span><span style="color: #555;">flatp</span> t <span style="color: #66cc66;">:</span><span style="color: #555;">refresh</span> t <span style="color: #66cc66;">:</span><span style="color: #555;">limit</span> <span style="color: #cc66cc;">1</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">where</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">sql-expression</span> <span style="color: #66cc66;">:</span><span style="color: #555;">attribute</span> pk<span style="color: #66cc66;">&#41;</span> val<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    #<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">restore-sql-reader-syntax-state</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> inst <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> inst<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> create/update <span style="color: #66cc66;">&#40;</span>cls pk val <span style="color: #66cc66;">&amp;</span>rest args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>inst <span style="color: #66cc66;">&#40;</span>get-instance cls pk val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> inst
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #'reinitialize-instance inst args<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #'make-instance cls pk val args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Usage:</h4>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">def-view-class</span> person <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>id
        <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> person-id
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-kind</span> <span style="color: #66cc66;">:</span><span style="color: #555;">key</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #b1b100;">integer</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">id</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>last-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> person-last-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">varchar</span> <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">last-</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>first-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> person-first-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">varchar</span> <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">first-</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar john <span style="color: #66cc66;">&#40;</span>create/update 'person <span style="color: #66cc66;">:</span><span style="color: #555;">id</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If there is row with id 3 in the database, it will grab that (or, if the key is not unique, it will grab the first matching row.)  If there is no entry with that id, it will create a new one, that can then be updated the normal way:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>person-last-<span style="color: #b1b100;">name</span> john<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>person-first-<span style="color: #b1b100;">name</span> john<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Johnson&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The other alternative is to get and set all in one go:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar john <span style="color: #66cc66;">&#40;</span>create/update 'person
                            <span style="color: #66cc66;">:</span><span style="color: #555;">id</span> <span style="color: #cc66cc;">3</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">last-</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;Johnson&quot;</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">first-</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>It&#8217;s pretty simple but it is very useful.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Create%2Fupdate+function+for+CLSQL&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Create%2Fupdate+function+for+CLSQL+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/create-update-function-clsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

