2004-11-26 Sureshkumar T <tsureshkumar@novell.com>
authorSureshkumar T <suresh@mono-cvs.ximian.com>
Fri, 26 Nov 2004 07:50:48 +0000 (07:50 -0000)
committerSureshkumar T <suresh@mono-cvs.ximian.com>
Fri, 26 Nov 2004 07:50:48 +0000 (07:50 -0000)
In System.Data.Odbc:
* OdbcParameter.cs: Fixed Parameter Size property as well as
insufficient buffer errors. bug #68749.

In Test/System.Data.Odbc:
* OdbcParameterCollectionTest.cs: New file for testing
OdbcParameterCollection's Add method.

svn path=/trunk/mcs/; revision=36621

mcs/class/System.Data/System.Data.Odbc/ChangeLog
mcs/class/System.Data/System.Data.Odbc/OdbcParameter.cs
mcs/class/System.Data/Test/System.Data.Odbc/ChangeLog
mcs/class/System.Data/Test/System.Data.Odbc/OdbcParameterCollectionTest.cs [new file with mode: 0644]

index f48450997fca1bf00747e8a51c6360eb3fbf20ba..6745ee7caf02919e722acca22ee166a48d3b3d5b 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-26  Sureshkumar T  <tsureshkumar@novell.com>
+
+       * OdbcParameter.cs: Fixed Parameter Size property as well as
+       insufficient buffer errors. bug #68749.
+
 2004-09-14  Sebastien Pouliot  <sebastien@ximian.com>
 
        * OdbcPermission.cs: Added internal constructor accepting an Odbc
index 407dd52689f8b64a87dfad209501d2d6591d80d7..bad499f6e65641189d2e3e65e26bdaa5812417b1 100644 (file)
@@ -81,6 +81,17 @@ namespace System.Data.Odbc
                {
                        this.name = name;
                        this.ParamValue = value;
+                        
+                        if (value != null && !value.GetType ().IsValueType) {
+                                Type type = value.GetType ();
+                                if (type.IsArray)
+                                        size = type.GetElementType () == typeof (byte) ? 
+                                                ((Array) value).Length : 0;
+                                else
+                                        size = value.ToString ().Length;
+                        }
+
+
                }
 
                public OdbcParameter (string name, OdbcType dataType) 
@@ -263,13 +274,17 @@ namespace System.Data.Odbc
                                // Init string buffer\r
                                 if (ParamValue is String)
                                         paramValueString = "\'"+paramValueString+"\'";
+
+                                 int minSize = size;
+                                 minSize = size > 20 ? size : 20;
+                                if (buffer == null || buffer.Length < minSize)
+                                         buffer = new byte[minSize];
+                                 else
+                                         buffer.Initialize();
                                  
-                                if (buffer == null || buffer.Length < ((size > 20) ? size : 20))\r
-                                       buffer = new byte[(size > 20) ? size : 20];\r
-                               else\r
-                                       buffer.Initialize();\r
-                               // Convert value into string and store into buffer\r
-                               System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);\r
+                                 // Convert value into string and store into buffer
+                                 minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
+                                 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);\r
                        }\r
                        bufferIsSet = true;\r
                }\r
index e7c1a8833ab1146664acf1cf6c4ab176ea97bb85..c1b17ab845ba247cf22a395ff62b382ed2fe05a4 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-26  Sureshkumar T  <tsureshkumar@novell.com>
+
+       * OdbcParameterCollectionTest.cs: New file for testing
+       OdbcParameterCollection's Add method.
+
 2004-09-15  Sebastien Pouliot  <sebastien@ximian.com>
 
        * OdbcPermissionTest.cs: New. Unit tests for OdbcPermission.
diff --git a/mcs/class/System.Data/Test/System.Data.Odbc/OdbcParameterCollectionTest.cs b/mcs/class/System.Data/Test/System.Data.Odbc/OdbcParameterCollectionTest.cs
new file mode 100644 (file)
index 0000000..7417fe6
--- /dev/null
@@ -0,0 +1,103 @@
+//
+// OdbcParameterCollectionTest.cs - NUnit Test Cases for testing the
+//                          OdbcParameterCollection class
+// Author:
+//      Sureshkumar T (TSureshkumar@novell.com)
+//
+// Copyright (c) 2004 Novell Inc., and the individuals listed
+// on the ChangeLog entries.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Data;
+using System.Data.Odbc;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Data.Odbc
+{
+
+        [TestFixture]
+        public class OdbcParameterCollectionTest : MySqlOdbcBaseClient
+        {
+                [Test]
+                public void OdbcParameterAddTest ()
+                {
+                        AddTest (new OdbcCommand ());
+                }
+
+                /// <remarks>
+                /// Test for parameter length of various data types.
+                /// </remarks>
+                public void AddTest (OdbcCommand cmd)
+                {
+                        OdbcParameter param = cmd.Parameters.Add ("param1", (int) 1);
+                        param = cmd.Parameters.Add ("param1", (long) 1);
+                        Assert.AreEqual (0, param.Size, "#1");
+                        param = cmd.Parameters.Add ("param1", (float) 1.0);
+                        Assert.AreEqual (0, param.Size, "#2");
+                        param = cmd.Parameters.Add ("param1", (double) 1.0);
+                        Assert.AreEqual (0, param.Size, "#3");
+                        param = cmd.Parameters.Add ("param1", 
+                                                    System.Text.ASCIIEncoding.ASCII.GetBytes("this is considerably long test"));
+                        Assert.AreEqual (30, param.Size, "#4");
+                        param = cmd.Parameters.Add ("param1", true);
+                        Assert.AreEqual (0, param.Size, "#5");
+                        param = cmd.Parameters.Add ("param1", "suresh");
+                        Assert.AreEqual (6, param.Size, "#6");
+                        param = cmd.Parameters.Add ("param1", DateTime.Now);
+                        Assert.AreEqual (0, param.Size, "#7");
+                        param = cmd.Parameters.Add ("param1", (object) DateTime.Now);
+                        Assert.AreEqual (0, param.Size, "#8");
+
+                        int [] arr = new int [] {1, 2, 3} ;
+                        param = cmd.Parameters.Add ("param1", arr);
+
+                        Assert.AreEqual (0, param.Size, "#8");
+       
+                }
+
+                /// <remarks>
+                /// This tests whether the value is trimmed to the
+                /// given length while passing parameters
+                /// </remarks>
+                [Test]
+                public void ParameterLengthTrimTest ()
+                {
+                        OpenConnection ();
+                        try {
+                                OdbcCommand cmd = new OdbcCommand();
+                                cmd.Connection  = conn;
+                                cmd.CommandType = CommandType.Text;
+                                cmd.CommandText = "SELECT count(*) FROM test WHERE col_char=?";
+                                                                                                                             
+                                OdbcParameter param = cmd.Parameters.Add("@col_char", OdbcType.Text, 15);
+                                param.Value = DateTime.Now.ToString ();
+                                Assert.AreEqual (15, param.Size, "#1");
+                                Convert.ToInt32(cmd.ExecuteScalar());
+                        } finally {
+                                CloseConnection ();
+                                
+                        }
+                }
+        }
+}