5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Reflection;
\r
29 using System.Data.SqlClient;
\r
30 using System.Collections.Generic;
\r
33 using System.Data.Linq.Mapping;
\r
35 using DbLinq.Data.Linq;
\r
36 using DbLinq.Data.Linq.SqlClient;
\r
38 using DbLinq.Vendor;
\r
41 using DataContext = System.Data.Linq.DataContext;
\r
42 using DataLinq = System.Data.Linq;
\r
43 using ITable = System.Data.Linq.ITable;
\r
44 using System.Data.Linq.SqlClient;
\r
46 using DataContext = DbLinq.Data.Linq.DataContext;
\r
47 using DataLinq = DbLinq.Data.Linq;
\r
48 using ITable = DbLinq.Data.Linq.ITable;
\r
49 using DbLinq.Data.Linq.SqlClient;
\r
52 namespace DbLinq.SqlServer
\r
54 [Vendor(typeof(SqlServerProvider), typeof(Sql2000Provider), typeof(Sql2005Provider))]
\r
58 class SqlServerVendor : Vendor.Implementation.Vendor
\r
60 public override string VendorName { get { return "SqlServer"; } }
\r
62 protected readonly SqlServerSqlProvider sqlProvider = new SqlServerSqlProvider();
\r
63 public override ISqlProvider SqlProvider { get { return sqlProvider; } }
\r
65 //NOTE: for Oracle, we want to consider 'Array Binding'
\r
66 //http://download-west.oracle.com/docs/html/A96160_01/features.htm#1049674
\r
69 /// for large number of rows, we want to use BULK INSERT,
\r
70 /// because it does not fill up the translation log.
\r
71 /// This is enabled for tables where Vendor.UserBulkInsert[db.Table] is true.
\r
73 public override void BulkInsert<T>(DataLinq.Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction)
\r
75 //use TableLock for speed:
\r
76 var bulkCopy = new SqlBulkCopy((SqlConnection)transaction.Connection, SqlBulkCopyOptions.TableLock, null);
\r
78 bulkCopy.DestinationTableName = table.Context.Mapping.GetTable(typeof(T)).TableName;
\r
79 //bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
\r
81 var dt = new DataTable();
\r
83 //KeyValuePair<PropertyInfo, ColumnAttribute>[] columns = AttribHelper.GetColumnAttribs2(typeof(T));
\r
84 var columns = table.Context.Mapping.GetTable(typeof(T)).RowType.PersistentDataMembers;
\r
86 foreach (var column in columns)
\r
88 //if (pair.Value.IsDbGenerated)
\r
89 // continue; //don't skip - all fields would be shifted
\r
91 var dc = new DataColumn();
\r
92 dc.ColumnName = column.MappedName;
\r
93 dc.DataType = column.Member.GetMemberType();
\r
97 //TODO: cross-check null values against CanBeNull specifier
\r
98 //object[] indices = new object[] { };
\r
99 foreach (T row in rows)
\r
101 DataRow dr = dt.NewRow();
\r
102 //use reflection to retrieve object's fields (TODO: optimize this later)
\r
103 foreach (var pair in columns)
\r
105 //if (pair.Value.IsDbGenerated)
\r
106 // continue; //don't assign IDENTITY col
\r
107 object value = pair.Member.GetMemberValue(row);
\r
108 dr[pair.MappedName] = value;
\r
113 bulkCopy.WriteToServer(dt);
\r
117 public override System.Data.Linq.IExecuteResult ExecuteMethodCall(DataContext context, System.Reflection.MethodInfo method, params object[] sqlParams)
\r
119 throw new NotImplementedException();
\r