<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: query code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 03:29:06 GMT</pubDate>
    <description>DZone Snippets: query code</description>
    <item>
      <title>Adding helpful error messages to your Ruby code</title>
      <link>http://snippets.dzone.com/posts/show/5446</link>
      <description>This Ruby code raises an error if the XPath query fails because the attribute being queried did not exist for the given element. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def map_pattr(node, fieldx, valuex)&lt;br /&gt;    begin&lt;br /&gt;    parameter = node.root.elements["parameter[@field='#{fieldx}']"]&lt;br /&gt;    parameter.add_attribute('value', valuex)&lt;br /&gt;    parameter&lt;br /&gt;    &lt;br /&gt;    rescue&lt;br /&gt;      puts 'feedpopulated.rb: map_attr() the field ' + fieldx + ' was not found in params.'&lt;br /&gt;      raise&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Notice that a raise statement is used to ensure that the system error message is raised and any further code execution is halted.&lt;br /&gt;&lt;br /&gt;Without adding a customized helpful message I would be left scratching my head trying to work out what the following system error message meant.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;./feedpopulated.rb:27:in `map_pattr': undefined method `add_attribute' for nil:NilClass (NoMethodError)&lt;br /&gt;     from ./feedpopulated.rb:51:in `create_record'&lt;br /&gt;     from ./feedpopulated.rb:49:in `each'&lt;br /&gt;     from ./feedpopulated.rb:49:in `create_record'&lt;br /&gt;     from ./recordx.rb:91:in `call_create'&lt;br /&gt;     from ./s3fileuploader_handler.rb:14:in `call'&lt;br /&gt;     from ./s3fileuploader_handler.rb:40:in `invoke'&lt;br /&gt;     from ./uploadtwitteraudio.rb:22:in `initialize'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from ./uploadtwitteraudio.rb:18:in `initialize'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72:in `new'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide - Exceptions, Catch, and Throw&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Wed, 30 Apr 2008 11:40:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5446</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Excel : Make a query on a Oracle database and return the result (useful for sheet formulas)</title>
      <link>http://snippets.dzone.com/posts/show/4518</link>
      <description>// This should be pasted in a module of the workbook&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Function ORAQUERY(strHost As String, strDatabase As String, strSQL As String, strUser As String, strPassword As String)&lt;br /&gt;  Dim strConOracle, oConOracle, oRsOracle&lt;br /&gt;  Dim StrResult As String&lt;br /&gt;  &lt;br /&gt;  StrResult = ""&lt;br /&gt;  &lt;br /&gt;  strConOracle = "Driver={Microsoft ODBC for Oracle}; " &amp; _&lt;br /&gt;         "CONNECTSTRING=(DESCRIPTION=" &amp; _&lt;br /&gt;         "(ADDRESS=(PROTOCOL=TCP)" &amp; _&lt;br /&gt;         "(HOST=" &amp; strHost &amp; ")(PORT=1521))" &amp; _&lt;br /&gt;         "(CONNECT_DATA=(SERVICE_NAME=" &amp; strDatabase &amp; "))); uid=" &amp; strUser &amp; " ;pwd=" &amp; strPassword &amp; ";"&lt;br /&gt;  Set oConOracle = CreateObject("ADODB.Connection")&lt;br /&gt;  Set oRsOracle = CreateObject("ADODB.Recordset")&lt;br /&gt;  oConOracle.Open strConOracle&lt;br /&gt;  Set oRsOracle = oConOracle.Execute(strSQL)&lt;br /&gt;  Do While Not oRsOracle.EOF&lt;br /&gt;      If StrResult &lt;&gt; "" Then&lt;br /&gt;        StrResult = StrResult &amp; Chr(10) &amp; oRsOracle.Fields(0).Value&lt;br /&gt;      Else&lt;br /&gt;        StrResult = oRsOracle.Fields(0).Value&lt;br /&gt;      End If&lt;br /&gt;    oRsOracle.MoveNext&lt;br /&gt;  Loop&lt;br /&gt;  oConOracle.Close&lt;br /&gt;  Set oRsOracle = Nothing&lt;br /&gt;  Set oConOracle = Nothing&lt;br /&gt;  ORAQUERY = StrResult&lt;br /&gt;End Function&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 10 Sep 2007 15:26:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4518</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Detect a field edit in Excel and refresh a Query</title>
      <link>http://snippets.dzone.com/posts/show/3613</link>
      <description>&lt;code&gt;&lt;br /&gt;Private Sub Worksheet_Change(ByVal Target As Range)&lt;br /&gt;    Dim wks As Worksheet&lt;br /&gt;    Set wks = ActiveSheet&lt;br /&gt;&lt;br /&gt;    If Target.Row = 1 And Target.Column = 1 Then&lt;br /&gt;      wks.QueryTables(1).Refresh&lt;br /&gt;    End If&lt;br /&gt;&lt;br /&gt;    Set wks = Nothing&lt;br /&gt;End Sub&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Mar 2007 04:29:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3613</guid>
      <author>davetrane (David Davis)</author>
    </item>
    <item>
      <title>One liner to list query methods for an object</title>
      <link>http://snippets.dzone.com/posts/show/3214</link>
      <description>// List query methods for an object&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;puts Object.methods.sort.join("\n").grep(/\?/)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 28 Dec 2006 00:41:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3214</guid>
      <author>MarkStevenWoods (Mark Woods)</author>
    </item>
    <item>
      <title>conditioner for ActiveRecord-friendly conditions from a collection</title>
      <link>http://snippets.dzone.com/posts/show/2510</link>
      <description>I frequently have a collection of values that I want to match in an ActiveRecord query, but it would be nice if I could let ActiveRecord handle checking the data and escaping it properly.  So, I wrote this method to return ActiveRecord-friendly conditions, such as: &lt;code&gt;["user_id=? AND job_id=?", 3, 4]&lt;/code&gt; based on the 'raw' conditions you feed to it, such as: &lt;code&gt;[['user_id', 3], ['job_id', 4]]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# Returns ActiveRecord-friendly conditions based on the given&lt;br /&gt;# raw conditions; handles grouping based on like field names;&lt;br /&gt;# allows different boolean operators in raw conditions;&lt;br /&gt;# allows different comparison operators in raw conditions;&lt;br /&gt;# raw conditions setup:&lt;br /&gt;# [[field name, desired value, bool. op., comp. op.], ...]&lt;br /&gt;# name = condition[0]&lt;br /&gt;# value = condition[1]&lt;br /&gt;# bool_type = condition[2]&lt;br /&gt;# comparison = condition[3]&lt;br /&gt;# raw conditions example:&lt;br /&gt;# [['type_id', '4', 'OR'], ['created_on', Date.new, 'AND', '&lt;=']]&lt;br /&gt;def conditioner( raw_conditions )&lt;br /&gt;  return nil if raw_conditions.nil? || raw_conditions.empty?&lt;br /&gt;  &lt;br /&gt;  conditions = ['(']&lt;br /&gt;  count = 0&lt;br /&gt;  prev_name = raw_conditions[0][0]&lt;br /&gt;  raw_conditions.each do |condition|&lt;br /&gt;    name = condition[0]&lt;br /&gt;      &lt;br /&gt;    conditions[0] &lt;&lt; ') AND ' if prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; ' ' &lt;&lt; ( condition[2] || 'OR' ) &lt;&lt; ' ' unless count == 0 || prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; '(' if prev_name != name&lt;br /&gt;    conditions[0] &lt;&lt; name + ' ' &lt;&lt; ( condition[3] || '=' ) + ' ?'&lt;br /&gt;      &lt;br /&gt;    conditions &lt;&lt; condition[1]&lt;br /&gt;      &lt;br /&gt;    prev_name = name&lt;br /&gt;    count += 1&lt;br /&gt;  end&lt;br /&gt;  conditions[0] &lt;&lt; ')'&lt;br /&gt;&lt;br /&gt;  conditions&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This way, you can do something like the following:&lt;br /&gt;&lt;code&gt;model_ids = Model.find( :all ).map( &amp;:id )&lt;br /&gt;raw_conditions = model_ids.collect { |id| ['model_id', id] }&lt;br /&gt;conditions = conditioner( raw_conditions )&lt;br /&gt;desired_collection = OtherModel.find( :all, :conditions =&gt; conditions )&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If your query needs to depend on more than one factor, you might do something like the following:&lt;br /&gt;&lt;code&gt;if test&lt;br /&gt;  raw_conditions = [['user_id', 3]]&lt;br /&gt;else&lt;br /&gt;  raw_conditions = [['user_id', 4], ['groups.name', 'dev']]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;team_ids.each { |id| raw_conditions &lt;&lt; ['team_id', id, 'AND'] }&lt;/code&gt;</description>
      <pubDate>Thu, 31 Aug 2006 22:02:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2510</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>Execute arbitary SQL in JDBC &amp; get column names etc from the meta data</title>
      <link>http://snippets.dzone.com/posts/show/2014</link>
      <description>This will execute an arbitary SQL string in JDBC and extract the column names. I extracted this code from a much larger module I wrote years ago, so it isn't complete and hasn't been tested. You have been warned.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    /*&lt;br /&gt;    ** connection is a java.sql.Connection obtained in the usual way.&lt;br /&gt;    */&lt;br /&gt;&lt;br /&gt;    PreparedStatement statement =&lt;br /&gt;	connection.prepareStatement (sqlString);&lt;br /&gt;    statement.setMaxRows (configModel.getMaxRows ());&lt;br /&gt;&lt;br /&gt;    if (statement.execute ())&lt;br /&gt;    {&lt;br /&gt;	ResultSet resultSet = statement.getResultSet ();&lt;br /&gt;	ResultSetMetaData metaData = resultSet.getMetaData ();&lt;br /&gt;&lt;br /&gt;	/*&lt;br /&gt;	** Get the column names.&lt;br /&gt;	*/&lt;br /&gt;&lt;br /&gt;	for (int i = 0 ; i &lt; metaData.getColumnCount () ; i++)&lt;br /&gt;	{&lt;br /&gt;	    int columnType = metaData.getColumnType (i + 1);&lt;br /&gt;	    String columnName = metaData.getColumnLabel (i + 1);&lt;br /&gt;&lt;br /&gt;	    /*&lt;br /&gt;	    ** Do something with columnType &amp; columnName.&lt;br /&gt;	    */&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/*&lt;br /&gt;	** Fetch the rows.&lt;br /&gt;	*/&lt;br /&gt;&lt;br /&gt;	while (resultSet.next ())&lt;br /&gt;	{&lt;br /&gt;	    String value;&lt;br /&gt;&lt;br /&gt;	    for (int i = 0 ; i &lt; metaData.getColumnCount () ; i++)&lt;br /&gt;		value = resultSet.getString (i + 1);&lt;br /&gt;	}&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;	/*&lt;br /&gt;	** Query was probably update/insert/delete.&lt;br /&gt;	*/&lt;br /&gt;&lt;br /&gt;	int rowCount = statement.getUpdateCount ();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    statement.close ();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 14 May 2006 20:25:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2014</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Create Temporary MySQL Table</title>
      <link>http://snippets.dzone.com/posts/show/1437</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;USE reference;&lt;br /&gt;&lt;br /&gt;CREATE TEMPORARY TABLE fulltxt&lt;br /&gt;SELECT surname,atitle,title,type,journal,pqid,volume, issn,issue,spage,year&lt;br /&gt;FROM citations&lt;br /&gt;WHERE volume &gt; 0&lt;br /&gt;AND issue &gt; 0&lt;br /&gt;AND spage &gt; 0&lt;br /&gt;AND issn != ''&lt;br /&gt;AND pqid != ''&lt;br /&gt;; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 16:04:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1437</guid>
      <author>offspinner ()</author>
    </item>
    <item>
      <title>query same table simultaneously with mysql</title>
      <link>http://snippets.dzone.com/posts/show/1435</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;USE reference;&lt;br /&gt;&lt;br /&gt;SELECT c2.pqid AS candidate,c1.surname,c1.atitle,c1.title,c1.type,c1.journal,c1.pqid,c1.volume, c1.issn,c1.issue,c1.spage,c1.year&lt;br /&gt;FROM citations AS c1, citations AS c2&lt;br /&gt;WHERE c1.issn = c2.issn&lt;br /&gt;AND c1.volume = c2.volume&lt;br /&gt;AND c1.issue = c2.issue&lt;br /&gt;AND c1.year = c2.year&lt;br /&gt;AND c1.spage = c2.spage&lt;br /&gt;AND c1.pqid != c2.pqid&lt;br /&gt;AND c1.pqid = ''&lt;br /&gt;; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 15:50:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1435</guid>
      <author>offspinner ()</author>
    </item>
    <item>
      <title>Parsing a query string with String#scan into a Hash</title>
      <link>http://snippets.dzone.com/posts/show/1313</link>
      <description>For more information on the desired hash output, etc. see:&lt;br /&gt;&lt;br /&gt;http://redhanded.hobix.com/inspect/injectingAHashBackwardsAndTheMergeBlock.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;qs = "post[id]=4&amp;post[nick]=_why&amp;post[message]=GROSS &amp; FOO!&amp;a=1"  # query string&lt;br /&gt;&lt;br /&gt;hash1 = {}&lt;br /&gt;hash2 = {}&lt;br /&gt;resulthash = {}&lt;br /&gt;count = 0&lt;br /&gt;&lt;br /&gt;qs.scan(/(post|a)(\[|=)(.*?)(?=(&amp;post\[|&amp;a=|$))/) {&lt;br /&gt;&lt;br /&gt;var = $1 &lt;&lt; $2 &lt;&lt; $3  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;case var&lt;br /&gt;&lt;br /&gt;when /^(post)\[(.*?)\]=(.*?)$/ then &lt;br /&gt;               count += 1&lt;br /&gt;               postnum = $1 &lt;&lt; count.to_s&lt;br /&gt;               hash1.update(postnum =&gt; {$2 =&gt; $3})&lt;br /&gt;&lt;br /&gt;when /^(a)=(.*?)$/ then resulthash.update($1 =&gt; $2)&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;hash1.each_pair {|k, v| hash2.update(v)}&lt;br /&gt;resulthash.update("post" =&gt; hash2)&lt;br /&gt;&lt;br /&gt;puts resulthash.inspect &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 29 Jan 2006 21:48:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1313</guid>
      <author>ntk ()</author>
    </item>
  </channel>
</rss>
