2017-01-23 14:41:18 +05:30
// Copyright 2015 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.
2020-03-22 20:42:55 +05:30
package dialects
2017-01-23 14:41:18 +05:30
import (
2020-03-22 20:42:55 +05:30
"context"
2021-08-13 04:41:42 +05:30
"database/sql"
2017-04-07 07:17:25 +05:30
"errors"
2017-01-23 14:41:18 +05:30
"fmt"
2019-06-23 20:52:43 +05:30
"net/url"
2017-01-23 14:41:18 +05:30
"strconv"
"strings"
2020-03-22 20:42:55 +05:30
"xorm.io/xorm/core"
"xorm.io/xorm/schemas"
2017-01-23 14:41:18 +05:30
)
var (
mssqlReservedWords = map [ string ] bool {
2019-08-27 07:47:23 +05:30
"ADD" : true ,
"EXTERNAL" : true ,
"PROCEDURE" : true ,
"ALL" : true ,
"FETCH" : true ,
"PUBLIC" : true ,
"ALTER" : true ,
"FILE" : true ,
"RAISERROR" : true ,
"AND" : true ,
"FILLFACTOR" : true ,
"READ" : true ,
"ANY" : true ,
"FOR" : true ,
"READTEXT" : true ,
"AS" : true ,
"FOREIGN" : true ,
"RECONFIGURE" : true ,
"ASC" : true ,
"FREETEXT" : true ,
"REFERENCES" : true ,
"AUTHORIZATION" : true ,
"FREETEXTTABLE" : true ,
"REPLICATION" : true ,
"BACKUP" : true ,
"FROM" : true ,
"RESTORE" : true ,
"BEGIN" : true ,
"FULL" : true ,
"RESTRICT" : true ,
"BETWEEN" : true ,
"FUNCTION" : true ,
"RETURN" : true ,
"BREAK" : true ,
"GOTO" : true ,
"REVERT" : true ,
"BROWSE" : true ,
"GRANT" : true ,
"REVOKE" : true ,
"BULK" : true ,
"GROUP" : true ,
"RIGHT" : true ,
"BY" : true ,
"HAVING" : true ,
"ROLLBACK" : true ,
"CASCADE" : true ,
"HOLDLOCK" : true ,
"ROWCOUNT" : true ,
"CASE" : true ,
"IDENTITY" : true ,
"ROWGUIDCOL" : true ,
"CHECK" : true ,
"IDENTITY_INSERT" : true ,
"RULE" : true ,
"CHECKPOINT" : true ,
"IDENTITYCOL" : true ,
"SAVE" : true ,
"CLOSE" : true ,
"IF" : true ,
"SCHEMA" : true ,
"CLUSTERED" : true ,
"IN" : true ,
"SECURITYAUDIT" : true ,
"COALESCE" : true ,
"INDEX" : true ,
"SELECT" : true ,
"COLLATE" : true ,
"INNER" : true ,
"SEMANTICKEYPHRASETABLE" : true ,
"COLUMN" : true ,
"INSERT" : true ,
2017-01-23 14:41:18 +05:30
"SEMANTICSIMILARITYDETAILSTABLE" : true ,
2019-08-27 07:47:23 +05:30
"COMMIT" : true ,
"INTERSECT" : true ,
"SEMANTICSIMILARITYTABLE" : true ,
"COMPUTE" : true ,
"INTO" : true ,
"SESSION_USER" : true ,
"CONSTRAINT" : true ,
"IS" : true ,
"SET" : true ,
"CONTAINS" : true ,
"JOIN" : true ,
"SETUSER" : true ,
"CONTAINSTABLE" : true ,
"KEY" : true ,
"SHUTDOWN" : true ,
"CONTINUE" : true ,
"KILL" : true ,
"SOME" : true ,
"CONVERT" : true ,
"LEFT" : true ,
"STATISTICS" : true ,
"CREATE" : true ,
"LIKE" : true ,
"SYSTEM_USER" : true ,
"CROSS" : true ,
"LINENO" : true ,
"TABLE" : true ,
"CURRENT" : true ,
"LOAD" : true ,
"TABLESAMPLE" : true ,
"CURRENT_DATE" : true ,
"MERGE" : true ,
"TEXTSIZE" : true ,
"CURRENT_TIME" : true ,
"NATIONAL" : true ,
"THEN" : true ,
"CURRENT_TIMESTAMP" : true ,
"NOCHECK" : true ,
"TO" : true ,
"CURRENT_USER" : true ,
"NONCLUSTERED" : true ,
"TOP" : true ,
"CURSOR" : true ,
"NOT" : true ,
"TRAN" : true ,
"DATABASE" : true ,
"NULL" : true ,
"TRANSACTION" : true ,
"DBCC" : true ,
"NULLIF" : true ,
"TRIGGER" : true ,
"DEALLOCATE" : true ,
"OF" : true ,
"TRUNCATE" : true ,
"DECLARE" : true ,
"OFF" : true ,
"TRY_CONVERT" : true ,
"DEFAULT" : true ,
"OFFSETS" : true ,
"TSEQUAL" : true ,
"DELETE" : true ,
"ON" : true ,
"UNION" : true ,
"DENY" : true ,
"OPEN" : true ,
"UNIQUE" : true ,
"DESC" : true ,
"OPENDATASOURCE" : true ,
"UNPIVOT" : true ,
"DISK" : true ,
"OPENQUERY" : true ,
"UPDATE" : true ,
"DISTINCT" : true ,
"OPENROWSET" : true ,
"UPDATETEXT" : true ,
"DISTRIBUTED" : true ,
"OPENXML" : true ,
"USE" : true ,
"DOUBLE" : true ,
"OPTION" : true ,
"USER" : true ,
"DROP" : true ,
"OR" : true ,
"VALUES" : true ,
"DUMP" : true ,
"ORDER" : true ,
"VARYING" : true ,
"ELSE" : true ,
"OUTER" : true ,
"VIEW" : true ,
"END" : true ,
"OVER" : true ,
"WAITFOR" : true ,
"ERRLVL" : true ,
"PERCENT" : true ,
"WHEN" : true ,
"ESCAPE" : true ,
"PIVOT" : true ,
"WHERE" : true ,
"EXCEPT" : true ,
"PLAN" : true ,
"WHILE" : true ,
"EXEC" : true ,
"PRECISION" : true ,
"WITH" : true ,
"EXECUTE" : true ,
"PRIMARY" : true ,
"WITHIN" : true ,
"EXISTS" : true ,
"PRINT" : true ,
"WRITETEXT" : true ,
"EXIT" : true ,
"PROC" : true ,
2017-01-23 14:41:18 +05:30
}
2020-03-22 20:42:55 +05:30
2020-06-16 02:16:01 +05:30
mssqlQuoter = schemas . Quoter {
Prefix : '[' ,
Suffix : ']' ,
IsReserved : schemas . AlwaysReserve ,
}
2017-01-23 14:41:18 +05:30
)
type mssql struct {
2020-03-22 20:42:55 +05:30
Base
2020-07-21 17:58:27 +05:30
defaultVarchar string
defaultChar string
2017-01-23 14:41:18 +05:30
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) Init ( uri * URI ) error {
2020-03-22 20:42:55 +05:30
db . quoter = mssqlQuoter
2021-02-17 09:17:24 +05:30
db . defaultChar = "CHAR"
db . defaultVarchar = "VARCHAR"
2020-03-25 20:02:23 +05:30
return db . Base . Init ( db , uri )
2017-01-23 14:41:18 +05:30
}
2020-07-21 17:58:27 +05:30
func ( db * mssql ) SetParams ( params map [ string ] string ) {
defaultVarchar , ok := params [ "DEFAULT_VARCHAR" ]
if ok {
var t = strings . ToUpper ( defaultVarchar )
switch t {
case "NVARCHAR" , "VARCHAR" :
2020-09-08 23:27:19 +05:30
db . defaultVarchar = t
2020-07-21 17:58:27 +05:30
default :
db . defaultVarchar = "VARCHAR"
}
} else {
db . defaultVarchar = "VARCHAR"
}
defaultChar , ok := params [ "DEFAULT_CHAR" ]
if ok {
var t = strings . ToUpper ( defaultChar )
switch t {
case "NCHAR" , "CHAR" :
2020-09-08 23:27:19 +05:30
db . defaultChar = t
2020-07-21 17:58:27 +05:30
default :
db . defaultChar = "CHAR"
}
} else {
db . defaultChar = "CHAR"
}
}
2021-07-04 18:40:46 +05:30
func ( db * mssql ) Version ( ctx context . Context , queryer core . Queryer ) ( * schemas . Version , error ) {
rows , err := queryer . QueryContext ( ctx ,
"SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel') AS ProductLevel, SERVERPROPERTY ('edition') AS ProductEdition" )
if err != nil {
return nil , err
}
defer rows . Close ( )
var version , level , edition string
if ! rows . Next ( ) {
2021-08-13 04:41:42 +05:30
if rows . Err ( ) != nil {
return nil , rows . Err ( )
}
2021-07-04 18:40:46 +05:30
return nil , errors . New ( "unknow version" )
}
if err := rows . Scan ( & version , & level , & edition ) ; err != nil {
return nil , err
}
// MSSQL: Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64) Nov 30 2018 12:57:58 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)
return & schemas . Version {
Number : version ,
Level : level ,
Edition : edition ,
} , nil
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) SQLType ( c * schemas . Column ) string {
2017-01-23 14:41:18 +05:30
var res string
switch t := c . SQLType . Name ; t {
2021-08-13 04:41:42 +05:30
case schemas . Bool , schemas . Boolean :
2020-03-22 20:42:55 +05:30
res = schemas . Bit
2017-04-07 07:17:25 +05:30
if strings . EqualFold ( c . Default , "true" ) {
2017-01-23 14:41:18 +05:30
c . Default = "1"
2018-12-12 06:31:41 +05:30
} else if strings . EqualFold ( c . Default , "false" ) {
2017-01-23 14:41:18 +05:30
c . Default = "0"
}
2020-07-12 02:37:52 +05:30
return res
2020-03-22 20:42:55 +05:30
case schemas . Serial :
2017-01-23 14:41:18 +05:30
c . IsAutoIncrement = true
c . IsPrimaryKey = true
c . Nullable = false
2020-03-22 20:42:55 +05:30
res = schemas . Int
case schemas . BigSerial :
2017-01-23 14:41:18 +05:30
c . IsAutoIncrement = true
c . IsPrimaryKey = true
c . Nullable = false
2020-03-22 20:42:55 +05:30
res = schemas . BigInt
2021-08-13 04:41:42 +05:30
case schemas . Bytea , schemas . Binary :
2020-03-22 20:42:55 +05:30
res = schemas . VarBinary
2017-01-23 14:41:18 +05:30
if c . Length == 0 {
c . Length = 50
}
2021-08-13 04:41:42 +05:30
case schemas . Blob , schemas . TinyBlob , schemas . MediumBlob , schemas . LongBlob :
res = schemas . VarBinary
if c . Length == 0 {
res += "(MAX)"
}
case schemas . TimeStamp , schemas . DateTime :
if c . Length > 3 {
res = "DATETIME2"
} else {
return schemas . DateTime
}
2020-03-22 20:42:55 +05:30
case schemas . TimeStampz :
2017-01-23 14:41:18 +05:30
res = "DATETIMEOFFSET"
c . Length = 7
2021-08-13 04:41:42 +05:30
case schemas . MediumInt , schemas . TinyInt , schemas . SmallInt , schemas . UnsignedMediumInt , schemas . UnsignedTinyInt , schemas . UnsignedSmallInt :
2020-03-22 20:42:55 +05:30
res = schemas . Int
case schemas . Text , schemas . MediumText , schemas . TinyText , schemas . LongText , schemas . Json :
2020-09-08 23:27:19 +05:30
res = db . defaultVarchar + "(MAX)"
2020-03-22 20:42:55 +05:30
case schemas . Double :
res = schemas . Real
case schemas . Uuid :
res = schemas . Varchar
2017-01-23 14:41:18 +05:30
c . Length = 40
2020-03-22 20:42:55 +05:30
case schemas . TinyInt :
res = schemas . TinyInt
2017-08-22 17:09:52 +05:30
c . Length = 0
2021-07-04 18:40:46 +05:30
case schemas . BigInt , schemas . UnsignedBigInt , schemas . UnsignedInt :
2020-03-22 20:42:55 +05:30
res = schemas . BigInt
2019-09-26 00:32:54 +05:30
c . Length = 0
2020-09-08 23:27:19 +05:30
case schemas . NVarchar :
res = t
if c . Length == - 1 {
res += "(MAX)"
}
2020-07-21 17:58:27 +05:30
case schemas . Varchar :
res = db . defaultVarchar
2020-09-08 23:27:19 +05:30
if c . Length == - 1 {
res += "(MAX)"
}
2020-07-21 17:58:27 +05:30
case schemas . Char :
res = db . defaultChar
2020-09-08 23:27:19 +05:30
if c . Length == - 1 {
res += "(MAX)"
}
case schemas . NChar :
res = t
if c . Length == - 1 {
res += "(MAX)"
}
2017-01-23 14:41:18 +05:30
default :
res = t
}
2021-08-13 04:41:42 +05:30
if res == schemas . Int || res == schemas . Bit {
2020-07-12 02:37:52 +05:30
return res
2017-01-23 14:41:18 +05:30
}
hasLen1 := ( c . Length > 0 )
hasLen2 := ( c . Length2 > 0 )
if hasLen2 {
res += "(" + strconv . Itoa ( c . Length ) + "," + strconv . Itoa ( c . Length2 ) + ")"
} else if hasLen1 {
res += "(" + strconv . Itoa ( c . Length ) + ")"
}
return res
}
2021-08-13 04:41:42 +05:30
func ( db * mssql ) ColumnTypeKind ( t string ) int {
switch strings . ToUpper ( t ) {
case "DATE" , "DATETIME" , "DATETIME2" , "TIME" :
return schemas . TIME_TYPE
case "VARCHAR" , "TEXT" , "CHAR" , "NVARCHAR" , "NCHAR" , "NTEXT" :
return schemas . TEXT_TYPE
case "FLOAT" , "REAL" , "BIGINT" , "DATETIMEOFFSET" , "TINYINT" , "SMALLINT" , "INT" :
return schemas . NUMERIC_TYPE
default :
return schemas . UNKNOW_TYPE
}
}
2017-01-23 14:41:18 +05:30
func ( db * mssql ) IsReserved ( name string ) bool {
2020-03-22 20:42:55 +05:30
_ , ok := mssqlReservedWords [ strings . ToUpper ( name ) ]
2017-01-23 14:41:18 +05:30
return ok
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) SetQuotePolicy ( quotePolicy QuotePolicy ) {
switch quotePolicy {
case QuotePolicyNone :
var q = mssqlQuoter
q . IsReserved = schemas . AlwaysNoReserve
db . quoter = q
case QuotePolicyReserved :
var q = mssqlQuoter
q . IsReserved = db . IsReserved
db . quoter = q
case QuotePolicyAlways :
fallthrough
default :
db . quoter = mssqlQuoter
}
2017-01-23 14:41:18 +05:30
}
func ( db * mssql ) AutoIncrStr ( ) string {
return "IDENTITY"
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) DropTableSQL ( tableName string ) ( string , bool ) {
2017-01-23 14:41:18 +05:30
return fmt . Sprintf ( "IF EXISTS (SELECT * FROM sysobjects WHERE id = " +
"object_id(N'%s') and OBJECTPROPERTY(id, N'IsUserTable') = 1) " +
2020-03-22 20:42:55 +05:30
"DROP TABLE \"%s\"" , tableName , tableName ) , true
2017-01-23 14:41:18 +05:30
}
2021-01-05 11:58:51 +05:30
func ( db * mssql ) ModifyColumnSQL ( tableName string , col * schemas . Column ) string {
s , _ := ColumnString ( db . dialect , col , false )
2021-09-30 01:38:44 +05:30
return fmt . Sprintf ( "ALTER TABLE %s ALTER COLUMN %s" , db . quoter . Quote ( tableName ) , s )
2021-01-05 11:58:51 +05:30
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) IndexCheckSQL ( tableName , idxName string ) ( string , [ ] interface { } ) {
2017-01-23 14:41:18 +05:30
args := [ ] interface { } { idxName }
sql := "select name from sysindexes where id=object_id('" + tableName + "') and name=?"
return sql , args
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) IsColumnExist ( queryer core . Queryer , ctx context . Context , tableName , colName string ) ( bool , error ) {
2017-01-23 14:41:18 +05:30
query := ` SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ? `
2020-03-25 20:02:23 +05:30
return db . HasRecords ( queryer , ctx , query , tableName , colName )
2017-01-23 14:41:18 +05:30
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) IsTableExist ( queryer core . Queryer , ctx context . Context , tableName string ) ( bool , error ) {
2017-01-23 14:41:18 +05:30
sql := "select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1"
2020-03-25 20:02:23 +05:30
return db . HasRecords ( queryer , ctx , sql )
2017-01-23 14:41:18 +05:30
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) GetColumns ( queryer core . Queryer , ctx context . Context , tableName string ) ( [ ] string , map [ string ] * schemas . Column , error ) {
2017-01-23 14:41:18 +05:30
args := [ ] interface { } { }
s := ` select a . name as name , b . name as ctype , a . max_length , a . precision , a . scale , a . is_nullable as nullable ,
2019-10-03 02:17:20 +05:30
"default_is_null" = ( CASE WHEN c . text is null THEN 1 ELSE 0 END ) ,
2017-08-22 17:09:52 +05:30
replace ( replace ( isnull ( c . text , ' ' ) , '(' , ' ' ) , ')' , ' ' ) as vdefault ,
2020-03-22 20:42:55 +05:30
ISNULL ( p . is_primary_key , 0 ) , a . is_identity as is_identity
2017-08-22 17:09:52 +05:30
from sys . columns a
left join sys . types b on a . user_type_id = b . user_type_id
left join sys . syscomments c on a . default_object_id = c . id
2020-03-22 20:42:55 +05:30
LEFT OUTER JOIN ( SELECT i . object_id , ic . column_id , i . is_primary_key
FROM sys . indexes i
LEFT JOIN sys . index_columns ic ON ic . object_id = i . object_id AND ic . index_id = i . index_id
WHERE i . is_primary_key = 1
) as p on p . object_id = a . object_id AND p . column_id = a . column_id
2017-01-23 14:41:18 +05:30
where a . object_id = object_id ( ' ` + tableName + ` ' ) `
2020-03-25 20:02:23 +05:30
rows , err := queryer . QueryContext ( ctx , s , args ... )
2017-01-23 14:41:18 +05:30
if err != nil {
return nil , nil , err
}
defer rows . Close ( )
2020-03-22 20:42:55 +05:30
cols := make ( map [ string ] * schemas . Column )
2017-01-23 14:41:18 +05:30
colSeq := make ( [ ] string , 0 )
for rows . Next ( ) {
var name , ctype , vdefault string
var maxLen , precision , scale int
2019-10-03 02:17:20 +05:30
var nullable , isPK , defaultIsNull , isIncrement bool
err = rows . Scan ( & name , & ctype , & maxLen , & precision , & scale , & nullable , & defaultIsNull , & vdefault , & isPK , & isIncrement )
2017-01-23 14:41:18 +05:30
if err != nil {
return nil , nil , err
}
2020-03-22 20:42:55 +05:30
col := new ( schemas . Column )
2017-01-23 14:41:18 +05:30
col . Indexes = make ( map [ string ] int )
col . Name = strings . Trim ( name , "` " )
col . Nullable = nullable
2019-10-03 02:17:20 +05:30
col . DefaultIsEmpty = defaultIsNull
if ! defaultIsNull {
col . Default = vdefault
}
2017-08-22 17:09:52 +05:30
col . IsPrimaryKey = isPK
2019-10-03 02:17:20 +05:30
col . IsAutoIncrement = isIncrement
2017-01-23 14:41:18 +05:30
ct := strings . ToUpper ( ctype )
if ct == "DECIMAL" {
col . Length = precision
col . Length2 = scale
} else {
col . Length = maxLen
}
switch ct {
case "DATETIMEOFFSET" :
2020-03-22 20:42:55 +05:30
col . SQLType = schemas . SQLType { Name : schemas . TimeStampz , DefaultLength : 0 , DefaultLength2 : 0 }
2017-01-23 14:41:18 +05:30
case "NVARCHAR" :
2020-03-22 20:42:55 +05:30
col . SQLType = schemas . SQLType { Name : schemas . NVarchar , DefaultLength : 0 , DefaultLength2 : 0 }
2020-09-08 23:27:19 +05:30
if col . Length > 0 {
col . Length /= 2
col . Length2 /= 2
}
2021-08-13 04:41:42 +05:30
case "DATETIME2" :
col . SQLType = schemas . SQLType { Name : schemas . DateTime , DefaultLength : 7 , DefaultLength2 : 0 }
col . Length = scale
case "DATETIME" :
col . SQLType = schemas . SQLType { Name : schemas . DateTime , DefaultLength : 3 , DefaultLength2 : 0 }
col . Length = scale
2017-01-23 14:41:18 +05:30
case "IMAGE" :
2020-03-22 20:42:55 +05:30
col . SQLType = schemas . SQLType { Name : schemas . VarBinary , DefaultLength : 0 , DefaultLength2 : 0 }
2020-09-08 23:27:19 +05:30
case "NCHAR" :
if col . Length > 0 {
col . Length /= 2
col . Length2 /= 2
}
fallthrough
2017-01-23 14:41:18 +05:30
default :
2020-03-22 20:42:55 +05:30
if _ , ok := schemas . SqlTypes [ ct ] ; ok {
col . SQLType = schemas . SQLType { Name : ct , DefaultLength : 0 , DefaultLength2 : 0 }
2017-01-23 14:41:18 +05:30
} else {
return nil , nil , fmt . Errorf ( "Unknown colType %v for %v - %v" , ct , tableName , col . Name )
}
}
cols [ col . Name ] = col
colSeq = append ( colSeq , col . Name )
}
2021-08-13 04:41:42 +05:30
if rows . Err ( ) != nil {
return nil , nil , rows . Err ( )
}
2017-01-23 14:41:18 +05:30
return colSeq , cols , nil
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) GetTables ( queryer core . Queryer , ctx context . Context ) ( [ ] * schemas . Table , error ) {
2017-01-23 14:41:18 +05:30
args := [ ] interface { } { }
s := ` select name from sysobjects where xtype ='U' `
2020-03-25 20:02:23 +05:30
rows , err := queryer . QueryContext ( ctx , s , args ... )
2017-01-23 14:41:18 +05:30
if err != nil {
return nil , err
}
defer rows . Close ( )
2020-03-22 20:42:55 +05:30
tables := make ( [ ] * schemas . Table , 0 )
2017-01-23 14:41:18 +05:30
for rows . Next ( ) {
2020-03-22 20:42:55 +05:30
table := schemas . NewEmptyTable ( )
2017-01-23 14:41:18 +05:30
var name string
err = rows . Scan ( & name )
if err != nil {
return nil , err
}
table . Name = strings . Trim ( name , "` " )
tables = append ( tables , table )
}
2021-08-13 04:41:42 +05:30
if rows . Err ( ) != nil {
return nil , rows . Err ( )
}
2017-01-23 14:41:18 +05:30
return tables , nil
}
2020-03-25 20:02:23 +05:30
func ( db * mssql ) GetIndexes ( queryer core . Queryer , ctx context . Context , tableName string ) ( map [ string ] * schemas . Index , error ) {
2017-01-23 14:41:18 +05:30
args := [ ] interface { } { tableName }
s := ` SELECT
IXS . NAME AS [ INDEX_NAME ] ,
C . NAME AS [ COLUMN_NAME ] ,
IXS . is_unique AS [ IS_UNIQUE ]
FROM SYS . INDEXES IXS
INNER JOIN SYS . INDEX_COLUMNS IXCS
ON IXS . OBJECT_ID = IXCS . OBJECT_ID AND IXS . INDEX_ID = IXCS . INDEX_ID
INNER JOIN SYS . COLUMNS C ON IXS . OBJECT_ID = C . OBJECT_ID
AND IXCS . COLUMN_ID = C . COLUMN_ID
WHERE IXS . TYPE_DESC = ' NONCLUSTERED ' and OBJECT_NAME ( IXS . OBJECT_ID ) = ?
`
2020-03-25 20:02:23 +05:30
rows , err := queryer . QueryContext ( ctx , s , args ... )
2017-01-23 14:41:18 +05:30
if err != nil {
return nil , err
}
defer rows . Close ( )
2021-08-13 04:41:42 +05:30
indexes := make ( map [ string ] * schemas . Index )
2017-01-23 14:41:18 +05:30
for rows . Next ( ) {
var indexType int
var indexName , colName , isUnique string
err = rows . Scan ( & indexName , & colName , & isUnique )
if err != nil {
return nil , err
}
i , err := strconv . ParseBool ( isUnique )
if err != nil {
return nil , err
}
if i {
2020-03-22 20:42:55 +05:30
indexType = schemas . UniqueType
2017-01-23 14:41:18 +05:30
} else {
2020-03-22 20:42:55 +05:30
indexType = schemas . IndexType
2017-01-23 14:41:18 +05:30
}
colName = strings . Trim ( colName , "` " )
2017-04-07 07:17:25 +05:30
var isRegular bool
2021-02-17 09:17:24 +05:30
if ( strings . HasPrefix ( indexName , "IDX_" + tableName ) || strings . HasPrefix ( indexName , "UQE_" + tableName ) ) && len ( indexName ) > ( 5 + len ( tableName ) ) {
2017-01-23 14:41:18 +05:30
indexName = indexName [ 5 + len ( tableName ) : ]
2017-04-07 07:17:25 +05:30
isRegular = true
2017-01-23 14:41:18 +05:30
}
2020-03-22 20:42:55 +05:30
var index * schemas . Index
2017-01-23 14:41:18 +05:30
var ok bool
if index , ok = indexes [ indexName ] ; ! ok {
2020-03-22 20:42:55 +05:30
index = new ( schemas . Index )
2017-01-23 14:41:18 +05:30
index . Type = indexType
index . Name = indexName
2017-04-07 07:17:25 +05:30
index . IsRegular = isRegular
2017-01-23 14:41:18 +05:30
indexes [ indexName ] = index
}
index . AddColumn ( colName )
}
2021-08-13 04:41:42 +05:30
if rows . Err ( ) != nil {
return nil , rows . Err ( )
}
2017-01-23 14:41:18 +05:30
return indexes , nil
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) CreateTableSQL ( table * schemas . Table , tableName string ) ( [ ] string , bool ) {
2017-01-23 14:41:18 +05:30
if tableName == "" {
tableName = table . Name
}
2021-09-16 23:23:28 +05:30
quoter := db . dialect . Quoter ( )
var b strings . Builder
b . WriteString ( "IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = '" )
quoter . QuoteTo ( & b , tableName )
b . WriteString ( "' ) CREATE TABLE " )
quoter . QuoteTo ( & b , tableName )
b . WriteString ( " (" )
2017-01-23 14:41:18 +05:30
2021-09-16 23:23:28 +05:30
for i , colName := range table . ColumnsSeq ( ) {
2017-01-23 14:41:18 +05:30
col := table . GetColumn ( colName )
2021-09-16 23:23:28 +05:30
s , _ := ColumnString ( db . dialect , col , col . IsPrimaryKey && len ( table . PrimaryKeys ) == 1 )
b . WriteString ( s )
if i != len ( table . ColumnsSeq ( ) ) - 1 {
b . WriteString ( ", " )
}
2017-01-23 14:41:18 +05:30
}
2021-09-16 23:23:28 +05:30
if len ( table . PrimaryKeys ) > 1 {
b . WriteString ( ", PRIMARY KEY (" )
b . WriteString ( quoter . Join ( table . PrimaryKeys , "," ) )
b . WriteString ( ")" )
2017-01-23 14:41:18 +05:30
}
2021-09-16 23:23:28 +05:30
b . WriteString ( ")" )
return [ ] string { b . String ( ) } , true
2017-01-23 14:41:18 +05:30
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) ForUpdateSQL ( query string ) string {
2017-01-23 14:41:18 +05:30
return query
}
2020-03-22 20:42:55 +05:30
func ( db * mssql ) Filters ( ) [ ] Filter {
return [ ] Filter { }
2017-01-23 14:41:18 +05:30
}
2017-04-07 07:17:25 +05:30
type odbcDriver struct {
2021-08-13 04:41:42 +05:30
baseDriver
}
func ( p * odbcDriver ) Features ( ) * DriverFeatures {
return & DriverFeatures {
SupportReturnInsertedID : false ,
}
2017-04-07 07:17:25 +05:30
}
2020-03-22 20:42:55 +05:30
func ( p * odbcDriver ) Parse ( driverName , dataSourceName string ) ( * URI , error ) {
2017-04-07 07:17:25 +05:30
var dbName string
2019-06-23 20:52:43 +05:30
if strings . HasPrefix ( dataSourceName , "sqlserver://" ) {
u , err := url . Parse ( dataSourceName )
if err != nil {
return nil , err
}
dbName = u . Query ( ) . Get ( "database" )
} else {
kv := strings . Split ( dataSourceName , ";" )
for _ , c := range kv {
vv := strings . Split ( strings . TrimSpace ( c ) , "=" )
if len ( vv ) == 2 {
2021-08-13 04:41:42 +05:30
if strings . ToLower ( vv [ 0 ] ) == "database" {
2019-06-23 20:52:43 +05:30
dbName = vv [ 1 ]
}
2017-04-07 07:17:25 +05:30
}
}
}
if dbName == "" {
return nil , errors . New ( "no db name provided" )
}
2020-03-22 20:42:55 +05:30
return & URI { DBName : dbName , DBType : schemas . MSSQL } , nil
2017-04-07 07:17:25 +05:30
}
2021-08-13 04:41:42 +05:30
func ( p * odbcDriver ) GenScanResult ( colType string ) ( interface { } , error ) {
switch colType {
case "VARCHAR" , "TEXT" , "CHAR" , "NVARCHAR" , "NCHAR" , "NTEXT" :
fallthrough
case "DATE" , "DATETIME" , "DATETIME2" , "TIME" :
var s sql . NullString
return & s , nil
case "FLOAT" , "REAL" :
var s sql . NullFloat64
return & s , nil
case "BIGINT" , "DATETIMEOFFSET" :
var s sql . NullInt64
return & s , nil
case "TINYINT" , "SMALLINT" , "INT" :
var s sql . NullInt32
return & s , nil
default :
var r sql . RawBytes
return & r , nil
}
}