Skip to content

Commit

Permalink
fix #134: Add new Num* methods in CRUD builders to count number of re…
Browse files Browse the repository at this point in the history
…quired clause
  • Loading branch information
huandu committed Nov 30, 2023
1 parent eb77f78 commit a5eff69
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions createtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func (ctb *CreateTableBuilder) Option(opt ...string) *CreateTableBuilder {
return ctb
}

// NumDefine returns the number of definitions in CREATE TABLE.
func (ctb *CreateTableBuilder) NumDefine() int {
return len(ctb.defs)
}

// String returns the compiled INSERT string.
func (ctb *CreateTableBuilder) String() string {
s, _ := ctb.Build()
Expand Down
17 changes: 17 additions & 0 deletions createtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ func ExampleCreateTableBuilder_SQL() {
// /* before */ CREATE TEMPORARY TABLE IF NOT EXISTS demo.user /* after create */ (id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT "user id", name VARCHAR(255) NOT NULL COMMENT "user name") /* after define */ DEFAULT CHARACTER SET utf8mb4 AS SELECT * FROM old.user WHERE name LIKE ?
// [%Huan%]
}

func ExampleCreateTableBuilder_NumDefine() {
ctb := NewCreateTableBuilder()
ctb.CreateTable("demo.user").IfNotExists()
ctb.Define("id", "BIGINT(20)", "NOT NULL", "AUTO_INCREMENT", "PRIMARY KEY", `COMMENT "user id"`)
ctb.Define("name", "VARCHAR(255)", "NOT NULL", `COMMENT "user name"`)
ctb.Define("created_at", "DATETIME", "NOT NULL", `COMMENT "user create time"`)
ctb.Define("modified_at", "DATETIME", "NOT NULL", `COMMENT "user modify time"`)
ctb.Define("KEY", "idx_name_modified_at", "name, modified_at")
ctb.Option("DEFAULT CHARACTER SET", "utf8mb4")

// Count the number of definitions.
fmt.Println(ctb.NumDefine())

// Output:
// 5
}
5 changes: 5 additions & 0 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
return ib
}

// NumValue returns the number of values to insert.
func (ib *InsertBuilder) NumValue() int {
return len(ib.values)
}

// String returns the compiled INSERT string.
func (ib *InsertBuilder) String() string {
s, _ := ib.Build()
Expand Down
14 changes: 14 additions & 0 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,17 @@ func ExampleInsertBuilder_subSelect_oracle() {
// INSERT INTO demo.user (id, name) SELECT id, name FROM demo.test WHERE id = :1
// [1]
}

func ExampleInsertBuilder_NumValue() {
ib := NewInsertBuilder()
ib.InsertInto("demo.user")
ib.Cols("id", "name")
ib.Values(1, "Huan Du")
ib.Values(2, "Charmy Liu")

// Count the number of values.
fmt.Println(ib.NumValue())

// Output:
// 2
}
5 changes: 5 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ func (sb *SelectBuilder) BuilderAs(builder Builder, alias string) string {
return fmt.Sprintf("(%s) AS %s", sb.Var(builder), alias)
}

// NumCol returns the number of columns to select.
func (sb *SelectBuilder) NumCol() int {
return len(sb.selectCols)
}

// String returns the compiled SELECT string.
func (sb *SelectBuilder) String() string {
s, _ := sb.Build()
Expand Down
15 changes: 15 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,18 @@ func ExampleSelectBuilder_customSELECT() {
// SELECT id, name FROM user WHERE id IN (?, ?, ?)
// [1 2 3]
}

func ExampleSelectBuilder_NumCol() {
sb := NewSelectBuilder()
sb.Select("id", "name", "created_at")
sb.From("demo.user")
sb.Where(
sb.GreaterThan("id", 1234),
)

// Count the number of columns.
fmt.Println(sb.NumCol())

// Output:
// 3
}
5 changes: 5 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ func (ub *UpdateBuilder) Limit(limit int) *UpdateBuilder {
return ub
}

// NumAssignment returns the number of assignments to update.
func (ub *UpdateBuilder) NumAssignment() int {
return len(ub.assignments)
}

// String returns the compiled UPDATE string.
func (ub *UpdateBuilder) String() string {
s, _ := ub.Build()
Expand Down
16 changes: 16 additions & 0 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ func ExampleUpdateBuilder_SQL() {
// Output:
// /* before */ UPDATE demo.user /* after update */ SET type = ? /* after set */ ORDER BY id DESC /* after order by */ LIMIT 10 /* after limit */
}

func ExampleUpdateBuilder_NumAssignment() {
ub := NewUpdateBuilder()
ub.Update("demo.user")
ub.Set(
ub.Assign("type", "sys"),
ub.Incr("credit"),
"modified_at = UNIX_TIMESTAMP(NOW())",
)

// Count the number of assignments.
fmt.Println(ub.NumAssignment())

// Output:
// 3
}

0 comments on commit a5eff69

Please sign in to comment.