2007-07-22 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds.Protocol / TdsBulkCopy.cs
1 //
2 // Mono.Data.Tds.Protocol.TdsBulkCopy.cs
3 //
4 // Author:
5 //   Nagappan A (anagappan@novell.com)
6 //
7 // Copyright (C) 2007 Novell Inc
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 #if NET_2_0
30 using Mono.Security.Protocol.Ntlm;
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Diagnostics;
35 using System.Net.Sockets;
36 using System.Text;
37
38 namespace Mono.Data.Tds.Protocol {
39         public class TdsBulkCopy
40         {
41                 #region Fields
42
43                 Tds tds;
44                 #endregion
45
46                 #region Constructors
47
48                 public TdsBulkCopy (Tds tds)
49                 {
50                         this.tds = tds;
51                 }
52
53                 #endregion
54
55                 #region Methods
56                 public bool SendColumnMetaData (string colMetaData)
57                 {
58                         tds.Comm.StartPacket (TdsPacketType.Query);
59                         tds.Comm.Append (colMetaData);
60                         tds.ExecBulkCopyMetaData (30, false);
61                         return true;
62                 }
63
64                 public bool BulkCopyStart (TdsMetaParameterCollection parameters)
65                 {
66                         tds.Comm.StartPacket (TdsPacketType.Bulk);
67                         tds.Comm.Append ((byte) TdsPacketSubType.ColumnMetadata);
68                         short count = 0;
69                         foreach (TdsMetaParameter param in parameters) {
70                                 if (param.Value != null)
71                                         continue;
72                                 count++;
73                         }
74                         tds.Comm.Append (count);
75                         if (parameters != null) {
76                                 foreach (TdsMetaParameter param in parameters) {
77                                         if (param.Value != null)
78                                                 continue;
79                                         tds.Comm.Append ((short) 0x00);
80                                         tds.Comm.Append ((short) 0x0a);
81                                         WriteParameterInfo (param);
82                                         tds.Comm.Append ((byte) param.ParameterName.Length);
83                                         tds.Comm.Append (param.ParameterName);
84                                 }
85                         }
86                         return true;
87                 }
88
89                 public bool BulkCopyData (object o, int size, bool isNewRow)
90                 {
91                         if (isNewRow) {
92                                 tds.Comm.Append ((byte) TdsPacketSubType.Row);
93                         }
94                         if (size > 0) {
95                                 tds.Comm.Append ((short) size);
96                         }
97                         tds.Comm.Append (o);
98                         return true;
99                 }
100
101                 public bool BulkCopyEnd ()
102                 {
103                         tds.Comm.Append ((short) TdsPacketSubType.Done);
104                         tds.ExecBulkCopy (30, false);
105                         return true;
106                 }
107
108                 private void WriteParameterInfo (TdsMetaParameter param)
109                 {
110                         /*
111                         Ms.net send non-nullable datatypes as nullable and allows setting null values
112                         to int/float etc.. So, using Nullable form of type for all data
113                         */
114                         param.IsNullable = true;
115                         TdsColumnType colType = param.GetMetaType ();
116                         param.IsNullable = false;
117
118                         tds.Comm.Append ((byte)colType); // type
119                                 
120                         int size = 0;
121                         if (param.Size == 0)
122                                 size = param.GetActualSize ();
123                         else
124                                 size = param.Size;
125
126                         /*
127                           If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
128                           FIXME: Need to check for other types
129                          */
130                         if (colType == TdsColumnType.BigNVarChar)
131                                 size <<= 1;
132                         if (tds.IsLargeType (colType))
133                                 tds.Comm.Append ((short)size); // Parameter size passed in SqlParameter
134                         else if (tds.IsBlobType (colType))
135                                 tds.Comm.Append (size); // Parameter size passed in SqlParameter
136                         else 
137                                 tds.Comm.Append ((byte)size);
138
139                         // Precision and Scale are non-zero for only decimal/numeric
140                         if ( param.TypeName == "decimal" || param.TypeName == "numeric") {
141                                 tds.Comm.Append ((param.Precision!=0)?param.Precision:(byte)28);
142                                 tds.Comm.Append (param.Scale);
143                         }
144                 }
145                 #endregion
146         }
147 }
148 #endif