flush
[mcs.git] / class / Mono.Data.Tds / Mono.Data.Tds.Protocol / TdsBulkCopy.cs
blob2374eb71a79fc1e7c7be0f8f7ff9ebd738448f6d
1 //
2 // Mono.Data.Tds.Protocol.TdsBulkCopy.cs
3 //
4 // Author:
5 // Nagappan A (anagappan@novell.com)
6 //
7 // Copyright (C) 2007 Novell Inc
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.
29 #if NET_2_0
30 using System;
32 namespace Mono.Data.Tds.Protocol {
33 public class TdsBulkCopy
35 #region Fields
37 Tds tds;
38 #endregion
40 #region Constructors
42 public TdsBulkCopy (Tds tds)
44 this.tds = tds;
47 #endregion
49 #region Methods
50 public bool SendColumnMetaData (string colMetaData)
52 tds.Comm.StartPacket (TdsPacketType.Query);
53 tds.Comm.Append (colMetaData);
54 tds.ExecBulkCopyMetaData (30, false);
55 return true;
58 public bool BulkCopyStart (TdsMetaParameterCollection parameters)
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++;
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);
80 return true;
83 public bool BulkCopyData (object o, int size, bool isNewRow)
85 if (isNewRow) {
86 tds.Comm.Append ((byte) TdsPacketSubType.Row);
88 if (size > 0) {
89 tds.Comm.Append ((short) size);
91 tds.Comm.Append (o);
92 return true;
95 public bool BulkCopyEnd ()
97 tds.Comm.Append ((short) TdsPacketSubType.Done);
98 tds.ExecBulkCopy (30, false);
99 return true;
102 private void WriteParameterInfo (TdsMetaParameter param)
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
108 param.IsNullable = true;
109 TdsColumnType colType = param.GetMetaType ();
110 param.IsNullable = false;
112 tds.Comm.Append ((byte)colType); // type
114 int size = 0;
115 if (param.Size == 0)
116 size = param.GetActualSize ();
117 else
118 size = param.Size;
121 If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
122 FIXME: Need to check for other types
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);
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);
139 #endregion
142 #endif