Beispiel #1
0
        public void RecordSet_Constructor_DataTable()
        {
            PortableRecordset recordset = null;

            using (var dataset = TestDataProvider.GetDataSet())
            {
                Assert.IsTrue(dataset.Tables.Count > 0);
                using (var table = dataset.Tables[0])
                {
                    recordset = new PortableRecordset(table);
                    Assert.AreEqual(table.Columns.Count, recordset.Fields.Length);
                    Assert.AreEqual(table.Rows.Count, recordset.Records.Length);
                    for (var i = 0; i < table.Rows.Count; ++i)
                    {
                        for (var j = 0; j < table.Columns.Count; ++j)
                        {
                            if (table.Rows[i].IsNull(j))
                            {
                                Assert.IsNull(recordset[i][j]);
                            }
                            else
                            {
                                Assert.AreEqual(table.Rows[i][j], recordset[i][j]);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void RecordSet_Constructor_RowsOnly()
        {
            PortableRecordset recordset = null;

            using (var dataset = TestDataProvider.GetDataSet())
            {
                Assert.IsTrue(dataset.Tables.Count > 0);
                using (var table = dataset.Tables[0])
                {
                    var rows = new object[table.Rows.Count][];
                    for (var i = 0; i < table.Rows.Count; ++i)
                    {
                        rows[i] = table.Rows[i].ItemArray;
                    }

                    recordset = new PortableRecordset(table.TableName, rows);

                    Assert.AreEqual(table.Columns.Count, recordset.Fields.Length);
                    Assert.AreEqual(table.Rows.Count, recordset.Records.Length);
                    for (var i = 0; i < table.Rows.Count; ++i)
                    {
                        for (var j = 0; j < table.Columns.Count; ++j)
                        {
                            Assert.AreEqual(table.Rows[i][j], recordset[i][j]);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// for binary types
        /// </summary>
        /// <param name="recordset"></param>
        /// <returns></returns>
        public static object[] ToObjectArray(this PortableRecordset recordset)
        {
            var result = new object[recordset.Count];

            for (var i = 0; i < recordset.Count; ++i)
            {
                result[i] = recordset[i, 0];
            }
            return(result);
        }
        public static T[] ToArrayParallel <T>(this PortableRecordset recordset) where T : class
        {
            var result = new T[recordset.Count];

            Parallel.For(0, recordset.Count, i =>
            {
                result[i] = recordset.ToObject <T>(i);
            });
            return(result);
        }
        public static T[] ToArray <T>(this PortableRecordset recordset) where T : class
        {
            var result = new T[recordset.Count];

            for (var i = 0; i < recordset.Count; ++i)
            {
                result[i] = recordset.ToObject <T>(i);
            }
            return(result);
        }
 public static Task <T[]> ToArrayAsync <T>(this PortableRecordset recordset) where T : class
 {
     return(Task.Run(() =>
     {
         var result = new T[recordset.Count];
         for (var i = 0; i < recordset.Count; ++i)
         {
             result[i] = recordset.ToObject <T>(i);
         }
         return result;
     }));
 }
        /// <summary>
        /// for value/scalor types like string, datetime, and numeric types
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="recordset"></param>
        /// <returns></returns>
        public static T[] ToValueArray <T>(this PortableRecordset recordset) where T : struct
        {
            var result = new T[recordset.Count];

            if (recordset.Count > 0)
            {
                var type = typeof(T);
                type = type.IsNullable() ? Nullable.GetUnderlyingType(type) : type;
                var doConvert = type != recordset[0, 0].GetType();
                for (var i = 0; i < recordset.Count; ++i)
                {
                    result[i] = (T)(doConvert ? Convert.ChangeType(recordset[i][0], type) : recordset[i][0]);
                }
            }
            return(result);
        }
Beispiel #8
0
        public void RecordSet_JsonSerialization()
        {
            PortableRecordset recordset = null;

            using (var dataset = TestDataProvider.GetDataSet())
            {
                Assert.IsTrue(dataset.Tables.Count > 0);
                using (var table = dataset.Tables[0])
                {
                    recordset = new PortableRecordset(table);
                    Assert.AreEqual(table.Columns.Count, recordset.Fields.Length);
                    Assert.AreEqual(table.Rows.Count, recordset.Records.Length);
                    for (var i = 0; i < table.Rows.Count; ++i)
                    {
                        for (var j = 0; j < table.Columns.Count; ++j)
                        {
                            if (table.Rows[i].IsNull(j))
                            {
                                Assert.IsNull(recordset[i][j]);
                            }
                            else
                            {
                                Assert.AreEqual(table.Rows[i][j], recordset[i][j]);
                            }
                        }
                    }
                }
            }
            var json = JsonConvert.SerializeObject(recordset);
            var rc   = JsonConvert.DeserializeObject <PortableRecordset>(json);

            // using to string to avoid the stupid int32 vs int64 casting issue
            for (var i = 0; i < recordset.Count; ++i)
            {
                for (var j = 0; j < recordset.FieldCount; ++j)
                {
                    if (recordset[i][j] == null)
                    {
                        Assert.IsNull(rc[i][j]);
                    }
                    else
                    {
                        Assert.AreEqual(recordset[i][j].ToString(), rc[i][j].ToString());
                    }
                }
            }
        }
Beispiel #9
0
        public void RecordSet_Append_Array()
        {
            var size = 2;
            var rows = new object[size][];
            var c    = 0;

            for (var i = 0; i < size; ++i)
            {
                rows[i] = new object[size];
                for (var j = 0; j < size; ++j)
                {
                    rows[i][j] = ++c;
                }
            }

            var recordset = new PortableRecordset("test", rows);

            Assert.AreEqual(size, recordset.Count);
            c = 0;
            for (var i = 0; i < size; ++i)
            {
                for (var j = 0; j < size; ++j)
                {
                    Assert.AreEqual(++c, recordset[i][j]);
                }
            }

            recordset.Append(rows);
            Assert.AreEqual(size * 2, recordset.Count);
            for (var o = 0; o < 2; ++o)
            {
                c = 0;
                for (var i = 0; i < size; ++i)
                {
                    for (var j = 0; j < size; ++j)
                    {
                        Assert.AreEqual(++c, recordset[i][j]);
                    }
                }
            }
        }
        public static T ToObject <T>(this PortableRecordset recordset, long recordIndex) where T : class
        {
            // allows activator to use non-public constructors
            var result = (T)Activator.CreateInstance(
                typeof(T),
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                null, null, null);

            for (var i = 0; i < TypeInfoCache <T> .ReadWriteProperties.Length; ++i)
            {
                var property     = TypeInfoCache <T> .ReadWriteProperties[i];
                var propertyName = TypeInfoCache <T> .ReadWritePropertyNames[i];
                var value        = recordset[recordIndex, propertyName];
                if (value != null)
                {
                    //todo: this can be optimized by passing in the table definition or a cache of row types from the ToArray methods
                    //todo: this can be optimized by getting the underlying nullable types in TypeInfoCache
                    var propertyType = property.PropertyType.IsNullable() ? Nullable.GetUnderlyingType(property.PropertyType) : property.PropertyType;
                    value = propertyType == value.GetType() ? value : Convert.ChangeType(value, propertyType);
                    property.SetValue(result, value);
                }
            }
            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TInterface"></typeparam>
        /// <typeparam name="TImplentation">The type to use to for the interface implementation. Must have public or protected constructor.</typeparam>
        /// <param name="recordset"></param>
        /// <returns></returns>
        public static TInterface[] ToArray <TInterface, TImplentation>(this PortableRecordset recordset) where TImplentation : class, TInterface
        {
            var interfaceType = typeof(TInterface);

            if (!interfaceType.IsInterface)
            {
                throw new OrmException($"Generic argument TInterface resolves to {interfaceType.FullName} which is not an interface.");
            }

            var instanceType = typeof(TImplentation);

            if (instanceType.GetInterface(interfaceType.Name) == null)
            {
                throw new OrmException($"Genertic argument TImplentation{instanceType.FullName} must implement interface {interfaceType.FullName}");
            }

            var result = new TInterface[recordset.Count];

            for (var i = 0; i < recordset.Count; ++i)
            {
                result[i] = recordset.ToObject <TImplentation>(i);
            }
            return(result);
        }