public StringBuilder BuildClass(ClassParameters parameters)
 {
     return SimpleClassCreator.Generator.Execute(parameters);
 }
 public StringBuilder BuildGridViewColumns(ClassParameters parameters)
 {
     return SimpleClassCreator.Generator.GenerateGridViewColumns(parameters);
 }
        /// <summary>
        /// The main internal method that orchestrates the code generation for the provided parameters
        /// </summary>
        /// <returns>The generated class code as a StringBuilder</returns>
        private static StringBuilder GenerateClass(DotNetLanguage motif, ClassParameters parameters)
        {
            ClassParameters p = parameters;

            StringBuilder sb = new StringBuilder(),
                          sbColumns = new StringBuilder(),
                          sbProperties = new StringBuilder(),
                          sbObjectGen = new StringBuilder(),
                          sbUpdate = new StringBuilder(),
                          sbInsert = new StringBuilder();

            string tableName, primaryKey, sqlQuery;

            if (p.SourceType == SourceTypeEnum.TableName)
            {
                tableName = p.ClassSource;
                primaryKey = GetPrimaryKeyColumn(p.ConnectionString, tableName);
                sqlQuery = "SELECT TOP 1 * FROM " + p.ClassSource;
            }
            else
            {
                tableName = null;
                primaryKey = null;
                sqlQuery = p.ClassSource;
            }

            DataTable dt = GetSchema(p.ConnectionString, sqlQuery);

            DotNetLanguage.MemberInfo info = null;

            //Using Statements
            sb.Append(motif.Using).Append(" System").Append(motif.LineTerminator);
            sb.Append(motif.Using).Append(" System.Collections.Generic").Append(motif.LineTerminator);
            sb.Append(motif.Using).Append(" System.Data").Append(motif.LineTerminator);
            sb.Append(motif.Using).Append(" System.Text").Append(motif.LineTerminator);

            if (motif.IncludeWCFTags)
                sb.Append(motif.Using).Append(" System.Runtime.Serialization").Append(motif.LineTerminator);

            sb.Append(Environment.NewLine);

            //Open the Namespace
            if (parameters.IncludeNamespace)
                sb.Append(motif.OpenNamespace);

            //Add the [DataContract] attribute
            if (motif.IncludeWCFTags)
                sb.Append(motif.DataContract);

            //Open the Class
            sb.Append(motif.OpenClass);
            sb.Append(motif.EmptyConstructor);
            sb.Append(motif.CreateRegion("Properties"));

            //Data Collection and Property Generation
            foreach (DataColumn dc in dt.Columns)
            {
                info = new DotNetLanguage.MemberInfo(dc, p.LanguageType, p.MemberPrefix);

                //DataColumn as Property
                motif.CreateProperty(sbProperties, info);

                //Object Generation Code
                sbObjectGen.Append("obj.").Append(info.Property).Append(" = ").Append(info.ConvertTo).Append(motif.DataRowGet("dr", dc.ColumnName)).Append(")").Append(motif.LineTerminator);

                //I don't remember why I did any of this:
                if (tableName != null && info.ColumnName != primaryKey)
                {
                    //Column CSV
                    sbColumns.Append(dc.ColumnName).Append(", ");

                    //Update Statement Code
                    sbUpdate.Append("sb.Append(\"").Append(dc.ColumnName).Append("\").Append(\" = \").Append(").Append(info.StringValue).Append(").Append(\",\")").Append(motif.LineTerminator);

                    //Insert Statement Code
                    sbInsert.Append("sb.Append(").Append(info.StringValue).Append(").Append(\",\")").Append(motif.LineTerminator);
                }
            }

            //This section can only be performed if this is a single table that this code is being generated from.
            //Virtual Tables do not qualify because they won't have primary keys
            if (tableName != null)
            {
                //Trim the trailing patterns from these
                TrimEnd(sbColumns, "\",\" ");
                TrimEnd(sbUpdate, ".Append(\",\")" + motif.LineTerminator);
                TrimEnd(sbInsert, ".Append(\",\")" + motif.LineTerminator);
            }

            //Append the Class Private Members and Public Properties
            sb.Append(sbProperties);
            sb.Append(motif.EndRegion);

            //Object Generation Method
            motif.CreateObjectGenerationMethod(sb, sbObjectGen.ToString());

            if (tableName != null)
            {
                //Update Method
                if (dt.Columns[primaryKey] != null)
                    motif.CreateUpdateMethod(sb, sbUpdate.ToString(), new DotNetLanguage.MemberInfo(dt.Columns[primaryKey], p.LanguageType, p.MemberPrefix));

                //Insert Method
                motif.CreateInsertMethod(sb, sbColumns.ToString(), sbInsert.ToString());

                //AddString Methods
                motif.CreateAddStringMethods(sb);
            }

            sb.Append(motif.CloseClass);

            if (parameters.IncludeNamespace)
                sb.Append(motif.CloseNamespace);

            return sb;
        }
        /// <summary>
        /// Manufacture the physical C# or VB.Net code file
        /// </summary>
        /// <param name="fileName">The name of the file, including the file extension</param>
        /// <param name="sb">The StringBuilder that contains the generated code</param>
        private static void WriteClassToFile(ClassParameters p, StringBuilder sb)
        {
            string fullFilePath = Path.Combine(p.Filepath, p.Filename);

            using (StreamWriter sw = new StreamWriter(fullFilePath, false))
            {
                sw.Write(sb.ToString());
            }

            Console.WriteLine("{0} Characters Written to {1}", sb.Length, fullFilePath);
        }
        public static StringBuilder GenerateGridViewColumns(ClassParameters parameters)
        {
            StringBuilder sb = new StringBuilder();

            ClassParameters p = parameters;

            #region Make this reusable
            string tableName, primaryKey, sqlQuery;

            if (p.SourceType == SourceTypeEnum.TableName)
            {
                tableName = p.ClassSource;
                primaryKey = GetPrimaryKeyColumn(p.ConnectionString, tableName);
                sqlQuery = "SELECT TOP 1 * FROM " + p.ClassSource;
            }
            else
            {
                tableName = null;
                primaryKey = null;
                sqlQuery = p.ClassSource;
            }
            #endregion

            DataTable dt = GetSchema(p.ConnectionString, sqlQuery);

            foreach (DataColumn dc in dt.Columns)
            {
                sb.Append("<asp:BoundField HeaderText=\"")
                  .Append(dc.ColumnName)
                  .Append("\" DataField=\"")
                  .Append(dc.ColumnName)
                  .AppendLine("\">")
                  .AppendLine("<HeaderStyle HorizontalAlign=\"" + (IsNumber(dc.DataType) ? "Right" : "Left") + "\" />")
                  .AppendLine("</asp:BoundField>");
            }

            return sb;
        }
        public static StringBuilder Execute(ClassParameters parameters)
        {
            ClassParameters p = parameters;

            DotNetLanguage motif;

            if (p.LanguageType == CodeType.CSharp)
                motif = new CSharpLanguage(p.ClassName, p.IncludeWCFTags, p.BuildOutClassProperties);
            else
                motif = new VBDotNetLanguage(p.ClassName, p.IncludeWCFTags);

            if (parameters.IncludeNamespace)
                motif.NamespaceName = parameters.Namespace;

            motif.InitializeMotifValues();

            StringBuilder sb = GenerateClass(motif, p);

            if (p.SaveAsFile)
                WriteClassToFile(p, sb);

            return sb;
        }
示例#7
0
 public static StringBuilder BuildGridViewColumns(ClassParameters parameters)
 {
     return Client().BuildGridViewColumns(parameters);
 }
示例#8
0
 public static StringBuilder BuildClass(ClassParameters parameters)
 {
     return Client().BuildClass(parameters);
 }
        private ClassParameters CommonValidation()
        {
            ClassParameters obj = new ClassParameters();

            ConnectionManager.Connection con = CurrentConnection;

            if (!con.Verified && !TestConnectionString())
                return null;

            obj.ConnectionString = CurrentConnection.ConnectionString;

            obj.SourceType = GetSourceType();

            if (IsTextInvalid(txtSource, obj.SourceType.ToString() + " cannot be empty."))
                return null;

            obj.ClassSource = txtSource.Text;
            obj.SaveAsFile = cbSaveFileOnGeneration.IsChecked.Value;

            if (obj.SaveAsFile)
            {
                string s = "If saving file on generation, then {0} cannot be empty.";

                if (IsTextInvalid(txtPath, string.Format(s, "Path")))
                    return null;

                if (IsTextInvalid(txtFileName, string.Format(s, "File name")))
                    return null;
            }

            obj.Filepath = txtPath.Text;
            obj.Filename = txtFileName.Text;

            return obj;
        }