Skip to content

Commit

Permalink
Add limit and offset
Browse files Browse the repository at this point in the history
  • Loading branch information
codebysmirnov authored and Delisa-sama committed Sep 30, 2022
1 parent a828270 commit 0c6e2b0
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func exampleStmt() {
fmt.Println(translator.Translate(s))
// [archive block]
fmt.Println(translator.GetArgs(s))

// LIMIT AND OFFSET
s = statement.New("name", operators.EQ(values.String("Example"))).Limit(1).Offset(5)
// WHERE name = $3 LIMIT 1 OFFSET 5
fmt.Println(translator.Translate(s))
}

func main() {
Expand Down
21 changes: 21 additions & 0 deletions limit/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package limit

// Limit represents abstract limit
type Limit interface {
Get() uint
}

// limit represents the limit of a database query
type limit struct {
value uint
}

// Get returns the limit to a query
func (l limit) Get() uint {
return l.value
}

// NewLimit returns a new Limit object
func NewLimit(l uint) Limit {
return &limit{value: l}
}
21 changes: 21 additions & 0 deletions offset/offset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package offset

// Offset represents abstract offset
type Offset interface {
Get() uint
}

// offset represents the offset of a database query
type offset struct {
value uint
}

// Get returns the offset to a query
func (l offset) Get() uint {
return l.value
}

// NewOffset returns a new Offset object
func NewOffset(value uint) Offset {
return &offset{value: value}
}
30 changes: 28 additions & 2 deletions statement/statement.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package statement

import (
"github.com/Delisa-sama/stmt-builder/limit"
"github.com/Delisa-sama/stmt-builder/nodes"
"github.com/Delisa-sama/stmt-builder/offset"
"github.com/Delisa-sama/stmt-builder/sort"
)

// Statement represents tree of nodes that can be translated to query
type Statement struct {
root nodes.Node
sort sort.Sort
root nodes.Node
sort sort.Sort
limit limit.Limit
offset offset.Offset
}

// Operator represents operator for construct statement
Expand Down Expand Up @@ -41,6 +45,16 @@ func (s Statement) GetSort() sort.Sort {
return s.sort
}

// GetLimit returns the limit
func (s Statement) GetLimit() limit.Limit {
return s.limit
}

// GetOffset returns the offset
func (s Statement) GetOffset() offset.Offset {
return s.offset
}

// IsEmpty returns true if statement is empty
func (s Statement) IsEmpty() bool {
return s.root == nil
Expand Down Expand Up @@ -81,3 +95,15 @@ func (s Statement) Sort(columnNames []string, direction sort.Direction) Statemen
s.sort = sort.NewSort(columnNames, direction)
return s
}

// Limit returns statement with the limit
func (s Statement) Limit(l uint) Statement {
s.limit = limit.NewLimit(l)
return s
}

// Offset returns statement with the offset
func (s Statement) Offset(l uint) Statement {
s.offset = offset.NewOffset(l)
return s
}
32 changes: 32 additions & 0 deletions translators/sql_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"strings"
"time"

"github.com/Delisa-sama/stmt-builder/limit"
"github.com/Delisa-sama/stmt-builder/nodes"
"github.com/Delisa-sama/stmt-builder/offset"
"github.com/Delisa-sama/stmt-builder/sort"
"github.com/Delisa-sama/stmt-builder/statement"
)
Expand All @@ -25,6 +27,8 @@ type SQLTranslator struct {
type Statement interface {
GetRoot() nodes.Node
GetSort() sort.Sort
GetLimit() limit.Limit
GetOffset() offset.Offset
}

// SQLTranslatorOption represents option for SQLTranslator
Expand Down Expand Up @@ -91,6 +95,10 @@ func (t *SQLTranslator) Translate(s Statement) string {
}
queryBuilder.WriteString(t.translateSort(s.GetSort()))

queryBuilder.WriteString(t.translateLimit(s.GetLimit()))

queryBuilder.WriteString(t.translateOffset(s.GetOffset()))

return queryBuilder.String()
}

Expand Down Expand Up @@ -184,6 +192,30 @@ func (t *SQLTranslator) translateSort(s sort.Sort) string {
return sortBuilder.String()
}

func (t *SQLTranslator) translateLimit(l limit.Limit) string {
if l == nil {
return ""
}
limitBuilder := strings.Builder{}

limitBuilder.WriteString(" LIMIT ")
limitBuilder.WriteString(strconv.FormatUint(uint64(l.Get()), 10))

return limitBuilder.String()
}

func (t *SQLTranslator) translateOffset(o offset.Offset) string {
if o == nil {
return ""
}
offsetBuilder := strings.Builder{}

offsetBuilder.WriteString(" OFFSET ")
offsetBuilder.WriteString(strconv.FormatUint(uint64(o.Get()), 10))

return offsetBuilder.String()
}

// TranslateAndNode translates and node to sql
func (t *SQLTranslator) TranslateAndNode(node nodes.AndNode) string {
return " AND "
Expand Down
6 changes: 6 additions & 0 deletions translators/sql_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func TestSQLTranslator_Translate(t *testing.T) {
s: statement.New("name", operators.ILIKE("%abc%")),
want: " WHERE name LIKE '%abc%'",
},
{
name: "single statement LIKE expression",
placeholder: nil,
s: statement.New("name", operators.ILIKE("%abc%")).Limit(1).Offset(5),
want: " WHERE name LIKE '%abc%' LIMIT 1 OFFSET 5",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 0c6e2b0

Please sign in to comment.