require 'rdb'

database_name = 'IBLocal'
database =
  RDB::SESSION.databases.find{|i| i.database_name == database_name} ||
  RDB::Database.new(database_name, 'SYSDBA', 'masterkey')
query = RDB::Query.new database
query.sql.text = <<EOT
  select * from customer
EOT

prod0 = nil
Web::PRODUCER.on_html_tag = proc {
  |sender, replace, spec, name, params|
  case name
  when 'table'
    replace+= prod0.content
  end
  replace
}
prod0 = Web::PageProducer.new Web::MODULE
prod0.html_file = absolute_path('/DB/table') + '.htmt'
prod1 = nil
prod2 = nil
prod3 = nil
field = nil
fields = nil
prod0.on_html_tag = proc {
  |sender, replace, spec, name, params|
  case name
  when 'head'
    query.open
    query.fields.each{|field|
      replace+= prod1.content
    }
    query.close
  when 'body'
    query.open
    query.each{|fields|
      replace+= prod2.content
    }
    query.close
  end
  replace
}
prod1 = Web::PageProducer.new Web::MODULE
prod1.html_file = absolute_path('/DB/head') + '.htmt'
prod1.on_html_tag = proc {
  |sender, replace, spec, name, params|
  field.field_name
}
prod2 = Web::PageProducer.new Web::MODULE
prod2.html_file = absolute_path('/DB/body') + '.htmt'
prod2.on_html_tag = proc {
  |sender, replace, spec, name, params|
  fields.each{|field|
    replace+= prod3.content
  }
  replace
}
prod3 = Web::PageProducer.new Web::MODULE
prod3.html_file = absolute_path('/DB/cell') + '.htmt'
prod3.on_html_tag = proc {
  |sender, replace, spec, name, params|
  s=field.value.to_s; s.empty? ? '&nbsp;' : s
}
