Skip to content

Commit

Permalink
grafana: [xorm] Remove some unused functions && mssql related logics …
Browse files Browse the repository at this point in the history
…(#60788)

Commit: a3a0c013015abf19e98f0fcc634edea15ea7a0c1
  • Loading branch information
ying-jeanne authored and sourcegraph-bot committed Dec 29, 2022
1 parent 25fa182 commit e1f04cf
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 311 deletions.
2 changes: 1 addition & 1 deletion grafana/pkg/util/xorm/dialect_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions grafana/pkg/util/xorm/dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 0 additions & 6 deletions grafana/pkg/util/xorm/dialect_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "]%'))"
Expand Down
63 changes: 1 addition & 62 deletions grafana/pkg/util/xorm/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
package xorm

import (
"bufio"
"bytes"
"context"
"database/sql"
"encoding/gob"
"errors"
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions grafana/pkg/util/xorm/engine_cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 0 additions & 29 deletions grafana/pkg/util/xorm/engine_context.go

This file was deleted.

9 changes: 0 additions & 9 deletions grafana/pkg/util/xorm/engine_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions grafana/pkg/util/xorm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package xorm

import (
"context"
"database/sql"
"reflect"
"time"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions grafana/pkg/util/xorm/session_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -166,7 +160,6 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
}
}
cleanupProcessorsClosures(&session.afterClosures)
// --

return res.RowsAffected()
}
8 changes: 2 additions & 6 deletions grafana/pkg/util/xorm/session_exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions grafana/pkg/util/xorm/session_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 1 addition & 24 deletions grafana/pkg/util/xorm/session_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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",
Expand Down

0 comments on commit e1f04cf

Please sign in to comment.