public void NorthwindProcedureTest()
        {
            var dbReader = TestHelper.GetNorthwindReader();

            //remove the spCreateDiagram system sprocs
            dbReader.Exclusions.StoredProcedureFilter = new PrefixFilter("sp");
            var schema = dbReader.ReadAll();

            var procedure = schema.StoredProcedures
                            .Find(x => string.Equals(x.Name, "Employee Sales by Country", StringComparison.OrdinalIgnoreCase));

            if (procedure == null)
            {
                Assert.Inconclusive("No Employee Sales By Country found in Northwind");
            }

            //getting the procedure resultsets is a special call
            var runner = new ResultSetReader(schema);

            runner.Execute();

            var          directory  = TestHelper.CreateDirectory("NorthwindSproc");
            const string @namespace = "Northwind.Domain";
            var          settings   = new CodeWriterSettings
            {
                Namespace             = @namespace,
                CodeTarget            = CodeTarget.Poco,
                WriteStoredProcedures = true
            };

            var codeWriter = new CodeWriter(schema, settings);

            codeWriter.Execute(directory);

            var procedures = directory.GetDirectories("Procedures").FirstOrDefault();

            if (procedures == null)
            {
                Assert.Fail("Could not find Procedures subdirectory");
            }
            var files         = procedures.GetFiles("*.cs");
            var employeeSales = files.FirstOrDefault(f => f.Name == "EmployeeSalesByCountry.cs");

            Assert.IsNotNull(employeeSales,
                             "Should have written EmployeeSalesByCountry class for Employee Sales By Country procedure");

            var cs = File.ReadAllText(employeeSales.FullName);

            Assert.IsTrue(cs.Contains("public virtual IEnumerable<EmployeeSalesByCountryResult> Execute(DateTime? beginningDate, DateTime? endingDate)"),
                          "Generated input signature");

            var employeeSalesResult = files.FirstOrDefault(f => f.Name == "EmployeeSalesByCountryResult.cs");

            Assert.IsNotNull(employeeSalesResult,
                             "Should have written EmployeeSalesByCountryResult class for Employee Sales By Country procedure");

            cs = File.ReadAllText(employeeSalesResult.FullName);
            Assert.IsTrue(cs.Contains("public virtual string Country { get; set; }"),
                          "Generated property for resultSet column");
        }
Esempio n. 2
0
        public void TestHierarchicaMapping()
        {
            var container = new Container();

            container.Setup(c => c.ScanType <IHierarchy>());

            var locator = container.GetLocator();

            var reader = new DataReaderMock("IHierarchy.Id", "IHierarchy.Friend.Id", "IHierarchy.Children.Id", "IHierarchy.Children.Children.Id");

            reader.Add("A", "Friend_Of_A", "ChildOfA_1", "ChildOfChildOfA1_1");
            reader.Add("A", "Friend_Of_A", "ChildOfA_1", "ChildOfChildOfA1_2");
            reader.Add("A", "Friend_Of_A", "ChildOfA_2", "ChildOfChildOfA2_1");

            var entities = ResultSetReader.Read <IHierarchy>(reader, locator).ToList();

            Assert.Equal(1, entities.Count);

            Assert.Equal("A", entities[0].Id);
            Assert.Equal(2, entities[0].Children.Count());

            Assert.Equal(2, entities[0].Children.First().Children.Count());
            Assert.Equal(1, entities[0].Children.Last().Children.Count());

            Assert.Equal("ChildOfChildOfA2_1", entities[0].Children.Last().Children.Single().Id);

            Assert.NotNull(entities[0].Friend);
            Assert.Equal("Friend_Of_A", entities[0].Friend.Id);
        }
Esempio n. 3
0
        public void PkMappingTest()
        {
            var container = new Container();

            container.Setup(c => c.ScanType <ISimpleEntity>());

            var locator = container.GetLocator();

            var reader = new DataReaderMock("ISimpleEntity.Id", "ISimpleEntity.Text", "ISimpleEntity.Number");

            reader.Add(1, "Text1", 42);
            reader.Add(2, "Text2", 84);

            var entities = ResultSetReader.Read <ISimpleEntity>(reader, locator).ToList();

            Assert.Equal(2, entities.Count);

            for (var i = 1; i < 3; i++)
            {
                var entity = entities[i - 1];

                Assert.Equal(i, entity.Id);
                Assert.Equal($"Text{i}", entity.Text);
                Assert.Equal(42 * i, entity.Number);
            }
        }
Esempio n. 4
0
        public void TestRunnerWithNorthwind()
        {
            //smoke test - does this run without any exceptions
            var dbReader = TestHelper.GetNorthwindReader();
            var schema   = dbReader.ReadAll();

            var runner = new ResultSetReader(schema);

            runner.Execute();

            var          directory  = TestHelper.CreateDirectory("NorthwindSproc");
            const string @namespace = "Northwind.Domain";
            var          settings   = new CodeWriterSettings {
                Namespace = @namespace
            };

            var codeWriter = new CodeWriter(schema, settings);

            codeWriter.Execute(directory);

            Debug.WriteLine("Check project in " + directory.FullName);
        }
Esempio n. 5
0
        public IEnumerable <T> Select <T>(IQueryModel <T> query) where T : class
        {
            List <T> result;

            using (var transaction = m_transactionManager.Open(true))
            {
                var hasBuilder = query as IHasBuilder <T>;
                if (hasBuilder == null)
                {
                    throw new NotSupportedException(
                              $"Unsupported query type {query.GetType()}. It must implement {typeof(IHasBuilder<T>)} interface");
                }

                using (var reader = ExecuteReader(query, hasBuilder.OwnerBuilder, transaction))
                {
                    result = ResultSetReader.Read <T>(reader, m_locator).ToList();
                }

                transaction.Commit();
            }

            return(result);
        }
Esempio n. 6
0
        public void ReadResultSets()
        {
            using (var connection = new SqlConnection(Northwind))
            {
                connection.Open();
                var dr = new DatabaseSchemaReader.DatabaseReader(connection);
                dr.AllStoredProcedures();
                var schema = dr.DatabaseSchema;

                var rsr = new ResultSetReader(schema);
                rsr.Execute(connection);

                var sproc = schema.StoredProcedures.Find(x => x.Name == "SalesByCategory");
                Assert.IsNotNull(sproc);
                var rs = sproc.ResultSets.First();
                foreach (var rsColumn in rs.Columns)
                {
                    Console.WriteLine(rsColumn.Name);
                    Console.WriteLine(rsColumn.DbDataType);
                }
                Assert.IsNotNull(rs, "Stored procedure should return a result");
            }
        }
        public void MySqlProcedureTest()
        {
            const string providername     = "MySql.Data.MySqlClient";
            var          connectionString = ConnectionStrings.MySql;

            ProviderChecker.Check(providername, connectionString);

            var dbReader = new DatabaseReader(connectionString, providername);
            var schema   = dbReader.ReadAll();

            var procedure = schema.StoredProcedures
                            .Find(x => string.Equals(x.Name, "film_not_in_stock", StringComparison.OrdinalIgnoreCase));

            if (procedure == null)
            {
                Assert.Inconclusive("No sproc film_not_in_stock in Sakila");
            }

            //getting the procedure resultsets is a special call
            var runner = new ResultSetReader(schema);

            runner.Execute();

            var          directory  = TestHelper.CreateDirectory("MySqlSproc");
            const string @namespace = "MySqlSproc.Domain";
            var          settings   = new CodeWriterSettings
            {
                Namespace             = @namespace,
                CodeTarget            = CodeTarget.Poco,
                WriteStoredProcedures = true
            };

            //act
            var codeWriter = new CodeWriter(schema, settings);

            codeWriter.Execute(directory);

            //assert
            //This procedure produces a table of inventory ID numbers for the copies of the film not in stock,
            //and returns (in the p_film_count parameter) a count that indicates the number of rows in that table.

            //using (var connection = new MySqlConnection("Server=...;Database=sakila;Allow User Variables=True;"))
            //{
            //	var sproc = new FilmNotInStock(connection);
            //	var result = sproc.Execute(2, 1);
            //	Console.WriteLine(result.PFilmCount);
            //	Console.WriteLine(string.Join(",", result.FilmNotInStockResult0.Select(x => x.InventoryId)));
            //}

            var procedures = directory.GetDirectories("Procedures").FirstOrDefault();

            if (procedures == null)
            {
                Assert.Fail("Could not find Procedures subdirectory");
            }
            var files = procedures.GetFiles("*.cs");

            var filmNotInStock = files.FirstOrDefault(f => f.Name == "FilmNotInStock.cs");

            Assert.IsNotNull(filmNotInStock,
                             "Should have written FilmNotInStock class for film_not_in_stock procedure");

            var cs = File.ReadAllText(filmNotInStock.FullName);

            //there are 2 input parameters, an OUT parameter and a resultset.
            Assert.IsTrue(cs.Contains("public virtual FilmNotInStockResult Execute(int? pFilmId, int? pStoreId)"),
                          "Generated input signature");

            var result = files.FirstOrDefault(f => f.Name == "FilmNotInStockResult.cs");

            Assert.IsNotNull(result, "Should have written FilmNotInStockResult class");

            cs = File.ReadAllText(result.FullName);
            Assert.IsTrue(cs.Contains("public virtual int? PFilmCount { get; set; }"),
                          "Generated property for OUT parameter");
            Assert.IsTrue(cs.Contains("public virtual IList<FilmNotInStockResult0> FilmNotInStockResult0 { get; private set; }"),
                          "Generated collection property for resultset");
        }