Support Exists and NotExists #84

Merged
lunny merged 2 commits from lunny/add_exists into master 2022-04-01 12:07:13 +00:00
3 changed files with 130 additions and 0 deletions
Showing only changes of commit b941dd8498 - Show all commits

49
cond_exists.go Normal file
View File

@ -0,0 +1,49 @@
// Copyright 2022 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 (
"errors"
"io"
)
type condExists struct {
subQuery *Builder
}
var _ Cond = condExists{}
// Exists returns Cond via condition
func Exists(subQuery *Builder) Cond {
return &condExists{
subQuery: subQuery,
}
}
func (condExists condExists) WriteTo(w Writer) error {
if !condExists.IsValid() {
return errors.New("exists condition is nov valid")
}
if _, err := io.WriteString(w, "EXISTS ("); err != nil {
return err
}
if err := condExists.subQuery.WriteTo(w); err != nil {
return err
}
_, err := io.WriteString(w, ")")
return err
}
func (condExists condExists) And(conds ...Cond) Cond {
return And(condExists, And(conds...))
}
func (condExists condExists) Or(conds ...Cond) Cond {
return Or(condExists, Or(conds...))
}
func (condExists condExists) IsValid() bool {
return condExists.subQuery != nil
}

32
cond_exists_test.go Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2022 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 (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExists(t *testing.T) {
cond1 := Exists(Select("*").From("table").Where(Eq{"col1": 1}))
sql, err := ToBoundSQL(cond1)
assert.NoError(t, err)
assert.EqualValues(t, "EXISTS (SELECT * FROM table WHERE col1=1)", sql)
}
func TestNotExists1(t *testing.T) {
cond1 := Exists(Select("*").From("table").Where(Eq{"col1": 1}))
sql, err := ToBoundSQL(Not{cond1})
assert.NoError(t, err)
assert.EqualValues(t, "NOT EXISTS (SELECT * FROM table WHERE col1=1)", sql)
}
func TestNotExists2(t *testing.T) {
cond1 := NotExists(Select("*").From("table").Where(Eq{"col1": 1}))
sql, err := ToBoundSQL(cond1)
assert.NoError(t, err)
assert.EqualValues(t, "NOT EXISTS (SELECT * FROM table WHERE col1=1)", sql)
}

49
cond_not_exists.go Normal file
View File

@ -0,0 +1,49 @@
// Copyright 2022 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 (
"errors"
"io"
)
type condNotExists struct {
subQuery *Builder
}
var _ Cond = condNotExists{}
// NotExists returns Cond via condition
func NotExists(subQuery *Builder) Cond {
return &condNotExists{
subQuery: subQuery,
}
}
func (condNotExists condNotExists) WriteTo(w Writer) error {
if !condNotExists.IsValid() {
return errors.New("exists condition is nov valid")
}
if _, err := io.WriteString(w, "NOT EXISTS ("); err != nil {
return err
}
if err := condNotExists.subQuery.WriteTo(w); err != nil {
return err
}
_, err := io.WriteString(w, ")")
return err
}
func (condNotExists condNotExists) And(conds ...Cond) Cond {
return And(condNotExists, And(conds...))
}
func (condNotExists condNotExists) Or(conds ...Cond) Cond {
return Or(condNotExists, Or(conds...))
}
func (condNotExists condNotExists) IsValid() bool {
return condNotExists.subQuery != nil
}