PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-2.0
  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
  26. using System;
  27. using System.Linq;
  28. using System.Reflection;
  29. using System.Data.SqlClient;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Data;
  33. using System.Data.Linq.Mapping;
  34. using DbLinq.Data.Linq;
  35. using DbLinq.Data.Linq.SqlClient;
  36. using DbLinq.Util;
  37. using DbLinq.Vendor;
  38. #if MONO_STRICT
  39. using DataContext = System.Data.Linq.DataContext;
  40. using DataLinq = System.Data.Linq;
  41. using ITable = System.Data.Linq.ITable;
  42. using System.Data.Linq.SqlClient;
  43. #else
  44. using DataContext = DbLinq.Data.Linq.DataContext;
  45. using DataLinq = DbLinq.Data.Linq;
  46. using ITable = DbLinq.Data.Linq.ITable;
  47. using DbLinq.Data.Linq.SqlClient;
  48. #endif
  49. namespace DbLinq.SqlServer
  50. {
  51. [Vendor(typeof(SqlServerProvider), typeof(Sql2000Provider), typeof(Sql2005Provider))]
  52. #if !MONO_STRICT
  53. public
  54. #endif
  55. class SqlServerVendor : Vendor.Implementation.Vendor
  56. {
  57. public override string VendorName { get { return "SqlServer"; } }
  58. protected readonly SqlServerSqlProvider sqlProvider = new SqlServerSqlProvider();
  59. public override ISqlProvider SqlProvider { get { return sqlProvider; } }
  60. protected override void AppendServer(StringBuilder connectionString, string host)
  61. {
  62. // As per http://www.connectionstrings.com/sql-server,
  63. // port numbers are separated from host names via comma
  64. AppendConnectionString(connectionString, ConnectionStringServer, host.Replace(':', ','));
  65. }
  66. //NOTE: for Oracle, we want to consider 'Array Binding'
  67. //http://download-west.oracle.com/docs/html/A96160_01/features.htm#1049674
  68. /// <summary>
  69. /// for large number of rows, we want to use BULK INSERT,
  70. /// because it does not fill up the translation log.
  71. /// This is enabled for tables where Vendor.UserBulkInsert[db.Table] is true.
  72. /// </summary>
  73. public override void BulkInsert<T>(DataLinq.Table<T> table, List<T> rows, int pageSize, IDbTransaction transaction)
  74. {
  75. //use TableLock for speed:
  76. var bulkCopy = new SqlBulkCopy((SqlConnection)transaction.Connection, SqlBulkCopyOptions.TableLock, null);
  77. bulkCopy.DestinationTableName = table.Context.Mapping.GetTable(typeof(T)).TableName;
  78. //bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
  79. var dt = new DataTable();
  80. //KeyValuePair<PropertyInfo, ColumnAttribute>[] columns = AttribHelper.GetColumnAttribs2(typeof(T));
  81. var columns = table.Context.Mapping.GetTable(typeof(T)).RowType.PersistentDataMembers;
  82. foreach (var column in columns)
  83. {
  84. //if (pair.Value.IsDbGenerated)
  85. // continue; //don't skip - all fields would be shifted
  86. var dc = new DataColumn();
  87. dc.ColumnName = column.MappedName;
  88. dc.DataType = column.Member.GetMemberType();
  89. if (dc.DataType.IsNullable())
  90. {
  91. dc.AllowDBNull = true;
  92. dc.DataType = dc.DataType.GetNullableType();
  93. }
  94. dt.Columns.Add(dc);
  95. }
  96. //TODO: cross-check null values against CanBeNull specifier
  97. //object[] indices = new object[] { };
  98. foreach (T row in rows)
  99. {
  100. DataRow dr = dt.NewRow();
  101. //use reflection to retrieve object's fields (TODO: optimize this later)
  102. foreach (var pair in columns)
  103. {
  104. //if (pair.Value.IsDbGenerated)
  105. // continue; //don't assign IDENTITY col
  106. object value = pair.Member.GetMemberValue(row);
  107. dr[pair.MappedName] = value ?? DBNull.Value;
  108. }
  109. //dr[1
  110. dt.Rows.Add(dr);
  111. }
  112. bulkCopy.WriteToServer(dt);
  113. }
  114. public override System.Data.Linq.IExecuteResult ExecuteMethodCall(DataContext context, System.Reflection.MethodInfo method, params object[] sqlParams)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. }
  119. }