Esempio n. 1
0
        /// <summary>
        /// Gets the SP schema collection.
        /// </summary>
        /// <param name="providerName">Name of the provider.</param>
        /// <returns></returns>
        public static List <StoredProcedure> GetSPSchemaCollection(string providerName)
        {
            List <StoredProcedure> _sps = new List <StoredProcedure>();

            string[] sps = GetSPList(providerName, true);

            DataProvider provider = Providers[providerName];

            {
                Utility.WriteTrace(String.Format("Generating Stored Procedure collection for {0}", providerName));
                int generatedSprocs = 0;
                foreach (string s in sps)
                {
                    string spName       = s;
                    string spSchemaName = "";
                    int    i            = s.IndexOf('.');
                    if (i >= 0)
                    {
                        spName       = s.Substring(i + 1);
                        spSchemaName = s.Substring(0, i);
                    }
                    if (CodeService.ShouldGenerate(spName, provider.IncludeProcedures, provider.ExcludeProcedures, provider))
                    {
                        //declare the sp
                        StoredProcedure sp = new StoredProcedure(spName, provider, spSchemaName);

                        //get the params
                        using (IDataReader rdr = GetSPParams(spName, providerName))
                        {
                            while (rdr.Read())
                            {
                                StoredProcedure.Parameter par = new StoredProcedure.Parameter();

                                provider.SetParameter(rdr, par);
                                par.QueryParameter = provider.FormatParameterNameForSQL(par.Name);
                                par.DisplayName    = Utility.GetParameterName(par.Name, provider);
                                sp.Parameters.Add(par);
                            }
                            rdr.Close();
                        }
                        _sps.Add(sp);
                        generatedSprocs++;
                    }
                }
                Utility.WriteTrace(String.Format("Finished! {0} of {1} procedures generated.", generatedSprocs, sps.GetLength(0)));
            }
            return(_sps);
        }
Esempio n. 2
0
        /// <summary>
        /// Scripts the data.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="providerName">Name of the provider.</param>
        /// <returns></returns>
        public override string ScriptData(string tableName, string providerName)
        {
            StringBuilder result = new StringBuilder();

            if (CodeService.ShouldGenerate(tableName, providerName))
            {
                StringBuilder fieldList       = new StringBuilder();
                StringBuilder statements      = new StringBuilder();
                StringBuilder insertStatement = new StringBuilder();

                insertStatement.Append("INSERT INTO ");
                insertStatement.Append(tableName);
                insertStatement.Append(" ");

                //pull the schema for this table
                TableSchema.Table table = Query.BuildTableSchema(tableName, providerName);

                //build the insert list.
                bool isFirst = true;
                foreach (TableSchema.TableColumn col in table.Columns)
                {
                    if (!isFirst)
                    {
                        fieldList.Append(",");
                    }

                    isFirst = false;
                    fieldList.Append(col.ColumnName);
                }

                //complete the insert statement
                insertStatement.Append("(");
                insertStatement.Append(fieldList.ToString());
                insertStatement.AppendLine(")");

                //get the table data
                using (IDataReader rdr = new Query(table).ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        StringBuilder thisStatement = new StringBuilder();
                        thisStatement.Append(insertStatement);
                        thisStatement.Append("VALUES(");
                        //loop the schema and pull out the values from the reader
                        isFirst = true;
                        foreach (TableSchema.TableColumn col in table.Columns)
                        {
                            if (!isFirst)
                            {
                                thisStatement.Append(",");
                            }

                            isFirst = false;

                            if (Utility.IsNumeric(col))
                            {
                                thisStatement.Append(rdr[col.ColumnName]);
                            }
                            else
                            {
                                thisStatement.Append("'");
                                thisStatement.Append(rdr[col.ColumnName].ToString().Replace("'", "''"));
                            }
                        }
                        //add in a closing paren
                        thisStatement.AppendLine(")");
                        statements.Append(thisStatement.ToString());
                    }
                    rdr.Close();
                }

                result.Append("PRINT 'Begin inserting data in ");
                result.Append(tableName);
                result.AppendLine("'");

                result.Append(statements.ToString());
            }

            return(result.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.
        /// </summary>
        /// <param name="assemblyBuilder">The assembly builder that references the source code generated by the build provider.</param>
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            Utility.WriteTrace("Invoking BuildProvider");
            DataService.LoadProviders();
            ICodeLanguage language = new CSharpCodeLanguage();

            DirectoryInfo di = new DirectoryInfo(HostingEnvironment.ApplicationPhysicalPath + "\\App_Code");

            FileInfo[] fi = di.GetFiles("*.vb");
            if (fi.Length > 0)
            {
                language = new VBCodeLanguage();
            }

            StringBuilder code = new StringBuilder(language.DefaultUsingStatements);
            TurboCompiler tc   = new TurboCompiler();

            // loop over the providers, and generate code for each
            foreach (DataProvider provider in DataService.Providers)
            {
                Utility.WriteTrace(String.Format("Creating code for {0}", provider.Name));
                string[] tableList = DataService.GetTableNames(provider.Name);
                string[] viewList  = DataService.GetViewNames(provider.Name);

                foreach (string tbl in tableList)
                {
                    TurboTemplate tt = CodeService.BuildClassTemplate(tbl, language, provider);
                    tc.AddTemplate(tt);
                    if (provider.GenerateODSControllers)
                    {
                        TurboTemplate tODS = CodeService.BuildODSTemplate(tbl, language, provider);
                        tc.AddTemplate(tODS);
                    }
                }

                foreach (string view in viewList)
                {
                    TurboTemplate tt = CodeService.BuildViewTemplate(view, language, provider);
                    tc.AddTemplate(tt);
                }

                tc.AddTemplate(CodeService.BuildSPTemplate(language, provider));
            }

            if (DataService.Providers.Count > 0)
            {
                tc.AddTemplate(CodeService.BuildStructsTemplate(language, DataService.Provider));
            }

            foreach (TurboTemplate tt in tc.Templates)
            {
                tt.AddUsingBlock = false;
            }
            tc.Run();

            foreach (TurboTemplate tt in tc.Templates)
            {
                code.Append(tt.FinalCode);
            }

            assemblyBuilder.AddCodeCompileUnit(this, new CodeSnippetCompileUnit(code.ToString()));
        }
Esempio n. 4
0
        /// <summary>
        /// This is a copy and adaptation from the SQL Server provider and it
        /// needs more work as it does NOT support auto incrementing columns.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="providerName">Name of the provider.</param>
        /// <returns></returns>
        public override string ScriptData(string tableName, string providerName)
        {
            StringBuilder result = new StringBuilder();

            if (CodeService.ShouldGenerate(tableName, providerName))
            {
                StringBuilder fieldList       = new StringBuilder();
                StringBuilder insertStatement = new StringBuilder();
                StringBuilder statements      = new StringBuilder();

                insertStatement.Append("INSERT INTO [" + tableName + "] ");

                //pull the schema for this table
                TableSchema.Table table = Query.BuildTableSchema(tableName, providerName);

                //build the insert list.
                string lastColumnName = table.Columns[table.Columns.Count - 1].ColumnName;
                foreach (TableSchema.TableColumn col in table.Columns)
                {
                    fieldList.Append("[");
                    fieldList.Append(col.ColumnName);
                    fieldList.Append("]");

                    if (!Utility.IsMatch(col.ColumnName, lastColumnName))
                    {
                        fieldList.Append(", ");
                    }
                }

                //complete the insert statement
                insertStatement.Append("(");
                insertStatement.Append(fieldList);
                insertStatement.AppendLine(")");

                //get the table data
                IDataReader rdr = new Query(table).ExecuteReader();

                while (rdr.Read())
                {
                    StringBuilder thisStatement = new StringBuilder();
                    thisStatement.Append(insertStatement);
                    thisStatement.Append("VALUES(");
                    //loop the schema and pull out the values from the reader
                    foreach (TableSchema.TableColumn col in table.Columns)
                    {
                        if (!col.IsReadOnly)
                        {
                            object oData = rdr[col.ColumnName];
                            if (oData != null && oData != DBNull.Value)
                            {
                                if (col.DataType == DbType.Boolean)
                                {
                                    bool bData = Convert.ToBoolean(oData);
                                    thisStatement.Append(bData ? "1" : " 0");
                                }
                                else if (col.DataType == DbType.Byte)
                                {
                                    thisStatement.Append(oData);
                                }
                                else if (col.DataType == DbType.Binary)
                                {
                                    thisStatement.Append("0x");
                                    thisStatement.Append(Utility.ByteArrayToString((Byte[])oData).ToUpper());
                                }
                                else if (col.IsNumeric)
                                {
                                    thisStatement.Append(oData);
                                }
                                else if (col.IsDateTime)
                                {
                                    DateTime dt = DateTime.Parse(oData.ToString());
                                    thisStatement.Append("'");
                                    thisStatement.Append(dt.ToString("yyyy-MM-dd HH:mm:ss"));
                                    thisStatement.Append("'");
                                }
                                else
                                {
                                    thisStatement.Append("'");
                                    thisStatement.Append(oData.ToString().Replace("'", "''"));
                                    thisStatement.Append("'");
                                }
                            }
                            else
                            {
                                thisStatement.Append("NULL");
                            }

                            if (!Utility.IsMatch(col.ColumnName, lastColumnName))
                            {
                                thisStatement.Append(", ");
                            }
                        }
                    }

                    //add in a closing paren
                    thisStatement.AppendLine(");");
                    statements.Append(thisStatement);
                }
                rdr.Close();

                result.AppendLine("-- Begin inserting data in " + tableName);
                result.Append(statements);
            }
            return(result.ToString());
        }