public void SetAutoIncrementTrue()
        {
            var column = this.RandomOrDefault(item => CremaDataTypeUtility.CanUseAutoIncrement(item.DataType) && item.AutoIncrement == false && item.DefaultValue == DBNull.Value);

            column.AutoIncrement = true;
            Assert.AreEqual(true, column.AutoIncrement);
        }
Пример #2
0
 public override DataTemplate SelectTemplate(object item, DependencyObject container)
 {
     if (container is FrameworkElement fe)
     {
         if (item is NewRowItemViewModel viewModel)
         {
             if (CremaDataTypeUtility.IsBaseType(viewModel.DataType) == true)
             {
                 var dataType = CremaDataTypeUtility.GetType(viewModel.DataType);
                 return((DataTemplate)fe.FindResource(dataType.FullName));
             }
             else
             {
                 if (viewModel.IsFlag == true)
                 {
                     return((DataTemplate)fe.FindResource("CremaFlagTypeSelector"));
                 }
                 return((DataTemplate)fe.FindResource("CremaTypeSelector"));
             }
         }
         else if (item is NewRowParentItemViewModel parentViewModel)
         {
             return((DataTemplate)fe.FindResource("ParentSelector"));
         }
     }
     return(base.SelectTemplate(item, container));
 }
Пример #3
0
 public void SetAutoIncrementFalse()
 {
     this.attribute.DataType      = CremaDataTypeUtility.GetBaseTypes().Random(item => CremaDataTypeUtility.CanUseAutoIncrement(item));
     this.attribute.AutoIncrement = true;
     this.attribute.AutoIncrement = false;
     Assert.AreEqual(false, this.attribute.AutoIncrement);
 }
        protected override async Task <object[]> OnExecuteAsync(string domainID, string tableName, IDictionary <string, object> fields)
        {
            if (fields == null)
            {
                throw new ArgumentNullException(nameof(fields));
            }
            var domain = await this.CremaHost.GetDomainAsync(Guid.Parse(domainID));

            var contents = domain.Host as IEnumerable <ITableContent>;
            var content  = contents.FirstOrDefault(item => item.Dispatcher.Invoke(() => item.Table.Name) == tableName);

            if (content == null)
            {
                throw new TableNotFoundException(tableName);
            }
            var authentication = this.Context.GetAuthentication(this);
            var tableInfo      = content.Table.TableInfo;
            var row            = await content.AddNewAsync(authentication, null);

            foreach (var item in fields)
            {
                var typeName = tableInfo.Columns.First(i => i.DataType == item.Key).DataType;
                var type     = CremaDataTypeUtility.GetType(typeName);
                var value    = CremaConvert.ChangeType(item.Value, type);
                await row.SetFieldAsync(authentication, item.Key, value);
            }
            await content.EndNewAsync(authentication, row);

            return(tableInfo.Columns.Select(item => row[item.Name]).ToArray());
        }
Пример #5
0
        protected override void OnExecute()
        {
            var tableInfo = this.Content.Dispatcher.Invoke(() => this.Content.Table.TableInfo);
            var keys      = tableInfo.Columns.Where(item => item.IsKey).ToArray();
            var schema    = this.CreateSchema(tableInfo.Columns);

            var fieldList = new List <object>();

            for (var i = 0; i < this.Keys.Length; i++)
            {
                var key     = keys[i];
                var keyText = this.Keys[i];
                var type    = CremaDataTypeUtility.IsBaseType(key.DataType) ? CremaDataTypeUtility.GetType(key.DataType) : typeof(string);
                var value   = CremaConvert.ChangeType(keyText, type);
                fieldList.Add(value);
            }

            var authentication = this.CommandContext.GetAuthentication(this);
            var tableRow       = this.Content.Dispatcher.Invoke(() => this.Content.Find(authentication, fieldList.ToArray()));

            var terminal = new Terminal();

            if (terminal.ReadString("type 'remove':") != "remove")
            {
                return;
            }

            this.Content.Dispatcher.Invoke(() => { tableRow.Delete(authentication); });
        }
Пример #6
0
        public void NewWithNameAndInvalidType_Fail()
        {
            var attributeName = RandomUtility.NextIdentifier();
            var attributeType = typeof(string).Assembly.GetTypes().Random(item => CremaDataTypeUtility.IsBaseType(item) == false);

            new CremaAttribute(attributeName, attributeType);
        }
Пример #7
0
        public void NewWithNullNameAndType()
        {
            var columnType = CremaDataTypeUtility.GetBaseTypes().Random();
            var column     = new CremaDataColumn(null, columnType);

            Assert.AreNotEqual(Guid.Empty, column.ColumnID);
            Assert.AreEqual(-1, column.Index);
            Assert.AreEqual(string.Empty, column.ColumnName);
            Assert.AreEqual(columnType, column.DataType);
            Assert.AreEqual(string.Empty, column.Comment);
            Assert.IsFalse(column.IsKey);
            Assert.IsFalse(column.Unique);
            Assert.IsFalse(column.ReadOnly);
            Assert.AreEqual(null, column.Table);
            Assert.AreEqual(TagInfo.All, column.Tags);
            Assert.AreEqual(TagInfo.All, column.DerivedTags);
            Assert.AreEqual(string.Empty, column.Validation);
            Assert.AreEqual(string.Empty, column.Expression);
            Assert.IsNull(column.CremaType);
            Assert.IsTrue(column.AllowDBNull);
            Assert.IsFalse(column.AutoIncrement);
            Assert.AreEqual(DBNull.Value, column.DefaultValue);

            Assert.AreEqual(column.CreationInfo, SignatureDate.Empty);
            Assert.AreEqual(column.ModificationInfo, SignatureDate.Empty);
        }
Пример #8
0
        public void SetAutoIncrement(ITableColumn column, TaskContext context)
        {
            column.Dispatcher.Invoke(() =>
            {
                var autoIncrement = RandomUtility.NextBoolean();
                if (Verify(autoIncrement) == false)
                {
                    return;
                }
                column.SetAutoIncrement(context.Authentication, autoIncrement);
            });

            bool Verify(bool autoIncrement)
            {
                if (context.AllowException == true)
                {
                    return(true);
                }
                if (autoIncrement == true && CremaDataTypeUtility.CanUseAutoIncrement(column.DataType) == false)
                {
                    return(false);
                }
                return(true);
            }
        }
Пример #9
0
 public void SetAutoIncrementToInvalidIncrementType()
 {
     this.attribute.DataType      = CremaDataTypeUtility.GetBaseTypes().Random(item => CremaDataTypeUtility.CanUseAutoIncrement(item) == false);
     this.attribute.AutoIncrement = true;
     Assert.AreEqual(typeof(int), this.attribute.DataType);
     Assert.AreEqual(true, this.attribute.AutoIncrement);
 }
Пример #10
0
        public void SetDataType()
        {
            var newType = CremaDataTypeUtility.GetBaseTypes().Random(item => item != this.attribute.DataType);

            this.attribute.DataType = newType;
            Assert.AreEqual(newType, this.attribute.DataType);
        }
Пример #11
0
        public void SetDefaultValue_Fail()
        {
            var type1 = CremaDataTypeUtility.GetBaseTypes().Random(item => item != typeof(string));

            this.attribute.DataType     = type1;
            this.attribute.DefaultValue = RandomUtility.NextWord();
        }
Пример #12
0
 public static string GetRandomString(this ITableColumn tableColumn)
 {
     if (tableColumn.DefaultValue != null && RandomUtility.Next(3) == 0)
     {
         return(null);
     }
     else if (tableColumn.AllowNull == true && RandomUtility.Next(4) == 0)
     {
         return(null);
     }
     else
     {
         var template = tableColumn.Template;
         var dataType = tableColumn.DataType;
         if (CremaDataTypeUtility.IsBaseType(dataType) == false)
         {
             var type = template.GetType(dataType);
             return(type.GetRandomString());
         }
         else
         {
             var value = RandomUtility.Next(CremaDataTypeUtility.GetType(dataType));
             return(CremaConvert.ChangeType(value, typeof(string)) as string);
         }
     }
 }
Пример #13
0
 public void Create()
 {
     foreach (var item in CremaDataTypeUtility.GetBaseTypes())
     {
         var column = new CremaDataColumn(RandomUtility.NextIdentifier(), item);
         Assert.IsNotNull(column);
     }
 }
Пример #14
0
 public void DBNullToOther()
 {
     this.AddRows(DBNull.Value);
     foreach (var item in CremaDataTypeUtility.GetBaseTypes().Where(item => item != this.column.DataType))
     {
         column.DataType = typeof(float);
     }
 }
Пример #15
0
 public static string GenerateSchemaTypeName(string typeName)
 {
     if (CremaDataTypeUtility.IsBaseType(typeName) == true)
     {
         return(string.Format("{0}:{1}", "xs", typeName));
     }
     return(typeName);
 }
        public void SetAutoIncrementToInvalidIncrementType()
        {
            var column = this.RandomOrDefault(item => CremaDataTypeUtility.CanUseAutoIncrement(item.DataType) == false && item.CremaType == null && item.DefaultValue == DBNull.Value);

            column.AutoIncrement = true;
            Assert.AreEqual(typeof(int), column.DataType);
            Assert.AreEqual(true, column.AutoIncrement);
        }
Пример #17
0
        public void SetDefaultValue()
        {
            this.attribute.DataType = CremaDataTypeUtility.GetBaseTypes().Random();
            var newDefaultValue = RandomUtility.Next(this.attribute.DataType);

            this.attribute.DefaultValue = newDefaultValue;
            Assert.AreEqual(newDefaultValue, this.attribute.DefaultValue);
        }
Пример #18
0
        public static void InitializeRandom(this CremaDataColumn dataColumn)
        {
            var dataTable = dataColumn.Table;

            if (RandomUtility.Within(75) == true)
            {
                dataColumn.DataType = CremaDataTypeUtility.GetBaseTypes().Random();
            }
            else if (dataTable != null && dataTable.DataSet != null && dataTable.DataSet.Types.Any())
            {
                dataColumn.CremaType = dataTable.DataSet.Types.Random();
            }

            if (RandomUtility.Within(25) == true)
            {
                SetHopeType(dataColumn);
            }

            if (dataTable != null && dataTable.PrimaryKey.Any() == false)
            {
                dataColumn.IsKey = true;
            }
            else if (RandomUtility.Within(10) && dataColumn.DataType != typeof(bool))
            {
                dataColumn.IsKey  = true;
                dataColumn.Unique = RandomUtility.Within(75);
            }

            if (RandomUtility.Within(25) && dataColumn.DataType != typeof(bool))
            {
                var unique = RandomUtility.Within(75);
                if (unique != false || dataTable == null || dataTable.PrimaryKey.Count() != 1)
                {
                    dataColumn.Unique = unique;
                }
            }

            if (RandomUtility.Within(25) == true)
            {
                dataColumn.Comment = RandomUtility.NextString();
            }

            if (RandomUtility.Within(25) == true)
            {
                dataColumn.DefaultValue = dataColumn.GetRandomValue();
            }

            if (CremaDataTypeUtility.CanUseAutoIncrement(dataColumn.DataType) == true && dataColumn.DefaultValue == DBNull.Value)
            {
                dataColumn.AutoIncrement = RandomUtility.NextBoolean();
            }

            if (RandomUtility.Within(5) == true)
            {
                dataColumn.ReadOnly = true;
            }
        }
Пример #19
0
        public void SetDefaultValueToAutoIncrementColumn_Fail()
        {
            this.column.DataType      = CremaDataTypeUtility.GetBaseTypes().Random(item => CremaDataTypeUtility.CanUseAutoIncrement(item));
            this.column.AutoIncrement = true;
            var newDefaultValue = RandomUtility.Next(this.column.DataType);

            this.column.DefaultValue = newDefaultValue;
            Assert.AreEqual(newDefaultValue, this.column.DefaultValue);
        }
Пример #20
0
 private object GetDefaultValue(ColumnInfo columnInfo)
 {
     if (columnInfo.DefaultValue != null && CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
     {
         var type = CremaDataTypeUtility.GetType(columnInfo.DataType);
         return(CremaConvert.ChangeType(columnInfo.DefaultValue, type));
     }
     return(columnInfo.DefaultValue);
 }
Пример #21
0
        private void ReadColumnDataType(XmlSchemaSimpleType simpleType, CremaDataColumn column)
        {
            var typeName = simpleType.QualifiedName.Name;

            if (simpleType.QualifiedName.Namespace == XmlSchema.Namespace)
            {
                column.InternalDataType = CremaDataTypeUtility.GetType(typeName) ?? typeof(string);
            }
            else if (simpleType.QualifiedName.Name == typeof(Guid).GetTypeName() && simpleType.QualifiedName.Namespace == simpleType.GetSchema().TargetNamespace)
            {
                column.InternalDataType = typeof(Guid);
            }
            else
            {
                var categoryPath = PathUtility.Separator;

                if (simpleType.QualifiedName.Namespace == CremaSchema.BaseNamespace)
                {
                    if (this.version.Minor >= CremaSchema.MinorVersion)
                    {
                        categoryPath = simpleType.ReadAppInfoAsString(CremaSchema.TypeInfo, CremaSchema.CategoryPath, PathUtility.Separator);
                    }
                    else
                    {
                        var xmlRestriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;
                        if (xmlRestriction == null)
                        {
                            if (simpleType.Content is XmlSchemaSimpleTypeList == true)
                            {
                                simpleType = (simpleType.Content as XmlSchemaSimpleTypeList).BaseItemType;
                            }
                        }
                        var categoryName = simpleType.ReadAppInfoAsString(CremaSchema.TypeInfo, CremaSchema.Category) ?? string.Empty;
                        categoryPath = categoryName == string.Empty ? PathUtility.Separator : categoryName.WrapSeparator();
                    }
                }
                else
                {
                    if (this.version.Major >= CremaSchema.MajorVersion)
                    {
                        categoryPath = this.dataSet.GetTypeCategoryPath(simpleType.QualifiedName.Namespace);
                    }
                    else
                    {
                        categoryPath = PathUtility.Separator;
                    }
                }

                if (this.dataSet.Types.Contains(typeName, categoryPath) == false)
                {
                    this.ReadType(simpleType);
                }

                column.InternalCremaType = (InternalDataType)this.dataSet.Types[typeName, categoryPath];
            }
        }
Пример #22
0
        public void SetEmptyDefaultValue()
        {
            var dataType     = CremaDataTypeUtility.GetBaseTypes().Random();
            var defaultValue = CremaConvert.ToString(RandomUtility.Next(dataType));

            this.column.DefaultValue = defaultValue;
            this.column.DataTypeName = dataType.GetTypeName();
            this.column.DefaultValue = string.Empty;
            Assert.AreEqual(DBNull.Value, this.column.DefaultValue);
        }
Пример #23
0
        public static CodeTypeReference GetCodeType(this ColumnInfo columnInfo, CodeType codeType)
        {
            if (CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
            {
                return(new CodeTypeReference(CremaDataTypeUtility.GetType(columnInfo.DataType)));
            }
            var itemName = new ItemName(columnInfo.DataType);

            return(Utility.GenerateCodeType(TypeNamespace, itemName.Name));
        }
Пример #24
0
        public static void Copy(object sourceItem, object destItem, bool isBeingEdited)
        {
            if (HasError(sourceItem) == true)
            {
                throw new ArgumentException();
            }

            if (destItem is IEditableObject editable)
            {
                try
                {
                    if (isBeingEdited == false)
                    {
                        editable.BeginEdit();
                    }

                    if (destItem is ICustomTypeDescriptor descriptor1 && sourceItem is ICustomTypeDescriptor descriptor2)
                    {
                        var props1 = descriptor1.GetProperties();
                        var props2 = descriptor2.GetProperties();

                        foreach (PropertyDescriptor prop2 in props2)
                        {
                            var prop1 = props1[prop2.Name];
                            if (prop1 == null)
                            {
                                continue;
                            }

                            if (prop1.Name == CremaSchema.__RelationID__ || prop1.Name == CremaSchema.__ParentID__)
                            {
                                continue;
                            }

                            if (prop1.IsReadOnly == false && CremaDataTypeUtility.IsBaseType(prop1.PropertyType))
                            {
                                prop1.SetValue(destItem, prop2.GetValue(sourceItem));
                            }
                        }
                    }
                    if (isBeingEdited == false)
                    {
                        editable.EndEdit();
                    }
                }
                catch
                {
                    if (isBeingEdited == false)
                    {
                        editable.CancelEdit();
                    }
                    throw;
                }
            }
        }
Пример #25
0
        public void AddColumnByNameAndTypeMany()
        {
            var dataTable = new CremaDataTable();
            var dataTypes = CremaDataTypeUtility.GetBaseTypes();

            for (var i = 0; i < dataTypes.Length; i++)
            {
                var dataColumn = dataTable.Columns.Add($"Column{i}", dataTypes[i]);
            }
            Assert.AreEqual(dataTypes.Length, dataTable.Columns.Count);
        }
Пример #26
0
        public static async Task InitializeRandomAsync(this ITableColumn tableColumn, Authentication authentication)
        {
            var template = tableColumn.Template;
            var table    = tableColumn.Template.Target;

            if (RandomUtility.Within(75) == true)
            {
                await tableColumn.SetDataTypeAsync(authentication, CremaDataTypeUtility.GetBaseTypeNames().Random(item => item != typeof(bool).GetTypeName()));
            }
            else
            {
                await tableColumn.SetDataTypeAsync(authentication, template.SelectableTypes.Random());
            }

            if (template.Count == 0)
            {
                await tableColumn.SetIsKeyAsync(authentication, true);
            }
            else if (RandomUtility.Within(10) && tableColumn.DataType != typeof(bool).GetTypeName())
            {
                await tableColumn.SetIsKeyAsync(authentication, true);

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

            if (RandomUtility.Within(25) && tableColumn.DataType != typeof(bool).GetTypeName())
            {
                var unique = RandomUtility.Within(75);
                if (unique != false || template.PrimaryKey.Count() != 1)
                {
                    await tableColumn.SetIsUniqueAsync(authentication, unique);
                }
            }

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

            if (RandomUtility.Within(25) == true)
            {
                await tableColumn.SetDefaultValueAsync(authentication, await tableColumn.GetRandomStringAsync());
            }

            if (CremaDataTypeUtility.CanUseAutoIncrement(tableColumn.DataType) == true && tableColumn.DefaultValue == null)
            {
                await tableColumn.SetAutoIncrementAsync(authentication, RandomUtility.NextBoolean());
            }

            if (RandomUtility.Within(5) == true)
            {
                await tableColumn.SetIsReadOnlyAsync(authentication, true);
            }
        }
Пример #27
0
        public static CremaDataTable CreateNormalChild(CremaDataTable dataTable, string childName)
        {
            var child = dataTable.Childs.Add(childName);

            child.Columns.Add("ID", typeof(int)).IsKey = true;
            foreach (var item in CremaDataTypeUtility.GetBaseTypes())
            {
                child.Columns.Add($"column_{item.GetTypeName()}", item);
            }
            return(child);
        }
Пример #28
0
 static bool Predicate(CremaTemplateColumn item)
 {
     if (CremaDataTypeUtility.IsBaseType(item.DataTypeName) == false)
     {
         return(false);
     }
     if (item.AutoIncrement == true)
     {
         return(false);
     }
     return(true);
 }
Пример #29
0
        public void NewWithNullNameAndType()
        {
            var attributeType = CremaDataTypeUtility.GetBaseTypes().Random();
            var attribute     = new CremaAttribute(null, attributeType);

            Assert.AreEqual(string.Empty, attribute.AttributeName);
            Assert.AreEqual(attributeType, attribute.DataType);
            Assert.AreEqual(true, attribute.AllowDBNull);
            Assert.AreEqual(false, attribute.AutoIncrement);
            Assert.AreEqual(string.Empty, attribute.Comment);
            Assert.AreEqual(DBNull.Value, attribute.DefaultValue);
        }
Пример #30
0
        public void SetNullDefaultValue()
        {
            this.column = this.template.Columns.RandomOrDefault(Predicate);
            if (this.column == null)
            {
                return;
            }
            var dataType     = CremaDataTypeUtility.GetType(this.column.DataTypeName);
            var defaultValue = RandomUtility.Next(dataType);

            this.column.DefaultValue = defaultValue;
            this.column.DefaultValue = null;
            Assert.AreEqual(DBNull.Value, this.column.DefaultValue);