{
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)
// 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
--- /dev/null
+//
+// 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 ();
+
+ }
+ }
+ }
+}