Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            ArgsParser parser = new ArgsParser(
                new Parameter("assembly", "a", "the assembly from which the mapping should be generated"),
                new Parameter("model", "m", "the model from which the mapping should be generated"),
                new Parameter("namespace", "n", "maps only the namespace(s) you specify"),
                new Parameter("driver", "d", true, "the SQL driver to use, eg. SqlServer"),
                new Parameter("output", "o", true, "the file where the mapping should be generated")
            );
            parser.Description = "This tool helps you to generate a default mapping. Here are the available commands :";
            if (parser.Parse(args))
            {
                Model model = new Model();
                var visitor = new ModelMetaDataVisitor(model);
                string assembly = parser["assembly"];
                if (assembly != null)
                {
                    string @namespace = parser["namespace"];
                    string[] namespaces = null;
                    if (@namespace != null)
                        namespaces = @namespace.Split(' ');
                    foreach (IMetaData metadata in MetaDataFactory.FromAssembly(Assembly.LoadFile(assembly), namespaces))
                        metadata.Accept(visitor);
                }

                Mapping mapping = new Mapping();
                Type driver = Type.GetType(parser["driver"]);
                if (driver == null)
                    driver = Type.GetType("Evaluant.Uss.SqlMapper.Drivers." + parser["driver"] + ", Evaluant.Uss.SqlMapper");
                if (driver == null)
                {
                    Console.WriteLine("The driver named " + parser["driver"] + " could not be found");
                    return;
                }
                IMapper mapper = new DefaultMapper((IDriver)Activator.CreateInstance(driver), mapping);
                mapper.Map(model);
                XmlSerializer serializer = new XmlSerializer(typeof(Mapping));
                using (Stream s = File.OpenWrite(parser["output"]))
                {
                    serializer.Serialize(s, mapping);
                    s.Close();
                }
                Console.WriteLine("The mapping was generated successfully...");
                Console.ReadLine();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            ArgsParser parser = new ArgsParser(
                new Parameter("mapping", "m", true, "the mapping file from which to generate the script"),
                new Parameter("dialect", "d", true, "the dialect to use, eg. SqlServer"),
                new Parameter("output", "o", false, "the script file to be generated"));
            if (parser.Parse(args))
            {
                Mapping m = Mapping.Load(parser["mapping"]);
                m.Initialize(false);
                Type dialectType = Type.GetType(parser["dialect"]);
                if (dialectType == null)
                    dialectType = Type.GetType("Evaluant.Uss.SqlMapper.Dialects." + parser["dialect"] + ", Evaluant.Uss.SqlMapper");
                if (dialectType == null)
                {
                    Console.WriteLine("The dialect named " + parser["dialect"] + " could not be found");
                    return;
                }

                IDialect dialect = (IDialect)Activator.CreateInstance(dialectType);

                SqlMapperProvider provider = new SqlMapperProvider();
                provider.Dialect = dialect;
                provider.Driver = new Script();
                provider.Mapping = m;

                provider.Driver.Initialize(parser["output"] ?? "script.sql");
                provider.MarkAsInitialized();
                provider.CreatePersistenceEngine().InitializeRepository();

                //using (TextWriter file = File.CreateText())
                //{
                //    foreach (Table table in m.Tables.Values)
                //        file.WriteLine(dialect.Render((IDbExpression)new CreateTableStatement(table)));

                //    List<string> list = new List<string>();

                //    foreach (var entity in m.Entities)
                //    {
                //        foreach (var reference in entity.Value.References.Values)
                //        {
                //            bool isFirst = reference.Rules.Count > 1;

                //            foreach (var rule in reference.Rules)
                //            {
                //                if (list.Contains(reference.Name + "_" + rule.ParentTableName))
                //                    continue;

                //                list.Add(reference.Name + "_" + rule.ParentTableName);

                //                if (isFirst)
                //                {
                //                    isFirst = false;
                //                    file.WriteLine(dialect.Render((IDbExpression)new AlterTableAddStatement()
                //                    {
                //                        ConstraintName = "FK_" + rule.ParentTableName + "_" + reference.Name,
                //                        Table = rule.ChildTable,
                //                        Constraint = new ForeignKeyConstraint()
                //                        {
                //                            Fields = rule.ChildFields,
                //                            ReferencesTable = rule.ParentTable,
                //                            References = rule.ParentFields
                //                        }
                //                    }));
                //                }
                //                else
                //                {
                //                    file.WriteLine(dialect.Render((IDbExpression)new AlterTableAddStatement()
                //                    {
                //                        ConstraintName = "FK_" + rule.ParentTableName + "_" + reference.Name,
                //                        Table = rule.ParentTable,
                //                        Constraint = new ForeignKeyConstraint()
                //                        {
                //                            Fields = rule.ParentFields,
                //                            ReferencesTable = rule.ChildTable,
                //                            References = rule.ChildFields
                //                        }
                //                    }));

                //                }
                //            }
                //        }

                //    }
                //    file.Close();
                //}

                Console.WriteLine("The script was successfully generated...");
                Console.Read();
            }
        }