[feat] Sqlite3Grammar 调整

This commit is contained in:
what 2023-06-15 11:46:17 +08:00
parent 71def3bd03
commit 9ec1f7b888
4 changed files with 67 additions and 36 deletions

View File

@ -323,10 +323,8 @@ func Clone(original *Builder) *Builder {
}
} else if original.Connection.Config.Driver == DriverSqlite3 {
newBuilder.Grammar = &Sqlite3Grammar{
MysqlGrammar: &MysqlGrammar{
Prefix: original.Grammar.GetTablePrefix(),
Builder: &newBuilder,
},
Prefix: original.Grammar.GetTablePrefix(),
Builder: &newBuilder,
}
} else {
panic("不支持的数据库类型")
@ -338,7 +336,8 @@ func (b *Builder) Clone() *Builder {
return Clone(b)
}
/*CloneWithout
/*
CloneWithout
CloneWithoutClone the query without the given properties.
*/
func CloneWithout(original *Builder, without ...string) *Builder {
@ -394,7 +393,7 @@ func (b *Builder) Select(columns ...interface{}) *Builder {
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 {
qStr, bindings := b.CreateSub(query)
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
}
//CreateSub Creates a subquery and parse it.
// CreateSub Creates a subquery and parse it.
func (b *Builder) CreateSub(query interface{}) (string, []interface{}) {
var builder *Builder
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)
}
//AddBinding Add a binding to the query.
// AddBinding Add a binding to the query.
func (b *Builder) AddBinding(value []interface{}, bindingType string) *Builder {
if _, ok := Bindings[bindingType]; !ok {
log.Panicf("invalid binding type:%s\n", bindingType)
@ -914,7 +913,7 @@ func (b *Builder) AddBinding(value []interface{}, bindingType string) *Builder {
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{}) {
for _, key := range BindingKeysInOrder {
if bindings, ok := b.Bindings[key]; ok {
@ -1244,7 +1243,7 @@ func (b *Builder) OrWhereIn(params ...interface{}) *Builder {
return b.WhereIn(params...)
}
//column values [ boolean ]
// column values [ boolean ]
func (b *Builder) WhereNotIn(params ...interface{}) *Builder {
params = append(params, BOOLEAN_AND, true)
return b.WhereIn(params...)
@ -1261,10 +1260,11 @@ func (b *Builder) OrWhereNotIn(params ...interface{}) *Builder {
/*
WhereNull Add a "where null" clause to the query.
params takes in below order:
1. column string
2. boolean string in [2]string{"and","or"}
3. type string "not"
params takes in below order:
1. column string
2. boolean string in [2]string{"and","or"}
3. type string "not"
*/
func (b *Builder) WhereNull(column interface{}, params ...interface{}) *Builder {
paramsLength := len(params)
@ -1347,11 +1347,10 @@ func (b *Builder) OrWhereNotNull(column interface{}) *Builder {
/*
WhereBetween Add a where between statement to the query.
params takes in below order:
1. WhereBetween(column string,values []interface{"min","max"})
2. WhereBetween(column string,values []interface{"min","max"},"and/or")
3. WhereBetween(column string,values []interface{"min","max","and/or",true/false})
params takes in below order:
1. WhereBetween(column string,values []interface{"min","max"})
2. WhereBetween(column string,values []interface{"min","max"},"and/or")
3. WhereBetween(column string,values []interface{"min","max","and/or",true/false})
*/
func (b *Builder) WhereBetween(params ...interface{}) *Builder {
paramsLength := len(params)
@ -1432,9 +1431,9 @@ func (b *Builder) WhereBetweenColumns(column string, values []interface{}, param
return b
}
//AddTimeBasedWhere Add a time based (year, month, day, time) statement to the query.
//params order : timefuncionname column operator value boolean
//minimum : timefuncionname column value
// AddTimeBasedWhere Add a time based (year, month, day, time) statement to the query.
// params order : timefuncionname column operator value boolean
// minimum : timefuncionname column value
func (b *Builder) AddTimeBasedWhere(params ...interface{}) *Builder {
paramsLength := len(params)
var timeType = params[0]
@ -1490,7 +1489,7 @@ func (b *Builder) AddTimeBasedWhere(params ...interface{}) *Builder {
return b
}
//column operator value boolean
// column operator value boolean
func (b *Builder) WhereDate(params ...interface{}) *Builder {
p := append([]interface{}{CONDITION_TYPE_DATE}, params...)
return b.AddTimeBasedWhere(p...)
@ -1567,7 +1566,7 @@ func (b *Builder) WhereSub(column string, operator string, value func(builder *B
return b
}
//WhereExists Add an exists clause to the query.
// WhereExists Add an exists clause to the query.
// 1. WhereExists(cb,"and",false)
// 2. WhereExists(cb,"and")
// 3. WhereExists(cb)
@ -1897,7 +1896,7 @@ func (b *Builder) WhereMap(params map[string]interface{}) *Builder {
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 {
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.
*/
func (b *Builder) Aggregate(dest interface{}, fn string, column ...string) (result sql.Result, err error) {
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.
1. When(true,func(builder *Builder))
2. When(true,func(builder *Builder),func(builder *Builder)) //with default callback
1. When(true,func(builder *Builder))
2. When(true,func(builder *Builder),func(builder *Builder)) //with default callback
*/
func (b *Builder) When(boolean bool, cb ...func(builder *Builder)) *Builder {
if boolean {
@ -2323,7 +2320,8 @@ func (b *Builder) Value(dest interface{}, column string) (sql.Result, error) {
return b.First(dest, column)
}
/*Reset
/*
Reset
reset bindings and components
*/
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
}
/*ApplyBeforeQueryCallBacks
/*
ApplyBeforeQueryCallBacks
Invoke the "before query" modification callbacks.
*/
func (b *Builder) ApplyBeforeQueryCallBacks(t string, items ...map[string]any) {

2
db.go
View File

@ -31,8 +31,6 @@ func Open(config map[string]DBConfig) *DB {
},
}
db.Connection("default")
Engine = &db
return Engine

View File

@ -1,5 +1,3 @@
package db
type Sqlite3Grammar struct {
*MysqlGrammar
}
type Sqlite3Grammar = MysqlGrammar

36
sqlite3_test.go Normal file
View 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())
}