Beispiel #1
0
        private async Task <int> InsertBatchAsyncImp <T>(IEnumerable <T> pocos, BatchOptions options, bool sync)
        {
            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 += sync
                            ? ExecuteNonQueryHelper(cmd)
                            : await ExecuteNonQueryHelperAsync(cmd).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception x)
            {
                OnExceptionInternal(x);
                throw;
            }
            finally
            {
                CloseSharedConnectionInternal();
            }

            return(result);
        }
Beispiel #2
0
 public Task <int> UpdateBatchAsync <T>(IEnumerable <UpdateBatch <T> > pocos, BatchOptions options = null)
 {
     return(UpdateBatchAsyncImp(pocos, options, false));
 }
Beispiel #3
0
 public Task <int> InsertBatchAsync <T>(IEnumerable <T> pocos, BatchOptions options = null)
 {
     return(InsertBatchAsyncImp(pocos, options, false));
 }
Beispiel #4
0
        public async Task <int> UpdateBatchAsync <T>(IEnumerable <UpdateBatch <T> > pocos, BatchOptions options = null)
        {
            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.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 += await ExecuteNonQueryHelperAsync(cmd);
                    }
                }
            }
            catch (Exception x)
            {
                OnExceptionInternal(x);
                throw;
            }
            finally
            {
                CloseSharedConnectionInternal();
            }

            return(result);
        }