⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clsllblgenerator.cs

📁 LLBLGen 1.21 Sourcecode
💻 CS
📖 第 1 页 / 共 5 页
字号:
//////////////////////////////////////////////////////////////////////
// Part of LLBLGen sourcecode. See version information
//////////////////////////////////////////////////////////////////////
// COPYRIGHTS:
// Copyright (c)2002 Solutions Design. All rights reserved.
// 
// Released under the following license: (BSD2)
// -------------------------------------------
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met: 
//
// 1) Redistributions of source code must retain the above copyright notice, this list of 
//    conditions and the following disclaimer. 
// 2) Redistributions in binary form must reproduce the above copyright notice, this list of 
//    conditions and the following disclaimer in the documentation and/or other materials 
//    provided with the distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY SOLUTIONS DESIGN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOLUTIONS DESIGN OR CONTRIBUTORS BE LIABLE FOR 
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//
// The views and conclusions contained in the software and documentation are those of the authors 
// and should not be interpreted as representing official policies, either expressed or implied, 
// of Solutions Design. 
//
//////////////////////////////////////////////////////////////////////
// Contributers to the code:
//		- Frans Bouma [FB]
//////////////////////////////////////////////////////////////////////

using System;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Windows.Forms;

namespace LLBLGen
{
	
	/// <summary>
	/// Purpose: The code generator.
	/// </summary>
	public class clsLLBLGenerator
	{
		#region Class Member Declarations
		private	SqlConnection	m_scoMainConnection = new SqlConnection();
		private	frmMain			m_wMainWindow;
		private	bool			m_bNETIncludeAutoComplete, m_bNETIncludeJITSupport, 
								m_bNETIncludeLoadBalancingSupport, m_bNETIncludeObjectPoolingSupport, 
								m_bSPGenerateSelectAllSPs, m_bSPPrefixParams, 
								m_bSPGenerateDeleteSPs, m_bSPGenerateInsertSPs,	m_bSPGenerateUpdateSPs, 
								m_bSPGenerateSelectSPs, m_bSPIncludeComments, m_bSPIncludeDrops, 
								m_bSPIncludeErrorCodeSupport, m_bNETIncludeNullValueSupport, 
								m_bSPUseFieldListSelectClause, m_bNETIncludeTransactionSupport, 
								m_bNETIncludeCOMPlusSupport, m_bNETPrefixProperties, m_bNETStyleHungarian, 
								m_bNETIncludeComments, m_bSPIncludeNoCountStatements, m_bNETIncludeConnectionProviderSupport;
		private string			m_sSQLCatalog, m_sNETNamespace, m_sSPNamePrefix, m_sConnectionString,
								m_sSPOutputPath, m_sNETOutputPath, m_sNETClassPrefix, m_sAppVersion;
		private	ArrayList		m_alTables, m_alExcludedFields;
		private SortedList		m_slTypeToPrefix, m_slTypeToNETType, m_slTypeToSqlType, m_slTypeToCSCastType,
								m_slTypeToVBCastType;
		private int				m_iNETConnectionStringRetrieval, m_iNETOutputLanguage;
		#endregion
		
		public clsLLBLGenerator()
		{
			// Initialize global resources, used by tables and/or this class.
			InitTypePrefixTable();
			InitTypeNETTypeTable();
			InitTypeSqlTypeTable();
			InitTypeCSCastTypeTable();
			InitTypeVBCastTypeTable();
			
			// create a list for at least 256 tables. If more tables are found, the list is dynamically
			// increased.
			m_alTables = new ArrayList(256);
			
			// create a default arraylist for all excluded fields.
			m_alExcludedFields = new ArrayList();
		}
		

		/// <summary>
		/// Purpose: adds a new clsTable object to the table-list for processing.
		/// </summary>
		/// <param name="sTableName">The name of the table to process</param>
		/// <param name="iTableType">Type of SQLObject, f.e. table or view</param>
		public void AddTable(string sTableName, eSQLObjectType iTableType)
		{
			clsTable oTable = new clsTable(sTableName, m_sAppVersion);
			oTable.iTableType = (int)iTableType;
			m_alTables.Add(oTable);
		}
		
		
		/// <summary>
		/// Purpose: adds a new excluded field to the list of excluded fields.
		/// </summary>
		/// <param name="sExcludedField">Name of field to exclude</param>
		public void AddExcludedField(string sExcludedField)
		{
			m_alExcludedFields.Add(sExcludedField.ToLower());
		}
		
		
		/// <summary>
		/// Purpose: starts the code generation
		/// </summary>
		/// <returns>true if succeeded. It trows an Exception otherwise.</returns>
		public bool StartCodeGeneration()
		{
			try
			{
				// Check outputpaths. If they are not existing, create dirs.
				if(!Directory.Exists(m_sNETOutputPath.ToString()))
				{
					// create output directory for C# classes
					Directory.CreateDirectory(m_sNETOutputPath.ToString());
					m_wMainWindow.AddLineToFeedback("Directory " + m_sNETOutputPath + " created.");
				}
				if(!Directory.Exists(m_sSPOutputPath.ToString()))
				{
					// create output directory for stored procedure T-SQL script
					Directory.CreateDirectory(m_sSPOutputPath.ToString());
					m_wMainWindow.AddLineToFeedback("Directory " + m_sSPOutputPath + " created.");
				}
				
				// Open main connection
				m_scoMainConnection.Open();
				
				m_wMainWindow.AddLineToFeedback("SQL Server version: " + m_scoMainConnection.ServerVersion);
				
				// Per table received from the caller (which are stored in m_altables), initialize it,
				// and set properties.
				foreach(clsTable tCurrent in m_alTables)
				{
					tCurrent.scoActiveConnection = m_scoMainConnection;
					tCurrent.bNETIncludeAutoComplete = m_bNETIncludeAutoComplete;
					tCurrent.bNETIncludeComments = m_bNETIncludeComments;
					tCurrent.bNETIncludeCOMPlusSupport = m_bNETIncludeCOMPlusSupport;
					tCurrent.bNETIncludeJITSupport = m_bNETIncludeJITSupport;
					tCurrent.bNETIncludeLoadBalancingSupport = m_bNETIncludeLoadBalancingSupport;
					tCurrent.bNETIncludeNullValueSupport = m_bNETIncludeNullValueSupport;
					tCurrent.bNETIncludeTransactionSupport = m_bNETIncludeTransactionSupport;
					tCurrent.bNETIncludeObjectPoolingSupport = m_bNETIncludeObjectPoolingSupport;
					tCurrent.bNETIncludeConnectionProviderSupport = m_bNETIncludeConnectionProviderSupport;
					tCurrent.bNETPrefixProperties = m_bNETPrefixProperties;
					tCurrent.bNETStyleHungarian = m_bNETStyleHungarian;
					tCurrent.bSPGenerateDeleteSPs = m_bSPGenerateDeleteSPs;
					tCurrent.bSPGenerateInsertSPs = m_bSPGenerateInsertSPs;
					tCurrent.bSPGenerateSelectAllSPs = m_bSPGenerateSelectAllSPs;
					tCurrent.bSPGenerateSelectSPs = m_bSPGenerateSelectSPs;
					tCurrent.bSPGenerateUpdateSPs = m_bSPGenerateUpdateSPs;
					tCurrent.bSPIncludeComments = m_bSPIncludeComments;
					tCurrent.bSPIncludeDrops = m_bSPIncludeDrops;
					tCurrent.bSPIncludeErrorCodeSupport = m_bSPIncludeErrorCodeSupport;
					tCurrent.bSPIncludeNoCountStatements = m_bSPIncludeNoCountStatements;
					tCurrent.bSPPrefixParams = m_bSPPrefixParams;
					tCurrent.bSPUseFieldListSelectClause = m_bSPUseFieldListSelectClause;
					tCurrent.sNETClassPrefix = m_sNETClassPrefix;
					tCurrent.sNETNamespace = m_sNETNamespace;
					tCurrent.sNETOutputPath = m_sNETOutputPath;
					tCurrent.sSPNamePrefix = m_sSPNamePrefix;
					tCurrent.sNETOutputPath = m_sNETOutputPath;
					tCurrent.alExcludedFieldsInSPs = m_alExcludedFields;
					tCurrent.slTypeToPrefix = m_slTypeToPrefix;
					tCurrent.slTypeToNETType = m_slTypeToNETType;
					tCurrent.slTypeToSqlType = m_slTypeToSqlType;
					tCurrent.slTypeToCSCastType = m_slTypeToCSCastType;
					tCurrent.slTypeToVBCastType = m_slTypeToVBCastType;
					tCurrent.iNETConnectionStringRetrieval = m_iNETConnectionStringRetrieval;
					tCurrent.iNETOutputLanguage = m_iNETOutputLanguage;
				}
				
				///////////
				// For now: dump all T-SQL stored procs and other code in 1 big file.
				// This is better for processing the scripts (it's not nice to load lots of .sql files in
				// Query Analyzer :))
				///////////
			
				// Create T-SQL output stream
				string sSPOutputFile = m_sSPOutputPath + m_sSQLCatalog + "_LLBL_StoredProcedures.sql";
				if(File.Exists(sSPOutputFile))
				{
					// remove the outputfile.
					File.Delete(sSPOutputFile);
				}
				
				StreamWriter swSPOutput = new StreamWriter(sSPOutputFile);
				
				// Write header in T-SQL script.
				WriteSPOutputHeader(swSPOutput);
				
				// per table process it
				m_wMainWindow.AddLineToFeedback("Generating code for all selected tables / views in catalog '" + m_sSQLCatalog + "'");
				short iPct = 0, i=0;
				foreach(clsTable tCurrent in m_alTables)
				{
					iPct = (short)(((float)(i+1) / (float)m_alTables.Count) * 100.0f);
					m_wMainWindow.SetCurrentTableInFeedback(tCurrent.sTableName);
					
					// Get fields and fieldrelated data
					m_wMainWindow.AddLineToFeedback(((eSQLObjectType)tCurrent.iTableType).ToString() + ": "  + tCurrent.sTableName);
					tCurrent.GetFields();
					m_wMainWindow.AddLineToFeedback("\tFields retrieved.");

					// Generate SP code for this table and generate C# code.
					tCurrent.GenerateCode(swSPOutput);
					m_wMainWindow.AddLineToFeedback("\tCode for Stored Procedure(s) and .NET class generated.");

					m_wMainWindow.SetProgressBarValue(iPct);
					i++;
				}
				
				// done.
				swSPOutput.Close();

				// all done, write the baseclass and the app.config file, if necessary.
				switch(m_iNETOutputLanguage)
				{
					case (int)eDotNetLanguages.CSharp:
						WriteCSBaseClass();
						m_wMainWindow.AddLineToFeedback(".NET Base class generated.");
						if(m_bNETIncludeConnectionProviderSupport)
						{
							WriteCSConnectionProviderClass();
							m_wMainWindow.AddLineToFeedback(".NET ConnectionProvider class generated.");
						}
						break;
					case (int)eDotNetLanguages.VBDotNet:
						WriteVBBaseClass();
						m_wMainWindow.AddLineToFeedback(".NET Base class generated.");
						if(m_bNETIncludeConnectionProviderSupport)
						{
							WriteVBConnectionProviderClass();
							m_wMainWindow.AddLineToFeedback(".NET ConnectionProvider class generated.");
						}
						break;
				}
				if(m_iNETConnectionStringRetrieval==(int)eConnectionStringRetrieval.AppConfig)
				{
					WriteNETAppConfig();
				}

				// Signal done.
				m_wMainWindow.SetCurrentTableInFeedback("");
				m_wMainWindow.AddLineToFeedback("All generation of the .NET and T-SQL code has been completed succesfully!");
				
				return true;
			}
			catch(Exception ex)
			{
				// bubble error
				throw new Exception("clsLLBLGenerator::StartCodeGenerator::Error occured:" + ex.Message,ex);
			}
			finally
			{
				// close connection
				m_scoMainConnection.Close();
			}
		}
		
		
		/// <summary>
		/// Purpose: cleans up datastructures, after a codegeneration sequence was executed. 
		/// This routine is needed, so a new code generation sequence will not be using old data.
		/// </summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -