<?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>therning.org/ magnus &#187; sqlite3</title>
	<atom:link href="http://therning.org/magnus/archives/tag/sqlite3/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Thu, 12 Jan 2012 13:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Confusion with HaskellDB</title>
		<link>http://therning.org/magnus/archives/375</link>
		<comments>http://therning.org/magnus/archives/375#comments</comments>
		<pubDate>Fri, 05 Sep 2008 23:59:09 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[haskelldb]]></category>
		<category><![CDATA[hdbc]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=375</guid>
		<description><![CDATA[Dear LazyWeb, I&#8217;ve recently made some comparisons between HaskellDB and HDBC. My gut feeling is that I&#8217;d like to use HaskellDB due to its type safety and that it allows me to abstract away from SQL&#8212;something in me twitches every time I see strings containing SQL mixed in with other code, no matter what language [...]]]></description>
			<content:encoded><![CDATA[<p>Dear LazyWeb,</p>

<p>I&#8217;ve recently made some comparisons between <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/haskelldb">HaskellDB</a> and <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HDBC">HDBC</a>.  My
gut feeling is that I&#8217;d <em>like</em> to use HaskellDB due to its type safety and that
it allows me to abstract away from SQL&#8212;something in me twitches every time I
see strings containing SQL mixed in with other code, no matter what language
it is.  However there are some things that confuse me with HaskellDB.  After
overcoming the initial puzzlement that I seem to hit whenever I look at a
Haskell API I stumbled on the apparent lack of support for primary keys.  This
surprised me and to check if I overlooked something I wrote the following
code:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Environment
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">IO</span>
<span style="color: #06c; font-weight: bold;">import</span> Database<span style="color: #339933; font-weight: bold;">.</span>HaskellDB<span style="color: #339933; font-weight: bold;">.</span>HSQL<span style="color: #339933; font-weight: bold;">.</span>SQLite3
<span style="color: #06c; font-weight: bold;">import</span> Database<span style="color: #339933; font-weight: bold;">.</span>HaskellDB<span style="color: #339933; font-weight: bold;">.</span>DBSpec
&nbsp;
testDBOpts <span style="color: #339933; font-weight: bold;">=</span> DBOptions <span style="color: green;">&#123;</span> useBString<span style="color: #339933; font-weight: bold;">=</span>False <span style="color: green;">&#125;</span>
&nbsp;
sqliteOptions<span style="color: #339933; font-weight: bold;">=</span> SQLiteOptions <span style="color: green;">&#123;</span> filepath<span style="color: #339933; font-weight: bold;">=</span><span style="background-color: #3cb371;">&quot;replace me&quot;</span><span style="color: #339933; font-weight: bold;">,</span> mode<span style="color: #339933; font-weight: bold;">=</span>ReadMode <span style="color: green;">&#125;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    dbfn <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">fmap</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> getArgs
    <span style="color: #06c; font-weight: bold;">let</span> options <span style="color: #339933; font-weight: bold;">=</span> sqliteOptions <span style="color: green;">&#123;</span> filepath<span style="color: #339933; font-weight: bold;">=</span>dbfn <span style="color: green;">&#125;</span>
    sqliteConnect options <span style="color: green;">&#40;</span>dbToDBSpec False <span style="background-color: #3cb371;">&quot;extracted.db&quot;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="font-weight: bold;">print</span></pre></div></div>


<p>This is the surprising output when I point this to two different databases.
The first database has no primary key:</p>

<pre><code>% sqlite3 test1.db .dump
BEGIN TRANSACTION;
CREATE TABLE person (name text  not null,
                     age int);
COMMIT;
</code></pre>

<p>and the <code>DBInfo</code> looks the way I expected:</p>

<pre><code>% ./extract test1.db 
DBInfo {dbname = "extracted.db", opts = DBOptions {useBString = False},
tbls = [TInfo {tname = "person", cols = [CInfo {cname = "name",
descr = (StringT,False)},CInfo {cname = "age", descr = (StringT,True)}]}]}
</code></pre>

<p>The second database does have a primary key:</p>

<pre><code>% sqlite3 test2.db .dump
BEGIN TRANSACTION;
CREATE TABLE person (name text primary key not null, age int);
COMMIT;
</code></pre>

<p>but not only does the <code>DBInfo</code> lack any mention of it, it also contains the
table info twice:</p>

<pre><code>% ./extract test2.db 
DBInfo {dbname = "extracted.db", opts = DBOptions {useBString = False},
tbls = [TInfo {tname = "person", cols = [CInfo {cname = "name",
descr = (StringT,False)},CInfo {cname = "age", descr = (StringT,True)}]},
TInfo {tname = "person", cols = [CInfo {cname = "name",
descr = (StringT,False)},CInfo {cname = "age", descr = (StringT,True)}]}]}
</code></pre>

<p>Am I doing something fundamentally wrong or does HaskellDB not support the
notion of a primary key?</p>

<p>Is it a bug in HaskellDB that the second extracted <code>DBInfo</code> contains two
instances of <code>TInfo</code> when the database clearly only has one table?</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F375&amp;title=Confusion%20with%20HaskellDB" id="wpa2a_2">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/375/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

