Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds.Protocol / Tds80.cs
1 //
2 // Mono.Data.Tds.Protocol.Tds80.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //       Veerapuram Varadhan  (vvaradhan@novell.com)
7 //
8 // Copyright (C) 2002 Tim Coleman
9 // Copyright (C) 2008,2009 Novell Inc.
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using Mono.Data.Tds;
34 using System;
35
36 namespace Mono.Data.Tds.Protocol {
37         public class Tds80 : Tds70
38         {
39                 #region Fields
40
41                 public static readonly TdsVersion Version = TdsVersion.tds80;
42
43                 #endregion // Fields
44
45                 #region Constructors
46
47                 public Tds80 (string server, int port)
48                         : this (server, port, 512, 15)
49                 {
50                 }
51
52                 public Tds80 (string server, int port, int packetSize, int timeout)
53                         : base (server, port, packetSize, timeout, Version)
54                 {
55                 }
56
57                 #endregion // Constructors
58
59                 #region Properties
60                 
61                 protected override byte[] ClientVersion {
62                         get { return new byte[] {0x00, 0x0, 0x0, 0x71};}
63                 }
64                 protected override byte Precision {
65                         get { return 38; }
66                 }
67                 
68                 #endregion // Properties
69                 
70                 #region Methods
71
72                 public override bool Connect (TdsConnectionParameters connectionParameters)
73                 {
74                         //Console.WriteLine ("Tds80::Connect");
75                         return base.Connect (connectionParameters);
76                 }
77
78                 protected override void ProcessColumnInfo ()
79                 {
80                         // We are connected to a Sql 7.0 server
81                         if (TdsVersion < TdsVersion.tds80) {
82                                 base.ProcessColumnInfo ();
83                                 return;
84                         }
85                         
86                         // VARADHAN: TDS 8 Debugging
87                         //Console.WriteLine ("Tds80.cs: In ProcessColumnInfo... entry");
88                         int numColumns = Comm.GetTdsShort ();
89                         //Console.WriteLine ("Column count={0}", numColumns); TDS 8 Debugging
90                         for (int i = 0; i < numColumns; i += 1) {
91                                 byte[] flagData = new byte[4];
92                                 for (int j = 0; j < 4; j += 1) 
93                                         flagData[j] = Comm.GetByte ();
94
95                                 bool nullable = (flagData[2] & 0x01) > 0;
96                                 //bool caseSensitive = (flagData[2] & 0x02) > 0;
97                                 bool writable = (flagData[2] & 0x0c) > 0;
98                                 bool autoIncrement = (flagData[2] & 0x10) > 0;
99                                 bool isIdentity = (flagData[2] & 0x10) > 0;
100
101                                 TdsColumnType columnType = (TdsColumnType) (Comm.GetByte () & 0xff);
102                                 //Console.WriteLine ("Actual ColumnType: {0}", columnType);  TDS 8 Debugging
103
104                                 if ((byte) columnType == 0xef)
105                                         columnType = TdsColumnType.NChar;
106
107                                 TdsColumnType xColumnType = columnType;
108                                 if (IsLargeType (columnType)) {
109                                         if (columnType != TdsColumnType.NChar)
110                                                 columnType -= 128;
111                                 }
112
113                                 int columnSize;
114                                 string tableName = null;
115                                 byte[] collation = null;
116                                 int lcid = 0, sortId = 0;
117
118                                 if (IsBlobType (columnType)) {
119                                         columnSize = Comm.GetTdsInt ();
120                                 } else if (IsFixedSizeColumn (columnType)) {
121                                         columnSize = LookupBufferSize (columnType);
122                                 } else if (IsLargeType (xColumnType)) {
123                                         columnSize = Comm.GetTdsShort ();
124                                 } else  {
125                                         columnSize = Comm.GetByte () & 0xff;
126                                 }
127
128                                 if (xColumnType == TdsColumnType.BigChar || xColumnType == TdsColumnType.BigNVarChar ||
129                                     xColumnType == TdsColumnType.BigVarChar || xColumnType == TdsColumnType.NChar ||
130                                     xColumnType == TdsColumnType.NVarChar ||   xColumnType == TdsColumnType.Text ||
131                                     xColumnType == TdsColumnType.NText) {
132                                     // Read collation for SqlServer 2000 and beyond
133                                     collation = Comm.GetBytes (5, true);
134                                         lcid = TdsCollation.LCID (collation);
135                                         sortId = TdsCollation.SortId (collation);
136                                 }
137
138                                 if (IsBlobType (columnType)) {
139                                         tableName = Comm.GetString (Comm.GetTdsShort ());
140                                         //Console.WriteLine ("Tablename: "+tableName);  TDS 8 Debugging
141                                 }
142
143                                 byte precision = 0;
144                                 byte scale = 0;
145
146                                 switch (columnType) {
147                                 case TdsColumnType.NText:
148                                 case TdsColumnType.NChar:
149                                 case TdsColumnType.NVarChar:
150                                         columnSize /= 2;
151                                         break;
152                                 case TdsColumnType.Decimal:
153                                 case TdsColumnType.Numeric:
154                                         //Comm.Skip (1);
155                                         precision = Comm.GetByte ();
156                                         //Console.WriteLine ("Precision: {0}", precision);  TDS 8 Debugging
157                                         scale = Comm.GetByte ();
158                                         //Console.WriteLine ("Scale: {0}", scale);  TDS 8 Debugging
159                                         break;
160                                 }
161
162                                 string columnName = Comm.GetString (Comm.GetByte ());
163
164                                 TdsDataColumn col = new TdsDataColumn ();
165                                 Columns.Add (col);
166 #if NET_2_0
167                                 col.ColumnType = columnType;
168                                 col.ColumnName = columnName;
169                                 col.IsAutoIncrement = autoIncrement;
170                                 col.IsIdentity = isIdentity;
171                                 col.ColumnSize = columnSize;
172                                 col.NumericPrecision = precision;
173                                 col.NumericScale = scale;
174                                 col.IsReadOnly = !writable;
175                                 col.AllowDBNull = nullable;
176                                 col.BaseTableName = tableName;
177                                 col.LCID = lcid;
178                                 col.SortOrder = sortId;
179 #else
180                                 col ["ColumnType"] = columnType;
181                                 col ["ColumnName"] = columnName;
182                                 col ["IsAutoIncrement"] = autoIncrement;
183                                 col ["IsIdentity"] = isIdentity;
184                                 col ["ColumnSize"] = columnSize;
185                                 col ["NumericPrecision"] = precision;
186                                 col ["NumericScale"] = scale;
187                                 col ["IsReadOnly"] = !writable;
188                                 col ["AllowDBNull"] = nullable;
189                                 col ["BaseTableName"] = tableName;
190                                 col ["LCID"] = lcid;
191                                 col ["SortOrder"] = sortId;
192 #endif
193                         }
194                         //Console.WriteLine ("Tds80.cs: In ProcessColumnInfo... exit");  TDS 8 Debugging
195                 }
196
197                 protected override void ProcessOutputParam ()
198                 {
199                         // We are connected to a Sql 7.0 server
200                         if (TdsVersion < TdsVersion.tds80) {
201                                 base.ProcessOutputParam ();
202                                 return;
203                         }
204
205                         GetSubPacketLength ();
206                         
207                         Comm.Skip ((Comm.GetByte () & 0xff) <<1); // Parameter name
208                         Comm.Skip (1);  // Status: 0x01 - in case of OUTPUT parameter
209                                                         // Status: 0x02 - in case of return value of UDF
210                         Comm.Skip (4);  // Usertype - sizeof (ULong)
211
212                         TdsColumnType colType = (TdsColumnType) Comm.GetByte ();
213                         object value = GetColumnValue (colType, true);
214                         OutputParameters.Add (value);
215                 }
216                 
217                 public override void Execute (string commandText, TdsMetaParameterCollection parameters, int timeout, bool wantResults)
218                 {
219                         // We are connected to a Sql 7.0 server
220                         if (TdsVersion < TdsVersion.tds80) {
221                                 base.Execute (commandText, parameters, timeout, wantResults);
222                                 return;
223                         }
224
225                         Parameters = parameters;
226                         string sql = commandText;
227
228                         if (Parameters != null && Parameters.Count > 0) {
229                                 ExecRPC (TdsRpcProcId.ExecuteSql, commandText, parameters, timeout, wantResults);
230                         } else {
231                                 if (wantResults)
232                                         sql = BuildExec (commandText);
233                                 ExecuteQuery (sql, timeout, wantResults);
234                         }
235                 }
236                 
237                 public override void ExecPrepared (string commandText, TdsMetaParameterCollection parameters, int timeout, bool wantResults)
238                 {
239                         Parameters = parameters;
240                         // We are connected to a Sql 7.0 server
241                         if (TdsVersion < TdsVersion.tds80 || 
242                             Parameters == null || Parameters.Count < 1) {
243                                 base.ExecPrepared (commandText, parameters, timeout, wantResults);
244                                 return;
245                         }
246                         TdsMetaParameterCollection parms = new TdsMetaParameterCollection ();
247                         parms.Add (new TdsMetaParameter ("@Handle", "int", Int32.Parse (commandText)));
248                         foreach (TdsMetaParameter parm in Parameters)
249                                 parms.Add (parm);
250                         
251                         ExecRPC ("sp_execute", parms, timeout, wantResults);                    
252                 }
253
254                 #endregion // Methods
255         }
256 }