NoorPlugin = require("./noorplugin").NoorPlugin
class NoorQuery extends NoorPlugin
  qAsN4WithIdAsContext: (req, res) =>
    line_count = 0
    query = @noodb.q(null, null, null, req.params.g)
    all = query.all()
    for spogi in all
      res.write(spogi.asLineWithIdAsContext())
      line_count++
    if not line_count
      res.send("")
    res.end()
  qAsN5: (req, res) =>
    line_count = 0
    query = @noodb.q(null, null, null, req.params.g)
    all = query.all()
    for spogi in all
      res.write(spogi.asLine())
      line_count++
    if not line_count
      res.send("")
    res.end()
  queryAsN5: (query, res) =>
    line_count = 0
    all = query.all()
    for spogi in all
      res.write(spogi.asLine())
      line_count++
    if not line_count
      res.send("")
    res.end()
  qqAsN5: (req, res) =>
    @log.info("qqAsN5() req.query =",req.query)
    @queryAsN5(@noodb.q(req.query.s, req.query.p, req.query.o, req.query.g, req.query.i), res)
  queryAsNq: (req, res) =>
    line_count = 0
    terms = []
    factory = (noorquery) =>
      i = 1
      () =>
        k = req.params['k'+i]
        v = req.params['v'+i]
        console.log i,k,v
        if k? and v? and k.match(/[spogi]/)
          term = {}
          term[k] = v
          noorquery.log.warning "term #{i}:",term
          i = i + 1
          return
        return false
    getAnother = factory(@)
    another = getAnother()
    @log.warning "about to loop"
    j = 0
    while another
      j++
      if j > 3
        return
      @log.warning "================="
      terms.push(another)
      @log.warning "WHILE", another
      another = getAnother()
    line_count = 0
    for spogi in @noodb.query(terms, {which: 'all'})
      res.write(spogi.asLine())
      line_count++
    if not line_count
      res.send("")
    res.end()