コード例 #1
0
ファイル: ModelLoader.cs プロジェクト: softwaiter/netcoreORM
        private static void ProcessDelayCheckProperties()
        {
            IEnumerator <KeyValuePair <Property, DelayCheckPropertySetting> > e = sDelayCheckProperties.GetEnumerator();

            while (e.MoveNext())
            {
                Property p = e.Current.Key;
                DelayCheckPropertySetting dcps = e.Current.Value;
                if (p.Type == typeof(Model))
                {
                    Model m = ModelUtils.GetModel(p.TypeValue);
                    if (m == null)
                    {
                        throw new Exception(string.Concat("Model定义“", p.TypeValue, "”不存在。", dcps.Position));
                    }

                    Property joinP = null;
                    if (!string.IsNullOrWhiteSpace(p.JoinProp))
                    {
                        try
                        {
                            joinP = m.GetProperty(p.JoinProp);
                        }
                        catch
                        {
                            throw new Exception(string.Concat("关联模型“", p.TypeValue, "”中不存在属性“", p.JoinProp, "”的定义。", dcps.Position));
                        }
                    }

                    if (joinP == null)
                    {
                        joinP = m.GetPrimaryKey(0);
                    }

                    //TODO 多级关联属性这里会有问题,如果是多级关联,此时joinP的类型可能还是Model
                    p.RealType = joinP.Type;

                    if (dcps.FieldTypeNotSet)
                    {
                        p.FieldType = joinP.FieldType;
                    }
                    if (dcps.LengthNotSet)
                    {
                        p.Length = joinP.Length;
                    }
                }
            }
        }
コード例 #2
0
ファイル: ModelLoader.cs プロジェクト: softwaiter/netcoreORM
        internal static Model ParseModel(string modelFilePath, string parent)
        {
            Model model = new Model();

            model.Path = parent.ToLower();

            Xmtool.Xml().Iterate(modelFilePath, (nodeInfo) =>
            {
                if (nodeInfo.Path == "/model")
                {
                    if (!nodeInfo.IsEndNode)
                    {
                        string name = nodeInfo.GetAttribute("name");
                        if (name == null)
                        {
                            throw new Exception("缺少name属性。 " + modelFilePath + " - Line " + nodeInfo.Line);
                        }
                        else if (string.IsNullOrWhiteSpace(name))
                        {
                            throw new Exception("name属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                        }
                        model.Name = name.Trim();

                        string table = nodeInfo.GetAttribute("table");
                        if (table != null)
                        {
                            if (string.IsNullOrWhiteSpace(table))
                            {
                                throw new Exception("table属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                            model.Table = table.Trim();
                        }
                        else
                        {
                            model.Table = model.Name;
                        }

                        string beforeSaveProcStr = nodeInfo.GetAttribute("beforeSave");
                        if (beforeSaveProcStr != null)
                        {
                            beforeSaveProcStr = beforeSaveProcStr.Trim();
                            if (beforeSaveProcStr.Length > 4 &&
                                beforeSaveProcStr.StartsWith("{{") &&
                                beforeSaveProcStr.EndsWith("}}"))
                            {
                                model.BeforeSaveProcessor = beforeSaveProcStr.Substring(2, beforeSaveProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("beforeSave属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string afterSaveProcStr = nodeInfo.GetAttribute("afterSave");
                        if (afterSaveProcStr != null)
                        {
                            afterSaveProcStr = afterSaveProcStr.Trim();
                            if (afterSaveProcStr.Length > 4 &&
                                afterSaveProcStr.StartsWith("{{") &&
                                afterSaveProcStr.EndsWith("}}"))
                            {
                                model.AfterSaveProcessor = afterSaveProcStr.Substring(2, afterSaveProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("afterSave属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string beforeDeleteProcStr = nodeInfo.GetAttribute("beforeDelete");
                        if (beforeDeleteProcStr != null)
                        {
                            beforeDeleteProcStr = beforeDeleteProcStr.Trim();
                            if (beforeDeleteProcStr.Length > 4 &&
                                beforeDeleteProcStr.StartsWith("{{") &&
                                beforeDeleteProcStr.EndsWith("}}"))
                            {
                                model.BeforeDeleteProcessor = beforeDeleteProcStr.Substring(2, beforeDeleteProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("beforeDelete属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string afterDeleteProcStr = nodeInfo.GetAttribute("afterDelete");
                        if (afterDeleteProcStr != null)
                        {
                            afterDeleteProcStr = afterDeleteProcStr.Trim();
                            if (afterDeleteProcStr.Length > 4 &&
                                afterDeleteProcStr.StartsWith("{{") &&
                                afterDeleteProcStr.EndsWith("}}"))
                            {
                                model.AfterDeleteProcessor = afterDeleteProcStr.Substring(2, afterDeleteProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("afterDelete属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }
                    }
                }
                else if (nodeInfo.Path == "/model/property")
                {
                    if (!nodeInfo.IsEndNode)
                    {
                        Property p = new Property();
                        DelayCheckPropertySetting dcps = null;

                        string name = nodeInfo.GetAttribute("name");
                        if (name == null)
                        {
                            throw new Exception("缺少name属性。 " + modelFilePath + " - Line " + nodeInfo.Line);
                        }
                        else if (string.IsNullOrWhiteSpace(name))
                        {
                            throw new Exception("name属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                        }
                        p.Name = name.Trim();

                        string field = nodeInfo.GetAttribute("field");
                        if (field != null)
                        {
                            if (string.IsNullOrWhiteSpace(field))
                            {
                                throw new Exception("field属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                            p.Field = field.Trim();
                        }
                        else
                        {
                            p.Field = string.Concat("f_", p.Name.ToLower());
                        }

                        Type type      = typeof(string);
                        string typeStr = nodeInfo.GetAttribute("type");
                        if (typeStr != null)
                        {
                            type = GetPropertyType(typeStr);
                            if (type == null)
                            {
                                throw new Exception("type属性非法。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                            else if (type == typeof(Model) ||
                                     type == typeof(DynamicObjectExt))
                            {
                                p.TypeValue = typeStr;

                                dcps          = new DelayCheckPropertySetting();
                                dcps.Position = modelFilePath + " - Line " + nodeInfo.Line;
                            }
                        }
                        else
                        {
                            string aiStr = nodeInfo.GetAttribute("autoIncrement");
                            if (aiStr != null)
                            {
                                if (!string.IsNullOrWhiteSpace(aiStr) &&
                                    reBool.IsMatch(aiStr))
                                {
                                    type = typeof(Int32);
                                }
                            }
                        }
                        p.Type     = type;
                        p.RealType = type;

                        string joinPropStr = nodeInfo.GetAttribute("joinProp");
                        if (joinPropStr != null)
                        {
                            if (p.Type == typeof(Model))
                            {
                                if (string.IsNullOrWhiteSpace(joinPropStr))
                                {
                                    throw new Exception("joinProp属性不能为空。" + modelFilePath + " - Line " + nodeInfo.Line);
                                }
                                p.JoinProp = joinPropStr.Trim();
                            }
                            else
                            {
                                throw new Exception("joinProp属性只在type属性为Model类型时有效。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string fieldTypeStr = nodeInfo.GetAttribute("fieldType");
                        if (fieldTypeStr != null)
                        {
                            if (type == typeof(DynamicObjectExt))
                            {
                                throw new Exception(typeStr + "类型的Property禁止设置fieldType。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            try
                            {
                                p.FieldType = Enum.Parse <DbType>(fieldTypeStr, true);
                                p.RealType  = DbType2Type(p.FieldType);
                            }
                            catch
                            {
                                throw new Exception("fieldType属性非法。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }
                        else
                        {
                            p.FieldType = Type2DbType(type);
                            if (p.Type == typeof(Model))
                            {
                                dcps.FieldTypeNotSet = true;
                            }
                        }

                        string lengthStr = nodeInfo.GetAttribute("length");
                        if (lengthStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(lengthStr))
                            {
                                throw new Exception("length属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            if (!Xmtool.Regex().IsPositiveInteger(lengthStr))
                            {
                                throw new Exception("length属性必须是有效正整数。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            p.Length = int.Parse(lengthStr);
                        }
                        else
                        {
                            p.Length = FieldUtils.GetFieldLength(model, p.FieldType);
                            if (p.Type == typeof(DynamicObjectExt))
                            {
                                p.Length = 512;
                            }
                            else if (p.Type == typeof(Model))
                            {
                                dcps.LengthNotSet = true;
                            }
                        }

                        string minStr = nodeInfo.GetAttribute("min");
                        if (minStr != null)
                        {
                            if (FieldUtils.IsNumeric(p.FieldType))
                            {
                                if (string.IsNullOrWhiteSpace(minStr))
                                {
                                    throw new Exception("min属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                if (!Xmtool.Regex().IsNumber(minStr))
                                {
                                    throw new Exception("min属性必须是有效数值。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                p.MinValue = double.Parse(minStr);
                            }
                            else
                            {
                                throw new NotSupportedException(p.FieldType.ToString() + "类型不支持min设置。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string maxStr = nodeInfo.GetAttribute("max");
                        if (maxStr != null)
                        {
                            if (FieldUtils.IsNumeric(p.FieldType))
                            {
                                if (string.IsNullOrWhiteSpace(maxStr))
                                {
                                    throw new Exception("max属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                if (!Xmtool.Regex().IsNumber(maxStr))
                                {
                                    throw new Exception("max属性必须是有效数值。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                p.MaxValue = double.Parse(maxStr);
                            }
                            else
                            {
                                throw new NotSupportedException(p.FieldType.ToString() + "类型不支持max设置。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string precisionStr = nodeInfo.GetAttribute("precision");
                        if (precisionStr != null)
                        {
                            if (FieldUtils.IsFloat(p.FieldType))
                            {
                                if (string.IsNullOrWhiteSpace(precisionStr))
                                {
                                    throw new Exception("precision属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                if (!Xmtool.Regex().IsNaturalInteger(precisionStr))
                                {
                                    throw new Exception("precision属性必须是有效自然数。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                p.Precision = int.Parse(precisionStr);
                            }
                            else
                            {
                                throw new NotSupportedException(p.FieldType.ToString() + "类型不支持precision设置。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string notNullStr = nodeInfo.GetAttribute("notNull");
                        if (notNullStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(notNullStr))
                            {
                                throw new Exception("notNull属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            if (!reBool.IsMatch(notNullStr))
                            {
                                throw new Exception("notNull属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            p.IsNotNull = bool.Parse(notNullStr);
                        }

                        string descStr = nodeInfo.GetAttribute("desc");
                        if (!string.IsNullOrEmpty(descStr))
                        {
                            p.Description = descStr;
                        }

                        string autoIncrStr = nodeInfo.GetAttribute("autoIncrement");
                        if (autoIncrStr != null)
                        {
                            if (FieldUtils.IsInteger(p.FieldType))
                            {
                                if (string.IsNullOrWhiteSpace(autoIncrStr))
                                {
                                    throw new Exception("autoIncrement属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                if (!reBool.IsMatch(autoIncrStr))
                                {
                                    throw new Exception("autoIncrement属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                p.AutoIncrement = bool.Parse(autoIncrStr);
                            }
                            else
                            {
                                throw new NotSupportedException(p.FieldType.ToString() + "类型不支持autoIncrement设置。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string unsignedStr = nodeInfo.GetAttribute("unsigned");
                        if (unsignedStr != null)
                        {
                            if (FieldUtils.IsNumeric(p.FieldType))
                            {
                                if (string.IsNullOrWhiteSpace(unsignedStr))
                                {
                                    throw new Exception("unsigned属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                if (!reBool.IsMatch(unsignedStr))
                                {
                                    throw new Exception("unsigned属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                                }

                                p.Unsigned = bool.Parse(unsignedStr);
                            }
                            else
                            {
                                throw new NotSupportedException(p.FieldType.ToString() + "类型不支持unsigned设置。" + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string uniqueGroupStr = nodeInfo.GetAttribute("uniqueGroup");
                        if (uniqueGroupStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(uniqueGroupStr))
                            {
                                throw new Exception("uniqueGroup属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                            p.UniqueGroup = uniqueGroupStr.Trim();
                        }

                        string indexGroupStr = nodeInfo.GetAttribute("indexGroup");
                        if (indexGroupStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(indexGroupStr))
                            {
                                throw new Exception("indexGroup属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                            p.IndexGroup = indexGroupStr.Trim();
                        }

                        string primaryStr = nodeInfo.GetAttribute("primary");
                        if (primaryStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(primaryStr))
                            {
                                throw new Exception("primary属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            if (!reBool.IsMatch(primaryStr))
                            {
                                throw new Exception("primary属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            p.IsPrimaryKey = bool.Parse(primaryStr);
                        }

                        string defaultStr = nodeInfo.GetAttribute("defaultValue");
                        if (defaultStr != null)
                        {
                            p.DefaultValue = defaultStr.Trim();
                        }

                        string beforeProcStr = nodeInfo.GetAttribute("beforeSave");
                        if (beforeProcStr != null)
                        {
                            beforeProcStr = beforeProcStr.Trim();
                            if (beforeProcStr.Length > 4 &&
                                beforeProcStr.StartsWith("{{") &&
                                beforeProcStr.EndsWith("}}"))
                            {
                                p.BeforeSaveProcessor = beforeProcStr.Substring(2, beforeProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("beforeSave属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string afterProcStr = nodeInfo.GetAttribute("afterQuery");
                        if (afterProcStr != null)
                        {
                            afterProcStr = afterProcStr.Trim();
                            if (afterProcStr.Length > 4 &&
                                afterProcStr.StartsWith("{{") &&
                                afterProcStr.EndsWith("}}"))
                            {
                                p.AfterQueryProcessor = afterProcStr.Substring(2, afterProcStr.Length - 4);
                            }
                            else
                            {
                                throw new Exception("afterQuery属性必须是Processor。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }
                        }

                        string joinInsertStr = nodeInfo.GetAttribute("joinInsert");
                        if (joinInsertStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(joinInsertStr))
                            {
                                throw new Exception("joinInsert属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            if (!reBool.IsMatch(joinInsertStr))
                            {
                                throw new Exception("joinInsert属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            p.JoinInsert = bool.Parse(joinInsertStr);
                        }

                        string joinUpdateStr = nodeInfo.GetAttribute("joinUpdate");
                        if (joinUpdateStr != null)
                        {
                            if (string.IsNullOrWhiteSpace(joinUpdateStr))
                            {
                                throw new Exception("joinUpdate属性不能为空。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            if (!reBool.IsMatch(joinUpdateStr))
                            {
                                throw new Exception("joinUpdate属性必须是布尔型。 " + modelFilePath + " - Line " + nodeInfo.Line);
                            }

                            p.JoinUpdate = bool.Parse(joinUpdateStr);
                        }

                        model.AddProperty(p);

                        if (dcps != null)
                        {
                            sDelayCheckProperties.TryAdd(p, dcps);
                        }
                    }
                }

                return(true);
            });
            return(model);
        }