Example #1
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);
         }
     }
 }
Example #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));
 }
        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());
        }
Example #4
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); });
        }
 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);
 }
Example #6
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];
            }
        }
Example #7
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));
        }
Example #8
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);
Example #9
0
        public static CodeTypeReference GetCodeType(this ColumnInfo columnInfo)
        {
            if (CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
            {
                return(new CodeTypeReference(CremaDataTypeUtility.GetType(columnInfo.DataType)));
            }

            var itemName = new ItemName(columnInfo.DataType);

            if (string.IsNullOrEmpty(TypeNamespace) == true)
            {
                return(new CodeTypeReference(itemName.Name));
            }

            return(new CodeTypeReference(TypeNamespace + "." + itemName.Name));
        }
Example #10
0
 public object ToValue()
 {
     if (this.Value != null)
     {
         var type = this.Type == nameof(Guid) ? typeof(Guid) : CremaDataTypeUtility.GetType(this.Type);
         return(CremaXmlConvert.ToValue(this.Value, type));
     }
     else if (this.Type == nameof(DBNull))
     {
         return(DBNull.Value);
     }
     else
     {
         return(null);
     }
 }
Example #11
0
 internal JSchema CreateSchema(string dataType)
 {
     if (CremaDataTypeUtility.IsBaseType(dataType) == true)
     {
         return(JsonSchemaUtility.GetSchema(CremaDataTypeUtility.GetType(dataType)));
     }
     else
     {
         var memberNames = this.Content.Dispatcher.Invoke(() =>
         {
             var table       = this.Content.Table;
             var typeContext = table.GetService(typeof(ITypeContext)) as ITypeContext;
             var type        = typeContext[dataType] as IType;
             return(type.TypeInfo.Members.Select(i => i.Name).ToArray());
         });
         return(JsonSchemaUtility.CreateSchema(memberNames));
     }
 }
Example #12
0
        private static object GetRandomValue(this ITableRow tableRow, string columnName)
        {
            var content    = tableRow.Content;
            var table      = content.Table;
            var columnInfo = table.TableInfo.Columns.Where(item => item.Name == columnName).First();

            if (CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
            {
                var type = CremaDataTypeUtility.GetType(columnInfo.DataType);
                return(RandomUtility.Next(type));
            }
            else
            {
                var typeContext = table.GetService(typeof(ITypeContext)) as ITypeContext;
                var type        = typeContext[columnInfo.DataType] as IType;
                return(type.GetRandomValue());
            }
        }
Example #13
0
        public static bool CreateColumn(this CremaDataTable dataTable)
        {
            var columnName = RandomUtility.NextIdentifier();

            if (dataTable.Columns.Contains(columnName) == true)
            {
                return(false);
            }

            var column = dataTable.Columns.Add(columnName);

            if (dataTable.PrimaryKey.Any() == false)
            {
                column.IsKey = true;
            }
            else if (RandomUtility.Within(10))
            {
                column.IsKey  = true;
                column.Unique = RandomUtility.Within(75);
            }

            if (RandomUtility.Within(75) == true)
            {
                column.DataType = CremaDataTypeUtility.GetType(CremaDataTypeUtility.GetBaseTypeNames().Random());
            }
            else if (dataTable.DataSet.Types.Any())
            {
                column.CremaType = dataTable.DataSet.Types.Random();
            }

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

            if (CremaDataTypeUtility.CanUseAutoIncrement(column.DataType) == true)
            {
                column.AutoIncrement = RandomUtility.NextBoolean();
            }

            return(true);
        }
Example #14
0
        public static CodeTypeReference GetPropertyType(this ColumnInfo columnInfo)
        {
            CodeTypeReference codeTypeRef = null;

            if (CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
            {
                var runtimeType = CremaDataTypeUtility.GetType(columnInfo.DataType);
                codeTypeRef = new CodeTypeReference(runtimeType);
                if (runtimeType == typeof(string))
                {
                    codeTypeRef.SetCodeType(CodeType.Reference | CodeType.Const);
                }
            }
            else
            {
                var itemName = new ItemName(columnInfo.DataType);
                codeTypeRef = Utility.GenerateCodeType(TypeNamespace, itemName.Name);
            }
            return(codeTypeRef);
        }
Example #15
0
        private static async Task <object> GetRandomValueAsync(this ITableRow tableRow, string columnName)
        {
            var content   = tableRow.Content;
            var table     = content.Table;
            var tableInfo = await table.Dispatcher.InvokeAsync(() => table.TableInfo);

            var columnInfo = tableInfo.Columns.Where(item => item.Name == columnName).First();

            if (CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true)
            {
                var type = CremaDataTypeUtility.GetType(columnInfo.DataType);
                return(RandomUtility.Next(type));
            }
            else
            {
                var typeContext = table.GetService(typeof(ITypeContext)) as ITypeContext;
                var type        = await typeContext.Dispatcher.InvokeAsync(() => typeContext[columnInfo.DataType] as IType);

                return(await type.GetRandomValueAsync());
            }
        }
        public static async Task <string> GetRandomStringAsync(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)
                {
                    try
                    {
                        var domain         = template.Domain;
                        var dataBase       = domain.GetService(typeof(IDataBase)) as IDataBase;
                        var typeCollection = dataBase.GetService(typeof(ITypeCollection)) as ITypeCollection;
                        var typeName       = new ItemName(dataType);
                        var type           = await typeCollection.GetTypeAsync(typeName.Name, typeName.CategoryPath);

                        return(await type.GetRandomStringAsync());
                    }
                    catch (System.NullReferenceException e)
                    {
                        System.Diagnostics.Debugger.Launch();
                        throw e;
                    }
                }
                else
                {
                    var value = RandomUtility.Next(CremaDataTypeUtility.GetType(dataType));
                    return(CremaConvert.ChangeType(value, typeof(string)) as string);
                }
            }
        }
Example #17
0
        internal static object[] ReadFields(XmlReader reader, string propertyName)
        {
            reader.MoveToContent();
            if (reader.IsEmptyElement == false)
            {
                var fieldList = new List <object>();
                reader.ReadStartElement(propertyName);

                while (reader.MoveToContent() == XmlNodeType.Element)
                {
                    var attr = reader.GetAttribute("type");
                    if (reader.IsEmptyElement == false)
                    {
                        var text  = reader.ReadElementContentAsString();
                        var type  = attr == nameof(Guid) ? typeof(Guid) : CremaDataTypeUtility.GetType(attr);
                        var field = CremaXmlConvert.ToValue(text, type);
                        fieldList.Add(field);
                    }
                    else if (attr == nameof(DBNull))
                    {
                        fieldList.Add(DBNull.Value);
                        reader.Skip();
                    }
                    else
                    {
                        fieldList.Add(null);
                        reader.Skip();
                    }
                }
                reader.ReadEndElement();
                return(fieldList.ToArray());
            }
            else
            {
                reader.Skip();
                return(null);
            }
        }
Example #18
0
        public void SetNullDefaultValue()
        {
            this.column = this.template.Columns.Random(Predicate);
            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);

            bool Predicate(CremaTemplateColumn item)
            {
                if (CremaDataTypeUtility.IsBaseType(item.DataTypeName) == false)
                {
                    return(false);
                }
                if (item.AutoIncrement == true)
                {
                    return(false);
                }
                return(true);
            }
        }
        public object ToValue()
        {
            var type = CremaDataTypeUtility.GetType(this.Type);

            return(CremaConvert.ChangeType(this.Value, type));
        }
Example #20
0
        protected override async Task OnExecuteAsync(CancellationToken cancellationToken)
        {
            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 fields         = new JsonPropertiesInfo();
            var authentication = this.CommandContext.GetAuthentication(this);
            var tableRow       = await this.Content.FindAsync(authentication, fieldList.ToArray());

            this.Content.Dispatcher.Invoke(() =>
            {
                foreach (var item in tableInfo.Columns)
                {
                    var field = tableRow[item.Name];
                    if (field != null || item.AllowNull == false)
                    {
                        fields.Add(item.Name, field);
                    }
                }
            });

            var result = fields.Execute(schema);

            if (result == null)
            {
                return;
            }

            foreach (var item in fields)
            {
                if (result.ContainsKey(item.Key) == false)
                {
                    await tableRow.SetFieldAsync(authentication, item.Key, DBNull.Value);
                }
            }

            foreach (var item in result)
            {
                if (tableInfo.Columns.Any(i => i.Name == item.Key) == false)
                {
                    continue;
                }
                var value1 = tableRow[item.Key];
                var value2 = item.Value;
                if (object.Equals(value1, value2) == true)
                {
                    continue;
                }

                await tableRow.SetFieldAsync(authentication, item.Key, item.Value);
            }
        }