func (d *sqlDatabase) Query(q query) ([]record, error) { var rs []record var ws []string var ps []interface{} if q.Cmd != nil { ws = append(ws, "cmd LIKE ?") ps = append(ps, "%"+*q.Cmd+"%") } if q.Dir != nil { ws = append(ws, "dir = ?") ps = append(ps, q.Dir) } if q.Hostname != nil { ws = append(ws, "hostname = ?") ps = append(ps, q.Hostname) } if q.ShellSessionID != nil { ws = append(ws, "shell_session_id = ?") ps = append(ps, q.ShellSessionID) } var db *gorm.DB if q.SortByFreq { db = d.db.Table("records").Select(sqlSelectByFreq).Where(strings.Join(ws, " and "), ps...).Group(sqlGroupByFreq).Order("c desc").Limit(q.Limit).Offset(q.Offset) } else { db = d.db.Table("records").Select(sqlSelectByDate).Where(strings.Join(ws, " and "), ps...).Order("time desc").Limit(q.Limit).Offset(q.Offset) } err := db.Scan(&rs).Error return rs, err }
// NewRec returns a bool depending on whether or not it could find a record func (db *DB) NewRec(table, field, data string) bool { var d string if isADate(data) { t, err := time.Parse("2006-01-02 15:04:05 -0700 UTC", data) if err != nil { log.Fatalf("Problem parsing date: %s", err) } d = t.Format("2006-01-02 15:04:05") } var count int f := fmt.Sprintf("%s = ?", field) var row *gorm.DB if isADate(data) { row = db.Table(table).Where(f, d).Limit(1).Select(field).Count(&count) } else { row = db.Table(table).Where(f, data).Limit(1).Select(field).Count(&count) } row.Scan(&count) if count == 0 { return true } return false }