Exemplo n.º 1
0
        //--------------------------------------------------------------------------------------------------------------------

        protected void DoStoredProc(Program p, string fqstoredprocedure)
        {
            Helper h = new Helper();
            StoredProcedureParameterInfo sppi  = new StoredProcedureParameterInfo();
            StoredProcedureResultsetInfo sprsi = new StoredProcedureResultsetInfo();

            var schemastoredprocedure = h.SplitSchemaFromTable(fqstoredprocedure);

            string thedatabase     = h.GetCsharpClassName(null, null, p._databaseName);
            string csharpnamespace = p._namespace + "." + p._storedProcsSubDirectory;

            string csharpstoredproc = h.GetCsharpClassName(p._prefixObjectsWithSchema, schemastoredprocedure.Item1, schemastoredprocedure.Item2);
            string csharpfile       = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharpstoredproc + ".cs";

            string thefactoryclass   = "StoredProcedureFactory";
            string csharpfactoryfile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + thefactoryclass + ".cs";

            // do each stored proc in a separate file, but same factory class
            h.MessageVerbose("[{0}]", csharpfile);

            // check for dud parameters - cursor, table - ignore the sp if so
            if (sppi.HasDudParameterStoredProcedure(fqstoredprocedure))
            {
                h.MessageVerbose("[{0}] Ignoring stored procedure because it has dud parameters.", fqstoredprocedure);
                return;
            }

            // get sp parameters
            List <string> parameters = new List <string>();

            parameters.Add("connˡ");

            var spparameters = sppi.GetStoredProcedureParameterInfo(fqstoredprocedure);

            if (spparameters != null)
            {
                foreach (var spparameter in spparameters)
                {
                    parameters.Add(spparameter.Name);
                }
            }

            parameters.Add("tranˡ");

            // the results sets
            var rsi = sprsi.GetResultsetInfo(fqstoredprocedure);

            // write out the stored proc wrapper to its file
            using (StreamWriter sw = new StreamWriter(csharpfile, 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.StoredProcedureFactoryBase< " + "ns." + p._databaseSubDirectory + "." + thedatabase + "DatabaseSingletonHelper, " + "ns." + p._databaseSubDirectory + "." + thedatabase + "Database >"))
                    {
                        // execute block
                        h.MessageVerbose("[{0}].[{1}]", csharpnamespace, csharpstoredproc);

                        using (StoredProcedureFactoryExecuteBlock mb = new StoredProcedureFactoryExecuteBlock(
                                   p,
                                   sw,
                                   tab,
                                   fqstoredprocedure,
                                   csharpstoredproc,
                                   parameters,
                                   sppi,
                                   rsi))
                        {}
                    }     // end class
                }         // end namespace
            }             // eof

            // write out the classes for stored proc result sets, if any
            int i = 0;

            foreach (var rs in rsi.Resultsets)
            {
                i++;
                string therecordsetclass   = csharpstoredproc + h.IdentifierSeparator + "rs" + i;
                string csharprecordsetfile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharpstoredproc + ".rs" + i + ".cs";

                h.MessageVerbose("[{0}]", csharprecordsetfile);
                using (StreamWriter sw = new StreamWriter(csharprecordsetfile, 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++, therecordsetclass, "acr.RowBase"))
                        {
                            h.MessageVerbose("[{0}].[{1}] row", csharpnamespace, therecordsetclass);

                            using (StoredProcedureRowConstructorBlock mb = new StoredProcedureRowConstructorBlock(sw, tab, therecordsetclass, rs.Columns))
                            {}
                        } // end class
                    }     // end namespace
                }         // eof
            }
        }                 // end do sp
Exemplo n.º 2
0
        protected void DoStoredProcs(Program p)
        {
            Helper h = new Helper();

            List <StoredProcGeneratorParameters> threadParamList = new List <StoredProcGeneratorParameters>();

            h.MessageVerbose("### Generating code gen stored procs ###");
            _storedProcsSubDirectory = _codegen.SelectSingleNode("/CodeGen/StoredProcs/@SubDirectory").Value;

            List <string> storedprocedures = p._di.StoredProcedures.Get();

            if (storedprocedures.Count >= 1)               // anything to do ?
            {
                // create a tt class and ttlist class for each table type

                foreach (var fqtabletype in p._di.TableTypes.Get())
                {
                    Tuple <string, string> schematabletype = h.SplitSchemaFromTable(fqtabletype);

                    string csharpnamespace     = p._namespace + "." + p._storedProcsSubDirectory;
                    string csharptabletype     = h.GetCsharpClassName(p._prefixObjectsWithSchema, schematabletype.Item1, schematabletype.Item2);
                    string thetabletypeclass   = csharptabletype + h.IdentifierSeparator + "tt";
                    string csharptabletypefile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharptabletype + ".tt.cs";

                    string thetabletypelistclass   = csharptabletype + h.IdentifierSeparator + "ttlist";
                    string csharptabletypelistfile = p._directory + @"\" + p._storedProcsSubDirectory + @"\" + csharptabletype + ".ttlist.cs";
                    string ttlistbaseclass         = "scg.List< " + thetabletypeclass + " >, scg.IEnumerable< mss.SqlDataRecord >";

                    List <Tuple <string, string, Int16, Byte, Byte> > columns = p._di.TableTypeColumns.Get(fqtabletype);

                    // tt file
                    h.MessageVerbose("[{0}]", csharptabletypefile);
                    using (StreamWriter sw = new StreamWriter(csharptabletypefile, 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++, thetabletypeclass, "acr.RowBase"))
                                using (StoredProcedureRowConstructorBlock mb = new StoredProcedureRowConstructorBlock(sw, tab, thetabletypeclass, columns, true))
                                {}
                    }

                    // tt list file
                    h.MessageVerbose("[{0}]", csharptabletypelistfile);
                    using (StreamWriter sw = new StreamWriter(csharptabletypelistfile, 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++, thetabletypelistclass, ttlistbaseclass))
                                using (StoredProcedureTableTypeEnumeratorBlock mb = new StoredProcedureTableTypeEnumeratorBlock(sw, tab, thetabletypeclass, columns))
                                {}
                    }
                }

                // write each stored procedure in a file

                using (MyThreadPoolManager tpm = new MyThreadPoolManager(p._threads, storedprocedures.Count))                     // max threads: _threads, queue length: no of sp's
                {
                    foreach (string storedprocedure in storedprocedures)
                    {
                        StoredProcGeneratorParameters tgp = new StoredProcGeneratorParameters();
                        threadParamList.Add(tgp);

                        tgp.p = p;
                        tgp.fqstoredprocedure = storedprocedure;

                        tpm.Queue(new StoredProcGeneratorThreadPoolItem(tgp));
                    }
                    tpm.WaitUntilAllStarted();
                    tpm.WaitUntilAllFinished();
                }
            }
            h.MessageVerbose("### Generating code gen stored procs - done ###");

            // handle any thread exceptions
            foreach (StoredProcGeneratorParameters tgp in threadParamList)
            {
                if (tgp.exception != null)
                {
                    throw new ApplicationException("DoStoredProcs() worker thread exception", tgp.exception);
                }
            }
        }