Exemplo n.º 1
0
        public static async Task <ITableColumn> AddRandomColumnAsync(this ITableTemplate template, Authentication authentication)
        {
            var column = await template.AddNewAsync(authentication);

            await column.InitializeRandomAsync(authentication);

            await template.EndNewAsync(authentication, column);

            return(column);
        }
        public static async Task AddColumnAsync(this ITableTemplate template, Authentication authentication, string name, string typeName, string comment)
        {
            var column = await template.AddNewAsync(authentication);

            await column.SetNameAsync(authentication, name);

            await column.SetDataTypeAsync(authentication, typeName);

            if (comment != string.Empty)
            {
                await column.SetCommentAsync(authentication, comment);
            }
            await template.EndNewAsync(authentication, column);
        }
Exemplo n.º 3
0
        public static async Task <bool> CreateColumnAsync(this ITableTemplate template, Authentication authentication)
        {
            var columnName = RandomUtility.NextIdentifier();

            if (await template.ContainsAsync(columnName) == true)
            {
                return(false);
            }

            var column = await template.AddNewAsync(authentication);

            await column.SetNameAsync(authentication, columnName);

            if (template.PrimaryKey.Any() == false)
            {
                await column.SetIsKeyAsync(authentication, true);
            }
            else if (template.Count == 0 && RandomUtility.Within(10))
            {
                await column.SetIsKeyAsync(authentication, true);

                await column.SetIsUniqueAsync(authentication, RandomUtility.Within(75));
            }

            if (RandomUtility.Within(75) == true)
            {
                await column.SetTagsAsync(authentication, TagInfo.All);
            }
            else
            {
                await column.SetTagsAsync(authentication, tags.Random());
            }

            if (RandomUtility.Within(75) == true)
            {
                await column.SetDataTypeAsync(authentication, CremaDataTypeUtility.GetBaseTypeNames().Random());
            }
            else
            {
                await column.SetDataTypeAsync(authentication, template.SelectableTypes.Random());
            }

            if (RandomUtility.Within(25) == true)
            {
                await column.SetCommentAsync(authentication, RandomUtility.NextString());
            }

            if (CremaDataTypeUtility.CanUseAutoIncrement(column.DataType) == true)
            {
                await column.SetAutoIncrementAsync(authentication, RandomUtility.NextBoolean());
            }

            try
            {
                await template.EndNewAsync(authentication, column);
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
        public static async Task <bool> EditColumnsAsync(ITableTemplate template, Authentication authentication)
        {
            var columnCount = template.Dispatcher.Invoke(() => template.Count);
            var dataTypes   = template.Dispatcher.Invoke(() => template.SelectableTypes);
            var columnList  = new List <JsonColumnInfos.ItemInfo>(columnCount);
            var idToColumn  = new Dictionary <Guid, ITableColumn>(columnCount);

            template.Dispatcher.Invoke(() =>
            {
                foreach (var item in template)
                {
                    var column = new JsonColumnInfos.ItemInfo()
                    {
                        ID            = Guid.NewGuid(),
                        Name          = item.Name,
                        IsKey         = item.IsKey,
                        DataType      = item.DataType,
                        Comment       = item.Comment,
                        IsUnique      = item.IsUnique,
                        AutoIncrement = item.AutoIncrement,
                        DefaultValue  = item.DefaultValue,
                        Tags          = (string)item.Tags,
                        IsReadOnly    = item.IsReadOnly,
                        DisallowNull  = !item.AllowNull,
                    };
                    idToColumn.Add(column.ID, item);
                    columnList.Add(column);
                }
            });

            var schema         = JsonSchemaUtility.CreateSchema(typeof(JsonColumnInfos));
            var itemsSchema    = schema.Properties[nameof(JsonColumnInfos.Items)];
            var itemSchema     = itemsSchema.Items.First();
            var dataTypeSchema = itemSchema.Properties[nameof(JsonColumnInfos.ItemInfo.DataType)];

            dataTypeSchema.SetEnums(dataTypes);
            var tagSchema = itemSchema.Properties[nameof(JsonColumnInfos.ItemInfo.Tags)];

            tagSchema.SetEnums(TagInfoUtility.Names);

            var columns = new JsonColumnInfos()
            {
                Items = columnList.ToArray()
            };

            using (var editor = new JsonEditorHost(columns, schema))
            {
                if (editor.Execute() == false)
                {
                    return(false);
                }

                columns = editor.Read <JsonColumnInfos>();
            }

            //template.Dispatcher.Invoke(() =>
            //{
            foreach (var item in idToColumn.Keys.ToArray())
            {
                if (columns.Items.Any(i => i.ID == item) == false)
                {
                    var column = idToColumn[item];
                    await column.DeleteAsync(authentication);

                    idToColumn.Remove(item);
                }
            }

            for (var i = 0; i < columns.Items.Length; i++)
            {
                var item = columns.Items[i];
                if (item.ID == Guid.Empty)
                {
                    var column = await template.AddNewAsync(authentication);

                    item = await InitializeFieldsAsync(authentication, item, column);

                    await template.EndNewAsync(authentication, column);

                    item.ID = Guid.NewGuid();
                    idToColumn.Add(item.ID, column);
                    columns.Items[i] = item;
                }
                else if (idToColumn.ContainsKey(item.ID) == true)
                {
                    var column = idToColumn[item.ID];
                    await SetFieldsAsync(authentication, item, column);
                }
                else
                {
                    throw new InvalidOperationException($"{item.ID} is not existed column.");
                }
            }

            for (var i = 0; i < columns.Items.Length; i++)
            {
                var item   = columns.Items[i];
                var column = idToColumn[item.ID];
                await column.SetIndexAsync(authentication, i);
            }
            //});

            return(true);
        }