Example #1
0
        public void DMP004_GetColumnInfo_GivenPropertyWithColumnAttributeAndEntityWithOutExplicitAttribute()
        {
            var poco = new PetaPoco.Database.PocoData(typeof(Models.YottaEntity));

            // ついていなくてもとれる

            Assert.Equal(4, poco.Columns.Count);

            var colA = poco.Columns.Values.Single(x => x.PropertyInfo.Name == nameof(Models.YottaEntity.AnotherColumn));

            Assert.Equal("AnotherColumn", colA.ColumnName);
            Assert.False(colA.ResultColumn);

            var colB = poco.Columns.Values.Single(x => x.PropertyInfo.Name == nameof(Models.YottaEntity.ResultColumn));

            Assert.Equal("ResultColumn", colB.ColumnName);
            Assert.True(colB.ResultColumn);

            var colC = poco.Columns.Values.SingleOrDefault(x => x.PropertyInfo.Name == nameof(Models.YottaEntity.NotAColumn));

            Assert.Null(colC);

            var colD = poco.Columns.Values.Single(x => x.PropertyInfo.Name == nameof(Models.YottaEntity.Creater));

            Assert.Equal("Creater", colD.ColumnName);
            Assert.False(colD.ResultColumn);
        }
Example #2
0
        public void DMP002_GetTableInfo_GivenEntityWithOutTableAttribute()
        {
            var ti = new PetaPoco.Database.PocoData(typeof(Models.ExaEntity)).TableInfo;

            Assert.Equal("ExaEntity", ti.TableName);
            Assert.Equal("ID", ti.PrimaryKey);      // 勝手に"ID"という列がPK・・・としちゃう(ver5と違う)
            Assert.False(ti.AutoIncrement);         // AutoIncrementはfalse
            Assert.Null(ti.SequenceName);
        }
Example #3
0
        public void DMP001_GetTableInfo_GivenEntityWithTableAttribute()
        {
            var teraEntity = new PetaPoco.Database.PocoData(typeof(Models.TeraEntity)).TableInfo;

            Assert.Equal("TblTeraEntity", teraEntity.TableName);
            Assert.Equal("Id1,Id2,Id3", teraEntity.PrimaryKey);
            Assert.False(teraEntity.AutoIncrement);
            Assert.Null(teraEntity.SequenceName);

            var yottaEntity = new PetaPoco.Database.PocoData(typeof(Models.YottaEntity)).TableInfo;

            Assert.Equal("yotta_entities", yottaEntity.TableName);
            Assert.Equal("Id", yottaEntity.PrimaryKey);
            Assert.True(yottaEntity.AutoIncrement);
            Assert.Equal("SEQ_1", yottaEntity.SequenceName);
        }
Example #4
0
        public void DMP003_GetColumnInfo_GivenPropertyWithColumnAttributeAndEntityWithExplicitAttribute()
        {
            var poco = new PetaPoco.Database.PocoData(typeof(Models.TeraEntity));

            Assert.Equal(5, poco.Columns.Count);

            var colA = poco.Columns.Values.Single(x => x.PropertyInfo.Name == nameof(Models.TeraEntity.TheId2));

            Assert.Equal("Id2", colA.ColumnName);
            Assert.False(colA.ResultColumn);

            var colB = poco.Columns.Values.Single(x => x.PropertyInfo.Name == nameof(Models.TeraEntity.ResultColumn));

            Assert.Equal("result_column", colB.ColumnName);
            Assert.True(colB.ResultColumn);

            var colC = poco.Columns.Values.SingleOrDefault(x => x.PropertyInfo.Name == nameof(Models.TeraEntity.NotAColumn));

            Assert.Null(colC);
        }
Example #5
0
        /// <summary>
        /// Coerces an IDataReader to load an enumerable of T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rdr"></param>
        /// <param name="columnNames"></param>
        /// <param name="onItemCreated">Invoked when a new item is created</param>
        public static IEnumerable <T> ToEnumerable <T>(this IDataReader rdr, string sql, string connString, List <string> columnNames, Func <object, object> onItemCreated)
        {
            //mike added ColumnNames
            List <T> result = new List <T>();

            // PetaPoco code for faster object hydration...
            PetaPoco.Database.PocoData pd      = null;
            Func <IDataReader, T>      factory = null;

            while (rdr.Read())
            {
                T   instance = default(T);
                var type     = typeof(T);
                if (type.Name.Contains("AnonymousType"))
                {
                    //this is an anon type and it has read-only fields that are set
                    //in a constructor. So - read the fields and build it
                    //http://stackoverflow.com/questions/478013/how-do-i-create-and-access-a-new-instance-of-an-anonymous-class-passed-as-a-param
                    var      properties = type.GetProperties();
                    int      objIdx     = 0;
                    object[] objArray   = new object[properties.Length];

                    foreach (var prop in properties)
                    {
                        objArray[objIdx++] = rdr[prop.Name];
                    }

                    result.Add((T)Activator.CreateInstance(type, objArray));
                }
                //TODO: there has to be a better way to work with the type system
                else if (IsCoreSystemType(type))
                {
                    instance = (T)rdr.GetValue(0).ChangeTypeTo(type);
                    result.Add(instance);
                }
                else if (type.IsValueType)
                {
                    instance = Activator.CreateInstance <T>();
                    LoadValueType(rdr, ref instance);
                    result.Add(instance);
                }
                else
                {
                    // ===========================================
                    //      PetaPoco to the performance rescue
                    // -------------------------------------------
                    if (pd == null)
                    {
                        pd = PetaPoco.Database.PocoData.ForType(typeof(T));
                    }

                    if (factory == null)
                    {
                        if (columnNames != null && columnNames.Count > 0)
                        {
                            // Trim the SQL to last WHERE clause (so that we can reuse the emitted IL for faster object hydration).
                            // PetaPoco uses the (parameterized) SQL to distinguish between the factory methods, and with Linq
                            // expression in Subsonic, the full SQL is not unique (parameter values are generated inline in the SQL).
                            // Using this SQL untouched would result in new factories being created for every query with a WHERE clause,
                            // which would probably actually hurt overall performance.
                            factory = pd.GetFactory(sql.UntilLast("WHERE"), connString, false, 0, rdr.FieldCount, rdr, columnNames) as Func <IDataReader, T>;
                        }
                        else
                        {
                            factory = pd.GetFactory(sql, connString, false, 0, rdr.FieldCount, rdr) as Func <IDataReader, T>;
                        }
                    }

                    T poco = factory(rdr);
                    result.Add(poco);
                    // ===========================================
                }
            }

            return(result);
        }