[Adapdev-commits] Adapdev/src/Adapdev.CodeGen AbstractCodeDomTemplate.cs,1.4,1.5 AbstractCodeTemplat
Status: Beta
Brought to you by:
intesar66
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.CodeGen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv909/src/Adapdev.CodeGen Added Files: AbstractCodeDomTemplate.cs AbstractCodeTemplate.cs AbstractConfig.cs AbstractSchemaConfig.cs Adapdev.CodeGen.csproj CodeGenerator.cs CodeType.cs CompilerFactory.cs DataSetHelper.cs ICodeDomTemplate.cs ICodeDomTemplateEventArgs.cs ICodeTemplate.cs ICodeTemplateEventArgs.cs INVelocityTemplate.cs LanguageType.cs NVelocityCodeTemplate.cs NVelocityTableCodeTemplate.cs TableCodeDomTemplate.cs TableCodeTemplate.cs UnitTestType.cs Log Message: --- NEW FILE: DataSetHelper.cs --- using System; using System.Data; using Adapdev.Data; using Adapdev.Data.Schema; namespace Adapdev.CodeGen { public class DataHelper { private DatabaseSchema schema = null; public DataHelper(DatabaseSchema schema) { this.schema = schema; } public DataTable GetDataTable(DataSet dataset, string dataTableName) { return dataset.Tables[dataTableName]; } public DataColumn GetDataColumn(DataTable table, string dataColumnName) { return table.Columns[dataColumnName]; } public object GetColumnValue(DataRow row, string columnName) { return row[columnName]; } public DataSet GetDataSet(string query) { try { IDbCommand command = DbProviderFactory.CreateCommand(schema.DatabaseProviderType); command.CommandText = query; return DbProviderFactory.CreateDataSet(schema.ConnectionString, command, schema.DatabaseProviderType); } catch(Exception e) { Console.WriteLine(e.Message); } return null; } } } --- NEW FILE: AbstractCodeDomTemplate.cs --- namespace Adapdev.CodeGen { using System; using System.CodeDom; /// <summary> /// Summary description for AbstractTemplate. /// </summary> public abstract class AbstractCodeDomTemplate : ICodeDomTemplate { protected string fileName = String.Empty; protected string className = String.Empty; protected string outputDir = String.Empty; protected string nameSpace = String.Empty; protected bool overwrite = true; /// <summary> /// Creates a new <see cref="AbstractCodeDomTemplate"/> instance. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="className">Name of the class.</param> /// <param name="nameSpace">Namespace.</param> public AbstractCodeDomTemplate(string fileName, string className, string nameSpace) { this.fileName = fileName; this.className = className; this.nameSpace = nameSpace; } /// <summary> /// Gets or sets the name of the file. /// </summary> /// <value></value> public string FileName { get { return this.fileName; } set { this.fileName = value; } } /// <summary> /// Gets or sets the name of the class. /// </summary> /// <value></value> public string ClassName { get { return this.className; } set { this.className = value; } } /// <summary> /// Gets or sets the output directory. /// </summary> /// <value></value> public virtual string OutputDirectory { get { return this.outputDir; } set { this.outputDir = value; } } /// <summary> /// Gets or sets the namespace. /// </summary> /// <value></value> public virtual string Namespace { get { return this.nameSpace; } set { this.nameSpace = value; } } /// <summary> /// Gets or sets a value indicating whether generated file can be overwritten /// </summary> /// <value> /// <c>true</c> if it can be overwritten; otherwise, <c>false</c>. /// </value> public virtual bool Overwrite { get { return this.overwrite; } set { this.overwrite = value; } } /// <summary> /// Processes the custom code. /// </summary> public virtual void ProcessCustomCode() { } /// <summary> /// Gets the code compile unit. /// </summary> /// <returns></returns> public abstract CodeCompileUnit GetCodeCompileUnit(); } } --- NEW FILE: INVelocityTemplate.cs --- using System; using NVelocity; namespace Adapdev.CodeGen { /// <summary> /// Summary description for INVelocityTemplate. /// </summary> public interface INVelocityTemplate { /// <summary> /// Gets the context. /// </summary> /// <value></value> VelocityContext Context{get;} } } --- NEW FILE: ICodeTemplate.cs --- namespace Adapdev.CodeGen { /// <summary> /// Summary description for ICodeTemplate. /// </summary> public interface ICodeTemplate { /// <summary> /// Gets the code. /// </summary> /// <returns></returns> string GetCode(); /// <summary> /// Gets or sets the name of the file. /// </summary> /// <value></value> string FileName { get; set; } /// <summary> /// Gets or sets the name of the class. /// </summary> /// <value></value> string ClassName { get; set; } /// <summary> /// Gets or sets the namespace. /// </summary> /// <value></value> string Namespace { get; set; } /// <summary> /// Gets or sets the file extension. /// </summary> /// <value></value> string FileExtension { get; set; } /// <summary> /// Gets or sets the output directory. /// </summary> /// <value></value> string OutputDirectory { get; set; } /// <summary> /// Gets or sets a value indicating whether the generated file can be overwritten /// </summary> /// <value> /// <c>true</c> if overwrite; otherwise, <c>false</c>. /// </value> bool Overwrite { get; set; } /// <summary> /// Processes the custom code. /// </summary> void ProcessCustomCode(); } } --- NEW FILE: AbstractCodeTemplate.cs --- namespace Adapdev.CodeGen { using System; /// <summary> /// Summary description for AbstractTemplate. /// </summary> public abstract class AbstractCodeTemplate : ICodeTemplate { protected string fileName = String.Empty; protected string className = String.Empty; protected string fileExtension = String.Empty; protected string outputDir = String.Empty; protected string nameSpace = String.Empty; protected bool overwrite = true; /// <summary> /// Creates a new <see cref="AbstractCodeTemplate"/> instance. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> /// <param name="className">Name of the class.</param> /// <param name="nameSpace">Namespace.</param> public AbstractCodeTemplate(string fileName, string fileExtension, string className, string nameSpace) { this.fileName = fileName; this.fileExtension = fileExtension; this.className = className; this.nameSpace = nameSpace; } /// <summary> /// Gets or sets the name of the file. /// </summary> /// <value></value> public string FileName { get { return this.fileName; } set { this.fileName = value; } } /// <summary> /// Gets or sets the file extension. /// </summary> /// <value></value> public string FileExtension { get { return this.fileExtension; } set { this.fileExtension = value; } } /// <summary> /// Gets or sets the name of the class. /// </summary> /// <value></value> public string ClassName { get { return this.className; } set { this.className = value; } } /// <summary> /// Gets or sets the output directory. /// </summary> /// <value></value> public virtual string OutputDirectory { get { return this.outputDir; } set { this.outputDir = value; } } /// <summary> /// Gets or sets the namespace. /// </summary> /// <value></value> public virtual string Namespace { get { return this.nameSpace; } set { this.nameSpace = value; } } /// <summary> /// Gets or sets a value indicating whether the generated file can be overwritten /// </summary> /// <value> /// <c>true</c> if it can be overwritten; otherwise, <c>false</c>. /// </value> public virtual bool Overwrite { get { return this.overwrite; } set { this.overwrite = value; } } /// <summary> /// Processes the custom code. /// </summary> public virtual void ProcessCustomCode() { } /// <summary> /// Gets the code. /// </summary> /// <returns></returns> public abstract string GetCode(); } } --- NEW FILE: CodeGenerator.cs --- namespace Adapdev.CodeGen { using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections; using System.IO; public delegate void ICodeTemplateProcessedEventHandler(object sender, ICodeTemplateEventArgs e); public delegate void ICodeDomTemplateProcessedEventHandler(object sender, ICodeDomTemplateEventArgs e); /// <summary> /// Summary description for CodeGenerator. /// </summary> public class CodeGenerator { public event ICodeTemplateProcessedEventHandler ICodeTemplateProcessed; public event ICodeDomTemplateProcessedEventHandler ICodeDomTemplateProcessed; protected string outputDir = String.Empty; /// <summary> /// Creates a new <see cref="CodeGenerator"/> instance. /// </summary> /// <param name="outputDir">Output dir.</param> public CodeGenerator(string outputDir) { this.outputDir = outputDir; } /// <summary> /// Gets or sets the output dir. /// </summary> /// <value></value> public string OutputDir { get { return this.outputDir; } set { this.outputDir = value; } } /// <summary> /// Processes the <see cref="ICodeDomTemplate"/> to file. /// </summary> /// <param name="template">Template.</param> /// <param name="codeType">Code type.</param> /// <param name="refAssemblies">Reference assemblies.</param> public void ProcessICodeDomTemplateToFile(ICodeDomTemplate template, LanguageType codeType, params string[] refAssemblies) { string location = Path.Combine(this.outputDir, template.OutputDirectory); location = Path.Combine(location, codeType.ToString()); CodeCompileUnit ccu = template.GetCodeCompileUnit(); foreach(string s in refAssemblies) { ccu.ReferencedAssemblies.Add(s); } if (ccu != null) { // Obtains an ICodeGenerator from a CodeDomProvider class. CodeDomProvider provider = CodeProviderFactory.GetCodeProvider(codeType); ICodeGenerator gen = provider.CreateGenerator(); // If the directory doesn't exist, create it if (!Directory.Exists(location)) { Directory.CreateDirectory(location); } // Build path and filename string filePath = Path.Combine(location, template.FileName + "." + provider.FileExtension); if (File.Exists(filePath) && template.Overwrite) // If the filepath exists and it can be overwritten { // Creates a StreamWriter to an output file. StreamWriter sw = new StreamWriter(filePath, false); // Generates source code using the code generator. gen.GenerateCodeFromCompileUnit(ccu, sw, new CodeGeneratorOptions()); // Closes the output files. sw.Close(); } else if (!File.Exists(filePath)) // If the file path doesn't exist { // Creates a StreamWriter to an output file. StreamWriter sw = new StreamWriter(filePath, false); // Generates source code using the code generator. gen.GenerateCodeFromCompileUnit(ccu, sw, new CodeGeneratorOptions()); // Closes the output files. sw.Close(); } // Process any custom code template.ProcessCustomCode(); } this.OnICodeDomTemplateProcessed(new ICodeDomTemplateEventArgs(template)); } /// <summary> /// Processes the <see cref="ICodeTemplate"/> to a file. /// </summary> /// <param name="template">Template.</param> public void ProcessICodeTemplateToFile(ICodeTemplate template) { // Get the code string code = template.GetCode(); // Make sure it's not null or empty if (template != null && !code.Equals(String.Empty)) { // Build the output path, and create it if it doesn't exist string output = Path.Combine(this.outputDir, template.OutputDirectory); if (!Directory.Exists(output)) { Directory.CreateDirectory(output); } // Build the file path string filePath = Path.Combine(output, template.FileName + "." + template.FileExtension); if (File.Exists(filePath) && template.Overwrite) // If it exists and can be overwritten { // Creates a StreamWriter to an output file. StreamWriter sw = new StreamWriter(filePath, false); // Generates source code using the code generator. sw.Write(code); // Closes the output files. sw.Close(); } else if (!File.Exists(filePath)) // The file doesn't exist { // Creates a StreamWriter to an output file. StreamWriter sw = new StreamWriter(filePath, false); // Generates source code using the code generator. sw.Write(code); // Closes the output files. sw.Close(); } template.ProcessCustomCode(); } this.OnICodeTemplateProcessed(new ICodeTemplateEventArgs(template)); } /// <summary> /// Processes the <see cref="ICodeDomTemplate"/>s to files. /// </summary> /// <param name="templates">Templates.</param> /// <param name="codeType">Code type.</param> public void ProcessICodeDomTemplatesToFiles(ICollection templates, LanguageType codeType) { foreach (ICodeDomTemplate template in templates) { try { this.ProcessICodeDomTemplateToFile(template, codeType); } catch(Exception ex) { throw ex; } } } /// <summary> /// Processes the <see cref="ICodeTemplate"/>s to files. /// </summary> /// <param name="templates">Templates.</param> public void ProcessICodeDomTemplatesToFiles(ICollection templates) { foreach (ICodeDomTemplate template in templates) { this.ProcessICodeDomTemplateToFile(template, LanguageType.CPP); this.ProcessICodeDomTemplateToFile(template, LanguageType.CSHARP); this.ProcessICodeDomTemplateToFile(template, LanguageType.JSCRIPT); this.ProcessICodeDomTemplateToFile(template, LanguageType.JSHARP); this.ProcessICodeDomTemplateToFile(template, LanguageType.VBNET); } } public void ProcessICodeTemplatesToFiles(ICollection templates) { foreach (ICodeTemplate template in templates) { this.ProcessICodeTemplateToFile(template); } } /// <summary> /// Processes the templates to an assembly. /// </summary> /// <param name="templates">Templates.</param> /// <param name="codeType">Code type.</param> /// <param name="assembly">Assembly.</param> /// <param name="cparams">Cparams.</param> public void ProcessTemplatesToAssembly(ICollection templates, LanguageType codeType, string assembly, CompilerParameters cparams) { // Create compiler for the specified CodeDom CodeDomProvider provider = CodeProviderFactory.GetCodeProvider(codeType); ICodeCompiler gen = provider.CreateCompiler(); ArrayList files = new ArrayList(); // Generate the code foreach (ICodeDomTemplate template in templates) { this.ProcessICodeDomTemplateToFile(template, codeType); files.Add(template.FileName + "." + provider.FileExtension); } // Compile the code gen.CompileAssemblyFromFileBatch(cparams, (string[]) files.ToArray(typeof (string))); } /// <summary> /// Fires when the <see cref="ICodeTemplate"/> is processed /// </summary> /// <param name="e">E.</param> public void OnICodeTemplateProcessed(ICodeTemplateEventArgs e) { if (ICodeTemplateProcessed != null) { //Invokes the delegates. ICodeTemplateProcessed(this, e); } } /// <summary> /// Fires when the <see cref="ICodeDomTemplate"/> is processed /// </summary> /// <param name="e">E.</param> public void OnICodeDomTemplateProcessed(ICodeDomTemplateEventArgs e) { if (ICodeDomTemplateProcessed != null) { //Invokes the delegates. ICodeDomTemplateProcessed(this, e); } } // public void ProcessTemplateAssemblyToFiles(Assembly a, LanguageType codeType){} // public void ProcessTemplateAssemblyToAssembly(string assemblyToRead, string assemblyToWrite, LanguageType codeType, CompilerParameters cparams){} // // public void ProcessCodeCompileUnitToFile(CodeCompileUnit ccu, string fileName, LanguageType codeType){} // public void ProcessCodeCompileUnitsToFiles(ICollection ccus, string[] fileNames, LanguageType codeType){} // public void ProcessCodeCompileUnitsToAssembly(ICollection ccus, LanguageType codeType, string assembly, CompilerParameters cparams){} // public void ProcessCodeCompileUnitAssemblyToFiles(Assembly a, LanguageType codeType){} // public void ProcessCodeCompileUnitAssemblyToAssembly(string assemblyToRead, string assemblyToWrite, LanguageType codeType, CompilerParameters cparams){} } } --- NEW FILE: ICodeDomTemplateEventArgs.cs --- namespace Adapdev.CodeGen { using System; /// <summary> /// Summary description for ICodeDomTemplateEventArgs. /// </summary> public class ICodeDomTemplateEventArgs : EventArgs { private ICodeDomTemplate _template = null; /// <summary> /// Creates a new <see cref="ICodeDomTemplateEventArgs"/> instance. /// </summary> /// <param name="template">Template.</param> public ICodeDomTemplateEventArgs(ICodeDomTemplate template) { this._template = template; } /// <summary> /// Gets the <see cref="ICodeDomTemplate"/> /// </summary> /// <value></value> public ICodeDomTemplate ICodeDomTemplate { get { return this._template; } } } } --- NEW FILE: ICodeDomTemplate.cs --- namespace Adapdev.CodeGen { using System.CodeDom; /// <summary> /// Summary description for ICodeDomTemplate. /// </summary> public interface ICodeDomTemplate { /// <summary> /// Gets the code compile unit. /// </summary> /// <returns></returns> CodeCompileUnit GetCodeCompileUnit(); /// <summary> /// Gets or sets the name of the file. /// </summary> /// <value></value> string FileName { get; set; } /// <summary> /// Gets or sets the name of the class. /// </summary> /// <value></value> string ClassName { get; set; } /// <summary> /// Gets or sets the namespace. /// </summary> /// <value></value> string Namespace { get; set; } /// <summary> /// Gets or sets the output directory. /// </summary> /// <value></value> string OutputDirectory { get; set; } /// <summary> /// Gets or sets a value indicating whether the generated file can be overwritten /// </summary> /// <value> /// <c>true</c> if overwrite; otherwise, <c>false</c>. /// </value> bool Overwrite { get; set; } /// <summary> /// Processes the custom code. /// </summary> void ProcessCustomCode(); } } --- NEW FILE: NVelocityCodeTemplate.cs --- namespace Adapdev.CodeGen { using System; using System.IO; using NVelocity; using NVelocity.App; /// <summary> /// Summary description for NVelocityCodeTemplate. /// </summary> public class NVelocityCodeTemplate : AbstractCodeTemplate, INVelocityTemplate { protected string templateFile = String.Empty; protected VelocityContext context = null; /// <summary> /// Creates a new <see cref="NVelocityCodeTemplate"/> instance. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> /// <param name="overWrite">Over write.</param> /// <param name="templateFile">Template file.</param> /// <param name="outputDirectory">Output directory.</param> /// <param name="className">Name of the class.</param> /// <param name="nameSpace">Namespace.</param> public NVelocityCodeTemplate(string fileName, string fileExtension, bool overWrite, string templateFile, string outputDirectory, string className, string nameSpace) : base(fileName, fileExtension, className, nameSpace) { this.templateFile = templateFile; this.outputDir = outputDirectory; this.overwrite = overWrite; if (!File.Exists("nvelocity.properties")) File.Copy(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "nvelocity.properties"), Path.Combine(".", "nvelocity.properties"), true); if (!File.Exists("directive.properties")) File.Copy(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "directive.properties"), Path.Combine(".", "directive.properties"), true); Velocity.Init("nvelocity.properties"); context = new VelocityContext(); } /// <summary> /// Creates a new <see cref="NVelocityCodeTemplate"/> instance. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> /// <param name="templateFile">Template file.</param> /// <param name="outputDirectory">Output directory.</param> public NVelocityCodeTemplate(string fileName, string fileExtension, string templateFile, string outputDirectory) : this(fileName, fileExtension, true, templateFile, outputDirectory, String.Empty, String.Empty) { } /// <summary> /// Gets the code. /// </summary> /// <returns></returns> public override string GetCode() { if(!context.ContainsKey("namespace")) context.Put("namespace", this.nameSpace); if(!context.ContainsKey("classname")) context.Put("classname", this.className); if(!context.ContainsKey("filename")) context.Put("filename", this.fileName); context.Put("datetimenow", DateTime.Now); context.Put("template", this); StringWriter writer = new StringWriter(); Velocity.MergeTemplate(this.templateFile, context, writer); return writer.GetStringBuilder().ToString(); } /// <summary> /// Gets or sets the template file. /// </summary> /// <value></value> public string TemplateFile { get { return this.templateFile; } set { this.templateFile = value; } } public override bool Overwrite { get { return this.overwrite; } set { base.Overwrite = value; } } /// <summary> /// Gets the context. /// </summary> /// <value></value> public VelocityContext Context { get { return this.context; } } public string GetConversionExpression(string objectType) { string subt = objectType.Substring(objectType.IndexOf(".") + 1); return "Convert.To"+subt; } } } --- NEW FILE: ICodeTemplateEventArgs.cs --- namespace Adapdev.CodeGen { using System; /// <summary> /// Summary description for ICodeTemplateEventArgs. /// </summary> public class ICodeTemplateEventArgs : EventArgs { private ICodeTemplate _template = null; /// <summary> /// Creates a new <see cref="ICodeTemplateEventArgs"/> instance. /// </summary> /// <param name="template">Template.</param> public ICodeTemplateEventArgs(ICodeTemplate template) { this._template = template; } /// <summary> /// Gets the I code template. /// </summary> /// <value></value> public ICodeTemplate ICodeTemplate { get { return this._template; } } } } --- NEW FILE: LanguageType.cs --- namespace Adapdev.CodeGen { using System; /// <summary> /// Summary description for LanguageType. /// </summary> /// [Flags] public enum LanguageType { CSHARP = 1, JSHARP = 2, JSCRIPT = 4, VBNET = 8, CPP = 16, SQL = 32, OTHER = 64, JAVA = 128 } } --- NEW FILE: TableCodeDomTemplate.cs --- namespace Adapdev.CodeGen { using Adapdev.Data.Schema; public abstract class TableCodeDomTemplate : AbstractCodeDomTemplate { protected TableSchema ti; /// <summary> /// Creates a new <see cref="TableCodeDomTemplate"/> instance. /// </summary> /// <param name="ti">TableSchema.</param> /// <param name="nameSpace">Namespace.</param> /// <param name="fileName">Name of the file.</param> /// <param name="className">Name of the class.</param> public TableCodeDomTemplate(TableSchema ti, string nameSpace, string fileName, string className) : base(fileName, className, nameSpace) { this.ti = ti; } } } --- NEW FILE: Adapdev.CodeGen.csproj --- <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{2D8FC662-0244-49F1-8017-DFE73B191017}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "Adapdev.CodeGen" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "Adapdev.CodeGen" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "bin\Debug\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> <Reference Name = "Adapdev" Project = "{CC30A321-2569-4B1F-8E1A-781B5509B56D}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "Adapdev.Data" Project = "{08C5794D-44ED-4E75-A1C1-48A28C3D0044}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "Adapdev.NVelocity" Project = "{75D57D5C-250A-447C-80BC-2FF9DC8A14D2}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "MCppCodeDomProvider" AssemblyName = "MCppCodeDomProvider" HintPath = "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\PublicAssemblies\MCppCodeDomProvider.dll" AssemblyFolderKey = "hklm\publicassemblies" /> <Reference Name = "Microsoft.JScript" AssemblyName = "Microsoft.JScript" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.JScript.dll" /> <Reference Name = "Microsoft.VisualBasic" AssemblyName = "Microsoft.VisualBasic" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll" /> <Reference Name = "VJSharpCodeProvider" AssemblyName = "VJSharpCodeProvider" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\VJSharpCodeProvider.DLL" /> <Reference Name = "log4net" AssemblyName = "log4net" HintPath = "..\..\lib\log4net.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AbstractCodeDomTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AbstractCodeTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AbstractConfig.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AbstractSchemaConfig.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AdapdevAssemblyInfo.cs" Link = "..\AdapdevAssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "CodeGenerator.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "CodeType.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "CompilerFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "DataSetHelper.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ICodeDomTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ICodeDomTemplateEventArgs.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ICodeTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ICodeTemplateEventArgs.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "INVelocityTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "LanguageType.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NVelocityCodeTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NVelocityTableCodeTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TableCodeDomTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TableCodeTemplate.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "UnitTestType.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> --- NEW FILE: CodeType.cs --- using System; namespace Adapdev.Data.CodeGen { /// <summary> /// Summary description for CodeType. /// </summary> /// [FlagsAttribute] public enum CodeType { CSHARP, JSHARP, JSCRIPT, VBNET, CPP } } --- NEW FILE: AbstractConfig.cs --- namespace Adapdev.CodeGen { using System; using System.Collections; using System.IO; public delegate void ProcessCustomCodeHandler(string info); /// <summary> /// Summary description for AbstractConfig. /// </summary> public abstract class AbstractConfig { protected Hashtable properties = null; protected string nameSpace = String.Empty; protected ArrayList languages = new ArrayList(); protected string appdir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; protected string shareddir = String.Empty; protected string outputdir = String.Empty; /// <summary> /// Creates a new <see cref="AbstractConfig"/> instance. /// </summary> /// <param name="nameSpace">Namespace.</param> public AbstractConfig(string nameSpace) { this.nameSpace = nameSpace; shareddir = Path.Combine(this.appdir, "shared"); } /// <summary> /// Gets the base output directory for this template library /// </summary> /// <value></value> public virtual string PackageDirectory { get { return this.PackageName; } } /// <summary> /// Gets or sets the output directory. /// </summary> /// <value></value> public virtual string OutputDirectory { get { return this.outputdir; } set { this.outputdir = value; } } /// <summary> /// Gets the application directory. /// </summary> /// <value></value> public string ApplicationDirectory { get { return this.appdir; } } /// <summary> /// Gets or sets the shared directory. /// </summary> /// <value></value> public string SharedDirectory { get { return this.shareddir; } set { this.shareddir = value; } } /// <summary> /// Gets or sets the base namespace for this template library /// </summary> /// <value></value> public string NameSpace { get { return this.nameSpace; } set { this.nameSpace = value; } } /// <summary> /// Gets or sets the custom properties. /// </summary> /// <value></value> public virtual Hashtable CustomProperties { get { return this.properties; } set { this.properties = value; } } /// <summary> /// Processes the custom code. /// </summary> public virtual void ProcessCustomCode() { } public event ProcessCustomCodeHandler Processing; public void OnProcessing(string info) { if(Processing != null) { Processing(info); } } /// <summary> /// Gets the code DOM templates. /// </summary> /// <returns>A list of ICodeDomTemplates to process</returns> public abstract ArrayList GetCodeDomTemplates(); /// <summary> /// Gets the code templates. /// </summary> /// <returns>A list of ICodeTemplates to process</returns> public abstract ArrayList GetCodeTemplates(); // The name of the package. /// <summary> /// Gets the name of the package. /// </summary> /// <value></value> public abstract string PackageName { get; } /// <summary> /// Gets the package description. /// </summary> /// <value></value> public abstract string PackageDescription { get; } /// <summary> /// Gets the author. /// </summary> /// <value></value> public abstract string Author { get; } /// <summary> /// Gets the version. /// </summary> /// <value></value> public abstract string Version { get; } /// <summary> /// Gets the copyright. /// </summary> /// <value></value> public abstract string Copyright { get; } } } --- NEW FILE: AbstractSchemaConfig.cs --- namespace Adapdev.CodeGen { using System; using Adapdev.Data.Schema; /// <summary> /// Summary description for AbstractConfig. /// </summary> public abstract class AbstractSchemaConfig : AbstractConfig { protected string connectionString = String.Empty; protected DatabaseSchema databaseSchema = new DatabaseSchema(); /// <summary> /// Creates a new <see cref="AbstractSchemaConfig"/> instance. /// </summary> /// <param name="connectionString">Connection string.</param> /// <param name="nameSpace">Namespace.</param> /// <param name="databaseSchema">Database schema.</param> public AbstractSchemaConfig(string connectionString, string nameSpace, DatabaseSchema databaseSchema) : base(nameSpace) { this.connectionString = connectionString; this.databaseSchema = databaseSchema; } /// <summary> /// Gets or sets the connection string. /// </summary> /// <value></value> public string ConnectionString { get { return this.connectionString; } set { this.connectionString = value; } } /// <summary> /// Gets or sets the <see cref="DatabaseSchema"/> /// </summary> /// <value></value> public DatabaseSchema DatabaseSchema { get { return this.databaseSchema; } set { this.databaseSchema = value; } } } } --- NEW FILE: NVelocityTableCodeTemplate.cs --- namespace Adapdev.CodeGen { using System; using System.Collections; using System.IO; using System.Text; using Adapdev.Data; using Adapdev.Data.Schema; using Adapdev.Data.Sql; using NVelocity; using NVelocity.App; /// <summary> /// Summary description for NVelocityTableCodeTemplate. /// </summary> public class NVelocityTableCodeTemplate : TableCodeTemplate, INVelocityTemplate { protected DatabaseSchema di = null; protected string templateFile = String.Empty; protected VelocityContext context = null; /// <summary> /// Creates a new <see cref="NVelocityTableCodeTemplate"/> instance. /// </summary> /// <param name="di">Database Schema.</param> /// <param name="ti">Table Schema.</param> /// <param name="outputDirectory">Output directory.</param> /// <param name="templateFile">Template file.</param> /// <param name="nameSpace">Namespace.</param> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> /// <param name="className">Name of the class.</param> /// <param name="overWrite">Over write.</param> public NVelocityTableCodeTemplate(DatabaseSchema di, TableSchema ti, string outputDirectory, string templateFile, string nameSpace, string fileName, string fileExtension, string className, bool overWrite) : base(ti, nameSpace, fileName, fileExtension, className) { this.di = di; this.templateFile = templateFile; this.outputDir = outputDirectory; this.overwrite = overWrite; if (!File.Exists("nvelocity.properties")) File.Copy(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "nvelocity.properties"), Path.Combine(".", "nvelocity.properties"), true); if (!File.Exists("directive.properties")) File.Copy(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "directive.properties"), Path.Combine(".", "directive.properties"), true); Velocity.Init("nvelocity.properties"); context = new VelocityContext(); } /// <summary> /// Creates a new <see cref="NVelocityTableCodeTemplate"/> instance. /// </summary> /// <param name="di">Database Schema.</param> /// <param name="ti">Table Schema.</param> /// <param name="outputDirectory">Output directory.</param> /// <param name="templateFile">Template file.</param> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> public NVelocityTableCodeTemplate(DatabaseSchema di, TableSchema ti, string outputDirectory, string templateFile, string fileName, string fileExtension) : this(di, ti, outputDirectory, templateFile, String.Empty, fileName, fileExtension, String.Empty, true) { } /// <summary> /// Gets the code. /// </summary> /// <returns></returns> public override string GetCode() { context.Put("databaseschema", this.di); context.Put("tableschema", this.ti); if(!context.ContainsKey("namespace")) context.Put("namespace", this.nameSpace); if(!context.ContainsKey("classname")) context.Put("classname", this.className); if(!context.ContainsKey("filename")) context.Put("filename", this.fileName); context.Put("datetimenow", DateTime.Now); context.Put("datahelper", new DataHelper(di)); context.Put("providerinfo", ProviderInfoManager.GetInstance()); context.Put("template", this); context.Put("queryhelper", new QueryHelper()); context.Put("mysqlprovidertype", Adapdev.Data.DbProviderType.MYSQL); StringWriter writer = new StringWriter(); Velocity.MergeTemplate(this.templateFile, context, writer); return writer.GetStringBuilder().ToString(); } /// <summary> /// Gets the Sql to select one record by the primary key /// </summary> /// <returns></returns> public string GetSelectOneSql() { return this.GetSelectOneSql(this.ti); } /// <summary> /// Gets the Sql to select one record by the primary key /// </summary> /// <param name="ts">Ts.</param> /// <returns></returns> public string GetSelectOneSql(TableSchema ts) { if(ts.HasPrimaryKeys) { ISelectQuery query = QueryFactory.CreateSelectQuery(di.DatabaseType, di.DatabaseProviderType); query.SetTable(ts.Name); foreach (ColumnSchema ci in ts.OrdinalColumns.Values) { if(ci.IsActive)query.Add(ci.Name); } ArrayList pks = new ArrayList(ts.PrimaryKeys.Values); ColumnSchema pk = (ColumnSchema) pks[0]; ICriteria criteria = query.CreateCriteria(); criteria.DbProviderType = this.di.DatabaseProviderType; criteria.AddEqualTo(pk.Name); query.SetCriteria(criteria); return query.GetText(); } return ""; } /// <summary> /// Gets the insert SQL. /// </summary> /// <returns></returns> public string GetInsertSql() { return this.GetInsertSql(this.ti); } /// <summary> /// Gets the insert SQL. /// </summary> /// <param name="ts">Ts.</param> /// <returns></returns> public string GetInsertSql(TableSchema ts) { IInsertQuery query = QueryFactory.CreateInsertQuery(di.DatabaseType, di.DatabaseProviderType); query.SetTable(ts.Name); foreach (ColumnSchema ci in ts.SortedColumns.Values) { if (!ci.IsAutoIncrement && ci.IsActive) query.Add(ci.Name); } return query.GetText(); } /// <summary> /// Gets the update SQL. /// </summary> /// <returns></returns> public string GetUpdateSql() { return this.GetUpdateSql(this.ti); } /// <summary> /// Gets the update SQL. /// </summary> /// <returns></returns> public string GetUpdateSql(TableSchema ts) { IUpdateQuery query = QueryFactory.CreateUpdateQuery(di.DatabaseType, di.DatabaseProviderType); query.SetTable(ts.Name); foreach (ColumnSchema ci in ts.SortedColumns.Values) { if (!ci.IsAutoIncrement && ci.IsActive) query.Add(ci.Name); } ArrayList pks = new ArrayList(ts.PrimaryKeys.Values); ICriteria criteria = query.CreateCriteria(); criteria.DbProviderType = this.di.DatabaseProviderType; int i = 1; foreach(ColumnSchema pk in pks) { criteria.AddEqualTo(pk.Name); if(i < pks.Count) criteria.AddAnd(); i++; } query.SetCriteria(criteria); return query.GetText(); } /// <summary> /// Gets the delete one SQL. /// </summary> /// <returns></returns> public string GetDeleteOneSql() { return this.GetDeleteOneSql(this.ti); } /// <summary> /// Gets the sql to delete one record by primary key /// </summary> /// <returns></returns> public string GetDeleteOneSql(TableSchema ts) { if(ts.HasPrimaryKeys) { IDeleteQuery query = QueryFactory.CreateDeleteQuery(di.DatabaseType, di.DatabaseProviderType); query.SetTable(ts.Name); ArrayList pks = new ArrayList(ts.PrimaryKeys.Values); ColumnSchema pk = (ColumnSchema) pks[0]; ICriteria criteria = query.CreateCriteria(); criteria.DbProviderType = this.di.DatabaseProviderType; criteria.AddEqualTo(pk.Name); query.SetCriteria(criteria); return query.GetText(); } return ""; } /// <summary> /// Gets the foreign key SQL. /// </summary> /// <param name="ci">Ci.</param> /// <returns></returns> public string GetForeignKeySql(ColumnSchema ci) { return this.GetForeignKeySql(this.ti, ci); } /// <summary> /// Gets the foreign key SQL. /// </summary> /// <param name="ts">Ts.</param> /// <param name="columnSchema">Column schema.</param> /// <returns></returns> public string GetForeignKeySql(TableSchema ts, ColumnSchema columnSchema) { ISelectQuery query = QueryFactory.CreateSelectQuery(di.DatabaseType); query.SetTable(ts.Name); foreach (ColumnSchema ci2 in ts.OrdinalColumns.Values) { if(ci2.IsActive)query.Add(ci2.Name); } ICriteria criteria = query.CreateCriteria(); criteria.DbProviderType = this.di.DatabaseProviderType; criteria.AddEqualTo(columnSchema.Name); query.SetCriteria(criteria); return query.GetText(); } /// <summary> /// Gets the name of the parameter. /// </summary> /// <param name="name">Name.</param> /// <returns></returns> public string GetParameterName(string name) { return QueryHelper.GetParameterName(name, this.di.DatabaseProviderType); } /// <summary> /// Gets the parameter value. /// </summary> /// <param name="c">C.</param> /// <returns></returns> public string GetParameterValue(DatabaseSchema d, ColumnSchema c) { if (c.NetType.Equals("System.DateTime")) { if(d.DatabaseType == Adapdev.Data.DbType.SQLSERVER) return c.Alias + ".Subtract(new TimeSpan(2,0,0,0)).ToOADate()"; else if(d.DatabaseType == Adapdev.Data.DbType.ORACLE) return c.Alias; else if (d.DatabaseType == Adapdev.Data.DbType.MYSQL) return c.Alias; else return c.Alias + ".ToOADate()"; } else return c.Alias; } public string GetParameterLength(ColumnSchema c) { switch(c.DataType.ToLower()) { case "binary": case "char": case "nchar": case "nvarchar": case "varbinary": case "varchar": return "(" + c.Length + ")"; } return String.Empty; } public string GetParameterText(ColumnSchema cs) { return this.GetParameterName(cs.Name) + " " + cs.DataType.ToLower() + this.GetParameterLength(cs); } /// <summary> /// Gets or sets the template file. /// </summary> /// <value></value> public string TemplateFile { get { return this.templateFile; } set { this.templateFile = value; } } public override bool Overwrite { get { return this.overwrite; } set { base.Overwrite = value; } } /// <summary> /// Gets the context. /// </summary> /// <value></value> public VelocityContext Context { get { return this.context; } } public string GetConversionExpression(string objectType) { string subt = objectType.Substring(objectType.IndexOf(".") + 1); return "Convert.To"+subt; } public string GetSqlServerSPInsertParams(TableSchema ts) { StringBuilder sb = new StringBuilder(); ArrayList al = new ArrayList(); // Get all columns that aren't autoincrement foreach(ColumnSchema cs in ts.SortedColumns.Values) { if(!cs.IsAutoIncrement && cs.IsActive)al.Add(cs); } // Build the list of parameters int i = 1; foreach(ColumnSchema cs in al) { if(cs.IsActive) { if(i < al.Count) { sb.Append(this.GetParameterText(cs) + "," + Environment.NewLine); } else { sb.Append(this.GetParameterText(cs) + Environment.NewLine); } i++; } } return sb.ToString(); } public string GetSqlServerSPUpdateParams(TableSchema ts) { int i = 1; StringBuilder sb = new StringBuilder(); foreach (ColumnSchema columnSchema in ts.SortedColumns.Values) { if(columnSchema.IsActive) { if(i < ts.SortedColumns.Count) { sb.Append(this.GetParameterText(columnSchema) + "," + Environment.NewLine); } else { sb.Append(this.GetParameterText(columnSchema) + Environment.NewLine); } } i++; } return sb.ToString(); } } } --- NEW FILE: CompilerFactory.cs --- namespace Adapdev.CodeGen { using System.CodeDom.Compiler; using Microsoft.CSharp; using Microsoft.JScript; using Microsoft.MCpp; using Microsoft.VisualBasic; using Microsoft.VJSharp; /// <summary> /// Summary description for CompilerFactory. /// </summary> public class CodeProviderFactory { /// <summary> /// Gets the code provider. /// </summary> /// <param name="codeType">Code type.</param> /// <returns></returns> public static CodeDomProvider GetCodeProvider(LanguageType codeType) { switch (codeType) { case LanguageType.CPP: return new MCppCodeProvider(); case LanguageType.CSHARP: return new CSharpCodeProvider(); case LanguageType.JSCRIPT: return new JScriptCodeProvider(); case LanguageType.JSHARP: return new VJSharpCodeProvider(); case LanguageType.VBNET: return new VBCodeProvider(); default: return new CSharpCodeProvider(); } } } } --- NEW FILE: UnitTestType.cs --- using System; namespace Adapdev.Data.CodeGen { /// <summary> /// Summary description for UnitTestType. /// </summary> public enum UnitTestType { CSUNIT, NUNIT, ZANEBUG, MBUNIT } } --- NEW FILE: TableCodeTemplate.cs --- namespace Adapdev.CodeGen { using System; using Adapdev.Data.Schema; public abstract class TableCodeTemplate : AbstractCodeTemplate { protected TableSchema ti; /// <summary> /// Creates a new <see cref="TableCodeTemplate"/> instance. /// </summary> /// <param name="ti">TableSchema.</param> /// <param name="nameSpace">Namespace.</param> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> /// <param name="className">Name of the class.</param> public TableCodeTemplate(TableSchema ti, string nameSpace, string fileName, string fileExtension, string className) : base(fileName, fileExtension, className, nameSpace) { this.ti = ti; } /// <summary> /// Creates a new <see cref="TableCodeTemplate"/> instance. /// </summary> /// <param name="ti">TableSchema.</param> /// <param name="fileName">Name of the file.</param> /// <param name="fileExtension">File extension.</param> public TableCodeTemplate(TableSchema ti, string fileName, string fileExtension) : base(fileName, fileExtension, String.Empty, String.Empty) { this.ti = ti; } } } |