//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Data;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.EditData
{
///
/// Small class that stores information needed by the edit data service to properly process
/// edits into scripts.
///
public class EditColumnMetadata
{
///
/// Constructs a simple edit column metadata provider
///
public EditColumnMetadata()
{
HasExtendedProperties = false;
}
#region Basic Properties (properties provided by SMO)
///
/// If set, this is a string representation of the default value. If set to null, then the
/// column does not have a default value.
///
public string DefaultValue { get; set; }
///
/// Escaped identifier for the name of the column
///
public string EscapedName { get; set; }
///
/// Whether or not the column is computed
///
public bool IsComputed { get; set; }
///
/// Whether or not the column is deterministically computed
///
public bool IsDeterministic { get; set; }
///
/// Whether or not the column is an identity column
///
public bool IsIdentity { get; set; }
///
/// The ordinal ID of the column
///
public int Ordinal { get; set; }
#endregion
#region Extended Properties (properties provided by SqlClient)
public DbColumnWrapper DbColumn { get; private set; }
///
/// Whether or not the column has extended properties
///
public bool HasExtendedProperties { get; private set; }
///
/// Whether or not the column is calculated on the server side. This could be a computed
/// column or a identity column.
///
public bool? IsCalculated { get; private set; }
///
/// Whether or not the column is used in a key to uniquely identify a row
///
public bool? IsKey { get; private set; }
///
/// Whether or not the column can be trusted for uniqueness
///
public bool? IsTrustworthyForUniqueness { get; private set; }
#endregion
///
/// Extracts extended column properties from the database columns from SQL Client
///
/// The column information provided by SQL Client
public void Extend(DbColumnWrapper dbColumn)
{
Validate.IsNotNull(nameof(dbColumn), dbColumn);
DbColumn = dbColumn;
// A column is trustworthy for uniqueness if it can be updated or it has an identity
// property. If both of these are false (eg, timestamp) we can't trust it to uniquely
// identify a row in the table
IsTrustworthyForUniqueness = dbColumn.IsUpdatable || dbColumn.IsIdentity.HasTrue();
// A key column is determined by whether it is a key
IsKey = dbColumn.IsKey;
// A column is calculated if it is identity, computed, or otherwise not updatable
IsCalculated = IsIdentity || IsComputed || !dbColumn.IsUpdatable;
// Mark the column as extended
HasExtendedProperties = true;
}
}
}