From 9ec1f7b888f38ddd564245080b8c240d350fcbd3 Mon Sep 17 00:00:00 2001 From: what Date: Thu, 15 Jun 2023 11:46:17 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20Sqlite3Grammar=20=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builder.go | 61 +++++++++++++++++++++++----------------------- db.go | 2 -- sqlite3_grammar.go | 4 +-- sqlite3_test.go | 36 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 36 deletions(-) create mode 100644 sqlite3_test.go diff --git a/builder.go b/builder.go index 40fbffa..bf10342 100644 --- a/builder.go +++ b/builder.go @@ -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) { diff --git a/db.go b/db.go index 217fdef..c31962f 100644 --- a/db.go +++ b/db.go @@ -31,8 +31,6 @@ func Open(config map[string]DBConfig) *DB { }, } - db.Connection("default") - Engine = &db return Engine diff --git a/sqlite3_grammar.go b/sqlite3_grammar.go index 35042e1..d25a4d5 100644 --- a/sqlite3_grammar.go +++ b/sqlite3_grammar.go @@ -1,5 +1,3 @@ package db -type Sqlite3Grammar struct { - *MysqlGrammar -} +type Sqlite3Grammar = MysqlGrammar diff --git a/sqlite3_test.go b/sqlite3_test.go new file mode 100644 index 0000000..f939b14 --- /dev/null +++ b/sqlite3_test.go @@ -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()) +}