2 // Mono.Data.Tds.Protocol.Tds42.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) 2002 Tim Coleman
12 namespace Mono.Data.Tds.Protocol {
13 public class Tds42 : Tds
17 public static readonly TdsVersion Version = TdsVersion.tds42;
23 public Tds42 (string server, int port)
24 : this (server, port, 512, 15)
28 public Tds42 (string server, int port, int packetSize, int timeout)
29 : base (server, port, packetSize, timeout, Version)
33 #endregion // Constructors
37 public override bool Connect (TdsConnectionParameters connectionParameters)
40 throw new InvalidOperationException ("The connection is already open.");
42 SetCharset (connectionParameters.Charset);
43 SetLanguage (connectionParameters.Language);
46 byte[] empty = new byte[0];
48 Comm.StartPacket (TdsPacketType.Logon);
50 // hostname (offset 0)
51 byte[] tmp = Comm.Append (connectionParameters.Hostname, 30, pad);
52 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
54 // username (offset 31 0x1f)
55 tmp = Comm.Append (connectionParameters.User, 30, pad);
56 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
58 // password (offset 62 0x3e)
59 tmp = Comm.Append (connectionParameters.Password, 30, pad);
60 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
62 // hostproc (offset 93 0x5d)
63 Comm.Append ("00000116", 8, pad);
65 // unused (offset 109 0x6d)
66 Comm.Append (empty, (30-14), pad);
69 Comm.Append ((byte) 0x0);
70 Comm.Append ((byte) 0xa0);
71 Comm.Append ((byte) 0x24);
72 Comm.Append ((byte) 0xcc);
73 Comm.Append ((byte) 0x50);
74 Comm.Append ((byte) 0x12);
77 Comm.Append ((byte) 8);
79 // Byte order of 2 byte ints
80 // 2 = <MSB, LSB>, 3 = <LSB, MSB>
81 Comm.Append ((byte) 3);
83 // Byte order of 4 byte ints
84 // 0 = <MSB, LSB>, 1 = <LSB, MSB>
85 Comm.Append ((byte) 1);
87 // Character representation
88 // (6 = ASCII, 7 = EBCDIC)
89 Comm.Append ((byte) 6);
91 // Eight byte floating point representation
92 // 4 = IEEE <MSB, ..., LSB>
94 // 10 = IEEE <LSB, ..., MSB>
96 Comm.Append ((byte) 10);
98 // Eight byte date format
99 // 8 = <MSB, ..., LSB>
100 Comm.Append ((byte) 9);
103 Comm.Append ((byte) 1);
105 // disallow dump/load and bulk insert
106 Comm.Append ((byte) 1);
108 // sql interface type
109 Comm.Append ((byte) 0);
111 // type of network connection
112 Comm.Append ((byte) 0);
116 Comm.Append (empty, 7, pad);
118 tmp = Comm.Append (connectionParameters.ApplicationName, 30, pad);
119 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
122 tmp = Comm.Append (DataSource, 30, pad);
123 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
126 Comm.Append (empty, 2, pad);
127 tmp = Comm.Append (connectionParameters.Password, 253, pad);
128 Comm.Append ((byte) (tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));
131 Comm.Append ((byte) (((byte) Version) / 10));
132 Comm.Append ((byte) (((byte) Version) % 10));
133 Comm.Append ((byte) 0);
134 Comm.Append ((byte) 0);
137 tmp = Comm.Append (connectionParameters.ProgName, 10, pad);
138 Comm.Append ((byte) (tmp.Length < 10 ? tmp.Length : 10));
141 Comm.Append ((byte) 6);
143 // Tell the server we can handle SQLServer version 6
144 Comm.Append ((byte) 0);
146 // Send zero to tell the server we can't handle any other version
147 Comm.Append ((byte) 0);
148 Comm.Append ((byte) 0);
150 // auto convert short
151 Comm.Append ((byte) 0);
154 Comm.Append ((byte) 0x0d);
157 Comm.Append ((byte) 0x11);
160 tmp = Comm.Append (Language, 30, pad);
161 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
163 // notify on lang change
164 Comm.Append ((byte) 1);
166 // security label hierarchy
167 Comm.Append ((short) 0);
169 // security components
170 Comm.Append (empty, 8, pad);
173 Comm.Append ((short) 0);
175 // security login role
176 Comm.Append ((byte) 0);
179 tmp = Comm.Append (Charset, 30, pad);
180 Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
182 // notify on charset change
183 Comm.Append ((byte) 1);
185 // length of tds packets
186 tmp = Comm.Append (PacketSize.ToString (), 6, pad);
187 Comm.Append ((byte) 3);
189 // pad out to a longword
190 Comm.Append (empty, 8, pad);
200 protected override TdsPacketColumnInfoResult ProcessColumnInfo ()
204 int totalLength = Comm.GetTdsShort ();
207 TdsPacketColumnInfoResult result = new TdsPacketColumnInfoResult ();
209 while (bytesRead < totalLength) {
214 byte[] flagData = new byte[4];
215 for (int i = 0; i < 4; i += 1) {
216 flagData[i] = Comm.GetByte ();
219 bool nullable = (flagData[2] & 0x01) > 0;
220 bool caseSensitive = (flagData[2] & 0x02) > 0;
221 bool writable = (flagData[2] & 0x0c) > 0;
222 bool autoIncrement = (flagData[2] & 0x10) > 0;
224 string tableName = String.Empty;
225 TdsColumnType columnType = (TdsColumnType) Comm.GetByte ();
229 if (columnType == TdsColumnType.Text || columnType == TdsColumnType.Image) {
233 int tableNameLength = Comm.GetTdsShort ();
235 tableName = Comm.GetString (tableNameLength);
236 bytesRead += tableNameLength;
237 bufLength = 2 << 31 - 1;
239 else if (columnType == TdsColumnType.Decimal || columnType == TdsColumnType.Numeric) {
240 bufLength = Comm.GetByte ();
242 precision = Comm.GetByte ();
244 scale = Comm.GetByte ();
247 else if (IsFixedSizeColumn (columnType))
248 bufLength = LookupBufferSize (columnType);
250 bufLength = (int) Comm.GetByte () & 0xff;
254 int index = result.Add (new TdsSchemaInfo ());
255 result[index]["NumericPrecision"] = precision;
256 result[index]["NumericScale"] = scale;
257 result[index]["ColumnSize"] = bufLength;
258 result[index]["ColumnName"] = ColumnNames[index];
259 result[index]["ColumnType"] = columnType;
260 result[index]["BaseTableName"] = tableName;
261 result[index]["AllowDBNull"] = nullable;
262 result[index]["IsReadOnly"] = !writable;
268 #endregion // Methods