diff --git a/pkg/util/xorm/dialect_oracle.go b/pkg/util/xorm/dialect_oracle.go index 15010ca5a37..d23ab1678af 100644 --- a/pkg/util/xorm/dialect_oracle.go +++ b/pkg/util/xorm/dialect_oracle.go @@ -755,7 +755,7 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum } if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { - return nil, nil, fmt.Errorf("Unknown colType %v %v", *dataType, col.SQLType) + return nil, nil, fmt.Errorf("unknown colType %v %v", *dataType, col.SQLType) } col.Length = dataLen diff --git a/pkg/util/xorm/dialect_postgres.go b/pkg/util/xorm/dialect_postgres.go index ac6d4fe8960..7a941002f1b 100644 --- a/pkg/util/xorm/dialect_postgres.go +++ b/pkg/util/xorm/dialect_postgres.go @@ -1040,7 +1040,7 @@ WHERE c.relkind = 'r'::char AND c.relname = $1%s AND f.attnum > 0 ORDER BY f.att col.SQLType = core.SQLType{Name: strings.ToUpper(dataType), DefaultLength: 0, DefaultLength2: 0} } if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { - return nil, nil, fmt.Errorf("Unknown colType: %v", dataType) + return nil, nil, fmt.Errorf("unknown colType: %v", dataType) } col.Length = maxLen @@ -1108,7 +1108,7 @@ func getIndexColName(indexdef string) []string { func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) { args := []interface{}{tableName} - s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1") + s := "SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1" if len(db.Schema) != 0 { args = append(args, db.Schema) s = s + " AND schemaname=$2" diff --git a/pkg/util/xorm/dialect_sqlite3.go b/pkg/util/xorm/dialect_sqlite3.go index 0a290f3c480..1cd46dde5c2 100644 --- a/pkg/util/xorm/dialect_sqlite3.go +++ b/pkg/util/xorm/dialect_sqlite3.go @@ -248,12 +248,6 @@ func (db *sqlite3) ForUpdateSql(query string) string { return query } -/*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) { - args := []interface{}{tableName} - sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))" - return sql, args -}*/ - func (db *sqlite3) IsColumnExist(tableName, colName string) (bool, error) { args := []interface{}{tableName} query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))" diff --git a/pkg/util/xorm/engine.go b/pkg/util/xorm/engine.go index 350b20be493..dc91346f887 100644 --- a/pkg/util/xorm/engine.go +++ b/pkg/util/xorm/engine.go @@ -5,15 +5,11 @@ package xorm import ( - "bufio" - "bytes" "context" "database/sql" "encoding/gob" "errors" "fmt" - "io" - "os" "reflect" "strconv" "strings" @@ -62,11 +58,6 @@ func (engine *Engine) CondDeleted(col *core.Column) builder.Cond { var cond = builder.NewCond() if col.SQLType.IsNumeric() { cond = builder.Eq{col.Name: 0} - } else { - // FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. - if engine.dialect.DBType() != core.MSSQL { - cond = builder.Eq{col.Name: zeroTime1} - } } if col.Nullable { @@ -1256,54 +1247,6 @@ func (engine *Engine) SumsInt(bean interface{}, colNames ...string) ([]int64, er return session.SumsInt(bean, colNames...) } -// ImportFile SQL DDL file -func (engine *Engine) ImportFile(ddlPath string) ([]sql.Result, error) { - file, err := os.Open(ddlPath) - if err != nil { - return nil, err - } - defer file.Close() - return engine.Import(file) -} - -// Import SQL DDL from io.Reader -func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) { - var results []sql.Result - var lastError error - scanner := bufio.NewScanner(r) - - semiColSpliter := func(data []byte, atEOF bool) (advance int, token []byte, err error) { - if atEOF && len(data) == 0 { - return 0, nil, nil - } - if i := bytes.IndexByte(data, ';'); i >= 0 { - return i + 1, data[0:i], nil - } - // If we're at EOF, we have a final, non-terminated line. Return it. - if atEOF { - return len(data), data, nil - } - // Request more data. - return 0, nil, nil - } - - scanner.Split(semiColSpliter) - - for scanner.Scan() { - query := strings.Trim(scanner.Text(), " \t\n\r") - if len(query) > 0 { - engine.logSQL(query) - result, err := engine.DB().Exec(query) - results = append(results, result) - if err != nil { - return nil, err - } - } - } - - return results, lastError -} - // nowTime return current time func (engine *Engine) nowTime(col *core.Column) (interface{}, time.Time) { t := time.Now() @@ -1339,11 +1282,7 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{} case core.DateTime, core.TimeStamp, core.Varchar: // !DarthPestilane! format time when sqlTypeName is core.Varchar. v = t.Format("2006-01-02 15:04:05") case core.TimeStampz: - if engine.dialect.DBType() == core.MSSQL { - v = t.Format("2006-01-02T15:04:05.9999999Z07:00") - } else { - v = t.Format(time.RFC3339Nano) - } + v = t.Format(time.RFC3339Nano) case core.BigInt, core.Int: v = t.Unix() default: diff --git a/pkg/util/xorm/engine_cond.go b/pkg/util/xorm/engine_cond.go index 17f50bd7df1..6ed52e861a8 100644 --- a/pkg/util/xorm/engine_cond.go +++ b/pkg/util/xorm/engine_cond.go @@ -31,9 +31,6 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{}, continue } - if engine.dialect.DBType() == core.MSSQL && (col.SQLType.Name == core.Text || col.SQLType.IsBlob() || col.SQLType.Name == core.TimeStampz) { - continue - } if col.SQLType.IsJson() { continue } diff --git a/pkg/util/xorm/engine_context.go b/pkg/util/xorm/engine_context.go deleted file mode 100644 index 12200264ae8..00000000000 --- a/pkg/util/xorm/engine_context.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2019 The Xorm Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.8 -// +build go1.8 - -package xorm - -import "context" - -// Context creates a session with the context -func (engine *Engine) Context(ctx context.Context) *Session { - session := engine.NewSession() - session.isAutoClose = true - return session.Context(ctx) -} - -// SetDefaultContext set the default context -func (engine *Engine) SetDefaultContext(ctx context.Context) { - engine.defaultContext = ctx -} - -// PingContext tests if database is alive -func (engine *Engine) PingContext(ctx context.Context) error { - session := engine.NewSession() - defer session.Close() - return session.PingContext(ctx) -} diff --git a/pkg/util/xorm/engine_table.go b/pkg/util/xorm/engine_table.go index 77c595618ad..d9153c591fc 100644 --- a/pkg/util/xorm/engine_table.go +++ b/pkg/util/xorm/engine_table.go @@ -43,15 +43,6 @@ func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string return tbName } -// tbName get some table's table name -func (session *Session) tbNameNoSchema(table *core.Table) string { - if len(session.statement.AltTableName) > 0 { - return session.statement.AltTableName - } - - return table.Name -} - func (engine *Engine) tbNameNoSchema(tablename interface{}) string { switch tablename.(type) { case []string: diff --git a/pkg/util/xorm/interface.go b/pkg/util/xorm/interface.go index 4ebef3b2856..9bde13a391e 100644 --- a/pkg/util/xorm/interface.go +++ b/pkg/util/xorm/interface.go @@ -5,7 +5,6 @@ package xorm import ( - "context" "database/sql" "reflect" "time" @@ -73,7 +72,6 @@ type EngineInterface interface { Before(func(interface{})) *Session Charset(charset string) *Session - Context(context.Context) *Session CreateTables(...interface{}) error DBMetas() ([]*core.Table, error) Dialect() core.Dialect diff --git a/pkg/util/xorm/session_delete.go b/pkg/util/xorm/session_delete.go index 95f3e0770df..b73391a07f4 100644 --- a/pkg/util/xorm/session_delete.go +++ b/pkg/util/xorm/session_delete.go @@ -78,9 +78,6 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } else { deleteSQL += " WHERE " + inSQL } - // TODO: how to handle delete limit on mssql? - case core.MSSQL: - return 0, ErrNotImplemented default: deleteSQL += orderSQL } @@ -113,9 +110,6 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } else { realSQL += " WHERE " + inSQL } - // TODO: how to handle delete limit on mssql? - case core.MSSQL: - return 0, ErrNotImplemented default: realSQL += orderSQL } @@ -166,7 +160,6 @@ func (session *Session) Delete(bean interface{}) (int64, error) { } } cleanupProcessorsClosures(&session.afterClosures) - // -- return res.RowsAffected() } diff --git a/pkg/util/xorm/session_exist.go b/pkg/util/xorm/session_exist.go index 03c51cee3cf..7082566a2da 100644 --- a/pkg/util/xorm/session_exist.go +++ b/pkg/util/xorm/session_exist.go @@ -45,18 +45,14 @@ func (session *Session) Exist(bean ...interface{}) (bool, error) { return false, err } - if session.engine.dialect.DBType() == core.MSSQL { - sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s WHERE %s", tableName, joinStr, condSQL) - } else if session.engine.dialect.DBType() == core.ORACLE { + if session.engine.dialect.DBType() == core.ORACLE { sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) %s AND ROWNUM=1", tableName, joinStr, condSQL) } else { sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE %s LIMIT 1", tableName, joinStr, condSQL) } args = condArgs } else { - if session.engine.dialect.DBType() == core.MSSQL { - sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s", tableName, joinStr) - } else if session.engine.dialect.DBType() == core.ORACLE { + if session.engine.dialect.DBType() == core.ORACLE { sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE ROWNUM=1", tableName, joinStr) } else { sqlStr = fmt.Sprintf("SELECT * FROM %s %s LIMIT 1", tableName, joinStr) diff --git a/pkg/util/xorm/session_insert.go b/pkg/util/xorm/session_insert.go index b5018991a5d..8111e6cb39b 100644 --- a/pkg/util/xorm/session_insert.go +++ b/pkg/util/xorm/session_insert.go @@ -353,9 +353,6 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { var tableName = session.statement.TableName() var output string - if session.engine.dialect.DBType() == core.MSSQL && len(table.AutoIncrement) > 0 { - output = fmt.Sprintf(" OUTPUT Inserted.%s", table.AutoIncrement) - } var buf = builder.NewWriter() if _, err := buf.WriteString(fmt.Sprintf("INSERT INTO %s", session.engine.Quote(tableName))); err != nil { @@ -503,7 +500,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { aiValue.Set(int64ToIntValue(id, aiValue.Type())) return 1, nil - } else if len(table.AutoIncrement) > 0 && (session.engine.dialect.DBType() == core.POSTGRES || session.engine.dialect.DBType() == core.MSSQL) { + } else if len(table.AutoIncrement) > 0 && (session.engine.dialect.DBType() == core.POSTGRES) { res, err := session.queryBytes(sqlStr, args...) if err != nil { diff --git a/pkg/util/xorm/session_update.go b/pkg/util/xorm/session_update.go index e13f1d56a0a..21465a5de8e 100644 --- a/pkg/util/xorm/session_update.go +++ b/pkg/util/xorm/session_update.go @@ -242,23 +242,6 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if len(condSQL) > 0 { condSQL = "WHERE " + condSQL } - } else if st.Engine.dialect.DBType() == core.MSSQL { - if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL && - table != nil && len(table.PrimaryKeys) == 1 { - cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)", - table.PrimaryKeys[0], limitValue, table.PrimaryKeys[0], - session.engine.Quote(tableName), condSQL), condArgs...) - - condSQL, condArgs, err = builder.ToSQL(cond) - if err != nil { - return 0, err - } - if len(condSQL) > 0 { - condSQL = "WHERE " + condSQL - } - } else { - top = fmt.Sprintf("TOP (%d) ", limitValue) - } } } @@ -269,13 +252,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 var tableAlias = session.engine.Quote(tableName) var fromSQL string if session.statement.TableAlias != "" { - switch session.engine.dialect.DBType() { - case core.MSSQL: - fromSQL = fmt.Sprintf("FROM %s %s ", tableAlias, session.statement.TableAlias) - tableAlias = session.statement.TableAlias - default: - tableAlias = fmt.Sprintf("%s AS %s", tableAlias, session.statement.TableAlias) - } + tableAlias = fmt.Sprintf("%s AS %s", tableAlias, session.statement.TableAlias) } sqlStr = fmt.Sprintf("UPDATE %v%v SET %v %v%v", diff --git a/pkg/util/xorm/statement.go b/pkg/util/xorm/statement.go index 3f7d87a216a..5c252529124 100644 --- a/pkg/util/xorm/statement.go +++ b/pkg/util/xorm/statement.go @@ -578,7 +578,7 @@ func (statement *Statement) col2NewColsWithQuote(columns ...string) []string { } func (statement *Statement) colmap2NewColsWithQuote() []string { - newColumns := make([]string, len(statement.columnMap), len(statement.columnMap)) + newColumns := make([]string, len(statement.columnMap)) copy(newColumns, statement.columnMap) for i := 0; i < len(statement.columnMap); i++ { newColumns[i] = statement.Engine.Quote(newColumns[i]) @@ -840,10 +840,6 @@ func (statement *Statement) genIndexSQL() []string { for _, index := range statement.RefTable.Indexes { if index.Type == core.IndexType { sql := statement.Engine.dialect.CreateIndexSql(tbName, index) - /*idxTBName := strings.Replace(tbName, ".", "_", -1) - idxTBName = strings.Replace(idxTBName, `"`, "", -1) - sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(idxTBName, idxName)), - quote(tbName), quote(strings.Join(index.Cols, quote(","))))*/ sqls = append(sqls, sql) } } @@ -1052,11 +1048,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n whereStr = " WHERE " + condSQL } - if dialect.DBType() == core.MSSQL && strings.Contains(statement.TableName(), "..") { - fromStr += statement.TableName() - } else { - fromStr += quote(statement.TableName()) - } + fromStr += quote(statement.TableName()) if statement.TableAlias != "" { if dialect.DBType() == core.ORACLE { @@ -1070,47 +1062,6 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n } pLimitN := statement.LimitN - if dialect.DBType() == core.MSSQL { - if pLimitN != nil { - LimitNValue := *pLimitN - top = fmt.Sprintf("TOP %d ", LimitNValue) - } - if statement.Start > 0 { - var column string - if len(statement.RefTable.PKColumns()) == 0 { - for _, index := range statement.RefTable.Indexes { - if len(index.Cols) == 1 { - column = index.Cols[0] - break - } - } - if len(column) == 0 { - column = statement.RefTable.ColumnsSeq()[0] - } - } else { - column = statement.RefTable.PKColumns()[0].Name - } - if statement.needTableName() { - if len(statement.TableAlias) > 0 { - column = statement.TableAlias + "." + column - } else { - column = statement.TableName() + "." + column - } - } - - var orderStr string - if needOrderBy && len(statement.OrderStr) > 0 { - orderStr = " ORDER BY " + statement.OrderStr - } - - var groupStr string - if len(statement.GroupByStr) > 0 { - groupStr = " GROUP BY " + statement.GroupByStr - } - mssqlCondi = fmt.Sprintf("(%s NOT IN (SELECT TOP %d %s%s%s%s%s))", - column, statement.Start, column, fromStr, whereStr, orderStr, groupStr) - } - } var buf strings.Builder fmt.Fprintf(&buf, "SELECT %v%v%v%v%v", distinct, top, columnStr, fromStr, whereStr) @@ -1132,7 +1083,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n fmt.Fprint(&buf, " ORDER BY ", statement.OrderStr) } if needLimit { - if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE { + if dialect.DBType() != core.ORACLE { if statement.Start > 0 { if pLimitN != nil { fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start) @@ -1142,7 +1093,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit, n } else if pLimitN != nil { fmt.Fprint(&buf, " LIMIT ", *pLimitN) } - } else if dialect.DBType() == core.ORACLE { + } else { if statement.Start != 0 || pLimitN != nil { oldString := buf.String() buf.Reset() @@ -1180,82 +1131,3 @@ func (statement *Statement) processIDParam() error { } return nil } - -func (statement *Statement) joinColumns(cols []*core.Column, includeTableName bool) string { - var colnames = make([]string, len(cols)) - for i, col := range cols { - if includeTableName { - colnames[i] = statement.Engine.Quote(statement.TableName()) + - "." + statement.Engine.Quote(col.Name) - } else { - colnames[i] = statement.Engine.Quote(col.Name) - } - } - return strings.Join(colnames, ", ") -} - -func (statement *Statement) convertIDSQL(sqlStr string) string { - if statement.RefTable != nil { - cols := statement.RefTable.PKColumns() - if len(cols) == 0 { - return "" - } - - colstrs := statement.joinColumns(cols, false) - sqls := splitNNoCase(sqlStr, " from ", 2) - if len(sqls) != 2 { - return "" - } - - var top string - pLimitN := statement.LimitN - if pLimitN != nil && statement.Engine.dialect.DBType() == core.MSSQL { - top = fmt.Sprintf("TOP %d ", *pLimitN) - } - - newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1]) - return newsql - } - return "" -} - -func (statement *Statement) convertUpdateSQL(sqlStr string) (string, string) { - if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 { - return "", "" - } - - colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true) - sqls := splitNNoCase(sqlStr, "where", 2) - if len(sqls) != 2 { - if len(sqls) == 1 { - return sqls[0], fmt.Sprintf("SELECT %v FROM %v", - colstrs, statement.Engine.Quote(statement.TableName())) - } - return "", "" - } - - var whereStr = sqls[1] - - // TODO: for postgres only, if any other database? - var paraStr string - if statement.Engine.dialect.DBType() == core.POSTGRES { - paraStr = "$" - } else if statement.Engine.dialect.DBType() == core.MSSQL { - paraStr = ":" - } - - if paraStr != "" { - if strings.Contains(sqls[1], paraStr) { - dollers := strings.Split(sqls[1], paraStr) - whereStr = dollers[0] - for i, c := range dollers[1:] { - ccs := strings.SplitN(c, " ", 2) - whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1]) - } - } - } - - return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v", - colstrs, statement.Engine.Quote(statement.TableName()), - whereStr) -} diff --git a/pkg/util/xorm/statement_args.go b/pkg/util/xorm/statement_args.go index 310f24d6b95..5909874cb4f 100644 --- a/pkg/util/xorm/statement_args.go +++ b/pkg/util/xorm/statement_args.go @@ -80,25 +80,13 @@ const insertSelectPlaceHolder = true func (statement *Statement) writeArg(w *builder.BytesWriter, arg interface{}) error { switch argv := arg.(type) { case bool: - if statement.Engine.dialect.DBType() == core.MSSQL { - if argv { - if _, err := w.WriteString("1"); err != nil { - return err - } - } else { - if _, err := w.WriteString("0"); err != nil { - return err - } + if argv { + if _, err := w.WriteString("true"); err != nil { + return err } } else { - if argv { - if _, err := w.WriteString("true"); err != nil { - return err - } - } else { - if _, err := w.WriteString("false"); err != nil { - return err - } + if _, err := w.WriteString("false"); err != nil { + return err } } case *builder.Builder: diff --git a/pkg/util/xorm/xorm.go b/pkg/util/xorm/xorm.go index ae71baed044..c3f60c56d66 100644 --- a/pkg/util/xorm/xorm.go +++ b/pkg/util/xorm/xorm.go @@ -110,10 +110,3 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { return engine, nil } - -// NewEngineWithParams new a db manager with params. The params will be passed to dialect. -func NewEngineWithParams(driverName string, dataSourceName string, params map[string]string) (*Engine, error) { - engine, err := NewEngine(driverName, dataSourceName) - engine.dialect.SetParams(params) - return engine, err -}