title: Database Functional Programming in Clojure
body: <p><em>"It is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail."</em> - Abraham Maslow</p>
  
  <p>I need to write a tool to dump a large hierarchical (SQL) database to XML. The hierarchy consists of a <code>Person</code> table with subsidiary <code>Address</code>, <code>Phone</code>, etc. tables.</p>
  
  <ul>
  <li><p>I have to dump <em>thousands</em> of rows, so I would like to do so incrementally and not keep the whole XML file in memory.</p></li>
  <li><p>I would like to isolate non-pure function code to a small portion of the application.</p></li>
  <li><p>I am thinking that this might be a good opportunity to explore FP and concurrency in Clojure. I can also show the benefits of immutable data and multi-core utilization to my skeptical co-workers.</p></li>
  </ul>
  
  <p>I'm not sure how the overall architecture of the application should be. I am thinking that I can use an impure function to retrieve the database rows and return a lazy sequence that can then be processed by a pure function that returns an XML fragment.</p>
  
  <p>For each <code>Person</code> row, I can create a <code>Future</code> and have several processed in parallel (the output order does not matter).</p>
  
  <p>As each <code>Person</code> is processed, the task will retrieve the appropriate rows from the <code>Address</code>, <code>Phone</code>, etc. tables and generate the nested XML.</p>
  
  <p>I can use a a generic function to process most of the tables, relying on database meta-data to get the column information, with special functions for the few tables that need custom processing. These functions could be listed in a <code>map(table name -&gt; function)</code>.</p>
  
  <p>Am I going about this in the right way? I can easily fall back to doing it in OO using Java,  but that would be no fun.</p>
  
  <p>BTW, are there any good books on FP patterns or architecture? I have several good books on Clojure, Scala, and F#, but although each covers the language well, none look at the "big picture" of function programming design.</p>