Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[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 System;
31
32 namespace Mono.Data.Tds.Protocol {
33         public class TdsBulkCopy
34         {
35                 #region Fields
36
37                 Tds tds;
38                 #endregion
39
40                 #region Constructors
41
42                 public TdsBulkCopy (Tds tds)
43                 {
44                         this.tds = tds;
45                 }
46
47                 #endregion
48
49                 #region Methods
50                 public bool SendColumnMetaData (string colMetaData)
51                 {
52                         tds.Comm.StartPacket (TdsPacketType.Query);
53                         tds.Comm.Append (colMetaData);
54                         tds.ExecBulkCopyMetaData (30, false);
55                         return true;
56                 }
57
58                 public bool BulkCopyStart (TdsMetaParameterCollection parameters)
59                 {
60                         tds.Comm.StartPacket (TdsPacketType.Bulk);
61                         tds.Comm.Append ((byte) TdsPacketSubType.ColumnMetadata);
62                         short count = 0;
63                         foreach (TdsMetaParameter param in parameters) {
64                                 if (param.Value != null)
65                                         continue;
66                                 count++;
67                         }
68                         tds.Comm.Append (count);
69                         if (parameters != null) {
70                                 foreach (TdsMetaParameter param in parameters) {
71                                         if (param.Value != null)
72                                                 continue;
73                                         tds.Comm.Append ((short) 0x00);
74                                         tds.Comm.Append ((short) 0x0a);
75                                         WriteParameterInfo (param);
76                                         tds.Comm.Append ((byte) param.ParameterName.Length);
77                                         tds.Comm.Append (param.ParameterName);
78                                 }
79                         }
80                         return true;
81                 }
82
83                 public bool BulkCopyData (object o, int size, bool isNewRow)
84                 {
85                         if (isNewRow) {
86                                 tds.Comm.Append ((byte) TdsPacketSubType.Row);
87                         }
88                         if (size > 0) {
89                                 tds.Comm.Append ((short) size);
90                         }
91                         tds.Comm.Append (o);
92                         return true;
93                 }
94
95                 public bool BulkCopyEnd ()
96                 {
97                         tds.Comm.Append ((short) TdsPacketSubType.Done);
98                         tds.ExecBulkCopy (30, false);
99                         return true;
100                 }
101
102                 private void WriteParameterInfo (TdsMetaParameter param)
103                 {
104                         /*
105                         Ms.net send non-nullable datatypes as nullable and allows setting null values
106                         to int/float etc.. So, using Nullable form of type for all data
107                         */
108                         param.IsNullable = true;
109                         TdsColumnType colType = param.GetMetaType ();
110                         param.IsNullable = false;
111
112                         tds.Comm.Append ((byte)colType); // type
113                                 
114                         int size = 0;
115                         if (param.Size == 0)
116                                 size = param.GetActualSize ();
117                         else
118                                 size = param.Size;
119
120                         /*
121                           If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
122                           FIXME: Need to check for other types
123                          */
124                         if (colType == TdsColumnType.BigNVarChar)
125                                 size <<= 1;
126                         if (tds.IsLargeType (colType))
127                                 tds.Comm.Append ((short)size); // Parameter size passed in SqlParameter
128                         else if (tds.IsBlobType (colType))
129                                 tds.Comm.Append (size); // Parameter size passed in SqlParameter
130                         else 
131                                 tds.Comm.Append ((byte)size);
132
133                         // Precision and Scale are non-zero for only decimal/numeric
134                         if ( param.TypeName == "decimal" || param.TypeName == "numeric") {
135                                 tds.Comm.Append ((param.Precision!=0)?param.Precision:(byte)29);
136                                 tds.Comm.Append (param.Scale);
137                         }
138                 }
139                 #endregion
140         }
141 }
142 #endif