Merge pull request #1163 from AerisG222/more_mvc_fixes
[mono.git] / mcs / class / Mono.Data.Tds / Mono.Data.Tds.Protocol / Tds50.cs
index 889f46afc1df81ac50f9e6637505916340a6d0aa..88219125df5f3d081963aed376c790a6cacc208f 100644 (file)
 using Mono.Data.Tds;
 using System;
 using System.Text;
+using System.Security;
 
-namespace Mono.Data.Tds.Protocol {
+namespace Mono.Data.Tds.Protocol
+{
        [MonoTODO ("FIXME: Can packetsize be anything other than 512?")]
-        public class Tds50 : Tds
+       public sealed class Tds50 : Tds
        {
                #region Fields
 
                public static readonly TdsVersion Version = TdsVersion.tds50;
                int packetSize;
-               bool isSelectQuery = false;
+               bool isSelectQuery;
 
                #endregion // Fields
 
@@ -70,7 +72,7 @@ namespace Mono.Data.Tds.Protocol {
                        StringBuilder set = new StringBuilder ();
                        StringBuilder declare = new StringBuilder ();
                        int count = 0;
-                       foreach (TdsMetaParameter p in Parameters) {    
+                       foreach (TdsMetaParameter p in Parameters) {
                                declare.Append (String.Format ("declare {0}\n", p.Prepare ()));
                                set.Append (String.Format ("select {0}=", p.ParameterName));
                                if (p.Direction == TdsParameterDirection.Input)
@@ -117,7 +119,7 @@ namespace Mono.Data.Tds.Protocol {
 
                        // password (offset 62 0x3e)
                        // 62-92
-                       tmp = Comm.Append (connectionParameters.Password, 30, pad);
+                       tmp = Comm.Append (GetPlainPassword(connectionParameters.Password), 30, pad);
                        Comm.Append ((byte) (tmp.Length < 30 ? tmp.Length : 30));
 
                        // hostproc (offset 93 0x5d)
@@ -186,7 +188,7 @@ namespace Mono.Data.Tds.Protocol {
                        // remote passwords
                        // 202-457      
                        Comm.Append (empty, 2, pad);
-                       tmp = Comm.Append (connectionParameters.Password, 253, pad);
+                       tmp = Comm.Append (GetPlainPassword(connectionParameters.Password), 253, pad);
                        Comm.Append ((byte) (tmp.Length < 253 ? tmp.Length + 2 : 253 + 2));
 
                        // tds version
@@ -256,7 +258,7 @@ namespace Mono.Data.Tds.Protocol {
 
                        // length of tds packets
                        // 557-563
-                       tmp = Comm.Append (PacketSize.ToString (), 6, pad);
+                       tmp = Comm.Append (this.packetSize.ToString (), 6, pad);
                        Comm.Append ((byte) (tmp.Length < 6 ? tmp.Length : 6));
 
                        Comm.Append (empty, 8, pad);
@@ -310,7 +312,7 @@ namespace Mono.Data.Tds.Protocol {
                public override void Execute (string sql, TdsMetaParameterCollection parameters, int timeout, bool wantResults)
                {
                        Parameters = parameters;
-                        string ex = BuildExec (sql);
+                       string ex = BuildExec (sql);
                        ExecuteQuery (ex, timeout, wantResults);
                }
 
@@ -319,8 +321,8 @@ namespace Mono.Data.Tds.Protocol {
                        Parameters = parameters;
                        ExecuteQuery (BuildProcedureCall (commandText), timeout, wantResults);
                }
-                
-                private string BuildProcedureCall (string procedure)
+
+               private string BuildProcedureCall (string procedure)
                {
                        string exec = String.Empty;
 
@@ -351,21 +353,19 @@ namespace Mono.Data.Tds.Protocol {
                                                count += 1;
                                        }
                                        
-                                       if (p.Direction == TdsParameterDirection.ReturnValue) {
+                                       if (p.Direction == TdsParameterDirection.ReturnValue)
                                                exec = p.ParameterName + "=";
-                                       }
                                }
                        }
-                        exec = "exec " + exec;
-                        
-                        string sql = String.Format ("{0}{1}{2}{3} {4}\n{5}", declare.ToString (), 
-                                                    set.ToString (), 
-                                                    exec, procedure, 
-                                                    BuildParameters (), select.ToString ());
+                       exec = "exec " + exec;
+
+                       string sql = String.Format ("{0}{1}{2}{3} {4}\n{5}", declare.ToString (),
+                               set.ToString (),
+                               exec, procedure,
+                               BuildParameters (), select.ToString ());
                        return sql;
                }
 
-                
                private string BuildParameters ()
                {
                        if (Parameters == null || Parameters.Count == 0)
@@ -422,7 +422,7 @@ namespace Mono.Data.Tds.Protocol {
                        case "image":
                        case "binary":
                        case "varbinary":
-                               return String.Format ("0x{0}", BitConverter.ToString ((byte[]) parameter.Value).Replace ("-", "").ToLower ());
+                               return String.Format ("0x{0}", BitConverter.ToString ((byte[]) parameter.Value).Replace ("-", string.Empty).ToLower ());
                        default:
                                return String.Format ("'{0}'", parameter.Value.ToString ().Replace ("'", "''"));
                        }
@@ -438,7 +438,7 @@ namespace Mono.Data.Tds.Protocol {
                                idBuilder.Append ((char) (rand.Next (26) + 65));
                        string id = idBuilder.ToString ();
 
-                       StringBuilder declare = new StringBuilder ();
+                       //StringBuilder declare = new StringBuilder ();
 
                
                        sql = String.Format ("create proc {0} as\n{1}", id, sql);
@@ -461,11 +461,10 @@ namespace Mono.Data.Tds.Protocol {
                        return id;
                }
 
-               protected override TdsDataColumnCollection ProcessColumnInfo ()
+               protected override void ProcessColumnInfo ()
                {
                        isSelectQuery = true; 
-                       TdsDataColumnCollection result = new TdsDataColumnCollection ();
-                       int totalLength = Comm.GetTdsShort ();  
+                       /*int totalLength = */Comm.GetTdsShort ();
                        int count = Comm.GetTdsShort ();
                        for (int i = 0; i < count; i += 1) {
                                string columnName = Comm.GetString (Comm.GetByte ());
@@ -507,20 +506,36 @@ namespace Mono.Data.Tds.Protocol {
                                if (isBlob)
                                        Comm.Skip (Comm.GetTdsShort ()); // Class ID
 
-                               int index = result.Add (new TdsDataColumn ());
-                               result[index]["NumericPrecision"] = precision;
-                               result[index]["NumericScale"] = scale;
-                               result[index]["ColumnSize"] = bufLength;
-                               result[index]["ColumnName"] = columnName;
-                               result[index]["AllowDBNull"] = allowDBNull;
-                               result[index]["IsReadOnly"] = !isUpdatable;
-                               result[index]["IsIdentity"] = isIdentity;
-                               result[index]["IsRowVersion"] = isRowVersion;
-                               result[index]["IsKey"] = isKey;
-                               result[index]["Hidden"] = hidden;
-                               result[index]["ColumnType"] = columnType;
+                               TdsDataColumn col = new TdsDataColumn ();
+                               Columns.Add (col);
+#if NET_2_0
+                               col.ColumnType = columnType;
+                               col.ColumnName = columnName;
+                               col.IsIdentity = isIdentity;
+                               col.IsRowVersion = isRowVersion;
+                               col.ColumnType = columnType;
+                               col.ColumnSize = bufLength;
+                               col.NumericPrecision = precision;
+                               col.NumericScale = scale;
+                               col.IsReadOnly = !isUpdatable;
+                               col.IsKey = isKey;
+                               col.AllowDBNull = allowDBNull;
+                               col.IsHidden = hidden;
+#else
+                               col ["ColumnType"] = columnType;
+                               col ["ColumnName"] = columnName;
+                               col ["IsIdentity"] = isIdentity;
+                               col ["IsRowVersion"] = isRowVersion;
+                               col ["ColumnType"] = columnType;
+                               col ["ColumnSize"] = bufLength;
+                               col ["NumericPrecision"] = precision;
+                               col ["NumericScale"] = scale;
+                               col ["IsReadOnly"] = !isUpdatable;
+                               col ["IsKey"] = isKey;
+                               col ["AllowDBNull"] = allowDBNull;
+                               col ["IsHidden"] = hidden;
+#endif
                        }
-                       return result;
                }
 
                private void SendParamFormat ()
@@ -555,9 +570,9 @@ namespace Mono.Data.Tds.Protocol {
 
                                Comm.Append ((byte) parameterName.Length);
                                Comm.Append (parameterName);
-                               Comm.Append (status);        
+                               Comm.Append (status);
                                Comm.Append (userType);
-                               Comm.Append ((byte) metaType);    
+                               Comm.Append ((byte) metaType);
 
                                if (!IsFixedSizeColumn (metaType))
                                        Comm.Append ((byte) p.Size);         // MAXIMUM SIZE
@@ -603,7 +618,7 @@ namespace Mono.Data.Tds.Protocol {
 
                protected override bool IsValidRowCount (byte status, byte op)
                {
-                       if (isSelectQuery) 
+                       if (isSelectQuery)
                                return (isSelectQuery = false);
 
                        // TODO : Need to figure out how to calculate rowcount inside stored 
@@ -611,7 +626,7 @@ namespace Mono.Data.Tds.Protocol {
                        // statements executing inside a StoredProcedure
 
                        if (((status & (byte)0x40) != 0) || ((status & (byte)0x10) == 0))
-                               return false ;
+                               return false;
 
                        return true;
                }