2 // Mono.Data.Tds.Protocol.Tds80.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Veerapuram Varadhan (vvaradhan@novell.com)
8 // Copyright (C) 2002 Tim Coleman
9 // Copyright (C) 2008,2009 Novell Inc.
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
36 namespace Mono.Data.Tds.Protocol {
37 public class Tds80 : Tds70
41 public static readonly TdsVersion Version = TdsVersion.tds80;
47 public Tds80 (string server, int port)
48 : this (server, port, 512, 15)
52 public Tds80 (string server, int port, int packetSize, int timeout)
53 : base (server, port, packetSize, timeout, Version)
57 #endregion // Constructors
61 protected override byte[] ClientVersion {
62 get { return new byte[] {0x00, 0x0, 0x0, 0x71};}
64 protected override byte Precision {
68 #endregion // Properties
72 public override bool Connect (TdsConnectionParameters connectionParameters)
74 //Console.WriteLine ("Tds80::Connect");
75 return base.Connect (connectionParameters);
78 protected override void ProcessColumnInfo ()
80 // We are connected to a Sql 7.0 server
81 if (TdsVersion < TdsVersion.tds80) {
82 base.ProcessColumnInfo ();
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 ();
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;
101 TdsColumnType columnType = (TdsColumnType) (Comm.GetByte () & 0xff);
102 //Console.WriteLine ("Actual ColumnType: {0}", columnType); TDS 8 Debugging
104 if ((byte) columnType == 0xef)
105 columnType = TdsColumnType.NChar;
107 TdsColumnType xColumnType = columnType;
108 if (IsLargeType (columnType)) {
109 if (columnType != TdsColumnType.NChar)
114 string tableName = null;
115 byte[] collation = null;
116 int lcid = 0, sortId = 0;
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 ();
125 columnSize = Comm.GetByte () & 0xff;
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);
138 if (IsBlobType (columnType)) {
139 tableName = Comm.GetString (Comm.GetTdsShort ());
140 //Console.WriteLine ("Tablename: "+tableName); TDS 8 Debugging
146 switch (columnType) {
147 case TdsColumnType.NText:
148 case TdsColumnType.NChar:
149 case TdsColumnType.NVarChar:
152 case TdsColumnType.Decimal:
153 case TdsColumnType.Numeric:
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
162 string columnName = Comm.GetString (Comm.GetByte ());
164 TdsDataColumn col = new TdsDataColumn ();
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;
178 col.SortOrder = sortId;
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;
191 col ["SortOrder"] = sortId;
194 //Console.WriteLine ("Tds80.cs: In ProcessColumnInfo... exit"); TDS 8 Debugging
197 protected override void ProcessOutputParam ()
199 // We are connected to a Sql 7.0 server
200 if (TdsVersion < TdsVersion.tds80) {
201 base.ProcessOutputParam ();
205 GetSubPacketLength ();
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)
212 TdsColumnType colType = (TdsColumnType) Comm.GetByte ();
213 object value = GetColumnValue (colType, true);
214 OutputParameters.Add (value);
217 public override void Execute (string commandText, TdsMetaParameterCollection parameters, int timeout, bool wantResults)
219 // We are connected to a Sql 7.0 server
220 if (TdsVersion < TdsVersion.tds80) {
221 base.Execute (commandText, parameters, timeout, wantResults);
225 Parameters = parameters;
226 string sql = commandText;
228 if (Parameters != null && Parameters.Count > 0) {
229 ExecRPC (TdsRpcProcId.ExecuteSql, commandText, parameters, timeout, wantResults);
232 sql = BuildExec (commandText);
233 ExecuteQuery (sql, timeout, wantResults);
237 public override void ExecPrepared (string commandText, TdsMetaParameterCollection parameters, int timeout, bool wantResults)
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);
246 TdsMetaParameterCollection parms = new TdsMetaParameterCollection ();
247 parms.Add (new TdsMetaParameter ("@Handle", "int", Int32.Parse (commandText)));
248 foreach (TdsMetaParameter parm in Parameters)
251 ExecRPC ("sp_execute", parms, timeout, wantResults);
254 #endregion // Methods