//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.SmoMetadataProvider;
using Microsoft.SqlServer.Management.SqlParser.Binder;
using Microsoft.SqlServer.Management.SqlParser.Common;
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
using Microsoft.SqlServer.Management.SqlParser.Parser;
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
{
///
/// Class for storing cached metadata regarding a parsed SQL file
///
internal class ScriptParseInfo
{
private ManualResetEvent buildingMetadataEvent = new ManualResetEvent(initialState: true);
private ParseOptions parseOptions = new ParseOptions();
private ServerConnection serverConnection;
///
/// Event which tells if MetadataProvider is built fully or not
///
public ManualResetEvent BuildingMetadataEvent
{
get { return this.buildingMetadataEvent; }
}
///
/// Gets or sets a flag determining is the LanguageService is connected
///
public bool IsConnected { get; set; }
///
/// Gets or sets the LanguageService SMO ServerConnection
///
public ServerConnection ServerConnection
{
get
{
return this.serverConnection;
}
set
{
this.serverConnection = value;
this.parseOptions = new ParseOptions(
batchSeparator: LanguageService.DefaultBatchSeperator,
isQuotedIdentifierSet: true,
compatibilityLevel: DatabaseCompatibilityLevel,
transactSqlVersion: TransactSqlVersion);
}
}
///
/// Gets the Language Service ServerVersion
///
public ServerVersion ServerVersion
{
get
{
return this.ServerConnection != null
? this.ServerConnection.ServerVersion
: null;
}
}
///
/// Gets the current DataEngineType
///
public DatabaseEngineType DatabaseEngineType
{
get
{
return this.ServerConnection != null
? this.ServerConnection.DatabaseEngineType
: DatabaseEngineType.Standalone;
}
}
///
/// Gets the current connections TransactSqlVersion
///
public TransactSqlVersion TransactSqlVersion
{
get
{
return this.IsConnected
? GetTransactSqlVersion(this.ServerVersion)
: TransactSqlVersion.Current;
}
}
///
/// Gets the current DatabaseCompatibilityLevel
///
public DatabaseCompatibilityLevel DatabaseCompatibilityLevel
{
get
{
return this.IsConnected
? GetDatabaseCompatibilityLevel(this.ServerVersion)
: DatabaseCompatibilityLevel.Current;
}
}
///
/// Gets the current ParseOptions
///
public ParseOptions ParseOptions
{
get
{
return this.parseOptions;
}
}
///
/// Gets or sets the SMO binder for schema-aware intellisense
///
public IBinder Binder { get; set; }
///
/// Gets or sets the previous SQL parse result
///
public ParseResult ParseResult { get; set; }
///
/// Gets or set the SMO metadata provider that's bound to the current connection
///
public SmoMetadataProvider MetadataProvider { get; set; }
///
/// Gets or sets the SMO metadata display info provider
///
public MetadataDisplayInfoProvider MetadataDisplayInfoProvider { get; set; }
///
/// Gets or sets the current autocomplete suggestion list
///
public IEnumerable CurrentSuggestions { get; set; }
///
/// Gets the database compatibility level from a server version
///
///
private static DatabaseCompatibilityLevel GetDatabaseCompatibilityLevel(ServerVersion serverVersion)
{
int versionMajor = Math.Max(serverVersion.Major, 8);
switch (versionMajor)
{
case 8:
return DatabaseCompatibilityLevel.Version80;
case 9:
return DatabaseCompatibilityLevel.Version90;
case 10:
return DatabaseCompatibilityLevel.Version100;
case 11:
return DatabaseCompatibilityLevel.Version110;
case 12:
return DatabaseCompatibilityLevel.Version120;
case 13:
return DatabaseCompatibilityLevel.Version130;
default:
return DatabaseCompatibilityLevel.Current;
}
}
///
/// Gets the transaction sql version from a server version
///
///
private static TransactSqlVersion GetTransactSqlVersion(ServerVersion serverVersion)
{
int versionMajor = Math.Max(serverVersion.Major, 9);
switch (versionMajor)
{
case 9:
case 10:
// In case of 10.0 we still use Version 10.5 as it is the closest available.
return TransactSqlVersion.Version105;
case 11:
return TransactSqlVersion.Version110;
case 12:
return TransactSqlVersion.Version120;
case 13:
return TransactSqlVersion.Version130;
default:
return TransactSqlVersion.Current;
}
}
}
}