in src/Microsoft.SqlTools.ServiceLayer/Admin/Database/DatabasePrototype.cs [288:630]
public DatabaseData(CDataContainer context, string databaseName)
{
// set prototype properties to match the database
Database db = context.Server.Databases[databaseName];
if (db == null)
{
context.Server.Databases.Refresh();
db = context.Server.Databases[databaseName];
}
isSystemDB = db.IsSystemObject;
ResourceManager manager = new ResourceManager("Microsoft.SqlTools.ServiceLayer.Localization.SR", typeof(DatabasePrototype).GetAssembly());
try
{
this.owner = db.Owner;
}
catch (Exception)
{
// TODO: fix the exception in SMO
this.owner = string.Empty;
}
// Databases that are restored from other servers might not have valid owners.
// If the logged in user is an administrator and the owner is not valid, show
// the owner as blank. Note that only administrators can successfully change
// execution context to dbo to perform the check for an invalid owner.
if ((9 <= context.SqlServerVersion) && context.LoggedInUserIsSysadmin)
{
try
{
DataSet dsResult = db.ExecuteWithResults(
"select suser_sname((select sid from sys.database_principals where name = N'dbo'));");
DataTable tableResult = dsResult.Tables[0];
if (tableResult.Rows.Count > 0 && tableResult.Columns.Count > 0)
{
DataRow rowResult = tableResult.Rows[0];
DataColumn colResult = tableResult.Columns[0];
if (string.IsNullOrEmpty(rowResult[colResult].ToString()))
{
this.owner = String.Empty;
}
}
else
{
this.owner = String.Empty;
}
}
catch (FailedOperationException)
{
// the owner is invalid, set the owner string to String.Empty
this.owner = String.Empty;
}
}
this.name = databaseName;
this.restrictAccess = db.DatabaseOptions.UserAccess;
try
{
this.databaseState = db.Status;
}
catch (Exception ex)
{
SqlException sqlException = CUtils.GetSqlException(ex);
if (null != sqlException && true == CUtils.IsPermissionDeniedException(sqlException))
{
this.databaseState = DatabaseStatus.Inaccessible;
}
else
{
throw;
}
}
this.closeCursorOnCommit = db.DatabaseOptions.CloseCursorsOnCommitEnabled;
this.defaultCursor = (db.IsSupportedProperty("LocalCursorsDefault") &&
db.DatabaseOptions.LocalCursorsDefault)
? DefaultCursor.Local
: DefaultCursor.Global;
if (db.IsSupportedProperty("AutoClose"))
{
this.autoClose = db.DatabaseOptions.AutoClose;
}
this.autoShrink = db.DatabaseOptions.AutoShrink;
this.autoCreateStatistics = db.DatabaseOptions.AutoCreateStatistics;
this.autoUpdateStatistics = db.DatabaseOptions.AutoUpdateStatistics;
this.ansiNullDefault = db.DatabaseOptions.AnsiNullDefault;
this.ansiNulls = db.DatabaseOptions.AnsiNullsEnabled;
this.quotedIdentifier = db.DatabaseOptions.QuotedIdentifiersEnabled;
this.recursiveTriggers = db.DatabaseOptions.RecursiveTriggersEnabled;
if (db.IsSupportedProperty("RecoveryModel"))
{
this.recoveryModel = db.DatabaseOptions.RecoveryModel;
}
if (db.IsSupportedProperty("LastBackupDate"))
{
this.lastBackupDate = db.LastBackupDate;
}
if (db.IsSupportedProperty("LastLogBackupDate"))
{
this.lastLogBackupDate = db.LastLogBackupDate;
}
if (Utils.IsSql12OrLater(context.Server.Information.Version.Major))
{
this.autoCreateStatisticsIncremental = db.DatabaseOptions.AutoCreateStatisticsIncremental;
}
// SQL Server 2000 options
if (7 < context.Server.Information.Version.Major)
{
if (context.IsNewObject)
{
this.collation = this.defaultCollation = manager.GetString("general_default");
}
else
{
this.collation = db.Collation;
}
this.isReadOnly = db.DatabaseOptions.ReadOnly;
if (db.IsSupportedProperty("PageVerify"))
{
this.pageVerify = db.DatabaseOptions.PageVerify;
}
this.ansiWarnings = db.DatabaseOptions.AnsiWarningsEnabled;
this.ansiPadding = db.DatabaseOptions.AnsiPaddingEnabled;
this.arithabort = db.DatabaseOptions.ArithmeticAbortEnabled;
this.numericRoundAbort = db.DatabaseOptions.NumericRoundAbortEnabled;
this.concatNullYieldsNull = db.DatabaseOptions.ConcatenateNullYieldsNull;
}
else
{
this.collation = String.Empty;
this.isReadOnly = false;
this.pageVerify = PageVerify.None;
this.ansiPadding = false;
this.ansiWarnings = false;
this.arithabort = false;
this.numericRoundAbort = false;
this.concatNullYieldsNull = false;
}
// DB_CHAINING supported in SQL Server 2000 SP3 and later
Version sql2000sp3 = new Version(8, 0, 760);
if (sql2000sp3 <= context.Server.Information.Version)
{
this.dbChaining = db.DatabaseOptions.DatabaseOwnershipChaining;
}
else
{
this.dbChaining = false;
}
// SQL Server 2005 options
if (8 < context.Server.Version.Major)
{
this.autoUpdateStatisticsAsync = db.DatabaseOptions.AutoUpdateStatisticsAsync;
this.trustworthy = db.DatabaseOptions.Trustworthy;
this.dateCorrelationOptimization = db.DatabaseOptions.DateCorrelationOptimization;
this.parameterization = db.DatabaseOptions.IsParameterizationForced;
this.isReadCommittedSnapshotOn = db.IsReadCommittedSnapshotOn;
if (db.IsSupportedProperty("BrokerEnabled"))
{
this.serviceBrokerGuid = db.ServiceBrokerGuid;
this.brokerEnabled = db.BrokerEnabled;
}
if (db.IsSupportedProperty("SnapshotIsolationState"))
{
this.allowSnapshotIsolation = !(db.SnapshotIsolationState == SnapshotIsolationState.Disabled);
}
}
else
{
this.autoUpdateStatisticsAsync = false;
this.trustworthy = false;
this.dateCorrelationOptimization = false;
this.brokerEnabled = false;
this.parameterization = false;
this.isReadCommittedSnapshotOn = false;
this.allowSnapshotIsolation = false;
}
this.varDecimalEnabled =
// db.IsVarDecimalStorageFormatSupported &&
db.IsVarDecimalStorageFormatEnabled;
// SQL Server 2008 options
// Both EncryptionEnabled and HonorPriority are added in SQL Server 2008 only and hence one condition is enough
if (db.IsSupportedProperty("EncryptionEnabled"))
{
this.encryptionEnabled = db.EncryptionEnabled;
this.honorBrokerPriority = db.HonorBrokerPriority;
this.varDecimalEnabled = true;
}
else
{
this.encryptionEnabled = false;
this.honorBrokerPriority = false;
}
if (db.IsSupportedProperty("FilestreamDirectoryName"))
{
this.filestreamDirectoryName = db.FilestreamDirectoryName;
this.filestreamNonTransactedAccess = db.FilestreamNonTransactedAccess;
}
try
{
if (context.Server.IsSupportedProperty("IsFullTextInstalled") && context.Server.IsFullTextInstalled)
{
// Full-text indexing will always be enabled in Katmai
if (db.IsSupportedProperty("IsFullTextEnabled"))
{
this.fullTextIndexingEnabled = db.IsFullTextEnabled;
}
else
{
this.fullTextIndexingEnabled = true;
}
}
}
catch (Exception ex)
{
SqlException sqlException = CUtils.GetSqlException(ex);
if (null != sqlException && true == CUtils.IsPermissionDeniedException(sqlException))
{
// assume false
this.fullTextIndexingEnabled = false;
}
else
{
throw;
}
}
if ((db.CompatibilityLevel == CompatibilityLevel.Version60) ||
(db.CompatibilityLevel == CompatibilityLevel.Version65))
{
string errorMessage = manager.GetString("error_60compatibility");
throw new InvalidOperationException(errorMessage);
}
this.databaseCompatibilityLevel = db.CompatibilityLevel;
if (db.IsSupportedProperty("ContainmentType"))
{
this.databaseContainmentType = db.ContainmentType;
this.defaultFulltextLanguageLcid = db.DefaultFullTextLanguage.Lcid;
this.defaultLanguageLcid = db.DefaultLanguage.Lcid;
this.nestedTriggersEnabled = db.NestedTriggersEnabled;
this.transformNoiseWords = db.TransformNoiseWords;
this.twoDigitYearCutoff = db.TwoDigitYearCutoff;
}
// SQL Server 2011 options
// TargetRecoveryTime is added in SQL Server 2011
if (db.IsSupportedProperty("TargetRecoveryTime"))
{
this.targetRecoveryTime = db.TargetRecoveryTime;
}
try
{
if (db.IsSupportedProperty("IsMirroringEnabled") && db.IsMirroringEnabled)
{
this.mirrorSafetyLevel = db.MirroringSafetyLevel;
this.witnessServer = db.MirroringWitness;
}
}
catch (Exception ex)
{
SqlException sqlException = CUtils.GetSqlException(ex);
if (null != sqlException && true == CUtils.IsPermissionDeniedException(sqlException))
{
/// do nothing
}
else
{
throw;
}
}
// SQL Server 2014 options
// DelayedDurability is added in SQL Server 2014
if (db.IsSupportedProperty("DelayedDurability"))
{
this.delayedDurability = db.DelayedDurability;
}
//Only fill in the Azure properties when connected to an Azure server
if (context.Server.ServerType == DatabaseEngineType.SqlAzureDatabase
&& context.Server.DatabaseEngineEdition != DatabaseEngineEdition.SqlOnDemand)
{
this.azureEditionDisplayValue = db.AzureEdition;
AzureEdition edition;
if (Enum.TryParse(db.AzureEdition, true, out edition))
{
this.azureEdition = edition;
}
else
{
// Unknown Azure DB Edition so we can't set a value, leave as default Standard
// Note that this is likely a
}
//Size is in MB, but if it's greater than a GB we want to display the size in GB
//We do this to be on par with what the management portal displays
if (db.Size >= 1024)
{
this.maxSize = new DbSize((int)(db.Size / 1024.0), DbSize.SizeUnits.GB);
}
else
{
this.maxSize = new DbSize((int)db.Size, DbSize.SizeUnits.MB);
}
this.GetServiceLevelObjectiveValues(context);
}
// Check if we support database scoped configurations on this server. Since these were all added at the same time,
// only check if MaxDop is supported rather than each individual property.
if (db.IsSupportedProperty("MaxDop"))
{
this.maxDop = db.MaxDop;
this.maxDopForSecondary = db.MaxDopForSecondary;
this.legacyCardinalityEstimation = db.LegacyCardinalityEstimation;
this.legacyCardinalityEstimationForSecondary = db.LegacyCardinalityEstimationForSecondary;
this.parameterSniffing = db.ParameterSniffing;
this.parameterSniffingForSecondary = db.ParameterSniffingForSecondary;
this.queryOptimizerHotfixes = db.QueryOptimizerHotfixes;
this.queryOptimizerHotfixesForSecondary = db.QueryOptimizerHotfixesForSecondary;
}
}