<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>MettaProgramming &#187; strings</title>
	<atom:link href="http://mettadore.com/tag/strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://mettadore.com</link>
	<description>Thoughts on Software and Technology</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:39:26 +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>Scala and the mystery of the quotes</title>
		<link>http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/</link>
		<comments>http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 17:48:49 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://mettadore.com/?p=10</guid>
		<description><![CDATA[As a kid, I loved the Hardy Boys mysteries, they always had interesting mysteries and plots that were just simple enough that you could figure them out at just the right time. Sometimes, programming is like a mystery, something happens unexpectedly, you hunt down clues, solve the mystery, and find that it leads to deeper [...]]]></description>
			<content:encoded><![CDATA[<p>As a kid, I loved the Hardy Boys mysteries, they always had interesting mysteries and plots that were just simple enough that you could figure them out at just the right time. Sometimes, programming is like a mystery, something happens unexpectedly, you hunt down clues, solve the mystery, and find that it leads to deeper mysteries.</p>
<p>A while ago, I started exploring Scala and wrote a post called Head Smacking in Scala on <a href="http://positivelyglorious.com">Positively Glorious!</a> (<a href="http://mettadore.com/scala/head-smacking-in-scala-xml-parsing/">Republished here</a> for convenience). Yesterday I found another head smacking problem that taught me a few things.<span id="more-10"></span></p>
<p>Recall that I was creating an XML parser to pull metadata and results from USGS Stream Gauge sites.<sup><a href="http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/#footnote_0_10" id="identifier_0_10" class="footnote-link footnote-identifier-link" title="See the original post for the code">1</a></sup> Yesterday, I decided to explore some of Scala&#8217;s DBC abilities so that I could populate my local database with the results. I added the following to my application (cribbed from <a href="http://la.scala.la/post/110327928/baby-steps-in-scala-dbc-and-postgresql">La.Scala.La</a>):</p>
<pre>import scala.dbc._
import scala.dbc.Syntax._
import scala.dbc.syntax.Statement._
import java.net.URI

object PgVendor extends Vendor {
  val uri = new URI("jdbc:postgresql://localhost:5432/dbname")
  val user = "dbuser"
  val pass = "dbpass"

  val retainedConnections = 5
  val nativeDriverClass = Class.forName("org.postgresql.Driver")
  val urlProtocolString = "jdbc:postgresql:"
}

object Runner extends Application {
  val db = new Database(PgVendor)

  val rows = db.executeStatement {
    select fields ("code" of characterVarying(20)) from ("site") where ("responsible_profile_id = 4")
  }
  for (val r &lt;- rows;
       val f &lt;- r.fields) {
    try {
      val str = f.content.sqlString
      val Station = new UsgsWaterQualityStationParser(str)
      println(Station.getMetaData)
    }
  }

  db.close
}</pre>
<p>The documentation on the scala.dbc library is woefully lacking, but I managed to pick up some information using a scala wiki detailing the <a href="http://scala.sygneca.com/libs/dbc">seemingly orphaned scala.dbc2 library</a>. It seemed to work, and gave me enough information to build a proper executeStatement.</p>
<p>The problem was that it didn&#8217;t work.</p>
<p>After some Hardy Boy-like investigation, I discovered a very interesting problem that I never expected.<sup><a href="http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/#footnote_1_10" id="identifier_1_10" class="footnote-link footnote-identifier-link" title="That&amp;#8217;s a great euphemism for &amp;#8220;I screwed up and created a bug&amp;#8221;   ">2</a></sup> It&#8217;s related to the sqlString&#8217;s return value, and has something to do with how Scala deals with double vs. single quotes.</p>
<p>Line 25 of the code shows the creation of a value using sqlString(), which, according to what documentation I can find, returns a Java String object. Because UsgsWaterQualityStationParser takes a String in the constructor, everything should work out. The problem, the string returned from sqlString() contains single quotes. Thus, the URL that gets sent to the USGS website is something like:</p>
<pre>http://qwwebservices.usgs.gov/Station/search?siteid=USGS-'12345678'</pre>
<p>instead of what it should properly be, which is:</p>
<pre>http://qwwebservices.usgs.gov/Station/search?siteid=USGS-12345678</pre>
<p>It&#8217;s a bit strange to me that sqlString would return a String value containing quotes, but I expect it&#8217;s because of something that I&#8217;m misunderstanding about the scala.dbc library. </p>
<p>As a bit of a hack, I just used String&#8217;s replace method:</p>
<pre>    try {
      val str = f.content.sqlString.replace("'","")
      val Station = new UsgsWaterQualityStationParser(str)
      println(Station.getMetaData)
    }</pre>
<p>Which worked. Running this<sup><a href="http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/#footnote_2_10" id="identifier_2_10" class="footnote-link footnote-identifier-link" title="Not in full, since I didn&amp;#8217;t want to create a DOS attack on the USGS&amp;#8217;s REST server with 21,000 requests!">3</a></sup> I was able to print out the metadata for the sites that I have in our database that are under the responsibility of the USGS. This solved the problem of <em>getting</em> the data, even if it left open the mystery of the quotes for a later time.</p>
<p>All that&#8217;s left now is to create the query such that only sites without metadata will be used (allowing me to do, say 50 sites at a time to limit server requests), and create insert statements to load the metadata into my database. The result, all the information I could want about each USGS site, in the USGS&#8217;s own format.</p>
<h3>Coda</h3>
<p>Despite the learning curve, I&#8217;m still having fun with Scala. The issues I have with type safety (I&#8217;m a Pythonic Duck Typer) and trying to wrap my head around Scala&#8217;s syntax and Java connectivity are a small price to pay for the power, ease and flexibility of the language.</p>
<ol class="footnotes"><li id="footnote_0_10" class="footnote">See the <a href="http://mettadore.com/scala/head-smacking-in-scala-xml-parsing/">original post</a> for the code</li><li id="footnote_1_10" class="footnote">That&#8217;s a great euphemism for &#8220;I screwed up and created a bug&#8221; <img src='http://mettadore.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </li><li id="footnote_2_10" class="footnote">Not in full, since I didn&#8217;t want to create a DOS attack on the USGS&#8217;s REST server with 21,000 requests!</li></ol>]]></content:encoded>
			<wfw:commentRss>http://mettadore.com/scala/scala-and-the-mystery-of-the-quotes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

