mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
fix #134: Add new Num* methods in CRUD builders to count number of required clause
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user