DataColumn _column;
// implementing class protocol
- protected abstract object GetValue (int index);
+ protected internal abstract object GetValue (int index);
internal abstract long GetInt64 (int index);
// used to set the array value to something neutral when the corresponding item is null (in the database sense)
{
object obj;
TypeCode tc;
-
+
+ if (value == null)
+ return null;
+
if (_type.IsInstanceOfType (value)) {
return value;
} else if ((tc = Type.GetTypeCode (_type)) == TypeCode.String) {
case TypeCode.UInt64:
return (Convert.ToUInt64 (value));
case TypeCode.DateTime:
+ if (value == DBNull.Value)
+ return DBNull.Value;
return (Convert.ToDateTime (value));
case TypeCode.Decimal:
return (Convert.ToDecimal (value));
default:
- throw new InvalidCastException ();
+ throw new InvalidCastException (string.Format ("Cannot convert from {0} to {1}", value.GetType ().FullName, _type.FullName));
}
} else if ((obj = GetExplicitValue (value)) != null) {
return (obj);
sealed class BitDataContainer : DataContainer {
BitArray _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((BitDataContainer) from)._values [from_index];
+ _values [to_index] = (bool) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class CharDataContainer : DataContainer {
char [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((CharDataContainer) from)._values [from_index];
+ _values [to_index] = (char) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class ByteDataContainer : DataContainer {
byte [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((ByteDataContainer) from)._values [from_index];
+ _values [to_index] = (byte) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class SByteDataContainer : DataContainer {
sbyte [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((SByteDataContainer) from)._values [from_index];
+ _values [to_index] = (sbyte) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class Int16DataContainer : DataContainer {
short [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((Int16DataContainer) from)._values [from_index];
+ _values [to_index] = (short) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class UInt16DataContainer : DataContainer {
ushort [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((UInt16DataContainer) from)._values [from_index];
+ _values [to_index] = (ushort) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class Int32DataContainer : DataContainer {
int [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((Int32DataContainer) from)._values [from_index];
+ _values [to_index] = (int) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class UInt32DataContainer : DataContainer {
uint [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((UInt32DataContainer) from)._values [from_index];
+ _values [to_index] = (uint) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class Int64DataContainer : DataContainer {
long [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((Int64DataContainer) from)._values [from_index];
+ _values [to_index] = (long) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class UInt64DataContainer : DataContainer {
ulong [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((UInt64DataContainer) from)._values [from_index];
+ _values [to_index] = (ulong) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class SingleDataContainer : DataContainer {
float [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((SingleDataContainer) from)._values [from_index];
+ _values [to_index] = (float) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
sealed class DoubleDataContainer : DataContainer {
double [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((DoubleDataContainer) from)._values [from_index];
+ _values [to_index] = (double) GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
class ObjectDataContainer : DataContainer {
object [] _values;
- protected override object GetValue (int index)
+ protected internal override object GetValue (int index)
{
return _values [index];
}
protected override void DoCopyValue (DataContainer from, int from_index, int to_index)
{
- _values [to_index] = ((ObjectDataContainer) from)._values [from_index];
+ _values [to_index] = GetContainerData (from.GetValue (from_index));
}
protected override int DoCompareValues (int index1, int index2)
using System;
using System.Collections;
+using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Globalization;
Assert.IsTrue (ex.Message.IndexOf ("'1'") != -1, "#B6");
}
}
-
+
+#if NET_4_0
+ [Test]
+ public void ImportRowTypeChangeTest ()
+ {
+ // this is from http://bugzilla.xamarin.com/show_bug.cgi?id=2926
+
+ Type [] types = new Type [] { typeof (string), typeof (sbyte), typeof (byte), typeof (short), typeof (ushort), typeof (int), typeof (uint), typeof (long), typeof (ulong), typeof (float), typeof (double), typeof (char), typeof (decimal), typeof (DateTime) };
+ object [] values = new object [] { "1", (sbyte) 1, (byte) 2, (short) 3, (ushort) 4, (int) 5, (uint) 6, (long) 7, (ulong) 8, (float) 9, (double) 10, 'z', (decimal) 13, new DateTime (24) };
+ int length = types.Length;
+
+ HashSet<Tuple<Type, Type>> invalid = new HashSet<Tuple<Type, Type>> () {
+ Tuple.Create (typeof (string), typeof (DateTime)),
+ Tuple.Create (typeof (sbyte), typeof (DateTime)),
+ Tuple.Create (typeof (byte), typeof (DateTime)),
+ Tuple.Create (typeof (short), typeof (DateTime)),
+ Tuple.Create (typeof (ushort), typeof (DateTime)),
+ Tuple.Create (typeof (int), typeof (DateTime)),
+ Tuple.Create (typeof (uint), typeof (DateTime)),
+ Tuple.Create (typeof (long), typeof (DateTime)),
+ Tuple.Create (typeof (ulong), typeof (DateTime)),
+ Tuple.Create (typeof (float), typeof (char)),
+ Tuple.Create (typeof (float), typeof (DateTime)),
+ Tuple.Create (typeof (double), typeof (char)),
+ Tuple.Create (typeof (double), typeof (DateTime)),
+ Tuple.Create (typeof (char), typeof (float)),
+ Tuple.Create (typeof (char), typeof (double)),
+ Tuple.Create (typeof (char), typeof (decimal)),
+ Tuple.Create (typeof (char), typeof (DateTime)),
+ Tuple.Create (typeof (Decimal), typeof (char)),
+ Tuple.Create (typeof (Decimal), typeof (DateTime)),
+ Tuple.Create (typeof (DateTime), typeof (sbyte)),
+ Tuple.Create (typeof (DateTime), typeof (byte)),
+ Tuple.Create (typeof (DateTime), typeof (short)),
+ Tuple.Create (typeof (DateTime), typeof (ushort)),
+ Tuple.Create (typeof (DateTime), typeof (int)),
+ Tuple.Create (typeof (DateTime), typeof (uint)),
+ Tuple.Create (typeof (DateTime), typeof (long)),
+ Tuple.Create (typeof (DateTime), typeof (ulong)),
+ Tuple.Create (typeof (DateTime), typeof (float)),
+ Tuple.Create (typeof (DateTime), typeof (double)),
+ Tuple.Create (typeof (DateTime), typeof (char)),
+ Tuple.Create (typeof (DateTime), typeof (decimal)),
+ };
+
+ for (int a = 0; a < length; a++) {
+ for (int b = 0; b < length; b++) {
+ DataSet ds = new DataSet ();
+ DataTable dt1 = ds.Tables.Add ("T1");
+ DataTable dt2 = ds.Tables.Add ("T2");
+
+ string name = "C-" + types [a].Name + "-to-" + types [b].Name;
+ dt1.Columns.Add (name, types [a]);
+ dt2.Columns.Add (name, types [b]);
+
+ DataRow r1 = dt1.NewRow ();
+ dt1.Rows.Add (r1);
+
+ r1 [0] = values [a];
+
+ if (invalid.Contains (Tuple.Create (types [a], types [b]))) {
+ try {
+ dt2.ImportRow (r1);
+ Assert.Fail ("#B: " + name + " expected ArgumentException");
+ } catch /*(ArgumentException)*/ {
+ continue;
+ }
+ } else {
+ dt2.ImportRow (r1);
+ DataRow r2 = dt2.Rows [0];
+ Assert.AreEqual (types [b], r2 [0].GetType (), "#A: " + name);
+ }
+ }
+ }
+ }
+#endif
+
[Test]
public void ClearReset () //To test Clear and Reset methods
{