diff --git a/models/commit.go b/models/commit.go index 87e427e8..6bca1be2 100644 --- a/models/commit.go +++ b/models/commit.go @@ -6,6 +6,9 @@ package models import ( "time" + "fmt" + + "github.com/go-xorm/builder" ) type Commit struct { @@ -20,3 +23,13 @@ type Commit struct { CommitterName string CommitterTime time.Time } + +func FulltextSearchCommits(q string) ([]*Commit, error) { + sess := x.NewSession() + sess.Where(builder.Match{"message", q}) + commits := make([]*Commit, 0) + if err := sess.Find(&commits); err != nil { + return nil, fmt.Errorf("Find: %v", err) + } + return commits, nil +} diff --git a/vendor/github.com/go-xorm/builder/cond_match.go b/vendor/github.com/go-xorm/builder/cond_match.go new file mode 100644 index 00000000..4a2e7fea --- /dev/null +++ b/vendor/github.com/go-xorm/builder/cond_match.go @@ -0,0 +1,36 @@ +// Copyright 2016 The Xorm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package builder + +import "fmt" + +// Match defines full-text match condition +type Match [2]string + +var _ Cond = Match{"", ""} + +// WriteTo write SQL to Writer +func (m Match) WriteTo(w Writer) error { + if _, err := fmt.Fprintf(w, "to_tsvector(%s) @@ to_tsquery(?)", m[0]); err != nil { + return err + } + w.Append(m[1]) + return nil +} + +// And implements And with other conditions +func (m Match) And(conds ...Cond) Cond { + return And(m, And(conds...)) +} + +// Or implements Or with other conditions +func (m Match) Or(conds ...Cond) Cond { + return Or(m, Or(conds...)) +} + +// IsValid tests if this condition is valid +func (m Match) IsValid() bool { + return len(m[0]) > 0 && len(m[1]) > 0 +}