Ejemplo n.º 1
0
        private static void AddChildType(SchemaColumn column)
        {
            Type type = column.ColumnType;

            if (type.IsSubclassOf(typeof(IItemChangeEvent)))
            {
                var args = type.GetGenericArguments();

                if (args.Length > 0)
                {
                    column.GenericArgs = args.Length;
                    column.Children    = new List <SchemaColumn>();
                    int number = 0;
                    foreach (var genericArg in args)
                    {
                        number++;
                        var item = new SchemaColumn();
                        item.Id         = number;
                        item.ColumnType = genericArg;

                        AddChildType(item);
                        column.Children.Add(item);
                    }
                    if (column.IsDictionary)
                    {
                        column.Children[0].Name  = "_key";
                        column.Children[0].IsKey = true;
                        column.Children[1].Name  = "_value";
                    }
                    else if (column.IsList)
                    {
                        column.Children[0].Name = "_list";
                    }
                }
                else
                {
                    var propertyList = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                       .Where(p => p.GetCustomAttributes(typeof(ProtoMemberAttribute), false).Length > 0).ToList();
                    if (propertyList.Count > 0)
                    {
                        column.Children = new List <SchemaColumn>();
                    }
                    int number = 0;
                    foreach (var property in propertyList)
                    {
                        number++;
                        var child = new SchemaColumn();
                        child.Id         = number;
                        child.Name       = property.Name;
                        child.ColumnType = property.PropertyType;
                        child.CanRead    = property.CanRead;
                        child.CanWrite   = property.CanWrite;

                        AddChildType(child);
                        column.Children.Add(child);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void WriteChildSchema(StringBuilder sb, SchemaColumn parent, int depth, bool isParentEnd)
        {
            string parentChar = "".PadLeft((depth - 1) * 4, ' ');
            string preChar    = "".PadLeft(depth * 4, ' ');

            sb.AppendFormat("{0}[\"{1}\"] = ", parentChar, parent.Name);
            sb.AppendLine("{");
            sb.AppendFormat("{0}{1},", preChar, parent.Id);
            sb.AppendLine();
            sb.AppendFormat("{0}{1},", preChar, "[\"_hasChild\"] = true");
            sb.AppendLine();

            if (parent.IsDictionary)
            {
                var key = parent.Children[0];
                var col = parent.Children[1];
                WriteColumnSchema(sb, key, depth, false);
                if (IsSupportType(col.ColumnType))
                {
                    WriteColumnSchema(sb, col, depth, true);
                }
                else
                {
                    WriteChildSchema(sb, col, depth + 1, true);
                }
            }
            else if (parent.IsList)
            {
                var col = parent.Children[0];
                if (IsSupportType(col.ColumnType))
                {
                    WriteColumnSchema(sb, col, depth, true);
                }
                else
                {
                    WriteChildSchema(sb, col, depth + 1, true);
                }
            }
            else
            {
                int index    = 0;
                int endIndex = parent.Children.Count - 1;
                foreach (var col in parent.Children)
                {
                    bool isEnd = index == endIndex;
                    if (col.HasChild)
                    {
                        WriteChildSchema(sb, col, depth + 1, isEnd);
                    }
                    else
                    {
                        WriteColumnSchema(sb, col, depth, isEnd);
                    }
                    index++;
                }
            }
            sb.AppendFormat("{0}", parentChar);
            sb.AppendLine(!isParentEnd ? "}," : "}");
        }
Ejemplo n.º 3
0
        private static void WriteColumnSchema(StringBuilder sb, SchemaColumn col, int depth, bool isEnd)
        {
            sb.AppendFormat("{0}[\"{1}\"] = ", "".PadLeft(depth * 4, ' '), col.Name);
            sb.Append("{");
            string colType = GetColumnType(col.ColumnType);

            sb.AppendFormat(col.IsKey ? "{0}, \"{1}\", true" : "{0}, \"{1}\"", col.Id, colType);
            sb.AppendLine(!isEnd ? "}," : "}");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 初始化架构信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="schema"></param>
        public static void InitSchema(Type type, SchemaTable schema)
        {
            var section = GetEntitySection();

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (type.IsSubclassOf(typeof(LogEntity)) && string.IsNullOrEmpty(schema.NameFormat))
            {
                schema.NameFormat = section.LogTableNameFormat;
            }
            if (!string.IsNullOrEmpty(schema.NameFormat) && !Exits(type))
            {
                _dynamicTables.Enqueue(schema);
            }

            //加载成员属性
            HashSet <string> keySet = new HashSet <string>();

            PropertyInfo[] propertyList = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            SchemaColumn   column;
            int            number = 0;

            foreach (PropertyInfo property in propertyList)
            {
                number++;
                //Ignore parent property
                bool isExtend = property.GetCustomAttributes <EntityFieldExtendAttribute>(false).Count() > 0;
                if (!isExtend && property.DeclaringType != property.ReflectedType)
                {
                    continue;
                }
                column = new SchemaColumn();
                var entityField = FindAttribute <EntityFieldAttribute>(property.GetCustomAttributes(false));
                if (entityField != null)
                {
                    column.Id                 = number;
                    column.Name               = string.IsNullOrEmpty(entityField.FieldName) ? property.Name : entityField.FieldName;
                    column.IsIdentity         = entityField.IsIdentity;
                    column.IdentityNo         = entityField.IdentityNo;
                    column.IsKey              = entityField.IsKey;
                    column.IsUnique           = entityField.IsUnique;
                    column.ColumnLength       = entityField.ColumnLength;
                    column.ColumnScale        = entityField.ColumnScale;
                    column.Isnullable         = entityField.Isnullable;
                    column.FieldModel         = entityField.FieldModel;
                    column.Disable            = entityField.Disable;
                    column.MinRange           = entityField.MinRange;
                    column.MaxRange           = entityField.MaxRange;
                    column.IsSerialized       = entityField.IsJsonSerialize;
                    column.JsonDateTimeFormat = entityField.JsonDateTimeFormat;
                    column.ColumnType         = property.PropertyType;
                    column.DbType             = entityField.DbType;
                    column.CanRead            = property.CanRead;
                    column.CanWrite           = property.CanWrite;

                    AddChildType(column);
                }
                else
                {
                    //必须要加EntityField标识
                    continue;
                    //column.Id = number;
                    //column.Name = property.Name;
                    //column.IsIdentity = false;
                    //column.IsKey = string.Compare(property.Name, "id", true) == 0;
                    //column.FieldModel = FieldModel.All;
                    //column.ColumnType = property.PropertyType;
                    //column.DbType = ColumnDbType.Varchar;
                }
                schema.Columns.TryAdd(column.Name, column);
                if (column.IsSerialized)
                {
                    schema.HasObjectColumns = true;
                }
                if (column.IsKey)
                {
                    keySet.Add(column.Name);
                }
            }
            //add modify time field
            if (section.EnableModifyTimeField && schema.AccessLevel == AccessLevel.ReadWrite)
            {
                column                    = new SchemaColumn();
                column.Id                 = number;
                column.Name               = "TempTimeModify";
                column.Isnullable         = true;
                column.FieldModel         = FieldModel.All;
                column.ColumnType         = typeof(DateTime);
                column.DbType             = ColumnDbType.DateTime;
                column.JsonDateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
                schema.Columns.TryAdd(column.Name, column);
            }

            schema.Keys = new string[keySet.Count];
            keySet.CopyTo(schema.Keys, 0);

            CheckTableSchema(schema);
            AddSchema(type, schema);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 初始化架构信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="schema"></param>
        public static void InitSchema(Type type, SchemaTable schema)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            //modify if exits then update.
            //if (Exits(type))
            //{
            //    return;
            //}

            if (type.IsSubclassOf(typeof(LogEntity)))
            {
                schema.IsLog = true;
                _logTables.Enqueue(type.FullName);
            }

            //加载成员属性
            HashSet <string> keySet = new HashSet <string>();

            PropertyInfo[] propertyList = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            SchemaColumn   column;
            int            number = 0;

            foreach (PropertyInfo property in propertyList)
            {
                number++;
                if (!Equals(property.DeclaringType, property.ReflectedType))
                {
                    continue;
                }
                column = new SchemaColumn();
                var entityField = FindAttribute <EntityFieldAttribute>(property.GetCustomAttributes(false));
                if (entityField != null)
                {
                    column.Id                 = number;
                    column.Name               = string.IsNullOrEmpty(entityField.FieldName) ? property.Name : entityField.FieldName;
                    column.IsIdentity         = entityField.IsIdentity;
                    column.IsKey              = entityField.IsKey;
                    column.ColumnLength       = entityField.ColumnLength;
                    column.ColumnScale        = entityField.ColumnScale;
                    column.Isnullable         = entityField.Isnullable;
                    column.FieldModel         = entityField.FieldModel;
                    column.Disable            = entityField.Disable;
                    column.MinRange           = entityField.MinRange;
                    column.MaxRange           = entityField.MaxRange;
                    column.IsJson             = entityField.IsJsonSerialize;
                    column.JsonDateTimeFormat = entityField.JsonDateTimeFormat;
                    column.ColumnType         = property.PropertyType;
                    column.DbType             = entityField.DbType;
                    column.CanRead            = property.CanRead;
                    column.CanWrite           = property.CanWrite;

                    AddChildType(column);
                }
                else
                {
                    //必须要加EntityField标识
                    continue;
                    //column.Id = number;
                    //column.Name = property.Name;
                    //column.IsIdentity = false;
                    //column.IsKey = string.Compare(property.Name, "id", true) == 0;
                    //column.FieldModel = FieldModel.All;
                    //column.ColumnType = property.PropertyType;
                    //column.DbType = ColumnDbType.Varchar;
                }
                schema.Columns.TryAdd(column.Name, column);
                if (column.IsKey)
                {
                    keySet.Add(column.Name);
                }
            }
            schema.Keys = new string[keySet.Count];
            keySet.CopyTo(schema.Keys, 0);

            CheckTableSchema(schema);
            AddSchema(type, schema);
        }
Ejemplo n.º 6
0
        private static void PushChildStack(MessageStructure parent, SchemaColumn parentColumn, object value)
        {
            if (parentColumn.IsDictionary)
            {
                var column = parentColumn.Children[1];
                dynamic dict = value;
                dynamic keys = dict.Keys;
                int count = dict.Count;
                parent.PushIntoStack(count);
                foreach (var key in keys)
                {
                    object item = dict[key];
                    var itemWriter = new MessageStructure();
                    itemWriter.PushIntoStack(key);
                    if (EntitySchemaSet.IsSupportType(column.ColumnType))
                    {
                        itemWriter.PushIntoStack(column.ColumnType, item);
                    }
                    else if (column.HasChild)
                    {
                        PushChildStack(itemWriter, column, item);
                    }
                    parent.PushIntoStack(itemWriter);
                }
            }
            else if (parentColumn.IsList)
            {
                var column = parentColumn.Children[0];
                dynamic list = value;
                int count = list.Count;
                parent.PushIntoStack(count);
                foreach (var item in list)
                {
                    var itemWriter = new MessageStructure();
                    if (EntitySchemaSet.IsSupportType(column.ColumnType))
                    {
                        itemWriter.PushIntoStack(column.ColumnType, item);
                    }
                    else if (column.HasChild)
                    {
                        PushChildStack(itemWriter, column, item);
                    }
                    parent.PushIntoStack(itemWriter);
                }
            }
            else
            {
                //child entity object
                parent.PushIntoStack(1);
                var typeAccessor = ObjectAccessor.Create(value, true);
                var itemWriter = new MessageStructure();
                foreach (var column in parentColumn.Children)
                {
                    try
                    {
                        var fieldValue = typeAccessor[column.Name];
                        if (EntitySchemaSet.IsSupportType(column.ColumnType))
                        {
                            itemWriter.PushIntoStack(column.ColumnType, fieldValue);
                        }
                        else if (column.HasChild)
                        {
                            PushChildStack(itemWriter, column, fieldValue);
                        }
                    }
                    catch
                    {
                    }
                }
                parent.PushIntoStack(itemWriter);

            }
        }