 
 fs            = require "fs"
 moment        = require "moment"
+_             = require "underscore"
 
 Query         = require "../query"
 toLatexTable  = require "../toLatexTable"

    fullQuery = [select, from, where, orderBy, limit].join " "
    
    console.log "fullQuery: ", fullQuery
    query = new Query 
      body: fullQuery
    
    query.run (err, rows) ->
      console.log "rows: ", rows
      
      formattedRows = applyColumnTemplates rows
      # ties logic, # records, second order calculations
      
      # convert rows to latex
      tableText = toLatexTable
        data: rows
        columns: columns
        
      console.log "tableText: ", tableText
      
      file = opts.outFilePath + report.file + ".tex"
      
      # row outfile
      fs.writeFileSync file, tableText, "utf8"

  }
]

applyColumnTemplates = (data, activeColumns) ->
  for column in activeColumns
    if column.template?
      for row in data
        row[column.property] = column.template row[column.property]
