<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>szeiger.de</title>
	
	<link>http://szeiger.de</link>
	<description>Stefan Zeiger's Software Development Weblog</description>
	<lastBuildDate>Sat, 13 Aug 2011 22:21:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/szeiger" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="szeiger" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Type-Level Computations with Functional Dependencies</title>
		<link>http://szeiger.de/blog/2011/08/14/type-level-computations-with-functional-dependencies/</link>
		<comments>http://szeiger.de/blog/2011/08/14/type-level-computations-with-functional-dependencies/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 22:21:03 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=85</guid>
		<description><![CDATA[It has been a long time since my last blog post about ScalaQuery, and in the meantime I have announced new features in my Twitter stream and in the release notes but now I need some more space to explain a refactoring I have been working on which should eventually lead to ScalaQuery 0.10.
Recap
In ScalaQuery [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a long time since my last blog post about <a href="http://scalaquery.org">ScalaQuery</a>, and in the meantime I have announced new features in my <a href="http://twitter.com/#!/StefanZeiger">Twitter stream</a> and in the <a href="http://implicit.ly/?sort=&#038;search=ScalaQuery">release notes</a> but now I need some more space to explain a refactoring I have been working on which should eventually lead to ScalaQuery 0.10.</p>
<h3>Recap</h3>
<p>In ScalaQuery 0.9, there is a simple relationship between the types of queries and the types of values they return: A <code>Query[ColumnBase[T]]</code> selects values of type <code>T</code>, so e.g. calling <code>.list</code> on such a query gives you a <code>List[T]</code>. You can create <code>Query</code> objects of types other than <code>ColumnBase[T] forSome { type T }</code> but the implicit conversion to a <code>StatementInvoker</code> (which allows you to call methods such as <code>.list</code>) is not available for them.</p>
<p>Let&#8217;s look at the different subtypes of <code>ColumnBase</code> that we can use in queries. First of all, all <em>tables</em> extend <code>ColumnBase</code>, so that <code>Table[T] &lt;: ColumnBase[T]</code>.</p>
<div class="wp_syntax">
<div class="code">
<pre>  object MyTable extends Table[(Int, String, String)]("MY_TABLE") {
    def a = column[Int]("A")
    def b = column[String]("B")
    def c = column[String]("C")
    def * = a ~ b ~ c
  }

  val q1 = Query(MyTable)
  // q1: Query[MyTable]
  val res1 = q1.list
  // res1: List[(Int, String, String)]
</pre>
</div>
</div>
<p>And, as implied by the name ColumnBase, <code>Column[T] &lt;: ColumnBase[T]</code>, so we can query for individual <em>columns</em>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q2 = for {
    t &lt;- MyTable // automatically lifted to a Query
  } yield t.a
  // q2: Query[Column[Int]]
  val res2 = q2.list
  // res2: List[Int]
</pre>
</div>
</div>
<p>So what about selecting multiple columns? The most natural notation would be the same as when you&#8217;re querying a Scala collection instead of a database:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q3 = for {
    t &lt;- MyTable
  } yield (t.a, t.b)
  // q3: Query[(Column[Int], Column[String])]
</pre>
</div>
</div>
<p>But this won&#8217;t work. We get a <code>Query[(ColumnBase[A], ColumnBase[B])]</code> but what we need is a <code>Query[ColumnBase[(A, B)]]</code> &#8212; wrapped in the opposite order! This <code>ColumnBase[Tuple2[A,B]]</code> is called a <code>Projection2[A,B]</code> in ScalaQuery (actually, a <code>Projection2[A,B]</code> is both a <code>ColumnBase[(A,B)]</code> and a <code>(Column[A], Column[B])</code>). You construct it with the <code>~</code> operator:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q3b = for {
    t &lt;- MyTable
  } yield t.a ~ t.b
  // q3b: Query[Projection2[Int, String]]
  val res3 = q3b.list
  // res3: List[(Int, String)]
</pre>
</div>
</div>
<h3>Type-Level Transformation</h3>
<p>For the sake of simplicity, instead of the implicit conversions and Invokers we will assume a method <code>list()</code> which takes a Query and returns a list of the results. At the moment, it would look like this:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def list[T](q: Query[ColumnBase[T]]): List[T]
</pre>
</div>
</div>
<p>We need to generalize it to:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def list[T, U](q: Query[T]): List[U]
</pre>
</div>
</div>
<p>where <code>U</code> is a transformed version of <code>T</code>, which we will write as <code>T =>> U</code>. So what kinds of transformations do we need? The existing functionality is covered by:</p>
<div class="wp_syntax">
<div class="code">
<pre>  ColumnBase[T] =>> T
</pre>
</div>
</div>
<p>For tuples, as used in <code>q3</code>, we need another rule:</p>
<div class="wp_syntax">
<div class="code">
<pre>  (T1, T2) =>> (U1, U2) where T1 =>> U1 and T2 =>> U2
</pre>
</div>
</div>
<p>Actually, we need one of those for all arities from 2 to 22:</p>
<div class="wp_syntax">
<div class="code">
<pre>  (T1, T2, T3) =>> (U1, U2, U3) where T1 =>> U1 and T2 =>> U2 and T3 =>> U3
  ...and so on
</pre>
</div>
</div>
<p>Note that these rules are recursive. They enable the transformation of arbitrarily nested tuples.</p>
<p>Just one more rule before we&#8217;re done: ScalaQuery&#8217;s <code>Projection</code> classes can only wrap Columns, so any primitive value is lifted implicitly:</p>
<div class="wp_syntax">
<div class="code">
<pre>  t.a ~ t.b ~ 42
  // : Projection3[Int, String, Int]
  //   == (Column[Int], Column[String], Column[Int])
</pre>
</div>
</div>
<p>If we allow the use of Tuples instead of Projections, we do not get this implicit lifting anymore, so we need to allow primitive values, too:</p>
<div class="wp_syntax">
<div class="code">
<pre>  T =>> T where a TypeMapper[T] is available
</pre>
</div>
</div>
<p>We can use <a href="http://www.chuusai.com/2011/07/16/fundeps-in-scala/">functional dependencies</a> to have the Scala compiler do the required transformation by turning =>> into a trait, every &#8220;where&#8221; condition into an implicit parameter, and every rule into an implicit method:</p>
<div class="wp_syntax">
<div class="code">
<pre>  trait =>> [-T, U]

  implicit def unpackColumnBase[T]: ColumnBase[T] =>> T = null
  implicit def unpackTuple2[T1, T2, U2, U2](implicit u1: T1 =>> U1, u2: T2 =>> U2): (T1, T2) =>> (U1, U2) = null
  ...
  implicit def unpack[T : TypeMapper]: T =>> T = null
</pre>
</div>
</div>
<p>In order to avoid ambiguities, we need to prioritize the implicits. We also add a nice error message in case a bad query type is used:</p>
<div class="wp_syntax">
<div class="code">
<pre>  @implicitNotFound(msg = "Don't know how to unpack ${T} (to ${U})")
  trait =>> [-T, U]

  object =>> extends LowPriority_=>> {
    implicit def unpackColumnBase[T]: ColumnBase[T] =>> T = null
  }

  trait LowPriority_=>> {
    implicit def unpackTuple2[T1, T2, U2, U2](implicit u1: T1 =>> U1, u2: T2 =>> U2): (T1, T2) =>> (U1, U2) = null
    ...
    implicit def unpack[T : TypeMapper]: T =>> T = null
  }
</pre>
</div>
</div>
<p>And finally, the <code>list()</code> method gets an implicit parameter to compute the <code>U</code> type.</p>
<div class="wp_syntax">
<div class="code">
<pre>  def list[T, U](q: Query[T])(implicit u: T =>> U): List[U]
</pre>
</div>
</div>
<p>That&#8217;s it, basically. We can now call <code>list()</code> with a Query of any nesting of tuples and columns and it will remove all <code>ColumnBase</code> type constructors for the result type. We still need to extract a value of the right type from the query result at run-time. That can be done with a method in an implementation of <code>=>></code> in a straight-forward way. For the purpose of computing the type at compile-time, the implementation does not matter, so we just used <code>null</code> above.</p>
<h3>Another Transformation</h3>
<p>There&#8217;s still a catch though. Take a look at the following queries:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q4a = for {
    a ~ b ~ c &lt;- MyTable.where(_.a === 1).map(t => t.a ~ t.b ~ 4) unionAll
                 MyTable.where(_.a === 2).map(t => t.a ~ t.b ~ 5)
  } yield a ~ b ~ (c*2)

  val q4b = for {
    (a, b, c) &lt;- MyTable.where(_.a === 1).map(t => (t.a, t.b, 4)) unionAll
                 MyTable.where(_.a === 2).map(t => (t.a, t.b, 5))
  } yield a ~ b ~ (c*2)
</pre>
</div>
</div>
<p>A <code>unionAll</code> operation takes multiple Queries of the same type and produces another Query of that type which represents their union. The values that come out of this operation need to encode the union as a ScalaQuery AST node. In <code>q4a</code> (old style), this value has the type <code>Projection3[Int, String, Int]</code> which is <code>(Column[Int], Column[String], Column[Int])</code>. The literal values 4 and 5 have been lifted to ConstColumns and the union operation can be properly encoded into the three resulting Columns.</p>
<p>In <code>q4b</code> (new style), the <code>unionAll</code> produces a <code>(Column[String], Column[Int], Int)</code> tuple and we have no way of encoding the union operation into the <code>Int</code> value! To solve this problem, union operations need to return a different type than that of their operands: One where all primitive values have been lifted to Columns, i.e. the opposite of the =>> transformation:</p>
<div class="wp_syntax">
<div class="code">
<pre>  @implicitNotFound(msg = "Don't know how to reify ${Packed} (to ${Reified})")
  sealed trait Reify[-Packed, Reified]

  object Reify extends ReifyLowPriority {
    implicit final def reifyColumnBase[T &lt;: ColumnBase[_]]: Reify[T, T] = null
  }

  trait ReifyLowPriority {
    implicit final def reifyPrimitive[T](implicit tm: TypeMapper[T]): Reify[T, ConstColumn[T]] = null
    implicit final def reifyTuple2[T1,T2, U1,U2, R1,R2](implicit u1: Reify[T1, R1], u2: Reify[T2, R2]): Reify[(T1,T2), (R1,R2)] = null
    ...
  }
</pre>
</div>
</div>
<h3>Limitations</h3>
<p>The actual reification at run-time is done in <code>=>></code>, so it is essential that the <code>=>></code> and <code>Reify</code> implementation match. It would be nicer to encode the reification directly in <code>=>></code> but this fails due to a limitation in Scala&#8217;s type inferencer. Compare the unpacking and reification for <code>ColumnBase</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  implicit def unpackColumnBase[T]: ColumnBase[T] =>> T
  implicit final def reifyColumnBase[T &lt;: ColumnBase[_]]: Reify[T, T]
</pre>
</div>
</div>
<p>In both cases, we get a type like <code>A &lt;: ColumnBase[B]</code> but one operation has to capture the <code>A</code> and the other one the <code>B</code> (we need the actual sub-type of <code>ColumnBase</code> for the reification to preserve table types). There is no way (that I know of) of doing both together. Exposing the type parameter through a type alias in <code>ColumnBase</code> does not help. It allows us to write the unpacking in the desired style but <code>T#Alias</code> is always inferred as <code>Any</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  implicit def unpackColumnBase[T &lt;: ColumnBase[_]]: T =>> T#Alias
</pre>
</div>
</div>
<p>A second problem is caused by Scala issue <a href="https://issues.scala-lang.org/browse/SI-3346">SI-3346</a>: Fundep implicits do not work on implicit conversions. I have been able to work around this in some cases but in others the situation looks hopeless. As long is this issue is not resolved, some extra methods calls for explicit unpacking might be needed.</p>
<p>You can find the work in progress in ScalaQuery&#8217;s <a href="https://github.com/szeiger/scala-query/tree/nested-tuples-query2">nested-tuples-query2</a> branch.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2011/08/14/type-level-computations-with-functional-dependencies/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updating European N1 from EPF21b to FRF85b</title>
		<link>http://szeiger.de/blog/2010/06/29/updating-european-n1-from-epf21b-to-frf85b/</link>
		<comments>http://szeiger.de/blog/2010/06/29/updating-european-n1-from-epf21b-to-frf85b/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 21:33:20 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=77</guid>
		<description><![CDATA[With some help from [diablo] @ freenode I just updated my Nexus One (from the UK; running the stock EPF21b build of Android 2.1) manually to the final Froyo build FRF85b. OTA updates should be out soon but if you don&#8217;t want to wait, here&#8217;s what to do:

Get the official OTA update from http://android.clients.google.com/packages/passion/signed-passion-ota-42745.dc39ca1f.zip, rename [...]]]></description>
			<content:encoded><![CDATA[<p>With some help from [diablo] @ freenode I just updated my Nexus One (from the UK; running the stock EPF21b build of Android 2.1) manually to the final Froyo build FRF85b. OTA updates should be out soon but if you don&#8217;t want to wait, here&#8217;s what to do:</p>
<ol>
<li>Get the official OTA update from <a href="http://android.clients.google.com/packages/passion/signed-passion-ota-42745.dc39ca1f.zip">http://android.clients.google.com/packages/passion/signed-passion-ota-42745.dc39ca1f.zip</a>, rename it to <em>update.zip</em> and put it into the root folder of the SD card in your N1. The MD5 checksum should be 94f103b38c726fe17a5ff8e7676a737b.</li>
<li>Unfortunately, the standard recovery image on a European N1 will not be able to verify this update, so you need to flash the stock US image first. This requires an unlocked bootloader. If you haven&#8217;t unlocked your N1 yet, you need to do it now. <strong>This will void your warranty and wipe all data on your phone!</strong> You can find various guides on how to this on the internet, e.g. <a href="http://nexusonehacks.net/nexus-one-hacks/step-by-step-guide-on-how-to-unlock-bootloader-on-your-nexus-one/">here</a>. Be sure to wipe manually after unlocking. All data gets wiped anyway but it&#8217;s possible that Froyo won&#8217;t boot if you don&#8217;t do a manual wipe, too.</li>
<li>Now get the stock recovery image from <a href="http://fon.gs/stockrecovery">here</a> and install it (as described on that page).</li>
<li>Reboot while holding the <i>volume down</i> button and select <i>Recovery</i> from the boot menu.</li>
<li>When the screen with the android and the exclamation mark appears, press <i>volume up</i> and <i>power</i> for a moment to get into the recovery menu. You may have to try this combination several times before you get the menu.</li>
<li>Select <i>Apply SDcard:update.zip</i> and wait for the update to be installed. The phone will reboot automatically (which can take a few minutes on the first reboot).</li>
</ol>
<p><strong>Do this at your own risk! Back up all data and settings first!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2010/06/29/updating-european-n1-from-epf21b-to-frf85b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Zipper for scala.xml</title>
		<link>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/</link>
		<comments>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 00:54:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=54</guid>
		<description><![CDATA[In recent discussions on the scala-internals and scala-xml mailing lists, there were calls for a mutable, DOM-like XML model, where nodes hold references to their parent element, so that all standard XPath axes can be supported. In this post I&#8217;d like to present a different representation which is based on the immutable and persistent scala.xml [...]]]></description>
			<content:encoded><![CDATA[<p>In recent discussions on the <a href="http://www.scala-lang.org/node/199">scala-internals and scala-xml mailing lists</a>, there were calls for a mutable, DOM-like XML model, where nodes hold references to their parent element, so that all standard XPath axes can be supported. In this post I&#8217;d like to present a different representation which is based on the immutable and persistent scala.xml model, is completely immutable itself, yet allows navigation along all axes and &#8220;mutation&#8221;. It is based on the <a href="http://en.wikipedia.org/wiki/Zipper_%28data_structure%29">Zipper</a> technique which was first described by Gérard Huet in <a href="http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf">this paper [PDF]</a>.</p>
<h3>The Problem</h3>
<p>Take this piece of XML (an Eclipse .project file):</p>
<div class="wp_syntax">
<div class="code">
<pre>&lt;projectDescription>
  &lt;name>scala-query&lt;/name>
  &lt;comment>&lt;/comment>
  &lt;projects>&lt;/projects>
  &lt;buildSpec>
    &lt;buildCommand>
      &lt;name>ch.epfl.lamp.sdt.core.scalabuilder&lt;/name>
      &lt;arguments>&lt;/arguments>
    &lt;/buildCommand>
  &lt;/buildSpec>
  &lt;natures>
    &lt;nature>ch.epfl.lamp.sdt.core.scalanature&lt;/nature>
    &lt;nature>org.eclipse.jdt.core.javanature&lt;/nature>
  &lt;/natures>
&lt;/projectDescription>
</pre>
</div>
</div>
<p>Its scala.xml model looks like this:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/basic-doc.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/basic-doc.png" alt="XML model" /></a></p>
<p>(In reality, a pretty-printed document contains a bunch of additional text nodes with all the whitespace between the elements, but we can safely ignore them and work with a simplified model.)</p>
<p>Imagine you have a reference to the first &#8220;nature&#8221; element which we will call the <em>context</em>. The only other node reachable from this context is the text node below it. You cannot navigate to its parent or sibling without starting at the root element and searching for the right spot.</p>
<p>An obvious solution to this problem is to add a reference from each node to its parent but that means giving up <em>persistence</em> (the sharing of data between different versions of the tree).</p>
<p>Let&#8217;s look at the situation without the parent reference first and assume you wanted to add a third &#8220;nature&#8221; element before the two existing ones. Since you cannot modify the &#8220;natures&#8221; element in this immutable data structure, you have to make a copy of it which contains the two original children plus the new one. Now that you have a new &#8220;natures&#8221; element, you make a copy of the &#8220;projectDescription&#8221; root element above it which contains all the original children except for the &#8220;natures&#8221; element which is replaced with the new one. This gives you two separate trees (the original and the modified one) which share most of their nodes:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/shared-tree.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/shared-tree.png" alt="XML models with shared sub-trees" /></a></p>
<p>(For the &#8220;natures&#8221; element we can even reuse the list of its children because we only prepend an element at the beginning. We need to copy the list of the root element&#8217;s children though because we replace its last element.)</p>
<p>Now try the same on a model with parent references. You can create the new &#8220;nature&#8221; element and a copy of the &#8220;natures&#8221; element above it. But what about the other two &#8220;nature&#8221; elements? You cannot reuse them because they contain a reference to the original &#8220;natures&#8221; element, so you have to create copies of them which point up to their new parent. You cannot even reuse the text nodes below them because they also reference their original parents. And now that you have copied the entire &#8220;natures&#8221; sub-tree, you face the same problem with the root element. All its children point up to the old root element, so you have to copy them recursively, too. In the end you have copied the tree entirely!</p>
<h3>The Zipper</h3>
<p>We can do better with the Zipper. The basic idea is to take an immutable data structure for the actual data (in this case XML documents in scala.xml models) and add a representation for a context that allows you to navigate through the data in all directions and modify parts of it (in the same way that you modified a document in the previous section &#8212; by creating a new document that shares most of the data).</p>
<p>We start with a type for the context:</p>
<div class="wp_syntax">
<div class="code">
<pre>sealed case class NodeLoc(node: Node, path: NodePath)
</pre>
</div>
</div>
<p>It consists of the node (or sub-tree) which it wraps plus a path to the root element. A path can either be the <em>top</em> (root) of the tree or a <em>hole</em>, i.e. the location of a node without the node itself:</p>
<div class="wp_syntax">
<div class="code">
<pre>sealed trait NodePath
final case object Top extends NodePath
final case class Hole(left: List[Node],
  parent: NodeLoc, right: List[Node]) extends NodePath
</pre>
</div>
</div>
<p>For example, here is a <code>NodeLoc</code> for the first &#8220;nature&#8221; element:</p>
<p><a href="http://szeiger.de/static/a-zipper-for-scala-xml/zipper-paths.svg"><img src="http://szeiger.de/static/a-zipper-for-scala-xml/zipper-paths.png" alt="XML model with Zipper" /></a></p>
<p>You can see that the original tree (in grey and yellow) is still intact. There are two <code>NodeLoc</code>s with holes which point up to a parent <code>NodeLoc</code> and contain lists of their preceding and following siblings. Note that the list of preceding siblings is in reverse order (compared to the corresponding list of children on the parent node) so that the first node in the list is the next element to the left of the hole.</p>
<p>We can now define the basic navigation methods on <code>NodeLoc</code>. In order to move to the left sibling, we create a new <code>NodeLoc</code> which contains the head of the &#8220;left&#8221; list as its node and a copy of the current hole with the remaining nodes on the &#8220;left&#8221; list and the current node appended to the &#8220;right&#8221; list:</p>
<div class="wp_syntax">
<div class="code">
<pre>  protected def create(node: Node, path: Hole) = NodeLoc(node, path)

  final def leftOpt = path match {
    case Hole(tl :: l, p, r) =>
      Some(create(tl, Hole(l, p, node :: r)))
    case _ => None
  }
</pre>
</div>
</div>
<p>Moving to the right is symmetrical to that:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def rightOpt = path match {
    case Hole(l, p, tr :: r) =>
      Some(create(tr, Hole(node :: l, p, r)))
    case _ => None
  }
</pre>
</div>
</div>
<p>Methods <code>left</code> and <code>right</code> which return a bare <code>NodeLoc</code> or throw an exception, and <code>isFirst</code> and <code>isLast</code> to ensure that you don&#8217;t run into such an exception, can be defined in the same way. I&#8217;m using <code>Option</code> here because it will be useful later on for creating the XPath axes.</p>
<p>Moving down to a child with the specified index position is accomplished by splitting the list of the current node&#8217;s children at the correct position, removing the child node and creating a <code>Hole</code> for the other children which points up to this <code>NodeLoc</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def downOpt(idx: Int) = {
    val ch = node.child
    if(ch.isEmpty) None
    else Some(create(ch.head,
      Hole(ch.tail.take(idx).reverse.toList,
           this, ch.drop(idx+1).toList)))
  }
</pre>
</div>
</div>
<h3>Mutable Yet Immutable</h3>
<p>Replacing the node at the current location is extremely simple. We just create a <code>NodeLoc</code> with the new <code>Node</code> and the current path:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def set(n: Node) = NodeLoc(n, path)
</pre>
</div>
</div>
<p>There is no need to propagate the change up to the root element at this point. We could, for example, move through all siblings, replacing each one with a new node, all in constant time (per sibling). Parent elements are not copied until we move up and &#8220;unzip&#8221; the path into a new tree. This is done by constructing a new list of children from the current node and its left and right siblings, and creating a copy of the old parent with the new children:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def upOpt = path match {
    case h : Hole =>
      val l = h.left.reverse ++ (node :: h.right)
      Some(NodeLoc(h.parent.node match {
        case e: Elem => e.copy(child = l)
        case _: Group => new Group(l)
      }, h.parent.path))
    case _ => None
  }
</pre>
</div>
</div>
<p>This is slightly messy with scala.xml because we have to match on the type of node and copy Elem and Group nodes (the only ones which can contain children) in different ways.</p>
<p>Always recreating the parent when moving up is a waste of time when no changes have been made to the current sub-tree. We can optimize for this case with a special <code>CachedParentNodeLoc</code> which returns the original parent when moving up:</p>
<div class="wp_syntax">
<div class="code">
<pre>final class CachedParentNodeLoc(node: Node, path: Hole)
extends NodeLoc(node, path) {
  override def upOpt = Some(path.parent)
  override protected def create(node: Node, path: Hole) =
    new CachedParentNodeLoc(node, path)
}

final class CachedTopNodeLoc(node: Node) extends NodeLoc(node, Top) {
  override def upOpt = None
  override protected def create(node: Node, path: Hole) =
    new CachedParentNodeLoc(node, path)
}

object NodeLoc {
  def apply(node: Node): NodeLoc = new CachedTopNodeLoc(node)
}
</pre>
</div>
</div>
<p>Note that the <code>leftOpt</code>, <code>rightOpt</code> and <code>downOpt</code> methods use <code>create(...)</code> instead of <code>NodeLoc(...)</code>, so that navigating left, right or down from a cached location returns another cached location.</p>
<h3>The XPath Axes</h3>
<p>The <a href="http://www.w3.org/TR/xpath">XPath specification</a> defines the following axes:</p>
<blockquote>
<ul>
<li>the <code>child</code> axis contains the children of the context node</li>
<li>the <code>descendant</code> axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace<br />
nodes</li>
<li>the <code>parent</code> axis contains the parent of the context node, if there is one</li>
<li>the <code>ancestor</code> axis contains the ancestors of the context node; the ancestors of the context node consist of the parent of context node and the parent&#8217;s parent and so on; thus, the ancestor axis will always include the root node, unless the context node is the root node</li>
<li>the <code>following-sibling</code> axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the <code>following-sibling</code> axis is empty</li>
<li>the <code>preceding-sibling</code> axis contains all the preceding siblings of the context node; if the context node is an attribute node or namespace node, the <code>preceding-sibling</code><br />
axis is empty</li>
<li>the <code>following</code> axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes</li>
<li>the <code>preceding</code> axis contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes and namespace nodes</li>
<li>the <code>attribute</code> axis contains the attributes of the context node; the axis will be empty unless the context node is an element</li>
<li>the <code>namespace</code> axis contains the namespace nodes of the context node; the axis will be empty unless the context node is an element</li>
<li>the <code>self</code> axis contains just the context node itself</li>
<li>the <code>descendant-or-self</code> axis contains the context node and the descendants of the context node</li>
<li>the <code>ancestor-or-self</code> axis contains the context node and the ancestors of the context node; thus, the ancestor axis will always include the root node</li>
</ul>
</blockquote>
<p>The <em>attribute</em> and <em>namespace</em> axes do not fit well into our model because scala.xml does not represent attributes and namespace declarations as <code>Node</code> values. All other axes can be defined as methods on <code>NodeLoc</code>.</p>
<p>We will represent an axis as an <code>Iterator[NodeLoc]</code>. The following helper method creates an iterator by starting with a given value and continually applying a function to it until it returns <code>None</code>. It is similar to <code>Iterator.iterate</code> in Scala 2.8 except the latter can only create infinite iterators.</p>
<div class="wp_syntax">
<div class="code">
<pre>  private final def iterate[T](start: Option[T])(f: T => Option[T]): Iterator[T] = new Iterator[T] {
    private[this] var acc = start
    def hasNext = acc.isDefined
    def next() = acc match {
      case None => throw new NoSuchElementException("next on empty iterator");
      case Some(x) => val res = x ; acc = f(x) ; res
    }
  }
</pre>
</div>
</div>
<p>We can use it like this to create the <em>child</em> axis: Start with the first child, then go right node by node until you reach the last child:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def childAxis = iterate(downOpt(0))(_.rightOpt)
</pre>
</div>
</div>
<p>The <em>descendant-or-self</em> axis consists of the current node plus the <em>descendant-or-self</em> axes of all children:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def descendantOrSelfAxis: Iterator[NodeLoc] =
    Iterator.single(this) ++ childAxis.flatMap(_.descendantOrSelfAxis)
</pre>
</div>
</div>
<p>And the <em>descendant</em> axis consists only of the <em>descendant-or-self</em> axes of all children, without the current node:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def descendantAxis = childAxis.flatMap(_.descendantOrSelfAxis)
</pre>
</div>
</div>
<p>The <em>parent</em>, <em>ancestor-or-self</em> and <em>ancestor</em> axes work similarly:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def parentAxis = upOpt.iterator

  final def ancestorOrSelfAxis: Iterator[NodeLoc] =
    Iterator.single(this) ++ upOpt.iterator.flatMap(_.ancestorOrSelfAxis)

  final def ancestorAxis = upOpt.iterator.flatMap(_.ancestorOrSelfAxis)
</pre>
</div>
</div>
<p>The <em>following-sibling</em> and <em>preceding-sibling</em> axes are straight-forward:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def followingSiblingAxis = iterate(rightOpt)(_.rightOpt)

  final def precedingSiblingAxis = iterate(leftOpt)(_.leftOpt)
</pre>
</div>
</div>
<p>For the <em>following</em> and <em>preceding</em> axes we need to define some helper methods for moving to the following or preceding node in document order:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def followingAxis = iterate(followingOpt)(_.followingOpt)

  final def followingOpt: Option[NodeLoc] =
    downOpt(0) orElse rightOutOpt

  private final def rightOutOpt: Option[NodeLoc] =
    rightOpt orElse upOpt.flatMap(_.rightOutOpt)

  final def precedingAxis = iterate(precedingOpt)(_.precedingOpt)

  final def precedingOpt: Option[NodeLoc] =
    leftOpt.map(n => n.downLastTransitiveOpt.getOrElse(n)) orElse upOpt

  private final def downLastTransitiveOpt: Option[NodeLoc] =
    downLastOpt.flatMap(_.downLastTransitiveOpt)

  final def downLastOpt = {
    val ch = node.child
    if(ch.isEmpty) None
    else Some(NodeLoc(ch.head, Hole(ch.reverse.toList.tail, this, Nil)))
  }
</pre>
</div>
</div>
<p>And finally the trivial <em>self</em> axis:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def selfAxis = Iterator.single(this)
</pre>
</div>
</div>
<p>Methods \ and \\ for finding all child or descendant elements with a given name can be defined by filtering the <em>child</em> and <em>descendant</em> axes:</p>
<div class="wp_syntax">
<div class="code">
<pre>  final def \ (Name: String) =
    for(n @ NodeLoc(Elem(_, Name, _, _, _*), _) &lt;- childAxis) yield n

  final def \\ (Name: String) =
    for(n @ NodeLoc(Elem(_, Name, _, _, _*), _) &lt;- descendantAxis) yield n
</pre>
</div>
</div>
<p>You can find the complete source code, including more convenience methods and a short demo <a href="http://gist.github.com/264070">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/12/27/a-zipper-for-scala-xml/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>New ScalaQuery Features</title>
		<link>http://szeiger.de/blog/2009/12/21/new-scala-query-features/</link>
		<comments>http://szeiger.de/blog/2009/12/21/new-scala-query-features/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 20:15:03 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=41</guid>
		<description><![CDATA[It&#8217;s been almost 3 months since my last summary of new features in ScalaQuery. I implemented some important changes in the following weeks but I only had little time to work on ScalaQuery after my extended one-month vacation ended in October.
Build system
Builds against newer versions of Scala 2.8 have been going well thanks to sbt [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been almost 3 months since my last <a href="http://szeiger.de/blog/2009/09/27/a-scala-query-update/">summary of new features in ScalaQuery</a>. I implemented some important changes in the following weeks but I only had little time to work on ScalaQuery after my extended one-month vacation ended in October.</p>
<h3>Build system</h3>
<p>Builds against newer versions of Scala 2.8 have been going well thanks to <a href="http://code.google.com/p/simple-build-tool/wiki/0_6_Summary">sbt 0.6</a> which separates the Scala version used by the build system from the version used by the project being built. You need to use at least version 0.6.7 of the new xsbt launcher to build ScalaQuery. Other dependencies are downloaded automatically by sbt. I wrote an implementation of sbt&#8217;s test interface for <a href="http://www.junit.org/">JUnit</a> which you can find <a href="http://github.com/szeiger/junit-interface">here</a> (binaries are <a href="http://scala-tools.org/repo-releases/com/novocode/junit-interface/">here</a> on scala-tools.org). It allows you to run ScalaQuery&#8217;s JUnit test cases from sbt with the <code>test</code> command.</p>
<p>All published artifacts now contain the Scala version in the artifact ID. You can find the latest ScalaQuery snapshot in scala-tools.org&#8217;s <a href="http://scala-tools.org/repo-snapshots/">snapshots repository</a> as:</p>
<dl>
<dt>groupId:</dt>
<dd>com.novocode</dd>
<dt>artifactId:</dt>
<dd>scala-query_2.8.0.Beta1-RC4</dd>
<dt>version:</dt>
<dd>1.0.0-SNAPSHOT</dd>
<dl>
<h3>JDBC escape syntax</h3>
<p>ScalaQuery now uses <a href="http://java.sun.com/javase/6/docs/technotes/guides/jdbc/getstart/statement.html#1006519">JDBC escape syntax</a> and scalar functions where possible for better portability between DB engines:</p>
<ul>
<li>String column concatenation (with the <code>++</code> operator) and the <code>startsWith</code> and <code>endsWith</code> methods on String columns have been moved up to <code>BasicProfile</code>.</li>
<li>Escape characters for LIKE expressions are added to the queries.</li>
<li>You can add other functions to be called with the <code>{fn ...}</code> syntax through <code>SimpleScalarFunction()</code> and <code>SimpleScalarFunction.nullary()</code>.</li>
<li>Various scalar functions are supported: <code>CONVERT</code>, <code>USER</code>, <code>DATABASE</code>, <code>CURDATE</code>, <code>CURTIME</code>, <code>PI</code>, <code>MOD</code>, <code>ABS</code>, <code>CEILING</code>, <code>FLOOR</code>, <code>SIGN</code>, <code>DEGREES</code>, <code>RADIANS</code>, <code>IFNULL</code>, <code>UCASE</code>, <code>LCASE</code>, <code>LTRIM</code>, <code>RTRIM</code>.</li>
<li>Literals for <code>Date</code>, <code>Time</code> and <code>Timestamp</code> values use the JDBC escape syntax.</li>
<li>Explicit inner, left outer, right outer and full outer joins are supported. For example:</li>
</ul>
<div class="wp_syntax">
<div class="code">
<pre>  val q = for {
    Join(c,p) &lt;- Categories innerJoin Posts on (_.id is _.category)
    _ &lt;- Query orderBy p.id
  } yield p.id ~ c.id ~ c.name ~ p.title
</pre>
</div>
</div>
<h3>Complex inserts</h3>
<p>Queries and columns can now be inserted into tables (using SQL&#8217;s <code>INSERT...SELECT</code> syntax). This allows you to use scalar functions when inserting individual records and to copy data produced by a query. For example:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q2 = for(s &lt;- Src1 if s.id &lt;= 2) yield s
  Dst2.insert(q2)
</pre>
</div>
</div>
<h3>Various changes</h3>
<ul>
<li>String columns are created as VARCHAR(254) by default (except in H2 where no size is needed).</li>
<li>The operators <code>===</code> and <code>!=</code> can be used instead of is and isNot. They have the proper precedence when used with other operators like <code>&#038;&</code> and <code>||</code>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/12/21/new-scala-query-features/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A ScalaQuery Update</title>
		<link>http://szeiger.de/blog/2009/09/27/a-scala-query-update/</link>
		<comments>http://szeiger.de/blog/2009/09/27/a-scala-query-update/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 00:03:30 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=35</guid>
		<description><![CDATA[Since my last post about ScalaQuery one month ago I have wasted quite some time trying to implement the OptionMapper type (which is used to make operations on columns interoperable between base types and Option types) in a way which does not require 2^n implicit functions and 2*n+2 type parameters for operations with n operands. [...]]]></description>
			<content:encoded><![CDATA[<p>Since my <a href="http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/">last post</a> about <a href="http://github.com/szeiger/scala-query">ScalaQuery</a> one month ago I have wasted quite some time trying to implement the <code>OptionMapper</code> type (which is used to make operations on columns interoperable between base types and <code>Option</code> types) in a way which does not require 2^n implicit functions and 2*n+2 type parameters for operations with n operands. The composable <code>OptionMapper</code> itself is not hard to write (see <code>NewOptionMapper</code> <a href="http://github.com/szeiger/scala-query/blob/new-option-mapper/src/main/scala/com/novocode/squery/combinator/TypeMapper.scala">here</a>) but it requires more type fidelity than ScalaQuery offers at the moment: Either <code>Projection</code>s need to preserve the subtypes of <code>Column</code>s they are storing, or the required types have to be reconstructed from implicit values where they are needed. You can find my failed attempts in the <a href="http://github.com/szeiger/scala-query/tree/projection-types">projection-types</a> and <a href="http://github.com/szeiger/scala-query/tree/new-option-mapper">new-option-mapper</a> branches. The first one might actually work once <a href="https://lampsvn.epfl.ch/trac/scala/ticket/2346">Scala bug #2346</a> is resolved. The second one would require a much more powerful type inferencer. For now, I just added an <code>OptionMapper3</code> for 3 parameters (which is needed by the <code>BETWEEN</code> operator).</p>
<p>There are also some interesting new features in ScalaQuery:</p>
<h3>Mapped Projections</h3>
<p>You can now create a bidirectional mapping of a <code>Projection</code> with the &lt;> operator. This is especially useful for a table&#8217;s &#8220;*&#8221; projection. The &lt;> operator takes a function from the projection tuple type <em>T</em> (either as a tuple or as multiple parameters) to the mapped type <em>R</em>, and a function from <em>R</em> back to <em>Some[T]</em>. The asymmetry with the unnecessary <em>Some</em> wrapper is deliberate, matching exactly the <code>apply</code> und <code>unapply</code> methods of a <em>case class</em>. For example, here is a simple <em>User</em> class and table:</p>
<div class="wp_syntax">
<div class="code">
<pre>  case class User(first: String, last: String)

  object Users extends Table[<b>User</b>]("users") {
    def first = column[String]("first", O NotNull)
    def last = column[String]("last", O NotNull)
    def * = first ~ last <b>&lt;> (User, User.unapply _)</b>
  }
</pre>
</div>
</div>
<p>Whenever you perform an insert, update or query directly on such a table (instead of a projection of individual columns), you can use the mapped type:</p>
<div class="wp_syntax">
<div class="code">
<pre>  Users.insert(User("Stefan", "Zeiger"))
  val someUsers: List[User] = Users.where(_.first startsWith "S").list
</pre>
</div>
</div>
<h3>Finders</h3>
<p>The new convenience method <code>Table.createFinderBy</code> allows you to create a simple <em>finder</em> query template which selects values from a table by matching on a single column, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val findUserByFirstName = Users.createFinderBy(_.first)
</pre>
</div>
</div>
<p>This is equivalent to:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val findUserByFirstName = for {
    first <- Parameters[String]
    u <- Users if u.first is first
  } yield u
</pre>
</div>
</div>
<h3>Updatable ResultSets</h3>
<p>JDBC allows you to modify a <code>ResultSet</code> which was created with the <code>CONCUR_UPDATABLE</code> result set concurrency. ScalaQuery now offers a high-level interface to this functionality with the <code>mutate</code> method, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q1 = for(u <- Users if u.last.is("Simpson") || u.last.is("Bouvier")) yield u
  q1.mutate { m =>
    if(m()._3 == "Bouvier") m() = m().copy(_3 = "Simpson")
    else if(m()._2 == "Homer") m.delete()
    else if(m()._2 == "Bart") m.insert((None, "Lisa", "Simpson"))
  }
</pre>
</div>
</div>
<h3>Statement Options</h3>
<p>The new <code>ResultSetType</code>, <code>ResultSetConcurrency</code> and <code>ResultSetHoldability</code> classes allow you to request the corresponding JDBC options when ScalaQuery creates a <code>PreparedStatement</code>, e.g.:</p>
<div class="wp_syntax">
<div class="code">
<pre>  myDB withSession {
    ResultSetType.ScrollInsensitive {
      <em>// All database calls in this block use TYPE_SCROLL_INSENSITIVE</em>
    }
  }
</pre>
</div>
</div>
<p>Or if you don't want to use the implicit <code>threadLocalSession</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre>  myDB withSession { s1 =>
    ResultSetType.ScrollInsensitive(s1) { s2 =>
      <em>// All database calls on s2 use TYPE_SCROLL_INSENSITIVE</em>
    }
  }
</pre>
</div>
</div>
<p>The default for all three options is <code>Auto</code> which gives you values suitable for the invoker method you are calling (e.g. <code>ResultSetConcurrency.Updatable</code> for <code>mutate</code> and <code>ResultSetConcurrency.ReadOnly</code> for all other methods).</p>
<h3>Build System</h3>
<p>I have rewritten most of the test classes as proper unit tests (using <a href="http://junit.org/">JUnit</a> 4) with assertions. I'd like to investigate <a href="http://www.artima.com/scalatest/">ScalaTest</a> and <a href="http://code.google.com/p/specs/">specs</a> further (possibly together with <a href="http://code.google.com/p/scalacheck/">ScalaCheck</a>) in the future to replace JUnit but the binary incompatibilities between different <a href="http://www.scala-lang.org/node/212/">Scala 2.8 snapshot builds</a> would only complicate the build process right now. If you build with <a href="http://code.google.com/p/simple-build-tool/">sbt</a>, you still need to download a suitable Scala 2.8 snapshot yourself and set the paths in the properties because the build is done in forking mode. This also means that you cannot use the "test" command to run the unit tests from sbt at the moment.</p>
<p><a href="http://www.h2database.com/">H2</a> and JUnit are declared as test dependencies in the sbt project definition and the Eclipse build path refers to the local copies in <em>lib_managed</em>. If you want to build ScalaQuery with Eclipse (as I usually do), just run "<code>sbt update</code>" once to pull in the required JARs.</p>
<p>I am using <a href="http://freemarker.org/">FreeMarker</a> templates and the <a href="http://fmpp.sourceforge.net/">FMPP</a> tool to create repetitive code for the <code>Projection</code> and <code>Parameters</code> classes. This is not yet integrated into the sbt build process. You can use the supplied Eclipse launcher instead (after downloading FMPP and pointing the launcher to the correct path). The generated sources are checked in, so you only need to do this if you want to change the templates and rebuild the Scala sources.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/09/27/a-scala-query-update/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Profiles, Drivers &amp; More</title>
		<link>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/</link>
		<comments>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 15:07:47 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=34</guid>
		<description><![CDATA[It&#8217;s been three weeks already since my last post about ScalaQuery so I&#8217;d like to provide an update on some recent changes.
Profiles &#038; Drivers
The biggest news is that ScalaQuery now supports database engine-specific features. Feature sets can be bundled in profiles which are then implemented by drivers (Of course, a driver can also add driver-specific [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been three weeks already since my <a href="http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/">last</a> post about <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> so I&#8217;d like to provide an update on some recent changes.</p>
<h3>Profiles &#038; Drivers</h3>
<p>The biggest news is that ScalaQuery now supports database engine-specific features. Feature sets can be bundled in <em>profiles</em> which are then implemented by <em>drivers</em> (Of course, a driver can also add driver-specific features which are not part of a profile). All previously existing features are now part of the <code>BasicProfile</code> which should work on all SQL databases with little or no customization. The <code>BasicProfile</code> provides:</p>
<ul>
<li>An object called <code>Implicit</code> with all implicit conversions which are required to use the profile&#8217;s features (including an implicit value of type <code>BasicProfile</code> which points to the driver implementing the profile).</li>
<li>Some methods for creating various statements and query templates. They have default implementations provided by the profile but can be overridden by other profiles or drivers. You do not usually call them directly.</li>
<li>A number of <code>TypeMapperDelegate</code> objects for the basic types supported by the profile. You still use <code>TypeMapper</code>s defined outside the profile but they do not implement the actual type mapping any more. Instead they ask the profile for a delegate implementation. (Alternatively, I could have moved the existing <code>TypeMapper</code> objects entirely into the profile but mappers for some basic types like <code>Int</code> and <code>Boolean</code> are used internally at many places. They would need to be threaded through several methods and constructors, thus complicating the implementation unnecessarily.)</li>
</ul>
<p>The <code>BasicProfile</code> is implemented by the <code>BasicDriver</code>.</p>
<p>There is also a new <code>ExtendedProfile</code> for features which are expected to be available in every commonly used SQL database system, albeit with a non-standard syntax. It currently provides string concatenation with the <code>++</code> operator (plus <code>startsWith</code> and <code>endsWith</code> methods which can be implemented with <code>LIKE</code> and string concatenation) and the <code>take</code> and <code>drop</code> methods needed for pagination.</p>
<p>If you only need to support a single database engine in your application, you can forget about profiles and just import a specific driver&#8217;s implicit conversions. Supporting multiple drivers is almost as simple: Give your DAO class (or whatever you have in your application design) a parameter of the required profile type and import this profile&#8217;s implicits. You can then call it with any driver which implements the profile. For example:</p>
<div class="wp_syntax">
<div class="code">
<pre>  def test(profile: ExtendedProfile) {
    import profile.Implicit._
    println("Using driver: "+profile.getClass.getName)
    val q1 = Users.where(_.name startsWith "quote ' and backslash \\").take(5)
    println(q1.selectStatement)
    val q2 = Users.where(_.name startsWith "St".bind).drop(10).take(5)
    println(q2.selectStatement)
    val q3 = Query(42 ~ "foo")
    println(q3.selectStatement)
    println
  }

  def main(args: Array[String]) {
    test(H2Driver)
    test(OracleDriver)
    test(MySQLDriver)
  }
</pre>
</div>
</div>
<p>This prints the different statements required for <a href="http://www.h2database.com/">H2</a>, <a href="http://www.oracle.com/database/index.html">Oracle</a> and <a href="http://www.mysql.com/">MySQL</a>:</p>
<div class="wp_syntax">
<div class="code">
<pre>Using driver: com.novocode.squery.combinator.extended.H2Driver$
SELECT t1.name FROM users t1 WHERE (t1.name like 'quote '' and backslash \%') LIMIT 5
SELECT t1.name FROM users t1 WHERE (t1.name like (? || '%')) LIMIT 5 OFFSET 10
SELECT 42,'foo'

Using driver: com.novocode.squery.combinator.extended.OracleDriver$
SELECT * FROM (SELECT t1.name FROM users t1 WHERE (t1.name like 'quote '' and backslash \%')) WHERE ROWNUM &lt;= 5
SELECT * FROM (SELECT t0.*, ROWNUM ROWNUM_O FROM (t1.name,ROWNUM ROWNUM_I FROM users t1 WHERE (t1.name like (? || '%'))) t0) WHERE ROWNUM_O BETWEEN (1+10) AND (10+5) ORDER BY ROWNUM_I
SELECT 42,'foo' FROM DUAL

Using driver: com.novocode.squery.combinator.extended.MySQLDriver$
SELECT t1.name FROM users t1 WHERE (t1.name like 'quote \' and backslash \\%') LIMIT 5
SELECT t1.name FROM users t1 WHERE (t1.name like concat(?,'%')) LIMIT 10,5
SELECT 42,'foo' FROM DUAL
</pre>
</div>
</div>
<h3>Updates</h3>
<p>You can now take a simple query (returning only named columns from a single table; no modifiers except <code>WHERE</code> restrictions) and call it as an <code>UPDATE</code> statement:</p>
<div class="wp_syntax">
<div class="code">
<pre>  val q = for(u &lt;- Users if u.id is 42) yield u.first ~ u.last
  q.update("foo", "bar")
</pre>
</div>
</div>
<h3>Filtering</h3>
<p>You may already have noticed in my previous post that the <code>Query</code> class now has a <code>filter</code> method which works like <code>where</code> for functions returning a <code>Column[Boolean]</code> or <code>Column[Option[Boolean]]</code>, so you can use Scala&#8217;s standard <em>if</em> clauses in for-comprehensions on queries. Unlike <code>where</code>, <code>filter</code> also accepts functions returning a plain <code>Boolean</code> value to enable the use of refutable patterns in queries (e.g. when destructuring a <code>Join</code>).</p>
<h3>Invokers</h3>
<p>Result type remapping and parameter application are now available for all <code>Invoker</code>s. These features were pushed down from the statement invokers for monadic queries into the invoker framework. Query templates are parameterized invokers now. They can be applied like any other invoker.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/08/27/profiles-drivers-and-more/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Removing Libraries and HomeGroup icons from the Windows 7 desktop</title>
		<link>http://szeiger.de/blog/2009/08/11/removing-libraries-and-homegroup-icons-from-the-windows-7-desktop/</link>
		<comments>http://szeiger.de/blog/2009/08/11/removing-libraries-and-homegroup-icons-from-the-windows-7-desktop/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 16:56:25 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=33</guid>
		<description><![CDATA[After importing the settings from my XP installation into the freshly installed copy of Windows 7 RTM with the included Windows Easy Transfer tool, I suddenly had two additional icons on my desktop: HomeGroup and Libraries. And there is no obvious way of removing them. You cannot just delete them and they are not listed [...]]]></description>
			<content:encoded><![CDATA[<p>After importing the settings from my XP installation into the freshly installed copy of Windows 7 RTM with the included Windows Easy Transfer tool, I suddenly had two additional icons on my desktop: HomeGroup and Libraries. And there is no obvious way of removing them. You cannot just delete them and they are not listed in the &#8220;Desktop Icon Settings&#8221; dialog, either.</p>
<p>A quick Google search (maybe I should have used Bing for this&#8230;) turned up one suggested solution which is mentioned in several forums: Use RegEdit to delete {B4FB3F98-C1EA-428d-A78A-D1F5659CBA93} and {031E4825-7B94-4dc3-B131-E946B44C8DD5} from HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace.</p>
<p>Well, don&#8217;t! This also removes HomeGroup and Libraries from the Explorer tree (and maybe other places). There must be a better solution. After all, the icons were not present on the desktop initially but they did show up in Explorer.</p>
<p>Under the assumption that all desktop icons (whether they are listed in &#8220;Desktop Icon Settings&#8221; or not) are managed in one place in the registry, I fired up RegEdit and Desktop Icon Settings, exported the registry, enabled the Control Panel icon on the desktop and then exported again. A diff of the two registry dumps showed one obvious change:</p>
<p>In HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\ClassicStartMenu a DWord entry named {5399E694-6CE5-4D6C-8FCE-1D8870FDCBA0} was changed from 1 to 0. The same entry was added with value 0 to HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel.</p>
<p>I checked the same folders under HKLM. They contain more entries, including {B4FB3F98-C1EA-428d-A78A-D1F5659CBA93} and {031E4825-7B94-4dc3-B131-E946B44C8DD5}. Hey, those IDs look familiar! I created copies of them with value 1 unter HKCU\&#8230;\ClassicStartMenu and HKCU\&#8230;\NewStartPanel and voilà, the icons were gone from the desktop.</p>
<p>I hope you don&#8217;t have to waste as much time on this problem as I did. Disclaimer: Changing the registry can mess up your system. Do this at your own risk!</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/08/11/removing-libraries-and-homegroup-icons-from-the-windows-7-desktop/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Efficient Parameterized Queries in ScalaQuery</title>
		<link>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/</link>
		<comments>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 17:22:04 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=32</guid>
		<description><![CDATA[One question about ScalaQuery which keeps coming up is that of perfomance. The question is usually in the form &#8220;How does it compare to JDBC?&#8221; but that&#8217;s like comparing apples and, well, apple trees. After all, ScalaQuery is a layer on top of JDBC which provides mainly two things:

A nicer, more Scala-like way of handling [...]]]></description>
			<content:encoded><![CDATA[<p>One question about <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> which keeps coming up is that of perfomance. The question is usually in the form &#8220;How does it compare to <a href="http://java.sun.com/javase/technologies/database/">JDBC</a>?&#8221; but that&#8217;s like comparing apples and, well, apple trees. After all, ScalaQuery is a layer on top of JDBC which provides mainly two things:</p>
<ul>
<li>A nicer, more Scala-like way of handling database connections, performing queries and reading result sets. This is not optional when you access a database in your application. You will wrap SQL statement execution and result set reading in some way or another to abstract from the low-level JDBC API. Anyway, the overhead here is quite low.</li>
<li>A way of composing queries with an internal DSL based on a query monad and combinators. That&#8217;s the part I want to talk about in this post.</li>
</ul>
<p>If you want to optimize the query generation away, you can always fall back to the <code>StaticQuery</code> and <code>DynamicQuery</code> classes. These work a bit like <a href="http://ibatis.apache.org/">iBATIS</a>, except your SQL code is embedded directly in your Scala code and not in some XML files. But you&#8217;re still writing SQL! That&#8217;s nice for the special cases which are not covered by the combinator queries and which do not need to be composable but it&#8217;s probably not the reason why you want to use ScalaQuery in the first place.</p>
<p>When you&#8217;re constructing a query in the query monad, you normally have some variables which are used in the query like this:</p>
<div class="wp_syntax">
<div class="code">
<pre>def userNameByID(id: Int) =
  for(u &lt;- Users if u.id is id) yield u.first
</pre>
</div>
</div>
<p>This would insert the user ID directly into the SQL statement, thus requiring a new statement to be generated by ScalaQuery and parsed and optimized by the database server (which can be very expensive) for every invocation of the query. We can improve this by using a <em>bind variable</em> for the user ID:</p>
<div class="wp_syntax">
<div class="code">
<pre>def userNameByID(id: Int) =
  for(u &lt;- Users if u.id is id.bind) yield u.first
</pre>
</div>
</div>
<p>Now the generated SQL statement is always the same (e.g. &#8220;<code>SELECT t1.first FROM users t1 WHERE (t1.id=?)</code>&#8220;), so the database server needs to calculate an execution plan for it only once and can reuse it on subsequent invocations. But the query is still constructed as a tree of dozens of objects and compiled to the same SQL statement every time you call <code>userNameByID</code>.</p>
<p>This can be remedied with <em>query templates</em>, a recent addition to ScalaQuery:</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByID = for {
  id &lt;- Parameters[Int]
  u &lt;- Users if u.id is id
} yield u.first
</pre>
</div>
</div>
<p>If you recall how for-comprehensions are desugared, this gets translated to <code>Parameters[Int].apply(...).flatMap(id => ...)</code>. The <code>apply</code> method on the <code>Parameters</code> object takes an implicit <code>TypeMapper</code> for each parameter type you specify and creates a <code>Parameters</code> instance. The <code>flatMap</code> method on <code>Parameters</code> takes a function which creates a <code>Query</code> and returns a <code>QueryTemplate</code> for it:</p>
<div class="wp_syntax">
<div class="code">
<pre>final class QueryTemplate[P, R](query: Query[ColumnBase[R]]) {
  def apply(param: P) = new AppliedQueryTemplate(built, param, query.value)
  lazy val built = QueryBuilder.buildSelect(query, NamingContext())
}

final class Parameters[P, C](c: C) {
  def flatMap[F](f: C => Query[ColumnBase[F]]): QueryTemplate[P, F] =
    new QueryTemplate[P, F](f(c))
  ...
}

object Parameters {
  def apply[P1](implicit tm1: TypeMapper[P1]) =
    new Parameters[P1, Column[P1]](new ParameterColumn(-1, tm1))
  ...
}
</pre>
</div>
</div>
<p>Note that, unlike <code>Query</code>, neither <code>Parameters</code> nor <code>QueryTemplate</code> is a monad. You cannot compose multiple parameter lists or query templates this way but by providing a suitable <code>flatMap</code> method, the parameters can be used as the first generator in a for-comprehension which otherwise operates in the <code>Query</code> monad.</p>
<p>Either one of the <code>userNameByID</code> functions/methods defined above can be used in the same way:</p>
<div class="wp_syntax">
<div class="code">
<pre>for(t &lt;- userNameByID(3)) println(t)
</pre>
</div>
</div>
<p>When you apply the parameters to a <code>QueryTemplate</code>, you get an <code>AppliedQueryTemplate</code> which can be lifted to a suitable <code>Invoker</code> by an implicit conversion (just like a <code>Query</code>). The first time you do this, the SQL code gets generated and then cached in the <code>QueryTemplate</code> for further applications.</p>
<p>If you specify more than one type parameter, the <code>Parameters</code> generator gives you a <code>Projection</code> instead of a single <code>Column</code>. It can be unpacked either with the extractor on the &#8220;<code>~</code>&#8221; object:</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByIDRangeAndProduct = for {
  min ~ max ~ product &lt;- Parameters[Int, Int, String]
  u &lt;- Users if u.id >= min &#038;&#038;
    u.id &lt;= max &#038;&#038;
    Orders.where(o => (u.id is o.userID) &#038;&#038; (o.product is product)).exists
} yield u.first
</pre>
</div>
</div>
<p>&#8230;or with the <code>Projection</code> extractor (which is slightly more efficient but not as nice to read):</p>
<div class="wp_syntax">
<div class="code">
<pre>val userNameByIDRangeAndProduct = for {
  Projection(min, max, product) &lt;- Parameters[Int, Int, String]
  u &lt;- Users if u.id >= min &#038;&#038;
    u.id &lt;= max &#038;&#038;
    Orders.where(o => (u.id is o.userID) &#038;&#038; (o.product is product)).exists
} yield u.first
</pre>
</div>
</div>
<p>ScalaQuery&#8217;s invoker framework provides methods for invoking queries with parameters but these are currently used by the simple queries only. I expect to integrate query templates with this system in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/08/06/efficient-parameterized-queries-in-scala-query/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ScalaQuery for different Scala versions</title>
		<link>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/</link>
		<comments>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 20:12:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=31</guid>
		<description><![CDATA[I have just created a new scala-2.7 branch for ScalaQuery. My original plan was to target only Scala 2.8 but since I&#8217;ve made lots of progress during the last few weeks and I&#8217;ve seen increased interest in ScalaQuery, I tried to build it with 2.7.5.
I had to change the semantics of SimpleFunction and SimpleBinaryOperator for [...]]]></description>
			<content:encoded><![CDATA[<p>I have just created a new <a href="http://github.com/szeiger/scala-query/tree/scala-2.7">scala-2.7</a> branch for <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a>. My original plan was to target only Scala 2.8 but since I&#8217;ve made lots of progress during the last few weeks and I&#8217;ve seen increased interest in ScalaQuery, I tried to build it with 2.7.5.</p>
<p>I had to change the semantics of <code>SimpleFunction</code> and <code>SimpleBinaryOperator</code> for 2.7 but I prefer the new version anyway, so it went into the main line. The code on the scala-2.7 branch is currently identical to the master branch, except for the test classes which are different in two regards:</p>
<ul>
<li>Although the Scala Language Specification mandates that the part left to the &#8220;<code>&lt;-</code>&#8221; in a for comprehension is a pattern, the wildcard pattern &#8220;<code>_</code>&#8221; does not work in 2.7. I have changed it to a dummy variable named &#8220;<code>__</code>&#8220;.</li>
<li>The type inferencer in 2.7 cannot infer the correct type for the implicit <code>OptionMapper</code> objects. <code>OptionMapper[_,_,_,_]</code> has four type parameters, the last one being used for the return type of functions which use the mapper, so it is not yet known when looking for an implicit mapper and gets inferred as <code>Nothing</code>. Scala 2.8 apparently knows that this type parameter is undetermined, finds the single matching implicit object for the other three parameters and then fills in the fourth. The type-correct interoperability of option and non-option types in ScalaQuery relies heavily on the improved type inferencer and I don&#8217;t see any way of making it work nicely with 2.7. The work-around is to add type annotations to the boolean operators, e.g. <code>a &#038;&#038; b</code> might become <code>a.&#038;&[Boolean,Option[Boolean]](b)</code>. Yuck!</li>
</ul>
<p>I&#8217;m still focused on Scala 2.8 as a target platform and will probably not spend much time integrating new features into the scala-2.7 branch but contributions are always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/22/scala-query-for-different-scala-versions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implicits on Implicits</title>
		<link>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/</link>
		<comments>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 22:51:55 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[ScalaQuery]]></category>

		<guid isPermaLink="false">http://szeiger.de/?p=30</guid>
		<description><![CDATA[I have recently made a change to ScalaQuery which enables the use of any type for a column without needing specific Column classes and factory methods on Table for it. For this I used an approach which I had not considered earlier and which I wasn&#8217;t even sure would work.
Scala (at least in version 2.7) [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently made a change to <a href="http://github.com/szeiger/scala-query/tree/master">ScalaQuery</a> which enables the use of <em>any</em> type for a column without needing specific <code>Column</code> classes and factory methods on <code>Table</code> for it. For this I used an approach which I had not considered earlier and which I wasn&#8217;t even sure would work.</p>
<p>Scala (at least in version 2.7) only considers single function calls for implicit conversions. Take the following definitions for example:</p>
<div class="wp_syntax">
<div class="code">
<pre>class A
class B(a: A)
class C(b: B)

implicit def aToB(a: A) = new B(a)
implicit def bToC(b: B) = new C(b)

val a = new A

def useB(b: B) = ...
def useC(c: C) = ...
</pre>
</div>
</div>
<p>You can call useB(a) because the compiler finds the implicit conversion aToB(a) but you cannot call useC(a) &#8212; the compiler does not try the chained call bToC(aToB(c)).</p>
<p>But this does not mean that an implicit conversion may not rely on an implicit value! The following works just fine:</p>
<div class="wp_syntax">
<div class="code">
<pre>trait Column[T]
class ConstColumn[T](value: T, tm: TypeMapper[T]) extends Column[T]

implicit def valueToColumn[T](value: T)(implicit tm: TypeMapper[T]) =
  new ConstColumn(value, tm)

trait TypeMapper[T]
implicit object IntTypeMapper extends TypeMapper[Int]

val c1: Column[_] = 42
</pre>
</div>
</div>
<p>The Scala compiler recognizes that <code>valueToColumn[Int]</code> provides the desired conversion from <code>Int</code> to <code>Column[_]</code> and then looks for the required implicit <code>TypeMapper[Int]</code> value, which is available through the <code>implicit IntTypeMapper</code> object.</p>
<p>In ScalaQuery, the real <code>TypeMapper</code> contains only a few methods and there are predefined TypeMappers for most of the basic types used by JDBC. That&#8217;s nice because it removes some redundancy from my code base but the better news is that it allows you to write your own TypeMappers. For example, if you wanted to get the <code>java.lang.Integer</code> columns back which I <a href="http://szeiger.de/blog/2009/06/20/dealing-with-nulls-in-scalaquery/">removed</a> in favor of <code>Int</code> and <code>Option[Int]</code>, you could add this implicit object to your code:</p>
<div class="wp_syntax">
<div class="code">
<pre>implicit object IntegerTypeMapper extends TypeMapper[java.lang.Integer] {
  def zero = null
  def sqlType = java.sql.Types.INTEGER
  def setValue(v: java.lang.Integer, p: PositionedParameters) =
    if(v eq null) p.setIntOption(None) else p.setIntOption(Some(v.intValue))
  def setOption(v: Option[java.lang.Integer], p: PositionedParameters) = v match {
    case Some(null) => p.setIntOption(None)
    case Some(i) => p.setIntOption(Some(i.intValue))
    case None => p.setIntOption(None)
  }
  def nextValue(r: PositionedResult) = r.nextIntOption match {
    case None => null
    case Some(i) => java.lang.Integer.valueOf(i)
  }
}
</pre>
</div>
</div>
<p>The same should work for any database engine- or domain-specific types.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2009/07/17/implicits-on-implicits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
