Add And and Or method for query condition

This commit is contained in:
Lunny Xiao 2013-11-09 21:13:16 +08:00
parent a1062be8d0
commit 2a6991886c
6 changed files with 40 additions and 20 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ _cgo_export.*
_testmain.go
*.exe
vendor

2
.gopmfile Normal file
View File

@ -0,0 +1,2 @@
[target]
path = github.com/lunny/xorm

View File

@ -452,6 +452,13 @@ func where(engine *Engine, t *testing.T) {
panic(err)
}
fmt.Println(users)
err = engine.Where("(id) > ?", 2).And("(id) < ?", 10).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)
}
func in(engine *Engine, t *testing.T) {

20
install
View File

@ -1,20 +0,0 @@
#!/usr/bin/env bash
if [ ! -f install ]; then
echo 'install must be run within its container folder' 1>&2
exit 1
fi
CURDIR=`pwd`
NEWPATH="$GOPATH/src/github.com/lunny/${PWD##*/}"
if [ ! -d "$NEWPATH" ]; then
ln -s $CURDIR $NEWPATH
fi
gofmt -w $CURDIR
cd $NEWPATH
go install ${PWD##*/}
cd $CURDIR
echo 'finished'

View File

@ -58,6 +58,18 @@ func (session *Session) Where(querystring string, args ...interface{}) *Session
return session
}
// Method Where provides custom query condition.
func (session *Session) And(querystring string, args ...interface{}) *Session {
session.Statement.And(querystring, args...)
return session
}
// Method Where provides custom query condition.
func (session *Session) Or(querystring string, args ...interface{}) *Session {
session.Statement.Or(querystring, args...)
return session
}
// Method Id provides converting id as a query condition
func (session *Session) Id(id int64) *Session {
session.Statement.Id(id)

View File

@ -69,6 +69,24 @@ func (statement *Statement) Where(querystring string, args ...interface{}) {
statement.Params = args
}
func (statement *Statement) And(querystring string, args ...interface{}) {
if statement.WhereStr != "" {
statement.WhereStr = fmt.Sprintf("(%v) AND (%v)", statement.WhereStr, querystring)
} else {
statement.WhereStr = querystring
}
statement.Params = append(statement.Params, args...)
}
func (statement *Statement) Or(querystring string, args ...interface{}) {
if statement.WhereStr != "" {
statement.WhereStr = fmt.Sprintf("(%v) OR (%v)", statement.WhereStr, querystring)
} else {
statement.WhereStr = querystring
}
statement.Params = append(statement.Params, args...)
}
func (statement *Statement) Table(tableNameOrBean interface{}) {
t := rType(tableNameOrBean)
if t.Kind() == reflect.String {