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.

128 lines
3.1 KiB

using Connected.Collections;
using Connected.Interop;
using Connected.ServiceModel.Annotations;
using Connected.ServiceModel.Client.Data.Remote;
namespace Connected.ServiceModel.Client.Data.Schema;
internal sealed class TableCreate : TableTransaction
{
//private const string KeysExceptionMessage = "A ISchemaColumn cannot have a PartitionKeyAttribute and PrimaryKeyAttribute set. Use either PartitionKey or PrimaryKey attribute.";
//private const string NoPartitionKeyMessage = "Schema must have at least one property with PartitionKeyAttribute.";
protected override async Task OnExecute()
{
var columns = new List<RemoteTableColumn>();
Context.Schema.Columns.SortByOrdinal();
foreach (var column in Context.Schema.Columns)
{
columns.Add(new RemoteTableColumn
{
DataType = CreateDataTypeMetaData(column),
IsPartitionKey = column.Property?.FindAttribute<PartitionKeyAttribute>() is not null,
IsPrimaryKey = column.IsPrimaryKey,
Name = column.Name
});
}
await Context.Remote.CreateTable(Context.Schema.Name, columns);
}
//private List<ISchemaColumn> PartitionKeys
//{
// get
// {
// var result = new List<ISchemaColumn>();
// foreach (var column in Context.Schema.Columns)
// {
// if (column.Property?.FindAttribute<PartitionKeyAttribute>() is not null)
// {
// if (column.IsPrimaryKey)
// throw new InvalidOperationException($"{KeysExceptionMessage} ({column.Name})");
// result.Add(column);
// }
// }
// return result;
// }
//}
//private List<ISchemaColumn> PrimaryKeys
//{
// get
// {
// var result = new List<ISchemaColumn>();
// foreach (var column in Context.Schema.Columns)
// {
// if (column.IsPrimaryKey)
// {
// if (column.Property?.FindAttribute<PartitionKeyAttribute>() is not null)
// throw new InvalidOperationException($"{KeysExceptionMessage} ({column.Name})");
// result.Add(column);
// }
// }
// return result;
// }
//}
//private string CommandText
//{
// get
// {
// var partitionKeys = PartitionKeys;
// var primaryKeys = PrimaryKeys;
// if (!partitionKeys.Any())
// throw new InvalidOperationException($"{NoPartitionKeyMessage} ({Context.Schema.Name})");
// var keysCount = partitionKeys.Count + primaryKeys.Count;
// var text = new StringBuilder();
// var name = Temporary ? TemporaryName : Context.Schema.Name;
// text.AppendLine($"CREATE TABLE {name}");
// text.AppendLine("(");
// var comma = string.Empty;
// for (var i = 0; i < Context.Schema.Columns.Count; i++)
// {
// text.AppendLine($"{comma} {CreateColumnCommandText(Context.Schema.Columns[i])}");
// comma = ",";
// }
// text.Append("PRIMARY KEY (");
// if (keysCount > 1)
// text.Append('(');
// for (var i = 0; i < partitionKeys.Count; i++)
// {
// text.Append(partitionKeys[i].Name);
// if (i < partitionKeys.Count - 1)
// text.Append(',');
// }
// if (keysCount > 1)
// text.Append(')');
// for (var i = 0; i < primaryKeys.Count; i++)
// {
// text.Append(primaryKeys[i].Name);
// if (i < primaryKeys.Count - 1)
// text.Append(',');
// }
// text.AppendLine(");");
// return text.ToString();
// }
}