import _ from "lodash" ; import { addTicks , DialectOptions , FKRow , makeCondition } from "./dialect-options" ; export const mssqlOptions = { name : 'mssql' , hasSchema : true , param { String } tableName param { String } schemaName return { String } getForeignKeysQuery : ( tableName , schemaName ) => { return `SELECT ccu.TABLE_NAME AS source_table,
                   ccu.CONSTRAINT_NAME AS constraint_name,
                   ccu.CONSTRAINT_SCHEMA AS source_schema,
                   ccu.COLUMN_NAME AS source_column,
                   kcu.TABLE_NAME AS target_table,
                   kcu.TABLE_SCHEMA AS target_schema,
                   kcu.COLUMN_NAME AS target_column,
                   tc.CONSTRAINT_TYPE AS constraint_type,
                   c.IS_IDENTITY AS is_identity
              FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
             INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
                ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
              LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
                ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
              LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
                ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME
               AND tc.CONSTRAINT_TYPE = 'FOREIGN KEY'
             INNER JOIN sys.columns c
                ON c.name = ccu.COLUMN_NAME
               AND c.object_id = OBJECT_ID(ccu.TABLE_SCHEMA + '.' + ccu.TABLE_NAME)
             WHERE tc.CONSTRAINT_TYPE != 'CHECK'
               AND ccu.TABLE_NAME = ${ addTicks ( tableName ) }
                   ${ makeCondition ( 'ccu.TABLE_SCHEMA' , schemaName ) }` ; } , param { String } tableName param { String } schemaName return { String } countTriggerQuery : ( tableName , schemaName ) => { const qname = addTicks ( ( schemaName ? schemaName + "." : "" ) + tableName ) ; return `SELECT COUNT(0) AS trigger_count
              FROM sys.objects tr,  sys.objects tb
             WHERE tr.type = 'TR'
               AND tr.parent_object_id = tb.object_id
               AND tb.object_id = OBJECT_ID(${ qname })` ; } , param { Object } record return { Bool } isForeignKey : ( record ) => { return _ . isObject ( record ) && _ . has ( record , 'constraint_type' ) && record . constraint_type === 'FOREIGN KEY' ; } , param { Object } record return { Bool } isUnique : ( record , records ) => { return _ . isObject ( record ) && _ . has ( record , 'constraint_type' ) && record . constraint_type === 'UNIQUE' ; } , param { Object } record return { Bool } isPrimaryKey : ( record ) => { return _ . isObject ( record ) && _ . has ( record , 'constraint_type' ) && record . constraint_type === 'PRIMARY KEY' ; } , param { Object } record return { Bool } isSerialKey : ( record ) => { return ( _ . isObject ( record ) && mssqlOptions . isPrimaryKey ( record ) && ( _ . has ( record , 'is_identity' ) && record . is_identity ) ) ; } , param { String } schemaName return { String } showTablesQuery : ( schemaName ? ) => { return `SELECT TABLE_NAME AS table_name, TABLE_SCHEMA AS table_schema
              FROM INFORMATION_SCHEMA.TABLES
             WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME != 'sysdiagrams'
                   ${ makeCondition ( "TABLE_SCHEMA" , schemaName ) }` ; } , showViewsQuery : ( schemaName ? ) => { return `SELECT TABLE_NAME AS table_name, TABLE_SCHEMA AS table_schema
              FROM INFORMATION_SCHEMA.TABLES
             WHERE TABLE_TYPE = 'VIEW'
                  ${ makeCondition ( "TABLE_SCHEMA" , schemaName ) }` ; } , /** Sequelize "describeTable" doesn't include precision and scale in mssql */ showPrecisionQuery : ( tableName , schemaName ? ) => { return `SELECT COLUMN_NAME AS column_name, NUMERIC_PRECISION AS numeric_precision, NUMERIC_SCALE AS numeric_scale
    FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${ tableName }'` + ( ! schemaName ? '' : ` AND TABLE_SCHEMA = '${ schemaName }'` ) ; } , } ;