I have written a SQL query in buffer.WriteString() but not able to use that buffer in db.Query().
buffer.WriteString(fmt.Sprintf(`SELECT c.id, c.company_name, ss.start_date, ss.shift_length, ss.bill_rate, ss.ot_hrs, ss.dt_hrs, ts.pay_rate, ts.wc_rate, ts.paid, td.wc
FROM company c
JOIN users u1 ON c.id = u1.company_id
JOIN schedule s ON u1.id = s.user_id
JOIN schedule_shifts ss ON s.id = ss.schedule_id
JOIN technician_shifts ts ON ss.id = ts.shift_id
JOIN users u ON u.id = ts.technician_id
JOIN technician_details td ON td.user_id = u.id
JOIN master_technicians mt ON mt.id = u.master_technician_id
WHERE mt.id = %d AND ts.confirmed = 'yes' AND ts.paid = 'yes'`, masterID))
if e.Type == "today" {
buffer.WriteString(fmt.Sprintf(`AND ts.paid_on = CURDATE()`))
} else if e.Type == "weekly" {
buffer.WriteString(fmt.Sprintf(`AND ts.paid_on > DATE_SUB(NOW(), INTERVAL 1 WEEK)`))
} else if e.Type == "monthly" {
buffer.WriteString(fmt.Sprintf(`AND ts.paid_on > DATE_SUB(NOW(), INTERVAL 1 MONTH)`))
} else {
if e.StartDate != "" && e.EndDate != "" {
if e.StartDate == e.EndDate {
buffer.WriteString(fmt.Sprintf(`AND ts.paid_on = %s`, e.StartDate))
} else {
buffer.WriteString(fmt.Sprintf(`AND ts.paid_on >= %s AND ts.paid_on <= %s`, e.StartDate, e.EndDate))
}
}
}
rows, err := db.Query(buffer)
if err != nil {
log.Panic(err.Error())
}
It gives me the error that
cannot use buffer (type bytes.Buffer) as type string in argument to db.Query
How can i do that?