Beispiel #1
0
        protected void DoView(Program p, string fqview)
        {
            Helper     h  = new Helper();
            ColumnInfo ci = new ColumnInfo();

            Tuple <string, string> schemaview = h.SplitSchemaFromTable(fqview);

            string thedatabase = h.GetCsharpClassName(null, null, p._databaseName);

            string csharpnamespace   = p._namespace + "." + p._viewSubDirectory;
            string resourcenamespace = p._resourceNamespace + "." + p._viewSubDirectory;

            string theclass   = h.GetCsharpClassName(p._prefixObjectsWithSchema, schemaview.Item1, schemaview.Item2);
            string csharpfile = p._directory + @"\" + p._viewSubDirectory + @"\" + theclass + ".cs";

            string csharpfactoryfile = csharpfile.Replace(".cs", "Factory.cs");
            string thefactoryclass   = theclass + "Factory";

            // config for this view, if any
            string  xpath = "/CodeGen/Views/View[@Class='" + theclass + "']";
            XmlNode view  = p._codegen.SelectSingleNode(xpath);

            // select sql
            string selectsql = "select * from {0} t ";

            // do class
            h.MessageVerbose("[{0}]", csharpfile);
            using (StreamWriter sw = new StreamWriter(csharpfile, false, UTF8Encoding.UTF8))
            {
                int tab = 0;

                // header
                h.WriteCodeGenHeader(sw);
                h.WriteUsing(sw);

                // namespace
                using (NamespaceBlock nsb = new NamespaceBlock(sw, tab++, csharpnamespace))
                {
                    using (ClassBlock cb = new ClassBlock(sw, tab++, theclass, "acr.RowBase"))
                    {
                        List <Tuple <string, string> > columns = ci.GetViewColumns(fqview);

                        // properties and constructor
                        using (RowConstructorBlock conb = new RowConstructorBlock(sw, tab, theclass, columns, null, ""))
                        {}
                    }     // end class
                }         // end namespace
            }             // eof

            // do class factory
            h.MessageVerbose("[{0}]", csharpfactoryfile);
            using (StreamWriter sw = new StreamWriter(csharpfactoryfile, false, UTF8Encoding.UTF8))
            {
                int tab = 0;

                // header
                h.WriteCodeGenHeader(sw);
                h.WriteUsing(sw, p._namespace);

                // namespace
                using (NamespaceBlock nsb = new NamespaceBlock(sw, tab++, csharpnamespace))
                {
                    using (ClassBlock cb = new ClassBlock(sw, tab++, thefactoryclass,
                                                          "acr.FactoryBase< " +
                                                          theclass + ", " +
                                                          "ns." + p._databaseSubDirectory + "." + thedatabase + "DatabaseSingletonHelper, " +
                                                          "ns." + p._databaseSubDirectory + "." + thedatabase + "Database >"
                                                          ))
                    {
                        // constructor
                        using (ViewFactoryConstructorBlock conb = new ViewFactoryConstructorBlock(sw, tab, fqview, selectsql, thefactoryclass))
                        {}

                        // default load all method
                        List <string> parameters = new List <string>();
                        Dictionary <string, string> parameterdictionary = new Dictionary <string, string>();

                        parameters = new List <string>();
                        parameters.Add("connˡ");
                        parameters.Add("topNˡ");
                        parameters.Add("orderByˡ");
                        parameters.Add("tranˡ");

                        parameterdictionary.Add("connˡ", "sds.SqlConnection");
                        parameterdictionary.Add("topNˡ", "int?");
                        parameterdictionary.Add("orderByˡ", "scg.List<acr.CodeGenOrderBy>");
                        parameterdictionary.Add("tranˡ", "sds.SqlTransaction");

                        // method
                        h.MessageVerbose("[{0}].[{1}].[{2}]", csharpnamespace, theclass, "Loadˡ");
                        using (ViewFactoryMethodBlock mb = new ViewFactoryMethodBlock(sw, tab, "Loadˡ", parameters, parameterdictionary, "", theclass, resourcenamespace))
                        {}

                        // load by where
                        parameters = new List <string>();
                        parameters.Add("connˡ");
                        parameters.Add("whereˡ");
                        parameters.Add("parametersˡ");
                        parameters.Add("topNˡ");
                        parameters.Add("orderByˡ");
                        parameters.Add("tranˡ");

                        parameterdictionary = new Dictionary <string, string>();
                        parameterdictionary.Add("connˡ", "sds.SqlConnection");
                        parameterdictionary.Add("whereˡ", "string");
                        parameterdictionary.Add("parametersˡ", "scg.List<sds.SqlParameter>");
                        parameterdictionary.Add("topNˡ", "int?");
                        parameterdictionary.Add("orderByˡ", "scg.List<acr.CodeGenOrderBy>");
                        parameterdictionary.Add("tranˡ", "sds.SqlTransaction");

                        h.MessageVerbose("[{0}].[{1}].[{2}]", csharpnamespace, theclass, "LoadByWhereˡ");
                        using (ViewFactoryMethodBlock mb = new ViewFactoryMethodBlock(sw, tab, "LoadByWhereˡ", parameters, parameterdictionary, "", theclass, resourcenamespace))
                        {}

                        // other methods
                        if (view != null)
                        {
                            XmlNodeList xmlmethods = view.SelectNodes("Methods/Method");
                            foreach (XmlNode xmlmethod in xmlmethods)
                            {
                                string themethod = xmlmethod.SelectSingleNode("@Name").InnerText;

                                // where resource
                                string whereresource = xmlmethod.SelectSingleNode("@Where").InnerText;

                                // parameters
                                parameters = new List <string>();
                                parameters.Add("connˡ");

                                XmlNodeList xmlparameters = xmlmethod.SelectNodes("Parameters/Parameter");
                                foreach (XmlNode xmlparameter in xmlparameters)
                                {
                                    parameters.Add(xmlparameter.SelectSingleNode("@Name").InnerText);
                                }

                                parameters.Add("topNˡ");
                                parameters.Add("orderByˡ");
                                parameters.Add("tranˡ");

                                parameterdictionary = new Dictionary <string, string>();
                                parameterdictionary.Add("connˡ", "sds.SqlConnection");

                                xmlparameters = xmlmethod.SelectNodes("Parameters/Parameter");
                                foreach (XmlNode xmlparameter in xmlparameters)
                                {
                                    parameterdictionary.Add(xmlparameter.SelectSingleNode("@Name").InnerText,
                                                            xmlparameter.SelectSingleNode("@Type").InnerText);
                                }

                                parameterdictionary.Add("topNˡ", "int?");
                                parameterdictionary.Add("orderByˡ", "scg.List<acr.CodeGenOrderBy>");
                                parameterdictionary.Add("tranˡ", "sds.SqlTransaction");

                                // method
                                h.MessageVerbose("[{0}].[{1}].method [{2}]", csharpnamespace, theclass, themethod);

                                using (ViewFactoryMethodBlock mb = new ViewFactoryMethodBlock(sw, tab, themethod, parameters, parameterdictionary, whereresource, theclass, resourcenamespace))
                                {}
                            }
                        }
                    } // end class
                }     // end namespace
            }         // eof
        }             // end do view
Beispiel #2
0
        protected void DoUnitTest2(Program p)
        {
            Helper     h  = new Helper();
            ColumnInfo ci = new ColumnInfo();

            // see if we want the unit test - is there something in the config file?
            XmlNode node = p._codegen.SelectSingleNode("/CodeGen/UnitTest");

            if (node == null)
            {
                h.MessageVerbose("No unit test block found - not generating unit test.");
                return;
            }

            // get list of tables in ri order - onlt tables that can be saved [ie have a primary key] are eligible
            // get tables that have primary keys - and associated field information

            foreach (var table in p._rihelper.SortedTables)
            {
                string        fqtablename = table.Item2;;
                List <string> pkcolumns   = p._di.PrimaryKeyColumns.Get(fqtablename, "PK");

                if (pkcolumns.Count > 0)
                {
                    // we want this table
                    _unitTestTables.Add(fqtablename);
                    _unitTestTablesReverse.Add(fqtablename);

                    // we want all its columns
                    _columnsMap.Add(fqtablename, ci.GetTableColumns(fqtablename));

                    // we want its computed columns
                    _computedColumnsMap.Add(fqtablename, p._di.ComputedColumns.Get(fqtablename));

                    // we want its identity columns
                    _identityColumnsMap.Add(fqtablename, p._di.IdentityColumns.Get(fqtablename));

                    // we want its timestamp columns
                    _timestampColumnsMap.Add(fqtablename, p._di.TimestampColumns.Get(fqtablename));
                }
            }

            _unitTestTablesReverse.Reverse();              // same list but in reverse order, for deleting

            // dump list of unit test tables
            h.MessageVerbose("### The following tables are supported for auto unit test [they have a primary key]: ###");
            foreach (string atable in _unitTestTables)
            {
                h.MessageVerbose("\t[{0}]", atable);
            }
            h.MessageVerbose("[{0}] unit test tables", _unitTestTables.Count);

            //
            // plumbing
            //

            // generate CodeGenUnitTestClass.cs
            EntryPoint(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.State.cs
            State(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.CodegenRunTimeSettings.cs
            CodegenRunTimeSettings(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.AssertObjectsNew.cs
            AssertObjectsNew(p, _theclass, _baseclass);

            //
            // object population and insert and update
            //

            // generate CodeGenUnitTestClass.PopulateObjectsForInsert.cs
            PopulateObjectsForInsert(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.PopulateObjectsForUpdate.cs
            PopulateObjectsForUpdate(p, _theclass, _baseclass);

            // create a Populate.#.cs file for each table
            // containing the function Populate_#( bool insert, # obj,  )
            Populate(p, _theclass, _baseclass);

            // create a PopulateOverride.#.cs file for each table
            // containing the function PopulateOverride_#( bool insert, # obj  )
            PopulateOverride(p, _theclass, _baseclass);

            //
            // object deletion
            //

            // generate CodeGenUnitTestClass.DeleteObjects.cs
            DeleteObjects(p, _theclass, _baseclass);

            //
            // field level assertions
            //

            // create a AssertAfterSave.#.cs file for each table
            // containing the function AssertAfterSave_#( bool insert, # lhs, # rhs )
            Assert(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.AssertObjectsAfterInsert.cs
            AssertObjectsAfterInsert(p, _theclass, _baseclass);

            // generate CodeGenUnitTestClass.AssertObjectsAfterUpdate.cs
            AssertObjectsAfterUpdate(p, _theclass, _baseclass);
        }