Beispiel #1
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);
        }
Beispiel #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));
 }
Beispiel #3
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);
         }
     }
 }
Beispiel #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); });
        }
Beispiel #5
0
 public static string GenerateSchemaTypeName(string typeName)
 {
     if (CremaDataTypeUtility.IsBaseType(typeName) == true)
     {
         return(string.Format("{0}:{1}", "xs", typeName));
     }
     return(typeName);
 }
 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);
 }
Beispiel #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));
        }
Beispiel #8
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;
                }
            }
        }
Beispiel #9
0
 static bool Predicate(CremaTemplateColumn item)
 {
     if (CremaDataTypeUtility.IsBaseType(item.DataTypeName) == false)
     {
         return(false);
     }
     if (item.AutoIncrement == true)
     {
         return(false);
     }
     return(true);
 }
Beispiel #10
0
        public static void Empty(object dataItem, bool isBeingEdited)
        {
            if (dataItem is IEditableObject editable)
            {
                try
                {
                    if (isBeingEdited == false)
                    {
                        editable.BeginEdit();
                    }

                    if (dataItem is ICustomTypeDescriptor descriptor)
                    {
                        var props = descriptor.GetProperties();
                        for (var i = 0; i < props.Count; i++)
                        {
                            var prop = props[i];
                            if (prop.IsBrowsable == false || prop.Name == CremaSchema.Index)
                            {
                                continue;
                            }
                            if (prop.Name == CremaSchema.__RelationID__ || prop.Name == CremaSchema.__ParentID__)
                            {
                                continue;
                            }
                            if (CremaDataTypeUtility.IsBaseType(prop.PropertyType) == false)
                            {
                                continue;
                            }
                            prop.ResetValue(dataItem);
                        }

                        SetItemEnabled(dataItem, false);
                    }

                    if (isBeingEdited == false)
                    {
                        editable.EndEdit();
                    }
                }
                catch
                {
                    if (isBeingEdited == false)
                    {
                        editable.CancelEdit();
                    }
                    throw;
                }
            }
        }
Beispiel #11
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));
        }
Beispiel #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());
            }
        }
Beispiel #13
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));
     }
 }
Beispiel #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);
        }
Beispiel #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());
            }
        }
Beispiel #16
0
        public static bool HasError(object item)
        {
            if (item is IDataErrorInfo errorInfo)
            {
                if (errorInfo.Error != string.Empty)
                {
                    return(true);
                }

                if (item is ICustomTypeDescriptor descriptor)
                {
                    foreach (PropertyDescriptor prop in descriptor.GetProperties())
                    {
                        if (CremaDataTypeUtility.IsBaseType(prop.PropertyType) == true && errorInfo[prop.Name] != string.Empty)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        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);
                }
            }
        }
Beispiel #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);
            }
        }
Beispiel #19
0
        public void SetDataType_Fail()
        {
            var newType = typeof(string).Assembly.GetTypes().Random(item => CremaDataTypeUtility.IsBaseType(item) == false);

            this.attribute.DataType = newType;
        }
Beispiel #20
0
        private void ReadTables(CremaDataSet dataSet, IDataBase dataBase, string filename, string itemNames)
        {
            var sheetNames = SpreadsheetReader.ReadSheetNames(filename);
            var tableInfos = dataBase.Dispatcher.Invoke(() =>
            {
                var query = from table in dataBase.TableContext.Tables
                            let tableName2 = SpreadsheetUtility.Ellipsis(table.Name)
                                             join sheetName in sheetNames on tableName2 equals sheetName
                                             where table.Name.GlobMany(itemNames) || table.Path.GlobMany(itemNames)
                                             orderby table.Name
                                             select table.TableInfo;

                return(query.ToArray());
            });

            var typeInfos = dataBase.Dispatcher.Invoke(() =>
            {
                var query = from table in dataBase.TableContext.Tables
                            let tableName2 = SpreadsheetUtility.Ellipsis(table.Name)
                                             join sheetName in sheetNames on tableName2 equals sheetName
                                             where table.Name.GlobMany(itemNames) || table.Path.GlobMany(itemNames)
                                             from column in table.TableInfo.Columns
                                             where CremaDataTypeUtility.IsBaseType(column.DataType) == false
                                             let type = dataBase.TypeContext[column.DataType] as IType
                                                        where type != null
                                                        select type.TypeInfo;

                return(query.Distinct().ToArray());
            });

            foreach (var item in typeInfos)
            {
                dataSet.Types.Add(item);
            }

            foreach (var item in tableInfos)
            {
                if (item.TemplatedParent != string.Empty)
                {
                    continue;
                }
                if (item.ParentName == string.Empty)
                {
                    dataSet.Tables.Add(item);
                }
                else
                {
                    dataSet.Tables[item.ParentName].Childs.Add(item);
                }
            }

            foreach (var item in tableInfos)
            {
                if (item.TemplatedParent != string.Empty && item.ParentName == string.Empty)
                {
                    var dataTable = dataSet.Tables[item.TemplatedParent];
                    dataTable.Inherit(item.TableName);
                }
            }

            var progress = new ConsoleProgress(this.Out)
            {
                Style = ConsoleProgressStyle.None
            };

            using (var reader = new SpreadsheetReader(filename))
            {
                reader.Read(dataSet, progress);
            }
        }
Beispiel #21
0
 public static bool IsCustomType(this ColumnInfo columnInfo)
 {
     return(CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == false);
 }
Beispiel #22
0
        public void NewWithNullNameAndInvalidType_Fail()
        {
            var attributeType = typeof(string).Assembly.GetTypes().Random(item => CremaDataTypeUtility.IsBaseType(item) == false);

            new CremaAttribute(null, attributeType);
        }
Beispiel #23
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);
            }
        }