Merge pull request #214 from QuickJack/cd2c570c5543963d987f51080218715407c5d4b9
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlBulkCopy.cs
1 //
2 // System.Data.SqlClient.SqlBulkCopy.cs
3 //
4 // Author:
5 //   Nagappan A (anagappan@novell.com)
6 //
7 // (C) Novell, Inc 2007
8
9 //
10 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 #if NET_2_0
32
33 using System;
34 using System.Data;
35 using System.Data.Common;
36 using Mono.Data.Tds;
37 using Mono.Data.Tds.Protocol;
38
39 namespace System.Data.SqlClient {
40         /// <summary>Efficient way to bulk load SQL Server table with several data rows at once</summary>
41         public sealed class SqlBulkCopy : IDisposable 
42         {
43                 #region Constants
44                 private const string transConflictMessage = "Must not specify SqlBulkCopyOptions.UseInternalTransaction " +
45                         "and pass an external Transaction at the same time.";
46                 
47                 private const SqlBulkCopyOptions insertModifiers =
48                         SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.TableLock |
49                         SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.FireTriggers;
50                 #endregion
51                 
52                 #region Fields
53
54                 private int _batchSize = 0;
55                 private int _notifyAfter = 0;
56                 private int _bulkCopyTimeout = 0;
57                 private SqlBulkCopyColumnMappingCollection _columnMappingCollection = new SqlBulkCopyColumnMappingCollection ();
58                 private string _destinationTableName = null;
59                 private bool ordinalMapping = false;
60                 private bool sqlRowsCopied = false;
61                 private bool isLocalConnection = false;
62                 private SqlConnection connection;
63                 private SqlTransaction externalTransaction;
64                 private SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default;
65
66                 #endregion
67
68                 #region Constructors
69                 public SqlBulkCopy (SqlConnection connection)
70                 {
71                         if (connection == null) {
72                                 throw new ArgumentNullException("connection");
73                         }
74                         
75                         this.connection = connection;
76                 }
77
78                 public SqlBulkCopy (string connectionString)
79                 {
80                         if (connectionString == null) {
81                                 throw new ArgumentNullException("connectionString");
82                         }
83                         
84                         this.connection = new SqlConnection (connectionString);
85                         isLocalConnection = true;
86                 }
87
88                 [MonoTODO]
89                 public SqlBulkCopy (string connectionString, SqlBulkCopyOptions copyOptions)
90                 {
91                         if (connectionString == null) {
92                                 throw new ArgumentNullException ("connectionString");
93                         }
94                         
95                         this.connection = new SqlConnection (connectionString);
96                         isLocalConnection = true;
97                         
98                         if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
99                                 throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
100                         
101                         this.copyOptions = copyOptions;
102                 }
103
104                 [MonoTODO]
105                 public SqlBulkCopy (SqlConnection connection, SqlBulkCopyOptions copyOptions, SqlTransaction externalTransaction)
106                 {
107                         if (connection == null) {
108                                 throw new ArgumentNullException ("connection");
109                         }
110                         
111                         this.connection = connection;
112                         this.copyOptions = copyOptions;
113                         
114                         if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction) {
115                                 if (externalTransaction != null)
116                                         throw new ArgumentException (transConflictMessage);
117                         }
118                         else
119                                 this.externalTransaction = externalTransaction;
120                         
121                         if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
122                                 throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
123                         
124                         this.copyOptions = copyOptions;
125                 }
126
127                 #endregion
128
129                 #region Properties
130
131                 public int BatchSize {
132                         get { return _batchSize; }
133                         set { _batchSize = value; }
134                 }
135
136                 public int BulkCopyTimeout {
137                         get { return _bulkCopyTimeout; }
138                         set { _bulkCopyTimeout = value; }
139                 }
140
141                 public SqlBulkCopyColumnMappingCollection ColumnMappings  {
142                         get { return _columnMappingCollection; }
143                 }
144
145                 public string DestinationTableName {
146                         get { return _destinationTableName; }
147                         set { _destinationTableName = value; }
148                 }
149
150                 public int NotifyAfter {
151                         get { return _notifyAfter; }
152                         set {
153                                 if (value < 0)
154                                         throw new ArgumentOutOfRangeException ("NotifyAfter should be greater than or equal to 0");
155                                 _notifyAfter = value;
156                         }
157                 }
158
159                 #endregion
160
161                 #region Methods
162
163                 public void Close ()
164                 {
165                         if (sqlRowsCopied == true) {
166                                 throw new InvalidOperationException ("Close should not be called from SqlRowsCopied event");
167                         }
168                         if (connection == null || connection.State == ConnectionState.Closed) {
169                                 return;
170                         }
171                         connection.Close ();
172                 }
173
174                 private DataTable [] GetColumnMetaData ()
175                 {
176                         DataTable [] columnMetaDataTables = new DataTable [2];
177                         SqlCommand cmd = new SqlCommand ("select @@trancount; " +
178                                                          "set fmtonly on select * from " +
179                                                          DestinationTableName + " set fmtonly off;" +
180                                                          "exec sp_tablecollations_90 '" +
181                                                          DestinationTableName + "'",
182                                                          connection);
183                         SqlDataReader reader = cmd.ExecuteReader ();
184                         int i = 0; // Skipping 1st result
185                         do {
186                                   if (i == 1) {
187                                         columnMetaDataTables [i - 1] = reader.GetSchemaTable ();
188                                   } else if (i == 2) {
189                                         SqlDataAdapter adapter = new SqlDataAdapter ();
190                                         adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
191                                         columnMetaDataTables [i - 1] = new DataTable ();
192                                         adapter.FillInternal (columnMetaDataTables [i - 1], reader);
193                                 }
194                                 i++;
195                         } while (reader.IsClosed == false && reader.NextResult());
196                         reader.Close ();
197                         return columnMetaDataTables;
198                 }
199
200                 private string GenerateColumnMetaData (SqlCommand tmpCmd, DataTable colMetaData, DataTable tableCollations)
201                 {
202                         bool flag = false;
203                         string statement = "";
204                         int i = 0;
205                         foreach (DataRow row in colMetaData.Rows) {
206                                 flag = false;
207                                 foreach (DataColumn col in colMetaData.Columns) { // FIXME: This line not required, remove later
208                                         object value = null;
209                                         if (_columnMappingCollection.Count > 0) {
210                                                 if (ordinalMapping) {
211                                                         foreach (SqlBulkCopyColumnMapping mapping
212                                                                  in _columnMappingCollection) {
213                                                                 if (mapping.DestinationOrdinal == i) {
214                                                                         flag = true;
215                                                                         break;
216                                                                 }
217                                                         }
218                                                 } else {
219                                                         foreach (SqlBulkCopyColumnMapping mapping
220                                                                  in _columnMappingCollection) {
221                                                                 if (mapping.DestinationColumn == (string) row ["ColumnName"]) {
222                                                                         flag = true;
223                                                                         break;
224                                                                 }
225                                                         }
226                                                 }
227                                                 if (flag == false)
228                                                         break;
229                                         }
230                                         if ((bool)row ["IsReadOnly"]) {
231                                                 if (ordinalMapping)
232                                                         value = false;
233                                                 else
234                                                         break;
235                                         }
236                                         SqlParameter param = new SqlParameter ((string) row ["ColumnName"],
237                                                                                ((SqlDbType) row ["ProviderType"]));
238                                         param.Value = value;
239                                         if ((int)row ["ColumnSize"] != -1) {
240                                                 param.Size = (int) row ["ColumnSize"];
241                                         }
242                                         tmpCmd.Parameters.Add (param);
243                                         break;
244                                 }
245                                 i++;
246                         }
247                         flag = false;
248                         bool insertSt = false;
249                         foreach (DataRow row in colMetaData.Rows) {
250                                 if (_columnMappingCollection.Count > 0) {
251                                         i = 0;
252                                         insertSt = false;
253                                         foreach (SqlParameter param in tmpCmd.Parameters) {
254                                                 if (ordinalMapping) {
255                                                         foreach (SqlBulkCopyColumnMapping mapping
256                                                                  in _columnMappingCollection) {
257                                                                 if (mapping.DestinationOrdinal == i && param.Value == null) {
258                                                                         insertSt = true;
259                                                                 }
260                                                         }
261                                                 } else {
262                                                         foreach (SqlBulkCopyColumnMapping mapping
263                                                                  in _columnMappingCollection) {
264                                                                 if (mapping.DestinationColumn == param.ParameterName &&
265                                                                     (string)row ["ColumnName"] == param.ParameterName) {
266                                                                         insertSt = true;
267                                                                         param.Value = null;
268                                                                 }
269                                                         }
270                                                 }
271                                                 i++;
272                                                 if (insertSt == true)
273                                                         break;
274                                         }
275                                         if (insertSt == false)
276                                                 continue;
277                                 }
278                                 if ((bool)row ["IsReadOnly"]) {
279                                         continue;
280                                 }
281                                 string columnInfo = "";
282                                 if ((int)row ["ColumnSize"] != -1) {
283                                         columnInfo = string.Format ("{0}({1})",
284                                                                     (SqlDbType) row ["ProviderType"],
285                                                                     row ["ColumnSize"]);
286                                 } else {
287                                         columnInfo = string.Format ("{0}", (SqlDbType) row ["ProviderType"]);
288                                 }
289                                 if (flag)
290                                         statement += ", ";
291                                 string columnName = (string) row ["ColumnName"];
292                                 statement += string.Format ("[{0}] {1}", columnName, columnInfo);
293                                 if (flag == false)
294                                         flag = true;
295                                 if (tableCollations != null) {
296                                         foreach (DataRow collationRow in tableCollations.Rows) {
297                                                 if ((string)collationRow ["name"] == columnName) {
298                                                         statement += string.Format (" COLLATE {0}", collationRow ["collation"]);
299                                                         break;
300                                                 }
301                                         }
302                                 }
303                         }
304                         return statement;
305                 }
306
307                 private void ValidateColumnMapping (DataTable table, DataTable tableCollations)
308                 {
309                         foreach (SqlBulkCopyColumnMapping _columnMapping in _columnMappingCollection) {
310                                 if (ordinalMapping == false &&
311                                     (_columnMapping.DestinationColumn == String.Empty ||
312                                      _columnMapping.SourceColumn == String.Empty))
313                                         throw new InvalidOperationException ("Mappings must be either all null or ordinal");
314                                 if (ordinalMapping &&
315                                     (_columnMapping.DestinationOrdinal == -1 ||
316                                      _columnMapping.SourceOrdinal == -1))
317                                         throw new InvalidOperationException ("Mappings must be either all null or ordinal");
318                                 bool flag = false;
319                                 if (ordinalMapping == false) {
320                                         foreach (DataRow row in tableCollations.Rows) {
321                                                 if ((string)row ["name"] == _columnMapping.DestinationColumn) {
322                                                         flag = true;
323                                                         break;
324                                                 }
325                                         }
326                                         if (flag == false)
327                                                 throw new InvalidOperationException ("ColumnMapping does not match");
328                                         flag = false;
329                                         foreach (DataColumn col in table.Columns) {
330                                                 if (col.ColumnName == _columnMapping.SourceColumn) {
331                                                         flag = true;
332                                                         break;
333                                                 }
334                                         }
335                                         if (flag == false)
336                                                 throw new InvalidOperationException ("ColumnName " +
337                                                                                      _columnMapping.SourceColumn +
338                                                                                      " does not match");
339                                 } else {
340                                         if (_columnMapping.DestinationOrdinal >= tableCollations.Rows.Count)
341                                                 throw new InvalidOperationException ("ColumnMapping does not match");
342                                 }
343                         }
344                 }
345
346                 private void BulkCopyToServer (DataTable table, DataRowState state)
347                 {
348                         if (connection == null || connection.State == ConnectionState.Closed)
349                                 throw new InvalidOperationException ("This method should not be called on a closed connection");
350                         if (_destinationTableName == null)
351                                 throw new ArgumentNullException ("DestinationTableName");
352                         if (isLocalConnection && connection.State != ConnectionState.Open)
353                                 connection.Open();
354                         
355                         if ((copyOptions & SqlBulkCopyOptions.KeepIdentity) == SqlBulkCopyOptions.KeepIdentity) {
356                                 SqlCommand cmd = new SqlCommand ("set identity_insert " +
357                                                                  table.TableName + " on",
358                                                                  connection);
359                                 cmd.ExecuteScalar ();
360                         }
361                         DataTable [] columnMetaDataTables = GetColumnMetaData ();
362                         DataTable colMetaData = columnMetaDataTables [0];
363                         DataTable tableCollations = columnMetaDataTables [1];
364
365                         if (_columnMappingCollection.Count > 0) {
366                                 if (_columnMappingCollection [0].SourceOrdinal != -1)
367                                         ordinalMapping = true;
368                                 ValidateColumnMapping (table, tableCollations);
369                         }
370
371                         SqlCommand tmpCmd = new SqlCommand ();
372                         TdsBulkCopy blkCopy = new TdsBulkCopy ((Tds)connection.Tds);
373                         if (((Tds)connection.Tds).TdsVersion >= TdsVersion.tds70) {
374                                 string statement = "insert bulk " + DestinationTableName + " (";
375                                 statement += GenerateColumnMetaData (tmpCmd, colMetaData, tableCollations);
376                                 statement += ")";
377                                 
378                                 #region Check requested options and add corresponding modifiers to the statement
379                                 if ((copyOptions & insertModifiers) != SqlBulkCopyOptions.Default) {
380                                         statement += " WITH (";
381                                         bool commaRequired = false;
382                                         
383                                         if ((copyOptions & SqlBulkCopyOptions.CheckConstraints) == SqlBulkCopyOptions.CheckConstraints) {
384                                                 if (commaRequired)
385                                                         statement += ", ";
386                                                 statement += "CHECK_CONSTRAINTS";
387                                                 commaRequired = true;
388                                         }
389                                         
390                                         if ((copyOptions & SqlBulkCopyOptions.TableLock) == SqlBulkCopyOptions.TableLock) {
391                                                 if (commaRequired)
392                                                         statement += ", ";
393                                                 statement += "TABLOCK";
394                                                 commaRequired = true;
395                                         }
396                                         
397                                         if ((copyOptions & SqlBulkCopyOptions.KeepNulls) == SqlBulkCopyOptions.KeepNulls) {
398                                                 if (commaRequired)
399                                                         statement += ", ";
400                                                 statement += "KEEP_NULLS";
401                                                 commaRequired = true;
402                                         }
403                                         
404                                         if ((copyOptions & SqlBulkCopyOptions.FireTriggers) == SqlBulkCopyOptions.FireTriggers) {
405                                                 if (commaRequired)
406                                                         statement += ", ";
407                                                 statement += "FIRE_TRIGGERS";
408                                                 commaRequired = true;
409                                         }
410                                         
411                                         statement += ")";
412                                 }
413                                 #endregion Check requested options and add corresponding modifiers to the statement
414                                 
415                                 blkCopy.SendColumnMetaData (statement);
416                         }
417                         blkCopy.BulkCopyStart (tmpCmd.Parameters.MetaParameters);
418                         long noRowsCopied = 0;
419                         foreach (DataRow row in table.Rows) {
420                                 if (row.RowState == DataRowState.Deleted)
421                                         continue; // Don't copy the row that's in deleted state
422                                 if (state != 0 && row.RowState != state)
423                                         continue;
424                                 bool isNewRow = true;
425                                 int i = 0;
426                                 foreach (SqlParameter param in tmpCmd.Parameters) {
427                                         int size = 0;
428                                         object rowToCopy = null;
429                                         if (_columnMappingCollection.Count > 0) {
430                                                 if (ordinalMapping) {
431                                                         foreach (SqlBulkCopyColumnMapping mapping
432                                                                  in _columnMappingCollection) {
433                                                                 if (mapping.DestinationOrdinal == i && param.Value == null) {
434                                                                         rowToCopy = row [mapping.SourceOrdinal];
435                                                                         SqlParameter parameter = new SqlParameter (mapping.SourceOrdinal.ToString (),
436                                                                                                                    rowToCopy);
437                                                                         if (param.MetaParameter.TypeName != parameter.MetaParameter.TypeName) {
438                                                                                 parameter.SqlDbType = param.SqlDbType;
439                                                                                 rowToCopy = parameter.Value = parameter.ConvertToFrameworkType (rowToCopy);
440                                                                         }
441                                                                         string colType = string.Format ("{0}", parameter.MetaParameter.TypeName);
442                                                                         if (colType == "nvarchar") {
443                                                                                 if (row [i] != null) {
444                                                                                         size = ((string) parameter.Value).Length;
445                                                                                         size <<= 1;
446                                                                                 }
447                                                                         } else {
448                                                                                 size = parameter.Size;
449                                                                         }
450                                                                         break;
451                                                                 }
452                                                         }
453                                                 } else {
454                                                         foreach (SqlBulkCopyColumnMapping mapping
455                                                                  in _columnMappingCollection) {
456                                                                 if (mapping.DestinationColumn == param.ParameterName) {
457                                                                         rowToCopy = row [mapping.SourceColumn];
458                                                                         SqlParameter parameter = new SqlParameter (mapping.SourceColumn, rowToCopy);
459                                                                         if (param.MetaParameter.TypeName != parameter.MetaParameter.TypeName) {
460                                                                                 parameter.SqlDbType = param.SqlDbType;
461                                                                                 rowToCopy = parameter.Value = parameter.ConvertToFrameworkType (rowToCopy);
462                                                                         }
463                                                                         string colType = string.Format ("{0}", parameter.MetaParameter.TypeName);
464                                                                         if (colType == "nvarchar") {
465                                                                                 if (row [mapping.SourceColumn] != null) {
466                                                                                         size = ((string) rowToCopy).Length;
467                                                                                         size <<= 1;
468                                                                                 }
469                                                                         } else {
470                                                                                 size = parameter.Size;
471                                                                         }
472                                                                         break;
473                                                                 }
474                                                         }
475                                                 }
476                                                 i++;
477                                         } else {
478                                                 rowToCopy = row [param.ParameterName];
479                                                 string colType = param.MetaParameter.TypeName;
480                                                 /*
481                                                   If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
482                                                   FIXME: Need to check for other types
483                                                 */
484                                                 if (colType == "nvarchar") {
485                                                         size = ((string) row [param.ParameterName]).Length;
486                                                         size <<= 1;
487                                                 } else {
488                                                         size = param.Size;
489                                                 }
490                                         }
491                                         if (rowToCopy == null)
492                                                 continue;
493                                         blkCopy.BulkCopyData (rowToCopy, size, isNewRow);
494                                         if (isNewRow)
495                                                 isNewRow = false;
496                                 } // foreach (SqlParameter)
497                                 if (_notifyAfter > 0) {
498                                         noRowsCopied ++;
499                                         if (noRowsCopied >= _notifyAfter) {
500                                                 RowsCopied (noRowsCopied);
501                                                 noRowsCopied = 0;
502                                         }
503                                 }
504                         } // foreach (DataRow)
505                         blkCopy.BulkCopyEnd ();
506                 }
507
508                 public void WriteToServer (DataRow [] rows)
509                 {
510                         if (rows == null)
511                                 throw new ArgumentNullException ("rows");
512                         DataTable table = new DataTable (rows [0].Table.TableName);
513                         foreach (DataColumn col in rows [0].Table.Columns) {
514                                 DataColumn tmpCol = new DataColumn (col.ColumnName, col.DataType);
515                                 table.Columns.Add (tmpCol);
516                         }
517                         foreach (DataRow row in rows) {
518                                 DataRow tmpRow = table.NewRow ();
519                                 for (int i = 0; i < table.Columns.Count; i++) {
520                                         tmpRow [i] = row [i];
521                                 }
522                                 table.Rows.Add (tmpRow);
523                         }
524                         BulkCopyToServer (table, 0);
525                 }
526
527                 public void WriteToServer (DataTable table)
528                 {
529                         BulkCopyToServer (table, 0);
530                 }
531
532                 public void WriteToServer (IDataReader reader)
533                 {
534                         DataTable table = new DataTable ();
535                         SqlDataAdapter adapter = new SqlDataAdapter ();
536                         adapter.FillInternal (table, reader);
537                         BulkCopyToServer (table, 0);
538                 }
539
540                 public void WriteToServer (DataTable table, DataRowState rowState)
541                 {
542                         BulkCopyToServer (table, rowState);
543                 }
544
545                 private void RowsCopied (long rowsCopied)
546                 {
547                         SqlRowsCopiedEventArgs e = new SqlRowsCopiedEventArgs (rowsCopied);
548                         if (null != SqlRowsCopied) {
549                                 SqlRowsCopied (this, e);
550                         }
551                 }
552
553                 #endregion
554
555                 #region Events
556
557                 public event SqlRowsCopiedEventHandler SqlRowsCopied;
558
559                 #endregion
560
561                 void IDisposable.Dispose ()
562                 {
563                         //throw new NotImplementedException ();
564                         if (isLocalConnection) {
565                                 Close ();
566                                 connection = null;
567                         }
568                 }
569
570         }
571 }
572
573 #endif