Added ParameterisedTable function - takes a parameterised string and query parameters and applies them after FROM in the generated SQL
This commit is contained in:
parent
0c98e7d712
commit
f394b1b51f
5
main.go
5
main.go
@ -518,6 +518,11 @@ func (s *DB) Table(name string) *DB {
|
|||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Table specify the table you would like to run db operations
|
||||||
|
func (s *DB) ParameterisedTable(query string, args ...interface{}) *DB {
|
||||||
|
return s.clone().search.ParameterisedTable(query, args...).db
|
||||||
|
}
|
||||||
|
|
||||||
// Debug start debug mode
|
// Debug start debug mode
|
||||||
func (s *DB) Debug() *DB {
|
func (s *DB) Debug() *DB {
|
||||||
return s.clone().LogMode(true)
|
return s.clone().LogMode(true)
|
||||||
|
39
scope.go
39
scope.go
@ -329,8 +329,12 @@ type dbTabler interface {
|
|||||||
|
|
||||||
// TableName return table name
|
// TableName return table name
|
||||||
func (scope *Scope) TableName() string {
|
func (scope *Scope) TableName() string {
|
||||||
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
if scope.Search != nil && scope.Search.tableName != nil {
|
||||||
return scope.Search.tableName
|
if str, ok := scope.Search.tableName.(string); ok {
|
||||||
|
return str
|
||||||
|
} else if exp, ok := scope.Search.tableName.(*expr); ok {
|
||||||
|
return exp.expr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tabler, ok := scope.Value.(tabler); ok {
|
if tabler, ok := scope.Value.(tabler); ok {
|
||||||
@ -346,11 +350,18 @@ func (scope *Scope) TableName() string {
|
|||||||
|
|
||||||
// QuotedTableName return quoted table name
|
// QuotedTableName return quoted table name
|
||||||
func (scope *Scope) QuotedTableName() (name string) {
|
func (scope *Scope) QuotedTableName() (name string) {
|
||||||
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
if scope.Search != nil && scope.Search.tableName != nil {
|
||||||
if strings.Contains(scope.Search.tableName, " ") {
|
var tableName string
|
||||||
return scope.Search.tableName
|
if str, ok := scope.Search.tableName.(string); ok {
|
||||||
|
tableName = str
|
||||||
|
} else if exp, ok := scope.Search.tableName.(*expr); ok {
|
||||||
|
tableName = exp.expr
|
||||||
}
|
}
|
||||||
return scope.Quote(scope.Search.tableName)
|
|
||||||
|
if strings.Contains(tableName, " ") {
|
||||||
|
return tableName
|
||||||
|
}
|
||||||
|
return scope.Quote(tableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return scope.Quote(scope.TableName())
|
return scope.Quote(scope.TableName())
|
||||||
@ -859,11 +870,25 @@ func (scope *Scope) joinsSQL() string {
|
|||||||
return strings.Join(joinConditions, " ") + " "
|
return strings.Join(joinConditions, " ") + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) tableSQL() string {
|
||||||
|
if str, ok := scope.Search.tableName.(string); ok {
|
||||||
|
return str
|
||||||
|
} else if expr, ok := scope.Search.tableName.(*expr); ok {
|
||||||
|
exp := expr.expr
|
||||||
|
for _, arg := range expr.args {
|
||||||
|
exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
|
||||||
|
}
|
||||||
|
return exp
|
||||||
|
} else {
|
||||||
|
return scope.QuotedTableName()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (scope *Scope) prepareQuerySQL() {
|
func (scope *Scope) prepareQuerySQL() {
|
||||||
if scope.Search.raw {
|
if scope.Search.raw {
|
||||||
scope.Raw(scope.CombinedConditionSql())
|
scope.Raw(scope.CombinedConditionSql())
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSQL(), scope.QuotedTableName(), scope.CombinedConditionSql()))
|
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSQL(), scope.tableSQL(), scope.CombinedConditionSql()))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
15
search.go
15
search.go
@ -20,7 +20,7 @@ type search struct {
|
|||||||
offset interface{}
|
offset interface{}
|
||||||
limit interface{}
|
limit interface{}
|
||||||
group string
|
group string
|
||||||
tableName string
|
tableName interface{}
|
||||||
raw bool
|
raw bool
|
||||||
Unscoped bool
|
Unscoped bool
|
||||||
ignoreOrderQuery bool
|
ignoreOrderQuery bool
|
||||||
@ -133,6 +133,19 @@ func (s *search) unscoped() *search {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func (s *search) Table(name string) *search {
|
||||||
|
// s.tableName = name
|
||||||
|
// return s
|
||||||
|
//}
|
||||||
|
|
||||||
|
func (s *search) ParameterisedTable(query string, values ...interface{}) *search {
|
||||||
|
s.tableName = &expr{
|
||||||
|
expr: query,
|
||||||
|
args: values,
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *search) Table(name string) *search {
|
func (s *search) Table(name string) *search {
|
||||||
s.tableName = name
|
s.tableName = name
|
||||||
return s
|
return s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user