[feat] Sqlite3Grammar 调整
This commit is contained in:
parent
71def3bd03
commit
9ec1f7b888
61
builder.go
61
builder.go
@ -323,10 +323,8 @@ func Clone(original *Builder) *Builder {
|
|||||||
}
|
}
|
||||||
} else if original.Connection.Config.Driver == DriverSqlite3 {
|
} else if original.Connection.Config.Driver == DriverSqlite3 {
|
||||||
newBuilder.Grammar = &Sqlite3Grammar{
|
newBuilder.Grammar = &Sqlite3Grammar{
|
||||||
MysqlGrammar: &MysqlGrammar{
|
Prefix: original.Grammar.GetTablePrefix(),
|
||||||
Prefix: original.Grammar.GetTablePrefix(),
|
Builder: &newBuilder,
|
||||||
Builder: &newBuilder,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic("不支持的数据库类型")
|
panic("不支持的数据库类型")
|
||||||
@ -338,7 +336,8 @@ func (b *Builder) Clone() *Builder {
|
|||||||
return Clone(b)
|
return Clone(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*CloneWithout
|
/*
|
||||||
|
CloneWithout
|
||||||
CloneWithoutClone the query without the given properties.
|
CloneWithoutClone the query without the given properties.
|
||||||
*/
|
*/
|
||||||
func CloneWithout(original *Builder, without ...string) *Builder {
|
func CloneWithout(original *Builder, without ...string) *Builder {
|
||||||
@ -394,7 +393,7 @@ func (b *Builder) Select(columns ...interface{}) *Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//SelectSub Add a subselect expression to the query.
|
// SelectSub Add a subselect expression to the query.
|
||||||
func (b *Builder) SelectSub(query interface{}, as string) *Builder {
|
func (b *Builder) SelectSub(query interface{}, as string) *Builder {
|
||||||
qStr, bindings := b.CreateSub(query)
|
qStr, bindings := b.CreateSub(query)
|
||||||
queryStr := fmt.Sprintf("( %s ) as %s", qStr, b.Grammar.Wrap(as))
|
queryStr := fmt.Sprintf("( %s ) as %s", qStr, b.Grammar.Wrap(as))
|
||||||
@ -433,7 +432,7 @@ func (b *Builder) SelectRaw(expression string, bindings ...[]interface{}) *Build
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//CreateSub Creates a subquery and parse it.
|
// CreateSub Creates a subquery and parse it.
|
||||||
func (b *Builder) CreateSub(query interface{}) (string, []interface{}) {
|
func (b *Builder) CreateSub(query interface{}) (string, []interface{}) {
|
||||||
var builder *Builder
|
var builder *Builder
|
||||||
if bT, ok := query.(*Builder); ok {
|
if bT, ok := query.(*Builder); ok {
|
||||||
@ -899,7 +898,7 @@ func (b *Builder) RightJoinSub(query interface{}, as string, first interface{},
|
|||||||
return b.join(Raw(expr), first, operator, second, joinType, false)
|
return b.join(Raw(expr), first, operator, second, joinType, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddBinding Add a binding to the query.
|
// AddBinding Add a binding to the query.
|
||||||
func (b *Builder) AddBinding(value []interface{}, bindingType string) *Builder {
|
func (b *Builder) AddBinding(value []interface{}, bindingType string) *Builder {
|
||||||
if _, ok := Bindings[bindingType]; !ok {
|
if _, ok := Bindings[bindingType]; !ok {
|
||||||
log.Panicf("invalid binding type:%s\n", bindingType)
|
log.Panicf("invalid binding type:%s\n", bindingType)
|
||||||
@ -914,7 +913,7 @@ func (b *Builder) AddBinding(value []interface{}, bindingType string) *Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetBindings Get the current query value bindings in a flattened slice.
|
// GetBindings Get the current query value bindings in a flattened slice.
|
||||||
func (b *Builder) GetBindings() (res []interface{}) {
|
func (b *Builder) GetBindings() (res []interface{}) {
|
||||||
for _, key := range BindingKeysInOrder {
|
for _, key := range BindingKeysInOrder {
|
||||||
if bindings, ok := b.Bindings[key]; ok {
|
if bindings, ok := b.Bindings[key]; ok {
|
||||||
@ -1244,7 +1243,7 @@ func (b *Builder) OrWhereIn(params ...interface{}) *Builder {
|
|||||||
return b.WhereIn(params...)
|
return b.WhereIn(params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//column values [ boolean ]
|
// column values [ boolean ]
|
||||||
func (b *Builder) WhereNotIn(params ...interface{}) *Builder {
|
func (b *Builder) WhereNotIn(params ...interface{}) *Builder {
|
||||||
params = append(params, BOOLEAN_AND, true)
|
params = append(params, BOOLEAN_AND, true)
|
||||||
return b.WhereIn(params...)
|
return b.WhereIn(params...)
|
||||||
@ -1261,10 +1260,11 @@ func (b *Builder) OrWhereNotIn(params ...interface{}) *Builder {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
WhereNull Add a "where null" clause to the query.
|
WhereNull Add a "where null" clause to the query.
|
||||||
params takes in below order:
|
|
||||||
1. column string
|
params takes in below order:
|
||||||
2. boolean string in [2]string{"and","or"}
|
1. column string
|
||||||
3. type string "not"
|
2. boolean string in [2]string{"and","or"}
|
||||||
|
3. type string "not"
|
||||||
*/
|
*/
|
||||||
func (b *Builder) WhereNull(column interface{}, params ...interface{}) *Builder {
|
func (b *Builder) WhereNull(column interface{}, params ...interface{}) *Builder {
|
||||||
paramsLength := len(params)
|
paramsLength := len(params)
|
||||||
@ -1347,11 +1347,10 @@ func (b *Builder) OrWhereNotNull(column interface{}) *Builder {
|
|||||||
/*
|
/*
|
||||||
WhereBetween Add a where between statement to the query.
|
WhereBetween Add a where between statement to the query.
|
||||||
|
|
||||||
params takes in below order:
|
params takes in below order:
|
||||||
1. WhereBetween(column string,values []interface{"min","max"})
|
1. WhereBetween(column string,values []interface{"min","max"})
|
||||||
2. WhereBetween(column string,values []interface{"min","max"},"and/or")
|
2. WhereBetween(column string,values []interface{"min","max"},"and/or")
|
||||||
3. WhereBetween(column string,values []interface{"min","max","and/or",true/false})
|
3. WhereBetween(column string,values []interface{"min","max","and/or",true/false})
|
||||||
|
|
||||||
*/
|
*/
|
||||||
func (b *Builder) WhereBetween(params ...interface{}) *Builder {
|
func (b *Builder) WhereBetween(params ...interface{}) *Builder {
|
||||||
paramsLength := len(params)
|
paramsLength := len(params)
|
||||||
@ -1432,9 +1431,9 @@ func (b *Builder) WhereBetweenColumns(column string, values []interface{}, param
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddTimeBasedWhere Add a time based (year, month, day, time) statement to the query.
|
// AddTimeBasedWhere Add a time based (year, month, day, time) statement to the query.
|
||||||
//params order : timefuncionname column operator value boolean
|
// params order : timefuncionname column operator value boolean
|
||||||
//minimum : timefuncionname column value
|
// minimum : timefuncionname column value
|
||||||
func (b *Builder) AddTimeBasedWhere(params ...interface{}) *Builder {
|
func (b *Builder) AddTimeBasedWhere(params ...interface{}) *Builder {
|
||||||
paramsLength := len(params)
|
paramsLength := len(params)
|
||||||
var timeType = params[0]
|
var timeType = params[0]
|
||||||
@ -1490,7 +1489,7 @@ func (b *Builder) AddTimeBasedWhere(params ...interface{}) *Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//column operator value boolean
|
// column operator value boolean
|
||||||
func (b *Builder) WhereDate(params ...interface{}) *Builder {
|
func (b *Builder) WhereDate(params ...interface{}) *Builder {
|
||||||
p := append([]interface{}{CONDITION_TYPE_DATE}, params...)
|
p := append([]interface{}{CONDITION_TYPE_DATE}, params...)
|
||||||
return b.AddTimeBasedWhere(p...)
|
return b.AddTimeBasedWhere(p...)
|
||||||
@ -1567,7 +1566,7 @@ func (b *Builder) WhereSub(column string, operator string, value func(builder *B
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//WhereExists Add an exists clause to the query.
|
// WhereExists Add an exists clause to the query.
|
||||||
// 1. WhereExists(cb,"and",false)
|
// 1. WhereExists(cb,"and",false)
|
||||||
// 2. WhereExists(cb,"and")
|
// 2. WhereExists(cb,"and")
|
||||||
// 3. WhereExists(cb)
|
// 3. WhereExists(cb)
|
||||||
@ -1897,7 +1896,7 @@ func (b *Builder) WhereMap(params map[string]interface{}) *Builder {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddNestedWhereQuery Add another query builder as a nested where to the query builder.
|
// AddNestedWhereQuery Add another query builder as a nested where to the query builder.
|
||||||
func (b *Builder) AddNestedWhereQuery(builder *Builder, boolean string) *Builder {
|
func (b *Builder) AddNestedWhereQuery(builder *Builder, boolean string) *Builder {
|
||||||
|
|
||||||
if len(builder.Wheres) > 0 {
|
if len(builder.Wheres) > 0 {
|
||||||
@ -1994,7 +1993,6 @@ func (b *Builder) DoesntExist() (notExists bool, err error) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Aggregate Execute an aggregate function on the database.
|
Aggregate Execute an aggregate function on the database.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
func (b *Builder) Aggregate(dest interface{}, fn string, column ...string) (result sql.Result, err error) {
|
func (b *Builder) Aggregate(dest interface{}, fn string, column ...string) (result sql.Result, err error) {
|
||||||
b.Dest = dest
|
b.Dest = dest
|
||||||
@ -2306,9 +2304,8 @@ func (b *Builder) Pluck(dest interface{}, params interface{}) (sql.Result, error
|
|||||||
/*
|
/*
|
||||||
When Apply the callback if the given "value" is truthy.
|
When Apply the callback if the given "value" is truthy.
|
||||||
|
|
||||||
1. When(true,func(builder *Builder))
|
1. When(true,func(builder *Builder))
|
||||||
2. When(true,func(builder *Builder),func(builder *Builder)) //with default callback
|
2. When(true,func(builder *Builder),func(builder *Builder)) //with default callback
|
||||||
|
|
||||||
*/
|
*/
|
||||||
func (b *Builder) When(boolean bool, cb ...func(builder *Builder)) *Builder {
|
func (b *Builder) When(boolean bool, cb ...func(builder *Builder)) *Builder {
|
||||||
if boolean {
|
if boolean {
|
||||||
@ -2323,7 +2320,8 @@ func (b *Builder) Value(dest interface{}, column string) (sql.Result, error) {
|
|||||||
return b.First(dest, column)
|
return b.First(dest, column)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Reset
|
/*
|
||||||
|
Reset
|
||||||
reset bindings and components
|
reset bindings and components
|
||||||
*/
|
*/
|
||||||
func (b *Builder) Reset(targets ...string) *Builder {
|
func (b *Builder) Reset(targets ...string) *Builder {
|
||||||
@ -2382,7 +2380,8 @@ func (b *Builder) After(callback func(builder *Builder, t string, res sql.Result
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
/*ApplyBeforeQueryCallBacks
|
/*
|
||||||
|
ApplyBeforeQueryCallBacks
|
||||||
Invoke the "before query" modification callbacks.
|
Invoke the "before query" modification callbacks.
|
||||||
*/
|
*/
|
||||||
func (b *Builder) ApplyBeforeQueryCallBacks(t string, items ...map[string]any) {
|
func (b *Builder) ApplyBeforeQueryCallBacks(t string, items ...map[string]any) {
|
||||||
|
2
db.go
2
db.go
@ -31,8 +31,6 @@ func Open(config map[string]DBConfig) *DB {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Connection("default")
|
|
||||||
|
|
||||||
Engine = &db
|
Engine = &db
|
||||||
|
|
||||||
return Engine
|
return Engine
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
type Sqlite3Grammar struct {
|
type Sqlite3Grammar = MysqlGrammar
|
||||||
*MysqlGrammar
|
|
||||||
}
|
|
||||||
|
36
sqlite3_test.go
Normal file
36
sqlite3_test.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var conn *Connection
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
database := Open(map[string]DBConfig{
|
||||||
|
"sqlite3": {
|
||||||
|
Driver: "sqlite3",
|
||||||
|
File: ":memory:",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
conn = database.Connection("sqlite3")
|
||||||
|
|
||||||
|
// 执行测试
|
||||||
|
code := m.Run()
|
||||||
|
|
||||||
|
// 退出测试
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteQuery(t *testing.T) {
|
||||||
|
dest := []map[string]any{}
|
||||||
|
|
||||||
|
t.Log(conn.Select("select 1 as a, 2 as b", nil, &dest))
|
||||||
|
t.Log("result: ", dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteBuilder(t *testing.T) {
|
||||||
|
t.Log(conn.Query().FromSub("select 1 as a, 2 as b", "tb").ToSql())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user