You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Data/Schema/Sql/Columns.cs

84 lines
3.0 KiB

2 years ago
using Connected.Entities.Annotations;
using Connected.Entities.Storage;
using System.Data;
using System.Text;
namespace Connected.Data.Schema.Sql;
internal class Columns : SynchronizationQuery<List<ISchemaColumn>>
{
public Columns(ExistingSchema existing)
{
Existing = existing;
}
private ExistingSchema Existing { get; }
protected override async Task<List<ISchemaColumn>> OnExecute()
{
var result = new List<ISchemaColumn>();
var rdr = await Context.OpenReader(new StorageOperation { CommandText = CommandText });
while (rdr.Read())
{
var column = new ExistingColumn(Existing)
{
IsNullable = !string.Equals(rdr.GetValue("IS_NULLABLE", string.Empty), "NO", StringComparison.OrdinalIgnoreCase),
DataType = SchemaExtensions.ToDbType(rdr.GetValue("DATA_TYPE", string.Empty)),
MaxLength = rdr.GetValue("CHARACTER_MAXIMUM_LENGTH", 0),
Name = rdr.GetValue("COLUMN_NAME", string.Empty),
};
if (column.DataType == DbType.Decimal || column.DataType == DbType.VarNumeric)
{
column.Precision = rdr.GetValue("NUMERIC_PRECISION", 0);
column.Scale = rdr.GetValue("NUMERIC_SCALE", 0);
}
if (column.DataType == DbType.DateTime2
|| column.DataType == DbType.Time
|| column.DataType == DbType.DateTimeOffset)
column.DatePrecision = rdr.GetValue("DATETIME_PRECISION", 0);
if (column.DataType == DbType.Date)
column.DateKind = DateKind.Date;
else if (column.DataType == DbType.DateTime)
{
if (string.Compare(rdr.GetValue("DATA_TYPE", string.Empty), "smalldatetime", true) == 0)
column.DateKind = DateKind.SmallDateTime;
}
else if (column.DataType == DbType.DateTime2)
column.DateKind = DateKind.DateTime2;
else if (column.DataType == DbType.Time)
column.DateKind = DateKind.Time;
else if (column.DataType == DbType.Binary)
{
if (string.Compare(rdr.GetValue("DATA_TYPE", string.Empty), "varbinary", true) == 0)
column.BinaryKind = BinaryKind.VarBinary;
else if (string.Compare(rdr.GetValue("DATA_TYPE", string.Empty), "binary", true) == 0)
column.BinaryKind = BinaryKind.Binary;
}
column.IsVersion = string.Equals(rdr.GetValue("DATA_TYPE", string.Empty), "timestamp", StringComparison.OrdinalIgnoreCase);
result.Add(column);
}
rdr.Close();
return result;
}
private string CommandText
{
get
{
var text = new StringBuilder();
text.AppendLine($"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{Context.Schema.SchemaName()}' AND TABLE_NAME = '{Context.Schema.Name}'");
return text.ToString();
}
}
}