コード例 #1
0
        private static void AddTableColumns(CodeGenConfig config, Table table)
        {
            if (CodeGenConfig.FindConfigList(config, "Column") != null)
            {
                return;
            }

            var eci     = string.IsNullOrEmpty(config.GetAttributeValue <string>("IncludeColumns")) ? new List <string>() : config.GetAttributeValue <string>("IncludeColumns").Split(',').Select(x => x.Trim()).ToList();
            var ecx     = string.IsNullOrEmpty(config.GetAttributeValue <string>("ExcludeColumns")) ? new List <string>() : config.GetAttributeValue <string>("ExcludeColumns").Split(',').Select(x => x.Trim()).ToList();
            var colList = new List <CodeGenConfig>();

            foreach (var col in table.Columns)
            {
                if (string.IsNullOrEmpty(col.Name))
                {
                    continue;
                }

                if (eci.Count > 0 && !eci.Contains(col.Name))
                {
                    continue;
                }

                if (ecx.Contains(col.Name))
                {
                    continue;
                }

                var c = new CodeGenConfig("Column", config);
                c.AttributeAdd("Name", col.Name);
                c.AttributeAdd("Type", col.Type);
                c.AttributeAdd("IsNullable", XmlConvert.ToString(col.IsNullable));
                c.AttributeAdd("IsIdentity", XmlConvert.ToString(col.IsIdentity));
                c.AttributeAdd("IsPrimaryKey", XmlConvert.ToString(col.IsPrimaryKey));
                c.AttributeAdd("IsComputed", XmlConvert.ToString(col.IsComputed));
                if (col.Length.HasValue)
                {
                    c.AttributeAdd("Length", XmlConvert.ToString(col.Length.Value));
                }

                if (col.Precision.HasValue)
                {
                    c.AttributeAdd("Precision", XmlConvert.ToString(col.Precision.Value));
                }

                if (col.Scale.HasValue)
                {
                    c.AttributeAdd("Scale", XmlConvert.ToString(col.Scale.Value));
                }

                c.AttributeAdd("DotNetType", col.DotNetType);

                colList.Add(c);
            }

            if (colList.Count > 0)
            {
                config.Children.Add("Column", colList);
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads the <see cref="CodeGenConfig"/> before the corresponding <see cref="CodeGenConfig.Children"/>.
        /// </summary>
        /// <param name="config">The <see cref="CodeGenConfig"/> being loaded.</param>
        public void LoadBeforeChildren(CodeGenConfig config)
        {
            CodeGenConfig entity = CodeGenConfig.FindConfig(config, "Entity");

            if (config.GetAttributeValue <bool>("UniqueKey"))
            {
                List <CodeGenConfig> paramList = new List <CodeGenConfig>();
                var layerPassing = new string[] { "Create", "Update" }.Contains(config.GetAttributeValue <string>("OperationType")) ? "ToManagerSet" : "All";
                var isMandatory = new string[] { "Create", "Update" }.Contains(config.GetAttributeValue <string>("OperationType")) ? "false" : "true";

                var propConfigs = CodeGenConfig.FindConfigList(config, "Property") ?? new List <CodeGenConfig>();
                foreach (CodeGenConfig propConfig in propConfigs)
                {
                    if (!propConfig.GetAttributeValue <bool>("UniqueKey"))
                    {
                        continue;
                    }

                    CodeGenConfig p = new CodeGenConfig("Parameter", config);
                    p.AttributeAdd("Name", propConfig.GetAttributeValue <string>("Name"));
                    p.AttributeAdd("LayerPassing", layerPassing);
                    p.AttributeAdd("IsMandatory", isMandatory);
                    ParameterConfigLoader.UpdateConfigFromProperty(p, propConfig.GetAttributeValue <string>("Name"));
                    paramList.Add(p);
                }

                if (paramList.Count > 0)
                {
                    config.Children.Add("Parameter", paramList);
                }
            }

            config.AttributeAdd("OperationType", "Get");
            config.AttributeAdd("ReturnType", "void");

            if (config.Attributes.ContainsKey("ReturnType"))
            {
                config.AttributeAdd("ReturnText", string.Format("A resultant {{{{{0}}}}}.", config.Attributes["ReturnType"]));
            }
            else
            {
                config.AttributeAdd("ReturnText", string.Format("A resultant {{{{{0}}}}}.", entity.Attributes["Name"]));
            }

            config.AttributeUpdate("ReturnText", config.Attributes["ReturnText"]);

            if (config.Attributes.ContainsKey("OperationType") && config.Attributes["OperationType"] == "Custom")
            {
                config.AttributeAdd("Text", CodeGenerator.ToSentenceCase(config.Attributes["Name"]));
            }

            if (config.Attributes.ContainsKey("Text"))
            {
                config.AttributeUpdate("Text", config.Attributes["Text"]);
            }

            config.AttributeAdd("PrivateName", CodeGenerator.ToPrivateCase(config.Attributes["Name"]));
            config.AttributeAdd("ArgumentName", CodeGenerator.ToCamelCase(config.Attributes["Name"]));
            config.AttributeAdd("QualifiedName", entity.Attributes["Name"] + config.Attributes["Name"]);
        }
コード例 #3
0
        /// <summary>
        /// Update the <see cref="CodeGenConfig"/> from a named <b>Property</b>.
        /// </summary>
        /// <param name="config">The <see cref="CodeGenConfig"/> to update.</param>
        /// <param name="propertyName">The <b>Property</b> name.</param>
        public static void UpdateConfigFromProperty(CodeGenConfig config, string propertyName)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            List <CodeGenConfig> propConfig = CodeGenConfig.FindConfigList(config, "Property");

            if (propConfig == null)
            {
                throw new CodeGenException($"Attribute value references Property '{propertyName}' that does not exist for Entity.");
            }

            CodeGenConfig itemConfig = null;

            foreach (CodeGenConfig p in propConfig)
            {
                if (p.Attributes["Name"] == propertyName)
                {
                    itemConfig = p;
                }
            }

            if (itemConfig == null)
            {
                throw new CodeGenException($"Attribute value references Property '{propertyName}' that does not exist for Entity.");
            }

            config.AttributeAdd("ArgumentName", CodeGenerator.ToCamelCase(config.Attributes["Name"]));
            config.AttributeAdd("Text", itemConfig.Attributes["Text"]);
            config.AttributeAdd("Type", itemConfig.Attributes["Type"]);
            config.AttributeAdd("LayerPassing", "All");

            if (itemConfig.Attributes.ContainsKey("Nullable"))
            {
                config.AttributeAdd("Nullable", itemConfig.Attributes["Nullable"]);
            }

            if (itemConfig.Attributes.ContainsKey("RefDataType"))
            {
                config.AttributeAdd("RefDataType", itemConfig.Attributes["RefDataType"]);
            }

            if (itemConfig.Attributes.ContainsKey("DataConverter"))
            {
                config.AttributeAdd("DataConverter", itemConfig.Attributes["DataConverter"]);
            }

            if (itemConfig.Attributes.ContainsKey("IsDataConverterGeneric"))
            {
                config.AttributeAdd("IsDataConverterGeneric", itemConfig.Attributes["IsDataConverterGeneric"]);
            }

            if (itemConfig.Attributes.ContainsKey("UniqueKey"))
            {
                config.AttributeAdd("UniqueKey", itemConfig.Attributes["UniqueKey"]);
            }
        }
コード例 #4
0
        private void ExcludeUdtColumns(CodeGenConfig config)
        {
            var ecx = string.IsNullOrEmpty(config.GetAttributeValue <string>("UdtExcludeColumns")) ? new List <string>() : config.GetAttributeValue <string>("UdtExcludeColumns").Split(',').Select(x => x.Trim()).ToList();

            if (ecx.Count == 0)
            {
                return;
            }

            var csConfig = CodeGenConfig.FindConfigList(config, "Column");

            if (csConfig != null)
            {
                foreach (CodeGenConfig cConfig in csConfig)
                {
                    if (ecx.Contains(cConfig.GetAttributeValue <string>("Name")))
                    {
                        cConfig.AttributeUpdate("UdtExclude", "true");
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Loads the <see cref="CodeGenConfig"/> after the corresponding <see cref="CodeGenConfig.Children"/>.
        /// </summary>
        /// <param name="config">The <see cref="CodeGenConfig"/> being loaded.</param>
        public void LoadAfterChildren(CodeGenConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var schema = config.GetAttributeValue <string>("Schema");
            var name   = config.GetAttributeValue <string>("Name");
            var table  = _tables.Where(x => x.Name == name && x.Schema == schema).SingleOrDefault();

            AddTableColumns(config, table);
            ExcludeUdtColumns(config);

            var autoGet    = config.GetAttributeValue <bool>("Get");
            var autoGetAll = config.GetAttributeValue <bool>("GetAll");
            var autoCreate = config.GetAttributeValue <bool>("Create");
            var autoUpdate = config.GetAttributeValue <bool>("Update");
            var autoUpsert = config.GetAttributeValue <bool>("Upsert");
            var autoDelete = config.GetAttributeValue <bool>("Delete");
            var autoMerge  = config.GetAttributeValue <bool>("Merge");

            // Check quick Get/Create/Update/Delete configurations.
            var spsConfig = CodeGenConfig.FindConfigList(config, "StoredProcedure");

            if (spsConfig != null)
            {
                foreach (CodeGenConfig spConfig in spsConfig)
                {
                    switch (spConfig.GetAttributeValue <string>("Name"))
                    {
                    case "Get": autoGet = false; break;

                    case "GetAll": autoGetAll = false; break;

                    case "Create": autoCreate = false; break;

                    case "Update": autoUpdate = false; break;

                    case "Upsert": autoUpsert = false; break;

                    case "Delete": autoDelete = false; break;

                    case "Merge": autoMerge = false; break;
                    }
                }
            }

            // Stop if no quick configs.
            if (!autoGet && !autoGetAll && !autoCreate && !autoUpdate && !autoUpsert && !autoDelete && !autoMerge)
            {
                return;
            }

            // Where no stored procedures already defined, need to add in the placeholder.
            if (spsConfig == null)
            {
                spsConfig = new List <CodeGenConfig>();
                config.Children.Add("StoredProcedure", spsConfig);
            }

            // Add each stored proecedure.
            if (autoMerge)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Merge");
                sp.AttributeAdd("Type", "Merge");
                spsConfig.Insert(0, sp);
            }

            if (autoDelete)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Delete");
                sp.AttributeAdd("Type", "Delete");
                spsConfig.Insert(0, sp);
            }

            if (autoUpsert)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Upsert");
                sp.AttributeAdd("Type", "Upsert");
                spsConfig.Insert(0, sp);
            }

            if (autoUpdate)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Update");
                sp.AttributeAdd("Type", "Update");
                spsConfig.Insert(0, sp);
            }

            if (autoCreate)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Create");
                sp.AttributeAdd("Type", "Create");
                spsConfig.Insert(0, sp);
            }

            if (autoGetAll)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "GetAll");
                sp.AttributeAdd("Type", "GetAll");

                if (config.Root.Attributes.ContainsKey("RefDatabaseSchema") && table.Schema == config.Root.GetAttributeValue <string>("RefDatabaseSchema"))
                {
                    var           obList = new List <CodeGenConfig>();
                    CodeGenConfig ob     = new CodeGenConfig("OrderBy", config);
                    ob.AttributeAdd("Name", "SortOrder");
                    ob.AttributeAdd("Order", "ASC");
                    obList.Add(ob);

                    ob = new CodeGenConfig("OrderBy", config);
                    ob.AttributeAdd("Name", "Code");
                    ob.AttributeAdd("Order", "ASC");
                    obList.Add(ob);

                    sp.Children.Add("OrderBy", obList);
                }

                spsConfig.Insert(0, sp);
            }

            if (autoGet)
            {
                CodeGenConfig sp = new CodeGenConfig("StoredProcedure", config);
                sp.AttributeAdd("Name", "Get");
                sp.AttributeAdd("Type", "Get");
                spsConfig.Insert(0, sp);
            }
        }
コード例 #6
0
        /// <summary>
        /// Loads the <see cref="CodeGenConfig"/> after the corresponding <see cref="CodeGenConfig.Children"/>.
        /// </summary>
        /// <param name="config">The <see cref="CodeGenConfig"/> being loaded.</param>
        public async Task LoadAfterChildrenAsync(CodeGenConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var autoGet    = config.GetAttributeValue <bool>("Get");
            var autoGetAll = config.GetAttributeValue <bool>("GetAll");
            var autoCreate = config.GetAttributeValue <bool>("Create");
            var autoUpdate = config.GetAttributeValue <bool>("Update");
            var autoPatch  = config.GetAttributeValue <bool>("Patch");
            var autoDelete = config.GetAttributeValue <bool>("Delete");

            // Check quick Get/Create/Update/Delete configurations.
            var opsConfig = CodeGenConfig.FindConfigList(config, "Operation");

            if (opsConfig != null)
            {
                foreach (CodeGenConfig opConfig in opsConfig)
                {
                    switch (opConfig.GetAttributeValue <string>("Name"))
                    {
                    case "Get": autoGet = false; break;

                    case "GetAll": autoGetAll = false; break;

                    case "Create": autoCreate = false; break;

                    case "Update": autoUpdate = false; break;

                    case "Patch": autoPatch = false; break;

                    case "Delete": autoDelete = false; break;
                    }
                }
            }

            // Stop if no quick configs.
            if (!autoGet && !autoGetAll && !autoCreate && !autoUpdate && !autoDelete)
            {
                return;
            }

            // Where no operations already defined, need to add in the placeholder.
            if (opsConfig == null)
            {
                opsConfig = new List <CodeGenConfig>();
                config.Children.Add("Operation", opsConfig);
            }

            // Where ReferenceData is being generated; add an 'Id' property to enable the code gen to function.
            var refDataType = config.GetAttributeValue <string>("RefDataType");

            if (refDataType != null)
            {
                var propsConfig = CodeGenConfig.FindConfigList(config, "Property");
                if (propsConfig == null)
                {
                    propsConfig = new List <CodeGenConfig>();
                    config.Children.Add("Property", propsConfig);
                }

                ApplyRefDataProperties(config, propsConfig, refDataType);
            }

            // Determine the WebApiRout based on the unique key.
            var webApiRoute = string.Join("/", CodeGenConfig.FindConfigList(config, "Property").Where(x => x.GetAttributeValue <bool>("UniqueKey")).Select(x => "{" + x.GetAttributeValue <string>("ArgumentName") + "}"));

            if (string.IsNullOrEmpty(webApiRoute))
            {
                return;
            }

            // Add each operation.
            if (autoDelete)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "Delete");
                o.AttributeAdd("OperationType", "Delete");
                o.AttributeAdd("UniqueKey", "true");
                o.AttributeAdd("WebApiRoute", webApiRoute);
                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }

            if (autoPatch)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "Patch");
                o.AttributeAdd("OperationType", "Patch");
                o.AttributeAdd("UniqueKey", "true");
                o.AttributeAdd("WebApiRoute", webApiRoute);
                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }

            if (autoUpdate)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "Update");
                o.AttributeAdd("OperationType", "Update");
                o.AttributeAdd("UniqueKey", "true");
                o.AttributeAdd("WebApiRoute", webApiRoute);

                if (refDataType != null)
                {
                    o.AttributeAdd("ValidatorFluent", $"Entity(new ReferenceDataValidator<{config.GetAttributeValue<string>("Name")}>())");
                }

                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }

            if (autoCreate)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "Create");
                o.AttributeAdd("OperationType", "Create");
                o.AttributeAdd("UniqueKey", "false");
                o.AttributeAdd("WebApiRoute", "");

                if (refDataType != null)
                {
                    o.AttributeAdd("ValidatorFluent", $"Entity(new ReferenceDataValidator<{config.GetAttributeValue<string>("Name")}>())");
                }

                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }

            if (autoGet)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "Get");
                o.AttributeAdd("OperationType", "Get");
                o.AttributeAdd("UniqueKey", "true");
                o.AttributeAdd("WebApiRoute", webApiRoute);
                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }

            if (autoGetAll)
            {
                CodeGenConfig o = new CodeGenConfig("Operation", config);
                o.AttributeAdd("Name", "GetAll");
                o.AttributeAdd("OperationType", "GetColl");
                o.AttributeAdd("WebApiRoute", "");
                opsConfig.Insert(0, await ApplyOperationLoaderAsync(o).ConfigureAwait(false));
            }
        }
コード例 #7
0
        /// <summary>
        /// Loads the <see cref="CodeGenConfig"/> before the corresponding <see cref="CodeGenConfig.Children"/>.
        /// </summary>
        /// <param name="config">The <see cref="CodeGenConfig"/> being loaded.</param>
        public Task LoadBeforeChildrenAsync(CodeGenConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (!config.Attributes.ContainsKey("Name"))
            {
                throw new CodeGenException("Operation element must have a Name property.");
            }

            CodeGenConfig entity = CodeGenConfig.FindConfig(config, "Entity") ?? throw new CodeGenException("Operation element must have an Entity element parent.");

            if (config.GetAttributeValue <bool>("UniqueKey"))
            {
                List <CodeGenConfig> paramList = new List <CodeGenConfig>();
                var layerPassing = new string[] { "Create", "Update" }.Contains(config.GetAttributeValue <string>("OperationType")) ? "ToManagerSet" : "All";
                var isMandatory = new string[] { "Create", "Update" }.Contains(config.GetAttributeValue <string>("OperationType")) ? "false" : "true";

                var propConfigs = CodeGenConfig.FindConfigList(config, "Property") ?? new List <CodeGenConfig>();
                foreach (CodeGenConfig propConfig in propConfigs)
                {
                    if (!propConfig.GetAttributeValue <bool>("UniqueKey"))
                    {
                        continue;
                    }

                    CodeGenConfig p = new CodeGenConfig("Parameter", config);
                    p.AttributeAdd("Name", propConfig.GetAttributeValue <string>("Name"));
                    p.AttributeAdd("LayerPassing", layerPassing);
                    p.AttributeAdd("IsMandatory", isMandatory);
                    ParameterConfigLoader.UpdateConfigFromProperty(p, propConfig.GetAttributeValue <string>("Name"));
                    paramList.Add(p);
                }

                if (paramList.Count > 0)
                {
                    config.Children.Add("Parameter", paramList);
                }
            }

            config.AttributeAdd("OperationType", "Get");
            config.AttributeAdd("ReturnType", "void");

            if (config.Attributes.ContainsKey("ReturnType"))
            {
                config.AttributeAdd("ReturnText", string.Format(System.Globalization.CultureInfo.InvariantCulture, "A resultant {{{{{0}}}}}.", config.Attributes["ReturnType"]));
            }
            else
            {
                config.AttributeAdd("ReturnText", string.Format(System.Globalization.CultureInfo.InvariantCulture, "A resultant {{{{{0}}}}}.", entity.Attributes["Name"]));
            }

            config.AttributeUpdate("ReturnText", config.Attributes["ReturnText"]);

            if (config.Attributes.ContainsKey("OperationType") && config.Attributes["OperationType"] == "Custom")
            {
                config.AttributeAdd("Text", CodeGenerator.ToSentenceCase(config.Attributes["Name"]));
            }

            if (config.Attributes.ContainsKey("Text"))
            {
                config.AttributeUpdate("Text", config.Attributes["Text"]);
            }

            config.AttributeAdd("PrivateName", CodeGenerator.ToPrivateCase(config.Attributes["Name"]));
            config.AttributeAdd("ArgumentName", CodeGenerator.ToCamelCase(config.Attributes["Name"]));
            config.AttributeAdd("QualifiedName", entity.Attributes["Name"] + config.Attributes["Name"]);

            return(Task.CompletedTask);
        }