예제 #1
0
    /// <summary>
    /// Executes the tool with the given parameters.
    /// </summary>
    public void Process(string [] args)
    {
        // Create a template factory
        TemplateFactory factory = new TemplateFactory();

        factory.TemporaryFile       = outputFile;
        factory.DeleteTemporaryFile = false;
        factory.CompileAssembly     = false;
        factory.ExtendClassName     = extendclass;
        factory.NamespaceName       = ns;
        factory.ClassName           = classname;

        // Parse the file, create the file, but don't compile
        using (TextReader reader = templateFile.OpenText())
        {
            factory.Create(reader, templateFile.FullName);
        }
    }
예제 #2
0
        public override void OnInit()
        {
            if (this.ItemsPanel == null)
            {
                this.ItemsPanel = TemplateFactory.Create <ControlTemplate>(this, ItemsControl.ItemsPanelProperty);
            }

            if (this.ItemTemplate == null)
            {
                this.ItemTemplate = TemplateFactory.Create <ControlTemplate>(this, ItemsControl.ItemTemplateProperty);
            }

            if (this.ItemTemplate == null)
            {
                throw new XamlException("没有得到类型" + this.GetType().FullName + "的ItemTemplate");
            }

            base.OnInit();
        }
        public CodeGenerator(Generator generator, IDbContextFilter filter)
        {
#pragma warning disable IDE0016 // Use 'throw' expression
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
#pragma warning restore IDE0016 // Use 'throw' expression

            var isEfCore      = Settings.GeneratorType == GeneratorType.EfCore;
            var isEfCore3Plus = Settings.IsEfCore3Plus();

            _generator = generator;
            _filter    = filter;

            _tables = filter.Tables
                      .Where(t => !t.IsMapping && (t.HasPrimaryKey || (t.IsView && isEfCore3Plus)))
                      .OrderBy(x => x.NameHumanCase)
                      .Select(tbl => new TableTemplateData(tbl))
                      .ToList();

            if (filter.IncludeStoredProcedures)
            {
                _storedProcs = filter.StoredProcs
                               .Where(s => s.IsStoredProcedure)
                               .OrderBy(x => x.NameHumanCase)
                               .Select(sp => new StoredProcTemplateData(
                                           sp.ReturnModels.Count > 0,
                                           sp.ReturnModels.Count == 1,
                                           sp.ReturnModels.Count > 1,
                                           sp.WriteStoredProcReturnType(_filter),
                                           sp.WriteStoredProcReturnModelName(filter),
                                           sp.WriteStoredProcFunctionName(filter),
                                           sp.WriteStoredProcFunctionParams(false),
                                           sp.WriteStoredProcFunctionParams(true),
                                           sp.StoredProcHasOutParams() || sp.ReturnModels.Count == 0,
                                           sp.WriteStoredProcFunctionOverloadCall(),
                                           sp.WriteStoredProcFunctionSetSqlParameters(false),
                                           sp.WriteStoredProcFunctionSetSqlParameters(true),
                                           sp.ReturnModels.Count == 1
                            ? // exec
                                           string.Format("EXEC @procResult = [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams()).Trim()
                            : string.Format("[{0}].[{1}]", sp.Schema.DbName, sp.DbName),
                                           sp.ReturnModels.Count == 1
                            ? // Async exec
                                           string.Format("EXEC [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams()).Trim()
                            : string.Format("[{0}].[{1}]", sp.Schema.DbName, sp.DbName),
                                           sp.WriteStoredProcReturnModelName(_filter),
                                           sp.WriteStoredProcFunctionSqlParameterAnonymousArray(true, true),
                                           sp.WriteStoredProcFunctionSqlParameterAnonymousArray(false, true),
                                           sp.WriteStoredProcFunctionDeclareSqlParameter(true),
                                           sp.WriteStoredProcFunctionDeclareSqlParameter(false),
                                           sp.Parameters.OrderBy(x => x.Ordinal).Select(sp.WriteStoredProcSqlParameterName).ToList(),
                                           sp.ReturnModels.Count,
                                           string.Format("EXEC @procResult = [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams())
                                           ))
                               .ToList();
            }
            else
            {
                _storedProcs = new List <StoredProcTemplateData>();
            }

            if (filter.IncludeTableValuedFunctions)
            {
                _tableValuedFunctions = filter.StoredProcs
                                        .Where(s => s.IsTableValuedFunction)
                                        .OrderBy(x => x.NameHumanCase)
                                        .Select(tvf => new TableValuedFunctionsTemplateData(
                                                    tvf.ReturnModels.Count == 1 && tvf.ReturnModels[0].Count == 1,
                                                    tvf.ReturnModels.Count == 1 && tvf.ReturnModels[0].Count == 1 ? tvf.ReturnModels[0][0].ColumnName : null,
                                                    tvf.WriteStoredProcFunctionName(_filter),
                                                    tvf.WriteStoredProcReturnModelName(_filter),
                                                    tvf.WriteStoredProcFunctionParams(false),
                                                    tvf.DbName,
                                                    tvf.Schema.DbName,
                                                    isEfCore ? tvf.WriteStoredProcFunctionDeclareSqlParameter(false) : tvf.WriteTableValuedFunctionDeclareSqlParameter(),
                                                    isEfCore
                            ? tvf.WriteStoredProcFunctionSqlParameterAnonymousArray(false, false)
                            : tvf.WriteTableValuedFunctionSqlParameterAnonymousArray(),
                                                    isEfCore ? tvf.WriteNetCoreTableValuedFunctionsSqlAtParams() : tvf.WriteStoredProcFunctionSqlAtParams(),
                                                    isEfCore3Plus ? "FromSqlRaw"  : "FromSql",
                                                    isEfCore3Plus ? "Set"         : "Query",
                                                    isEfCore3Plus ? "Entity"      : "Query",
                                                    isEfCore3Plus ? ".HasNoKey()" : string.Empty
                                                    ))
                                        .ToList();

                _tableValuedFunctionComplexTypes = filter.StoredProcs
                                                   .Where(s => s.IsTableValuedFunction &&
                                                          !Settings.StoredProcedureReturnTypes.ContainsKey(s.NameHumanCase) &&
                                                          !Settings.StoredProcedureReturnTypes.ContainsKey(s.DbName))
                                                   .OrderBy(x => x.NameHumanCase)
                                                   .Select(x => x.WriteStoredProcReturnModelName(_filter))
                                                   .ToList();
            }
            else
            {
                _tableValuedFunctions            = new List <TableValuedFunctionsTemplateData>();
                _tableValuedFunctionComplexTypes = new List <string>();
            }

            if (filter.IncludeScalarValuedFunctions)
            {
                _scalarValuedFunctions = filter.StoredProcs
                                         .Where(s => s.IsScalarValuedFunction &&
                                                s.Parameters.Any(x => x.Mode == StoredProcedureParameterMode.Out))
                                         .OrderBy(x => x.NameHumanCase)
                                         .Select(svf => new ScalarValuedFunctionsTemplateData(
                                                     svf.WriteStoredProcFunctionName(_filter),
                                                     svf.Parameters.Where(x => x.Mode == StoredProcedureParameterMode.Out).OrderBy(x => x.Ordinal).FirstOrDefault()?.PropertyType,
                                                     svf.WriteStoredProcFunctionParams(false),
                                                     svf.DbName,
                                                     svf.Schema.DbName
                                                     ))
                                         .ToList();
            }
            else
            {
                _scalarValuedFunctions = new List <ScalarValuedFunctionsTemplateData>();
            }

            var returnModelsUsed = new List <string>();
            foreach (var sp in _storedProcs)
            {
                if (returnModelsUsed.Contains(sp.ReturnModelName))
                {
                    sp.CreateDbSetForReturnModel = false;
                }
                else
                {
                    returnModelsUsed.Add(sp.ReturnModelName);
                }
            }

            _hasTables                          = _tables.Any();
            _hasStoredProcs                     = _storedProcs.Any();
            _hasTableValuedFunctions            = _tableValuedFunctions.Any();
            _hasScalarValuedFunctions           = _scalarValuedFunctions.Any();
            _hasTableValuedFunctionComplexTypes = _tableValuedFunctionComplexTypes.Any();
            _hasEnums = filter.Enums.Any();

            _globalUsings = new List <string>();
            _template     = TemplateFactory.Create();
            CalcGlobalUsings();
        }
예제 #4
0
        public void InMemoryString()
        {
            factory.TemporaryFile = null;
            ITemplate it = factory.Create("This is a test");
            Context   c  = new Context();

            Assert.AreEqual("This is a test", it.ToString(c));
        }