PageRenderTime 49ms CodeModel.GetById 42ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/SQlExport.Data.Adapters.Linq/ScriptHelper.cs

http://SQLExport.codeplex.com
C# | 181 lines | 148 code | 27 blank | 6 comment | 7 complexity | 4562d497b05ec278832299bf88019967 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Text.RegularExpressions;
  6using System.IO;
  7
  8namespace SqlExport.Data.Adapters.Linq
  9{
 10	internal class ScriptHelper
 11	{
 12		private string[] _usings;
 13
 14		public ScriptHelper( string[] usings )
 15		{
 16			this._usings = usings;
 17		}
 18
 19		public List<Exception> Errors { get; set; }
 20
 21		public int ScriptLineOffset { get; private set; }
 22
 23		public string BuildObjectCode( IRelationalSchemaAdapter schema )
 24		{
 25			Errors = new List<Exception>();
 26			StringBuilder code = new StringBuilder();
 27
 28			code.AppendLine( "using System;" );
 29			code.AppendLine( "using System.Data;" );
 30			code.AppendLine( "using System.Data.Common;" );
 31			code.AppendLine( "using System.Data.Linq;" );
 32			code.AppendLine( "using System.Data.Linq.Mapping;" );
 33			code.AppendLine( "using System.Linq;" );
 34			code.AppendLine( "using SqlExport.Data.Adapters.Linq;" );
 35
 36			code.AppendLine( "namespace SqlExport.Data.Adapters.Linq" );
 37			code.AppendLine( "{" );
 38			code.AppendLine( "	public abstract class GeneratedQueryWrapper" );
 39			code.AppendLine( "	{" );
 40
 41			// Data context.
 42			code.AppendLine( "		private DataContext _db =  null;" );
 43			// Tables.
 44			foreach( SchemaItem table in schema.GetTables() )
 45			{
 46				code.AppendFormat( "		protected Table<{0}> {0} {{ get {{ return _db.GetTable<{0}>(); }} }}\n",
 47					CodeSafeName( table.Name ) );
 48			}
 49			// Output SQL.
 50			code.AppendLine( "		protected void PrintSql( IQueryable query )" );
 51			code.AppendLine( "		{" );
 52			code.AppendLine( "			if( _db != null )" );
 53			code.AppendLine( "			{" );
 54			code.AppendLine( "				_db.GetCommand( query ).CommandText.Print();" );
 55			code.AppendLine( "			}" );
 56			code.AppendLine( "		}" );
 57			// Initialisation.
 58			code.AppendLine( "		public void InitDataContext( IDbConnection connection, DbTransaction transaction, int commandTimeout )" );
 59			code.AppendLine( "		{" );
 60			code.AppendLine( "			if( connection != null )" );
 61			code.AppendLine( "			{" );
 62			code.AppendLine( "				_db = new DataContext( connection );" );
 63			code.AppendLine( "				if( transaction != null )" );
 64			code.AppendLine( "				{" );
 65			code.AppendLine( "					_db.Transaction = transaction;" );
 66			code.AppendLine( "				}" );
 67			code.AppendLine( "				_db.CommandTimeout = commandTimeout;" );
 68			code.AppendLine( "			}" );
 69			code.AppendLine( "		}" );
 70			// Execution.
 71			code.AppendLine( "		public abstract void Execute();" );
 72
 73			code.AppendLine( "	}" );
 74
 75			foreach( SchemaItem table in schema.GetTables() )
 76			{
 77				string typeName = CodeSafeName( table.Name );
 78				Dictionary<string, int> columnNameCount = new Dictionary<string, int>();
 79				code.AppendFormat( "	[Table(Name=\"[{0}]\")]\n", table.Name );
 80				code.AppendFormat( "	public class {0}\n", typeName );
 81				code.AppendLine( "	{" );
 82				foreach( Column col in schema.GetColumns( table.Name ) )
 83				{
 84					string propertyName;
 85					string propertyType;
 86
 87					propertyName = CodeSafeName( col.Name );
 88					if( columnNameCount.ContainsKey( propertyName ) )
 89					{
 90						columnNameCount[propertyName]++;
 91						propertyName = string.Concat( propertyName, columnNameCount[propertyName] );
 92					}
 93					else
 94					{
 95						columnNameCount.Add( propertyName, 0 );
 96					}
 97					if( propertyName == typeName )
 98					{
 99						propertyName = string.Concat( "c_", propertyName );
100					}
101
102					Type fieldType = typeof( object );
103					try
104					{
105						fieldType = schema.GetColumnType( col );
106					}
107					catch( Exception exp )
108					{
109						Errors.Add( exp );
110					}
111					if( fieldType.IsValueType && col.Nullable.HasValue && col.Nullable.Value )
112					{
113						propertyType = string.Format( "{0}?", fieldType.Name );
114					}
115					else
116					{
117						propertyType = fieldType.Name;
118					}
119
120					code.AppendFormat( "		[Column(Name=\"[{0}]\")]\n", col.Name );
121					code.AppendFormat( "		public {0} {1} {{ get; set; }}\n", propertyType, propertyName );
122				}
123				code.AppendLine( "	}" );
124			}
125			code.AppendLine( "}" );
126
127			System.Diagnostics.Debug.WriteLine( code.ToString() );
128
129			return code.ToString();
130		}
131
132		public string BuildGeneratedQuery( TextReader script, IRelationalSchemaAdapter schema )
133		{
134			Errors = new List<Exception>();
135			StringBuilder code = new StringBuilder();
136
137			code.AppendLine( "using System;" );
138			code.AppendLine( "using System.Data;" );
139			code.AppendLine( "using System.Data.Common;" );
140			code.AppendLine( "using System.Data.Linq;" );
141			code.AppendLine( "using System.Data.Linq.Mapping;" );
142			code.AppendLine( "using System.Linq;" );
143			code.AppendLine( "using SqlExport.Data.Adapters.Linq;" );
144
145			foreach( string item in _usings )
146			{
147				code.AppendFormat( "using {0};\n", item );
148			}
149
150			code.AppendLine();
151			code.AppendFormat( "public class GeneratedQuery : GeneratedQueryWrapper, {0}\n", typeof( IGeneratedScript ).Name );
152			code.AppendLine( "{" );
153
154			// Execution.
155			code.AppendLine( "	public override void Execute()" );
156			code.AppendLine( "	{" );
157
158			ScriptLineOffset = _usings.Length + 12;
159			code.AppendLine( script.ReadToEnd() );
160
161			code.AppendLine( "	}" );
162
163			code.AppendLine( "}" );
164
165			System.Diagnostics.Debug.WriteLine( code.ToString() );
166
167			return code.ToString();
168		}
169
170		private string CodeSafeName( string name )
171		{
172			name = Regex.Replace( name, @"(?:^[0-9])|[^\w]", "_" );
173			if( Array.IndexOf<string>( SyntaxDefinition.CSharpKeywords, name ) >= 0 )
174			{
175				name = string.Concat( "_", name );
176			}
177			return name;
178		}
179	}
180}
181