func (q *Query) FirstResult() (Result, error) {
	q.Limit(1)
	results, err := q.Results()
	if err != nil {
		return nil, err
	}
	if len(results) == 0 {
		return nil, fmt.Errorf("No results found for Query:%s", q.QueryString())
	}
	return results[0], nil
}
FirstResult executes the SQL and returrns the first result
package query
import (
	"database/sql"
	"fmt"
	"sort"
	"strconv"
	"strings"
)
var Debug bool
func init() {
	Debug = false  
}
type Result map[string]interface{}
type Func func(q *Query) *Query
type Query struct {
	tablename  string
	primarykey string
	sql    string
	sel    string
	join   string
	where  string
	group  string
	having string
	order  string
	offset string
	limit  string
	args []interface{}
}
func New(t string, pk string) *Query {
	if database == nil {
		return nil
	}
	q := &Query{
		tablename:  t,
		primarykey: pk,
	}
	return q
}
func Exec(sql string, args ...interface{}) (sql.Result, error) {
	results, err := database.Exec(sql, args...)
	return results, err
}
func Rows(sql string, args ...interface{}) (*sql.Rows, error) {
	results, err := database.Query(sql, args...)
	return results, err
}
func (q *Query) Copy() *Query {
	return &Query{
		tablename:  q.tablename,
		primarykey: q.primarykey,
		sql:        q.sql,
		sel:        q.sel,
		join:       q.join,
		where:      q.where,
		group:      q.group,
		having:     q.having,
		order:      q.order,
		offset:     q.offset,
		limit:      q.limit,
		args:       q.args,
	}
}
func (q *Query) InsertJoin(a int64, b int64) error {
	return q.InsertJoins([]int64{a}, []int64{b})
}
func (q *Query) InsertJoins(a []int64, b []int64) error {
	if len(a) == 0 || len(b) == 0 {
		return fmt.Errorf("Null data for joins insert %s", q.table())
	}
	values := ""
	for _, av := range a {
		for _, bv := range b {
			if av != 0 && bv != 0 {
				values += fmt.Sprintf("(%d,%d),", av, bv)
			}
		}
	}
	values = strings.TrimRight(values, ",")
	sql := fmt.Sprintf("INSERT into %s VALUES %s;", q.table(), values)
	if Debug {
		fmt.Printf("JOINS SQL:%s\n", sql)
	}
	_, err := database.Exec(sql)
	return err
}
func (q *Query) UpdateJoins(id int64, a []int64, b []int64) error {
	if Debug {
		fmt.Printf("SetJoins %s %s=%d: %v %v \n", q.table(), q.pk(), id, a, b)
	}
	err := q.Where(fmt.Sprintf("%s=?", q.pk()), id).Delete()
	if err != nil {
		return err
	}
	if len(a) > 0 && len(b) > 0 {
		err = q.InsertJoins(a, b)
		if err != nil {
			return err
		}
	}
	return nil
}
func (q *Query) Insert(params map[string]string) (int64, error) {
	sql := q.insertSQL(params)
	if Debug {
		fmt.Printf("INSERT SQL:%s %v\n", sql, valuesFromParams(params))
	}
	id, err := database.Insert(sql, valuesFromParams(params)...)
	if err != nil {
		return 0, err
	}
	return id, nil
}
func (q *Query) insertSQL(params map[string]string) string {
	var cols, vals []string
	for i, k := range sortedParamKeys(params) {
		cols = append(cols, database.QuoteField(k))
		vals = append(vals, database.Placeholder(i+1))
	}
	query := fmt.Sprintf("INSERT INTO %s (%s) VALUES(%s) %s;", q.table(), strings.Join(cols, ","), strings.Join(vals, ","), database.InsertSQL(q.pk()))
	return query
}
func (q *Query) Update(params map[string]string) error {
	return q.UpdateAll(params)
}
func (q *Query) Delete() error {
	return q.DeleteAll()
}
func (q *Query) UpdateAll(params map[string]string) error {
	var output []string
	for _, key := range sortedParamKeys(params) {
		v := params[key]
		if v == "null" {
			output = append(output, fmt.Sprintf("%s=null", database.QuoteField(key)))
			delete(params, key)
		} else {
			output = append(output, fmt.Sprintf("%s=?", database.QuoteField(key)))
		}
	}
	querySQL := strings.Join(output, ",")
	q.Select(fmt.Sprintf("UPDATE %s SET %s", q.table(), querySQL))
	values := valuesFromParams(params)
	q.args = append(values, q.args...)
	if Debug {
		fmt.Printf("UPDATE SQL:%s\n%v\n", q.QueryString(), values)
	}
	_, err := q.Result()
	return err
}
func (q *Query) DeleteAll() error {
	q.Select(fmt.Sprintf("DELETE FROM %s", q.table()))
	if Debug {
		fmt.Printf("DELETE SQL:%s <= %v\n", q.QueryString(), q.args)
	}
	_, err := q.Result()
	return err
}
func (q *Query) Count() (int64, error) {
	s := q.sel
	countSelect := fmt.Sprintf("SELECT COUNT(%s) FROM %s", q.pk(), q.table())
	q.Select(countSelect)
	o := strings.Replace(q.order, "ORDER BY ", "", 1)
	q.order = ""
	var count int64
	rows, err := q.Rows()
	if err != nil {
		return 0, fmt.Errorf("Error querying database for count: %s\nQuery:%s", err, q.QueryString())
	}
	defer rows.Close()
	for rows.Next() {
		err = rows.Scan(&count)
		if err != nil {
			return 0, err
		}
	}
	q.Select(s)
	q.Order(o)
	q.reset()
	return count, err
}
func (q *Query) Result() (sql.Result, error) {
	results, err := database.Exec(q.QueryString(), q.args...)
	return results, err
}
func (q *Query) Rows() (*sql.Rows, error) {
	results, err := database.Query(q.QueryString(), q.args...)
	return results, err
}