go-query-string-builder
go-query-string-builder is Go library for building query string
Usage
import "github.com/mikaelim-id/go-query-string-builder/query"
SelectQuery
Init
selectQuery := SelectQuery{
SelectStatement: []string{"*",},
FromCommand: "test_table",
WhereClause: "name='adama",
GroupByClause: []string{"name"},
OrderByClause: []OrderBy{
{
Field: "name",
Asc: false,
},
{
Field: "address",
Asc: true,
},
},
Limit: 5,
Offset: nil,
}
AppendAndEqualCondition
Append Field = Value and condition in SelectQuery, will be ignored if value is empty.
selectQuery.AppendAndEqualCondition("name","name_value")
AppendAndCondition
Append and condition in SelectQuery.
selectQuery.AppendAndCondition("name=name_value")
AppendOrEqualCondition
Append Field = Value or condition in SelectQuery, will be ignored if value is empty.
selectQuery.AppendOrEqualCondition("name","name_value")
AppendOrCondition
Append or condition in SelectQuery.
selectQuery.AppendOrCondition("name=name_value")
Get Query String
selectQuery.build()
Example
pointer := 1
offset := 1
selectQuery := query.SelectQuery{
SelectStatement: []string{"name", "address", "phone", "date_of_birth", "location_id"},
FromCommand: "test_table",
GroupByClause: []string{"name", "address"},
OrderByClause: []OrderBy{
{
Field: "name",
Asc: false,
},
{
Field: "address",
Asc: true,
},
},
Limit: 5,
Offset: &offset,
}
selectQuery.AppendAndEqualCondition("name", "test_name")
selectQuery.AppendAndEqualCondition("value", 1)
selectQuery.AppendAndEqualCondition("pointer", &pointer)
selectQuery.AppendAndEqualCondition("address", nil)
selectQuery.AppendAndCondition("phone is not null")
selectQuery.AppendOrCondition(query.BuildGroupedOrCondition("location_id=1", "location_id=3"))
fmt.Println(selectQuery.build())
UpdateQuery
Init
updateQuery := UpdateQuery{
UpdateStatement: "test_table",
SetCommand: map[string]string{
"name": "new name value",
},
WhereClause: "name='adam'",
}
AppendSet
Append Field = Value set in UpdateQuery, will be ignored if value is empty.
updateQuery.AppendSet("address","new avenue")
AppendSetAllowEmptyValue
Append Field = Value set in UpdateQuery.
updateQuery.AppendSet("address","new avenue")
AppendAndEqualCondition
Append Field = Value and condition in SelectQuery, will be ignored if value is empty.
selectQuery.AppendAndEqualCondition("name","name_value")
AppendAndCondition
Append and condition in SelectQuery.
selectQuery.AppendAndCondition("name=name_value")
AppendOrEqualCondition
Append Field = Value or condition in SelectQuery, will be ignored if value is empty.
selectQuery.AppendOrEqualCondition("name","name_value")
AppendOrCondition
Append or condition in SelectQuery.
selectQuery.AppendOrCondition("name=name_value")
Get Query String
updateQuery.build()
Example
jsonbSetPtr := `jsonb_set(additional_data,'{review_by}','"test"')`
updateQuery := query.UpdateQuery{
UpdateStatement: "test_table",
}
updateQuery.AppendSet("name", "nama")
updateQuery.AppendSet("value", "val")
updateQuery.AppendSet("location_id", "loc")
updateQuery.AppendSet("form_data", `jsonb_set(form_data,'{additional_data,review_by}','"test"')`)
updateQuery.AppendSet("client_data", &jsonbSetPtr)
updateQuery.AppendAndEqualCondition("name", "test")
updateQuery.AppendAndEqualCondition("location_id", "test")
updateQuery.AppendAndEqualCondition("value", 1)
fmt.Println(updateQuery.build())
Helper
BuildGroupedAndCondition
Create grouped and condition.
groupeAndCondition := selectQuery.BuildGroupedAndCondition("name='adam'", "name='eve'")
fmt.Println(groupedAndCondition)
BuildGroupedOrCondition
Create grouped or condition.
groupedOrCondition := selectQuery.BuildGroupedOrCondition("name='adam'", "name='eve'")
fmt.Println(groupedOrCondition)