Skip to content

Commit

Permalink
Add LIKE and ILIKE operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Delisa-sama committed Sep 23, 2022
1 parent a8c03d2 commit a828270
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
35 changes: 35 additions & 0 deletions nodes/ilike.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nodes

// ILikeNode represents equals operator
type ILikeNode struct {
name NameNode
right Node
}

// NewILikeNode returns new ILikeNode
func NewILikeNode(name NameNode, right Node) ILikeNode {
return ILikeNode{
name: name,
right: right,
}
}

// Name returns name
func (n ILikeNode) Name() NameNode {
return n.name
}

// Childs returns collection of childs
func (n ILikeNode) Childs() []Node {
return []Node{n.right}
}

// Right returns child
func (n ILikeNode) Right() Node {
return n.right
}

// Accept accepts translate visitor to invoke TranslateILikeNode method
func (n ILikeNode) Accept(visitor TranslateVisitor) string {
return visitor.TranslateILikeNode(n)
}
2 changes: 2 additions & 0 deletions nodes/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ type TranslateVisitor interface {
TranslateFloatNode(FloatNode) string
TranslateUintNode(UintNode) string
TranslateBoolNode(BoolNode) string
TranslateLikeNode(LikeNode) string
TranslateILikeNode(ILikeNode) string
}
35 changes: 35 additions & 0 deletions nodes/like.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nodes

// LikeNode represents equals operator
type LikeNode struct {
name NameNode
right Node
}

// NewLikeNode returns new LikeNode
func NewLikeNode(name NameNode, right Node) LikeNode {
return LikeNode{
name: name,
right: right,
}
}

// Name returns name
func (n LikeNode) Name() NameNode {
return n.name
}

// Childs returns collection of childs
func (n LikeNode) Childs() []Node {
return []Node{n.right}
}

// Right returns child
func (n LikeNode) Right() Node {
return n.right
}

// Accept accepts translate visitor to invoke TranslateLikeNode method
func (n LikeNode) Accept(visitor TranslateVisitor) string {
return visitor.TranslateLikeNode(n)
}
20 changes: 20 additions & 0 deletions operators/ilike.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package operators

import (
"github.com/Delisa-sama/stmt-builder/nodes"
"github.com/Delisa-sama/stmt-builder/statement"
)

type ilike struct {
value string
}

// ILIKE represents ILIKE operator
func ILIKE(value string) statement.Operator {
return ilike{value: value}
}

// Node returns EqNode
func (o ilike) Node(leftOp string) nodes.Node {
return nodes.NewLikeNode(nodes.NewNameNode(leftOp), nodes.NewStringNode(o.value))
}
20 changes: 20 additions & 0 deletions operators/like.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package operators

import (
"github.com/Delisa-sama/stmt-builder/nodes"
"github.com/Delisa-sama/stmt-builder/statement"
)

type like struct {
value string
}

// LIKE represents LIKE operator
func LIKE(value string) statement.Operator {
return like{value: value}
}

// Node returns EqNode
func (o like) Node(leftOp string) nodes.Node {
return nodes.NewLikeNode(nodes.NewNameNode(leftOp), nodes.NewStringNode(o.value))
}
10 changes: 10 additions & 0 deletions translators/sql_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,13 @@ func (t *SQLTranslator) TranslateBoolNode(n nodes.BoolNode) string {
}
return "FALSE"
}

// TranslateLikeNode translates Like node to SQL
func (t *SQLTranslator) TranslateLikeNode(n nodes.LikeNode) string {
return " LIKE "
}

// TranslateILikeNode translates ILike node to SQL
func (t *SQLTranslator) TranslateILikeNode(n nodes.ILikeNode) string {
return " ILIKE "
}
12 changes: 12 additions & 0 deletions translators/sql_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ func TestSQLTranslator_Translate(t *testing.T) {
Sort(sort.By("id", "status"), sort.DESCDirection),
want: " WHERE (id = ? AND status IN (?,?)) ORDER BY id,status DESC",
},
{
name: "single statement LIKE expression",
placeholder: nil,
s: statement.New("name", operators.LIKE("%abc%")),
want: " WHERE name LIKE '%abc%'",
},
{
name: "single statement LIKE expression",
placeholder: nil,
s: statement.New("name", operators.ILIKE("%abc%")),
want: " WHERE name LIKE '%abc%'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a828270

Please sign in to comment.