diff --git a/engine.go b/engine.go index b60234e0..1ab2eed2 100644 --- a/engine.go +++ b/engine.go @@ -191,11 +191,6 @@ func (engine *Engine) SupportInsertMany() bool { return engine.dialect.SupportInsertMany() } -func (engine *Engine) quoteColumns(columnStr string) string { - columns := strings.Split(columnStr, ",") - return engine.dialect.Quoter().Join(columns, ",") -} - // Quote Use QuoteStr quote the string sql func (engine *Engine) Quote(value string) string { value = strings.TrimSpace(value) @@ -222,18 +217,6 @@ func (engine *Engine) QuoteTo(buf *strings.Builder, value string) { engine.dialect.Quoter().QuoteTo(buf, value) } -/* -func (engine *Engine) quote(sql string) string { - return engine.dialect.Quote(sql) -}*/ - -// SqlType will be deprecated, please use SQLType instead -// -// Deprecated: use SQLType instead -func (engine *Engine) SqlType(c *schemas.Column) string { - return engine.SQLType(c) -} - // SQLType A simple wrapper to dialect's core.SqlType method func (engine *Engine) SQLType(c *schemas.Column) string { return engine.dialect.SQLType(c) @@ -335,14 +318,6 @@ func (engine *Engine) logSQL(sqlStr string, sqlArgs ...interface{}) { } } -// Sql provides raw sql input parameter. When you have a complex SQL statement -// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL. -// -// Deprecated: use SQL instead. -func (engine *Engine) Sql(querystring string, args ...interface{}) *Session { - return engine.SQL(querystring, args...) -} - // SQL method let's you manually write raw SQL and operate // For example: // @@ -597,13 +572,6 @@ func (engine *Engine) Where(query interface{}, args ...interface{}) *Session { return session.Where(query, args...) } -// Id will be deprecated, please use ID instead -func (engine *Engine) Id(id interface{}) *Session { - session := engine.NewSession() - session.isAutoClose = true - return session.Id(id) -} - // ID method provoide a condition as (id) = ? func (engine *Engine) ID(id interface{}) *Session { session := engine.NewSession() @@ -1092,23 +1060,9 @@ func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) { return session.IsTableExist(beanOrTableName) } -// IdOf get id from one struct -// -// Deprecated: use IDOf instead. -func (engine *Engine) IdOf(bean interface{}) schemas.PK { - return engine.IDOf(bean) -} - // IDOf get id from one struct func (engine *Engine) IDOf(bean interface{}) schemas.PK { - return engine.IdOfV(reflect.ValueOf(bean)) -} - -// IdOfV get id from one value of struct -// -// Deprecated: use IDOfV instead. -func (engine *Engine) IdOfV(rv reflect.Value) schemas.PK { - return engine.IDOfV(rv) + return engine.IDOfV(reflect.ValueOf(bean)) } // IDOfV get id from one value of struct diff --git a/helpers.go b/helpers.go index 9aa3b1cb..75393ae3 100644 --- a/helpers.go +++ b/helpers.go @@ -214,10 +214,3 @@ func eraseAny(value string, strToErase ...string) string { return replacer.Replace(value) } - -func quoteColumns(cols []string, quoteFunc func(string) string, sep string) string { - for i := range cols { - cols[i] = quoteFunc(cols[i]) - } - return strings.Join(cols, sep+" ") -} diff --git a/helpers_test.go b/helpers_test.go index caf7b9f0..fc9ece27 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -16,12 +16,3 @@ func TestEraseAny(t *testing.T) { assert.EqualValues(t, "SELECT * FROM table.[table_name]", eraseAny(raw, "`")) assert.EqualValues(t, "SELECT * FROM table.table_name", eraseAny(raw, "`", "[", "]")) } - -func TestQuoteColumns(t *testing.T) { - cols := []string{"f1", "f2", "f3"} - quoteFunc := func(value string) string { - return "[" + value + "]" - } - - assert.EqualValues(t, "[f1], [f2], [f3]", quoteColumns(cols, quoteFunc, ",")) -} diff --git a/schemas/quote.go b/schemas/quote.go index e3571e34..5dac6d27 100644 --- a/schemas/quote.go +++ b/schemas/quote.go @@ -5,7 +5,6 @@ package schemas import ( - "fmt" "strings" ) @@ -76,7 +75,7 @@ func (q Quoter) Trim(s string) string { return s } -func TrimSpaceJoin(a []string, sep string) string { +func (q Quoter) Join(a []string, sep string) string { switch len(a) { case 0: return "" @@ -90,19 +89,21 @@ func TrimSpaceJoin(a []string, sep string) string { var b strings.Builder b.Grow(n) - b.WriteString(strings.TrimSpace(a[0])) - for _, s := range a[1:] { - b.WriteString(sep) + for i, s := range a { + if i > 0 { + b.WriteString(sep) + } + if q[0] != "" { + b.WriteString(q[0]) + } b.WriteString(strings.TrimSpace(s)) + if q[1] != "" { + b.WriteString(q[1]) + } } return b.String() } -func (q Quoter) Join(s []string, splitter string) string { - //return fmt.Sprintf("%s%s%s", q[0], TrimSpaceJoin(s, fmt.Sprintf("%s%s%s", q[1], splitter, q[0])), q[1]) - return q.Quote(TrimSpaceJoin(s, fmt.Sprintf("%s%s%s", q[1], splitter, q[0]))) -} - func (q Quoter) QuoteTo(buf *strings.Builder, value string) { if q.IsEmpty() { buf.WriteString(value) diff --git a/schemas/quote_test.go b/schemas/quote_test.go index af773c8b..5eea05d3 100644 --- a/schemas/quote_test.go +++ b/schemas/quote_test.go @@ -45,3 +45,13 @@ func TestQuoteTo(t *testing.T) { quoter.QuoteTo(buf, "noquote") assert.EqualValues(t, "noquote", buf.String()) } + +func TestJoin(t *testing.T) { + cols := []string{"f1", "f2", "f3"} + quoter := Quoter{"[", "]"} + + assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", ")) + + quoter = Quoter{"", ""} + assert.EqualValues(t, "f1, f2, f3", quoter.Join(cols, ", ")) +} diff --git a/session_cond.go b/session_cond.go index b16bdea8..72e3abc3 100644 --- a/session_cond.go +++ b/session_cond.go @@ -6,14 +6,6 @@ package xorm import "xorm.io/builder" -// Sql provides raw sql input parameter. When you have a complex SQL statement -// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL. -// -// Deprecated: use SQL instead. -func (session *Session) Sql(query string, args ...interface{}) *Session { - return session.SQL(query, args...) -} - // SQL provides raw sql input parameter. When you have a complex SQL statement // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL. func (session *Session) SQL(query interface{}, args ...interface{}) *Session { @@ -39,13 +31,6 @@ func (session *Session) Or(query interface{}, args ...interface{}) *Session { return session } -// Id provides converting id as a query condition -// -// Deprecated: use ID instead -func (session *Session) Id(id interface{}) *Session { - return session.ID(id) -} - // ID provides converting id as a query condition func (session *Session) ID(id interface{}) *Session { session.statement.ID(id) diff --git a/session_find.go b/session_find.go index 7ae54a5f..566e83dd 100644 --- a/session_find.go +++ b/session_find.go @@ -142,7 +142,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) if session.statement.JoinStr == "" { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.engine.quoteColumns(session.statement.GroupByStr) + columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr) } else { columnStr = session.statement.genColumnStr() } @@ -150,7 +150,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) } else { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.engine.quoteColumns(session.statement.GroupByStr) + columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr) } else { columnStr = "*" } @@ -417,7 +417,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in } else { session.engine.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean) - pk := session.engine.IdOf(bean) + pk := session.engine.IDOf(bean) xid, err := pk.ToString() if err != nil { return err diff --git a/session_insert.go b/session_insert.go index 83def3fc..b788e629 100644 --- a/session_insert.go +++ b/session_insert.go @@ -251,19 +251,21 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error } cleanupProcessorsClosures(&session.beforeClosures) + quoter := session.engine.dialect.Quoter() var sql string + colStr := quoter.Join(colNames, ",") if session.engine.dialect.DBType() == schemas.ORACLE { temp := fmt.Sprintf(") INTO %s (%v) VALUES (", - session.engine.Quote(tableName), - quoteColumns(colNames, session.engine.Quote, ",")) + quoter.Quote(tableName), + colStr) sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL", - session.engine.Quote(tableName), - quoteColumns(colNames, session.engine.Quote, ","), + quoter.Quote(tableName), + colStr, strings.Join(colMultiPlaces, temp)) } else { sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)", - session.engine.Quote(tableName), - quoteColumns(colNames, session.engine.Quote, ","), + quoter.Quote(tableName), + colStr, strings.Join(colMultiPlaces, "),(")) } res, err := session.exec(sql, args...) diff --git a/session_query.go b/session_query.go index 0623c90c..afed4bcb 100644 --- a/session_query.go +++ b/session_query.go @@ -36,7 +36,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa if session.statement.JoinStr == "" { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.engine.quoteColumns(session.statement.GroupByStr) + columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr) } else { columnStr = session.statement.genColumnStr() } @@ -44,7 +44,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa } else { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.engine.quoteColumns(session.statement.GroupByStr) + columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr) } else { columnStr = "*" } diff --git a/statement.go b/statement.go index aa218f77..9dc5bf52 100644 --- a/statement.go +++ b/statement.go @@ -940,6 +940,11 @@ func (statement *Statement) genConds(bean interface{}) (string, []interface{}, e return builder.ToSQL(statement.cond) } +func (statement *Statement) quoteColumnStr(columnStr string) string { + columns := strings.Split(columnStr, ",") + return statement.Engine.dialect.Quoter().Join(columns, ",") +} + func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, error) { v := rValue(bean) isStruct := v.Kind() == reflect.Struct @@ -955,7 +960,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, if len(statement.JoinStr) == 0 { if len(columnStr) == 0 { if len(statement.GroupByStr) > 0 { - columnStr = statement.Engine.quoteColumns(statement.GroupByStr) + columnStr = statement.quoteColumnStr(statement.GroupByStr) } else { columnStr = statement.genColumnStr() } @@ -963,7 +968,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, } else { if len(columnStr) == 0 { if len(statement.GroupByStr) > 0 { - columnStr = statement.Engine.quoteColumns(statement.GroupByStr) + columnStr = statement.quoteColumnStr(statement.GroupByStr) } } }