PageRenderTime 204ms CodeModel.GetById 101ms app.highlight 7ms RepoModel.GetById 94ms app.codeStats 0ms

/mcs/tools/sqlmetal/src/DbLinq.SqlServer/SqlServerVendor.cs

http://github.com/mono/mono
C# | 134 lines | 76 code | 15 blank | 43 comment | 1 complexity | de4dda483933e90cf4a96d991bd34e54 MD5 | raw file
  1#region MIT license
  2// 
  3// MIT license
  4//
  5// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
  6// 
  7// Permission is hereby granted, free of charge, to any person obtaining a copy
  8// of this software and associated documentation files (the "Software"), to deal
  9// in the Software without restriction, including without limitation the rights
 10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11// copies of the Software, and to permit persons to whom the Software is
 12// furnished to do so, subject to the following conditions:
 13// 
 14// The above copyright notice and this permission notice shall be included in
 15// all copies or substantial portions of the Software.
 16// 
 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23// THE SOFTWARE.
 24// 
 25#endregion
 26using System;
 27using System.Linq;
 28using System.Reflection;
 29using System.Data.SqlClient;
 30using System.Collections.Generic;
 31using System.Text;
 32using System.Data;
 33using System.Data.Linq.Mapping;
 34
 35using DbLinq.Data.Linq;
 36using DbLinq.Data.Linq.SqlClient;
 37using DbLinq.Util;
 38using DbLinq.Vendor;
 39
 40#if MONO_STRICT
 41using DataContext = System.Data.Linq.DataContext;
 42using DataLinq    = System.Data.Linq;
 43using ITable      = System.Data.Linq.ITable;
 44using System.Data.Linq.SqlClient;
 45#else
 46using DataContext = DbLinq.Data.Linq.DataContext;
 47using DataLinq    = DbLinq.Data.Linq;
 48using ITable      = DbLinq.Data.Linq.ITable;
 49using DbLinq.Data.Linq.SqlClient;
 50#endif
 51
 52namespace DbLinq.SqlServer
 53{
 54    [Vendor(typeof(SqlServerProvider), typeof(Sql2000Provider), typeof(Sql2005Provider))]
 55#if !MONO_STRICT
 56    public 
 57#endif
 58    class SqlServerVendor : Vendor.Implementation.Vendor
 59    {
 60        public override string VendorName { get { return "SqlServer"; } }
 61
 62        protected readonly SqlServerSqlProvider sqlProvider = new SqlServerSqlProvider();
 63        public override ISqlProvider SqlProvider { get { return sqlProvider; } }
 64
 65        protected override void AppendServer(StringBuilder connectionString, string host)
 66        {
 67            // As per http://www.connectionstrings.com/sql-server, 
 68            // port numbers are separated from host names via comma
 69            AppendConnectionString(connectionString, ConnectionStringServer, host.Replace(':', ','));
 70        }
 71
 72        //NOTE: for Oracle, we want to consider 'Array Binding'
 73        //http://download-west.oracle.com/docs/html/A96160_01/features.htm#1049674
 74
 75        /// <summary>
 76        /// for large number of rows, we want to use BULK INSERT, 
 77        /// because it does not fill up the translation log.
 78        /// This is enabled for tables where Vendor.UserBulkInsert[db.Table] is true.
 79        /// </summary>
 80        public override void BulkInsert<T>(DataLinq.Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction)
 81        {
 82            //use TableLock for speed:
 83            var bulkCopy = new SqlBulkCopy((SqlConnection)transaction.Connection, SqlBulkCopyOptions.TableLock, null);
 84
 85            bulkCopy.DestinationTableName = table.Context.Mapping.GetTable(typeof(T)).TableName;
 86            //bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
 87
 88            var dt = new DataTable();
 89
 90            //KeyValuePair<PropertyInfo, ColumnAttribute>[] columns = AttribHelper.GetColumnAttribs2(typeof(T));
 91            var columns = table.Context.Mapping.GetTable(typeof(T)).RowType.PersistentDataMembers;
 92
 93            foreach (var column in columns)
 94            {
 95                //if (pair.Value.IsDbGenerated)
 96                //    continue; //don't skip - all fields would be shifted
 97
 98                var dc = new DataColumn();
 99                dc.ColumnName = column.MappedName;
100                dc.DataType = column.Member.GetMemberType();
101                if (dc.DataType.IsNullable())
102                {
103                    dc.AllowDBNull  = true;
104                    dc.DataType     = dc.DataType.GetNullableType();
105                }
106                dt.Columns.Add(dc);
107            }
108
109            //TODO: cross-check null values against CanBeNull specifier
110            //object[] indices = new object[] { };
111            foreach (T row in rows)
112            {
113                DataRow dr = dt.NewRow();
114                //use reflection to retrieve object's fields (TODO: optimize this later)
115                foreach (var pair in columns)
116                {
117                    //if (pair.Value.IsDbGenerated)
118                    //    continue; //don't assign IDENTITY col
119                    object value = pair.Member.GetMemberValue(row);
120                    dr[pair.MappedName] = value ?? DBNull.Value;
121                }
122                //dr[1
123                dt.Rows.Add(dr);
124            }
125            bulkCopy.WriteToServer(dt);
126
127        }
128
129        public override System.Data.Linq.IExecuteResult ExecuteMethodCall(DataContext context, System.Reflection.MethodInfo method, params object[] sqlParams)
130        {
131            throw new NotImplementedException();
132        }
133    }
134}