#!/usr/bin/env ruby

# query some rdf data in our SQL store
# Dan Brickley <danbri@w3.org>
# $Id: qgrub.rb,v 1.8 2003/04/18 18:06:12 danbri Exp $

$LOAD_PATH.unshift '../lib/'
$LOAD_PATH.unshift '../../lib/'

require 'basicrdf'
require 'squish'
require 'dbi'


def query_grub

  query=<<EOQ;
    SELECT ?uri, ?title, ?desc, ?c, 
    WHERE 
      (rss::title ?uri ?title)
      (rss::description ?uri ?desc)
      (cm::Country ?uri ?c)
    USING
      rss for http://purl.org/rss/1.0/
      cm for http://chefmoz.org/rdf/elements/1.0/

EOQ

#      (cm::Neighborhood ?uri ?n)

  log=''

  dbi_driver = 'DBI:Pg:rdfweb1'
  dbi_user = 'danbri'
  dbi_pass =''
  q = SquishQuery.new.parseFromText (query)
  c=0;
  DBI.connect(  dbi_driver, dbi_user ,  dbi_pass ) do | dbh |

  log += "|| title || country || uri || description ||\n"

    dbh.select_all( q.toSQLQuery  ) do | hit |
      c+=1
      r=ResultRow.new hit
      log += "|| #{r.title} || #{r.c} || #{r.uri} || #{r.desc} ||\n"
    end
  end
  res=Hash.new
  res['log']=log
  res['hits']=c
  return res

end

########


# FIXME:
# add some 'if we're being called as a library' stuff here
#qr=query_grub 
#puts qr
