Inheritance: IPocoDataFactory
Example #1
0
        public TableInfo TableInfoForType(Type type)
        {
            PocoDataFactory.Guard(type);
            var pocoDataBuilder = _pocoDatas.Get(type, () => BaseClassFalbackPocoDataBuilder(type));

            return(pocoDataBuilder.BuildTableInfo());
        }
Example #2
0
        public void Test11()
        {
            var fakeReader = new FakeReader();
            var pocoDataFactory = new PocoDataFactory(new MapperCollection());
            var newPropertyMapper = new PropertyMapper();
            var pocoData = pocoDataFactory.ForType(typeof(NestedConvention));
            newPropertyMapper.Init(fakeReader, pocoData);
            newPropertyMapper.Map(fakeReader, new RowMapperContext() { PocoData = pocoData });

            var sw = Stopwatch.StartNew();

            for (int j = 0; j < 1000; j++)
            {
                newPropertyMapper = new PropertyMapper();
                newPropertyMapper.Init(fakeReader, pocoData);

                for (int i = 0; i < 1000; i++)
                {
                    newPropertyMapper.Map(fakeReader, new RowMapperContext() { PocoData = pocoData });
                }
            }

            sw.Stop();

            Console.WriteLine("Elapsed Time (ms): {0}", sw.ElapsedMilliseconds);
            //250
        }
Example #3
0
        public TableInfo TableInfoForType(Type type)
        {
            PocoDataFactory.Guard(type);
            var pocoDataBuilder = _pocoDatas.Get(type, () => Resolver(type, this));

            return(pocoDataBuilder.BuildTableInfo());
        }
Example #4
0
        private Sql GetExistsSql <T>(object primaryKeyorPoco, bool isPoco)
        {
            var index = 0;
            var pd    = PocoDataFactory.ForType(typeof(T));
            var primaryKeyValuePairs = GetPrimaryKeyValues(this, pd, pd.TableInfo.PrimaryKey, primaryKeyorPoco, isPoco);
            var sql  = string.Format(DatabaseType.GetExistsSql(), DatabaseType.EscapeTableName(pd.TableInfo.TableName), BuildPrimaryKeySql(this, primaryKeyValuePairs, ref index));
            var args = primaryKeyValuePairs.Select(x => x.Value).ToArray();

            return(new Sql(sql, args));
        }
Example #5
0
        public Task <int> UpdateAsync <T>(T poco, Expression <Func <T, object> > fields)
        {
            var expression = DatabaseType.ExpressionVisitor <T>(this, PocoDataFactory.ForType(typeof(T)));

            expression = expression.Select(fields);
            var columnNames = ((ISqlExpression)expression).SelectMembers.Select(x => x.PocoColumn.ColumnName);
            var otherNames  = ((ISqlExpression)expression).GeneralMembers.Select(x => x.PocoColumn.ColumnName);

            return(UpdateAsync(poco, columnNames.Union(otherNames)));
        }
Example #6
0
        public void Test1()
        {
            var pocoData = new PocoDataFactory(new MapperCollection()).ForType(typeof(RecursionUser));

            Assert.AreEqual(4, pocoData.Members.Count);
            Assert.AreEqual("Id", pocoData.Members[0].Name);
            Assert.AreEqual("Name", pocoData.Members[1].Name);
            Assert.AreEqual("Supervisor", pocoData.Members[2].Name);
            Assert.AreEqual("CreatedBy", pocoData.Members[3].Name);
        }
Example #7
0
        public async Task SaveAsync <T>(T poco)
        {
            var tableInfo = PocoDataFactory.TableInfoForType(poco.GetType());

            if (await IsNewAsync(poco).ConfigureAwait(false))
            {
                await InsertAsync(tableInfo.TableName, tableInfo.PrimaryKey, tableInfo.AutoIncrement, poco).ConfigureAwait(false);
            }
            else
            {
                await UpdateAsync(tableInfo.TableName, tableInfo.PrimaryKey, poco, null, null).ConfigureAwait(false);
            }
        }
Example #8
0
        public void NestedClassShouldBeMappedAsAComplexObject()
        {
            var pocoData = new PocoDataFactory(new MapperCollection()).ForType(typeof(ComplexMap));

            Assert.AreEqual(7, pocoData.Columns.Count);
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("Id"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("Name"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("NestedComplexMap__Id"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("NestedComplexMap__NestedComplexMap2__Id"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("NestedComplexMap__NestedComplexMap2__Name"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("NestedComplexMap2__Id"));
            Assert.AreEqual(true, pocoData.Columns.ContainsKey("NestedComplexMap2__Name"));
        }
Example #9
0
        private async Task <int> UpdateBatchAsyncImp <T>(IEnumerable <UpdateBatch <T> > pocos, BatchOptions options, bool sync)
        {
            options = options ?? new BatchOptions();
            int result = 0;

            try
            {
                OpenSharedConnectionInternal();
                PocoData pd = null;

                foreach (var batchedPocos in pocos.Chunkify(options.BatchSize))
                {
                    var preparedUpdates = batchedPocos.Select(x =>
                    {
                        if (pd == null)
                        {
                            pd = PocoDataFactory.ForType(x.Poco.GetType());
                        }
                        return(UpdateStatements.PrepareUpdate(this, pd, pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, x.Poco, null, x.Snapshot?.UpdatedColumns()));
                    }).ToArray();

                    var sql = new Sql();
                    foreach (var preparedUpdate in preparedUpdates)
                    {
                        if (preparedUpdate.Sql != null)
                        {
                            sql.Append(preparedUpdate.Sql + options.StatementSeperator, preparedUpdate.Rawvalues.ToArray());
                        }
                    }

                    using (var cmd = CreateCommand(_sharedConnection, sql.SQL, sql.Arguments))
                    {
                        result += sync
                            ? ExecuteNonQueryHelper(cmd)
                            : await ExecuteNonQueryHelperAsync(cmd).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception x)
            {
                OnExceptionInternal(x);
                throw;
            }
            finally
            {
                CloseSharedConnectionInternal();
            }

            return(result);
        }
Example #10
0
 public void NestedClassShouldBeAbleToGetValue()
 {
     var pocoData = new PocoDataFactory(new MapperCollection()).ForType(typeof(ComplexMap));
     var obj = new ComplexMap()
     {
         Name = "Bill",
         NestedComplexMap = new NestedComplexMap()
         {
             Id = 9
         }
     };
     var val = pocoData.Columns["NestedComplexMap__Id"].GetValue(obj);
     Assert.AreEqual(9, val);
 }
Example #11
0
        public async Task <int> InsertBatchAsync <T>(IEnumerable <T> pocos, BatchOptions options = null)
        {
            options = options ?? new BatchOptions();
            var result = 0;

            try
            {
                OpenSharedConnectionInternal();
                PocoData pd = null;

                foreach (var batchedPocos in pocos.Chunkify(options.BatchSize))
                {
                    var preparedInserts = batchedPocos.Select(x =>
                    {
                        if (pd == null)
                        {
                            pd = PocoDataFactory.ForType(x.GetType());
                        }
                        return(InsertStatements.PrepareInsertSql(this, pd, pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, pd.TableInfo.AutoIncrement, x));
                    }).ToArray();

                    var sql = new Sql();
                    foreach (var preparedInsertSql in preparedInserts)
                    {
                        sql.Append(preparedInsertSql.Sql + options.StatementSeperator, preparedInsertSql.Rawvalues.ToArray());
                    }

                    using (var cmd = CreateCommand(_sharedConnection, sql.SQL, sql.Arguments))
                    {
                        result += await ExecuteNonQueryHelperAsync(cmd);
                    }
                }
            }
            catch (Exception x)
            {
                OnExceptionInternal(x);
                throw;
            }
            finally
            {
                CloseSharedConnectionInternal();
            }

            return(result);
        }
Example #12
0
        /// <summary>
        /// Performs an SQL Insert
        /// </summary>
        /// <param name="tableName">The name of the table to insert into</param>
        /// <param name="primaryKeyName">The name of the primary key column of the table</param>
        /// <param name="autoIncrement">True if the primary key is automatically allocated by the DB</param>
        /// <param name="poco">The POCO object that specifies the column values to be inserted</param>
        /// <returns>The auto allocated primary key of the new record, or null for non-auto-increment tables</returns>
        /// <remarks>Inserts a poco into a table.  If the poco has a property with the same name
        /// as the primary key the id of the new record is assigned to it.  Either way,
        /// the new id is returned.</remarks>
        public virtual Task <object> InsertAsync <T>(string tableName, string primaryKeyName, bool autoIncrement, T poco)
        {
            var pd = PocoDataFactory.ForObject(poco, primaryKeyName, autoIncrement);

            return(InsertAsyncImp(pd, tableName, primaryKeyName, autoIncrement, poco, false));
        }
Example #13
0
        /// <summary>
        /// Performs an SQL Insert
        /// </summary>
        /// <param name="poco">The POCO object that specifies the column values to be inserted</param>
        /// <returns>The auto allocated primary key of the new record, or null for non-auto-increment tables</returns>
        /// <remarks>The name of the table, it's primary key and whether it's an auto-allocated primary key are retrieved
        /// from the POCO's attributes</remarks>
        public Task <object> InsertAsync <T>(T poco)
        {
            var tableInfo = PocoDataFactory.TableInfoForType(poco.GetType());

            return(InsertAsync(tableInfo.TableName, tableInfo.PrimaryKey, tableInfo.AutoIncrement, poco));
        }
Example #14
0
        public Task <int> DeleteAsync(object poco)
        {
            var tableInfo = PocoDataFactory.TableInfoForType(poco.GetType());

            return(DeleteAsync(tableInfo.TableName, tableInfo.PrimaryKey, poco));
        }
Example #15
0
        public Task <int> UpdateAsync(object poco, object primaryKeyValue, IEnumerable <string> columns)
        {
            var tableInfo = PocoDataFactory.TableInfoForType(poco.GetType());

            return(UpdateAsync(tableInfo.TableName, tableInfo.PrimaryKey, poco, primaryKeyValue, columns));
        }
Example #16
0
 public PocoData ForObject(object o, string primaryKeyName, bool autoIncrement)
 {
     return(PocoDataFactory.ForObjectStatic(o, primaryKeyName, autoIncrement, ForType));
 }