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

Language C# Lines 182
MD5 Hash 4562d497b05ec278832299bf88019967 Estimated Cost $3,634 (why?)
Repository https://SQLExport.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace SqlExport.Data.Adapters.Linq
{
	internal class ScriptHelper
	{
		private string[] _usings;

		public ScriptHelper( string[] usings )
		{
			this._usings = usings;
		}

		public List<Exception> Errors { get; set; }

		public int ScriptLineOffset { get; private set; }

		public string BuildObjectCode( IRelationalSchemaAdapter schema )
		{
			Errors = new List<Exception>();
			StringBuilder code = new StringBuilder();

			code.AppendLine( "using System;" );
			code.AppendLine( "using System.Data;" );
			code.AppendLine( "using System.Data.Common;" );
			code.AppendLine( "using System.Data.Linq;" );
			code.AppendLine( "using System.Data.Linq.Mapping;" );
			code.AppendLine( "using System.Linq;" );
			code.AppendLine( "using SqlExport.Data.Adapters.Linq;" );

			code.AppendLine( "namespace SqlExport.Data.Adapters.Linq" );
			code.AppendLine( "{" );
			code.AppendLine( "	public abstract class GeneratedQueryWrapper" );
			code.AppendLine( "	{" );

			// Data context.
			code.AppendLine( "		private DataContext _db =  null;" );
			// Tables.
			foreach( SchemaItem table in schema.GetTables() )
			{
				code.AppendFormat( "		protected Table<{0}> {0} {{ get {{ return _db.GetTable<{0}>(); }} }}\n",
					CodeSafeName( table.Name ) );
			}
			// Output SQL.
			code.AppendLine( "		protected void PrintSql( IQueryable query )" );
			code.AppendLine( "		{" );
			code.AppendLine( "			if( _db != null )" );
			code.AppendLine( "			{" );
			code.AppendLine( "				_db.GetCommand( query ).CommandText.Print();" );
			code.AppendLine( "			}" );
			code.AppendLine( "		}" );
			// Initialisation.
			code.AppendLine( "		public void InitDataContext( IDbConnection connection, DbTransaction transaction, int commandTimeout )" );
			code.AppendLine( "		{" );
			code.AppendLine( "			if( connection != null )" );
			code.AppendLine( "			{" );
			code.AppendLine( "				_db = new DataContext( connection );" );
			code.AppendLine( "				if( transaction != null )" );
			code.AppendLine( "				{" );
			code.AppendLine( "					_db.Transaction = transaction;" );
			code.AppendLine( "				}" );
			code.AppendLine( "				_db.CommandTimeout = commandTimeout;" );
			code.AppendLine( "			}" );
			code.AppendLine( "		}" );
			// Execution.
			code.AppendLine( "		public abstract void Execute();" );

			code.AppendLine( "	}" );

			foreach( SchemaItem table in schema.GetTables() )
			{
				string typeName = CodeSafeName( table.Name );
				Dictionary<string, int> columnNameCount = new Dictionary<string, int>();
				code.AppendFormat( "	[Table(Name=\"[{0}]\")]\n", table.Name );
				code.AppendFormat( "	public class {0}\n", typeName );
				code.AppendLine( "	{" );
				foreach( Column col in schema.GetColumns( table.Name ) )
				{
					string propertyName;
					string propertyType;

					propertyName = CodeSafeName( col.Name );
					if( columnNameCount.ContainsKey( propertyName ) )
					{
						columnNameCount[propertyName]++;
						propertyName = string.Concat( propertyName, columnNameCount[propertyName] );
					}
					else
					{
						columnNameCount.Add( propertyName, 0 );
					}
					if( propertyName == typeName )
					{
						propertyName = string.Concat( "c_", propertyName );
					}

					Type fieldType = typeof( object );
					try
					{
						fieldType = schema.GetColumnType( col );
					}
					catch( Exception exp )
					{
						Errors.Add( exp );
					}
					if( fieldType.IsValueType && col.Nullable.HasValue && col.Nullable.Value )
					{
						propertyType = string.Format( "{0}?", fieldType.Name );
					}
					else
					{
						propertyType = fieldType.Name;
					}

					code.AppendFormat( "		[Column(Name=\"[{0}]\")]\n", col.Name );
					code.AppendFormat( "		public {0} {1} {{ get; set; }}\n", propertyType, propertyName );
				}
				code.AppendLine( "	}" );
			}
			code.AppendLine( "}" );

			System.Diagnostics.Debug.WriteLine( code.ToString() );

			return code.ToString();
		}

		public string BuildGeneratedQuery( TextReader script, IRelationalSchemaAdapter schema )
		{
			Errors = new List<Exception>();
			StringBuilder code = new StringBuilder();

			code.AppendLine( "using System;" );
			code.AppendLine( "using System.Data;" );
			code.AppendLine( "using System.Data.Common;" );
			code.AppendLine( "using System.Data.Linq;" );
			code.AppendLine( "using System.Data.Linq.Mapping;" );
			code.AppendLine( "using System.Linq;" );
			code.AppendLine( "using SqlExport.Data.Adapters.Linq;" );

			foreach( string item in _usings )
			{
				code.AppendFormat( "using {0};\n", item );
			}

			code.AppendLine();
			code.AppendFormat( "public class GeneratedQuery : GeneratedQueryWrapper, {0}\n", typeof( IGeneratedScript ).Name );
			code.AppendLine( "{" );

			// Execution.
			code.AppendLine( "	public override void Execute()" );
			code.AppendLine( "	{" );

			ScriptLineOffset = _usings.Length + 12;
			code.AppendLine( script.ReadToEnd() );

			code.AppendLine( "	}" );

			code.AppendLine( "}" );

			System.Diagnostics.Debug.WriteLine( code.ToString() );

			return code.ToString();
		}

		private string CodeSafeName( string name )
		{
			name = Regex.Replace( name, @"(?:^[0-9])|[^\w]", "_" );
			if( Array.IndexOf<string>( SyntaxDefinition.CSharpKeywords, name ) >= 0 )
			{
				name = string.Concat( "_", name );
			}
			return name;
		}
	}
}
Back to Top