Skip to content

Commit

Permalink
downgrade go version & add drop table
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxulai committed Apr 6, 2024
1 parent f9b3b88 commit 61fdac9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
48 changes: 48 additions & 0 deletions drop_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sqls

import (
"strings"
)

type dropTableBuilder struct {
builder *sqlBuilder
table string
ifExists bool
}

func newDropTableBuilder() *dropTableBuilder {
builder := &dropTableBuilder{}
builder.builder = newSqlBuilder()
builder.ifExists = false
return builder
}

func DROP_TABLE(table string) *dropTableBuilder {
s := newDropTableBuilder()
s.table = table
return s
}

func (s *dropTableBuilder) IF_EXISTS() *dropTableBuilder {
s.ifExists = true
return s
}

func (s *dropTableBuilder) Param(v any) string {
return s.builder.Param(v)
}

func (s *dropTableBuilder) Params() []any {
return s.builder.Params(s.String())
}

func (s *dropTableBuilder) String() string {
var sqlString string
keyword := "DROP TABLE"
if s.ifExists {
keyword += " IF EXISTS"
}

sqlString += s.builder.join(keyword, "", []string{s.table}, ", ", "")
return strings.Trim(sqlString, " ")
}
15 changes: 15 additions & 0 deletions drop_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sqls

import (
"testing"
)

func TestDropTableAddConstraint(t *testing.T) {
s := DROP_TABLE("TABLE").IF_EXISTS()
result := s.String()
expected := "DROP TABLE IF EXISTS TABLE"

if result != expected {
t.Errorf("Case1() 返回值为 %s,期望值为 %s", result, expected)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/yinxulai/sqls

go 1.22
go 1.21.0

0 comments on commit 61fdac9

Please sign in to comment.