예제 #1
0
        public IEnumerable <IDictionary <string, object> > Insert(AdoAdapter adapter, string tableName, IEnumerable <IDictionary <string, object> > dataList, IDbTransaction transaction, Func <IDictionary <string, object>, Exception, bool> onError,
                                                                  bool resultRequired)
        {
            //ToDo: support onError collection
            List <IDictionary <string, object> > result = new List <IDictionary <string, object> >();

            if (transaction == null)
            {
                adapter.InTransaction(currentTransaction =>
                {
                    result = (List <IDictionary <string, object> >)Insert(adapter, tableName, dataList, currentTransaction, onError, resultRequired);
                });
                return(result);
            }


            var table           = adapter.GetSchema().FindTable(tableName);
            var tableColumns    = table.Columns.Select(c => (FbColumn)c).ToArray();
            var nameToFbColumns = tableColumns.ToDictionary(c => c.HomogenizedName, c => c);

            var insertContext = CreateInsertSqlContext(table.QualifiedName, tableColumns);

            var queryBuilder      = new FbBulkInsertQueryBuilder(resultRequired, insertContext.ReturnsExecuteBlockSql);
            var insertSqlProvider = new FbBulkInsertSqlProvider();
            var currentColumns    = new List <InsertColumn>();

            foreach (var data in dataList)
            {
                var insertData = data.Where(p => nameToFbColumns.ContainsKey(p.Key.Homogenize())).Select(kv => new InsertColumn
                {
                    Value  = kv.Value,
                    Column = nameToFbColumns[kv.Key.Homogenize()]
                }).ToArray();

                ExecuteBlockInsertSql insertSql = insertSqlProvider.GetInsertSql(insertContext, insertData, resultRequired);

                if (insertContext.SkipCommandParameters && !CanInsertInExecuteBlock(insertSql.InsertSql, queryBuilder))
                {
                    insertSql = insertSqlProvider.GetInsertSql(insertContext, insertData, resultRequired, skipCommandParameters: false);
                }

                if (queryBuilder.CanAddQuery(insertSql))
                {
                    queryBuilder.AddQuery(insertSql);
                    if (!insertContext.SkipCommandParameters)
                    {
                        currentColumns.AddRange(insertData);
                    }
                }
                else
                {
                    var subResult = CreateAndExecuteInsertCommand(transaction, currentColumns, queryBuilder.GetSql(), resultRequired);
                    if (resultRequired)
                    {
                        result.AddRange(subResult);
                    }
                    currentColumns.Clear();

                    queryBuilder = new FbBulkInsertQueryBuilder(resultRequired, insertContext.ReturnsExecuteBlockSql);
                    queryBuilder.AddQuery(insertSql);
                    if (!insertContext.SkipCommandParameters)
                    {
                        currentColumns.AddRange(insertData);
                    }
                }
            }

            if (queryBuilder.QueryCount > 0)
            {
                var subResult = CreateAndExecuteInsertCommand(transaction, currentColumns, queryBuilder.GetSql(), resultRequired);
                if (resultRequired)
                {
                    result.AddRange(subResult);
                }
            }

            return(result);
        }
        public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> dataList, IDbTransaction transaction, Func<IDictionary<string, object>, Exception, bool> onError,
            bool resultRequired)
        {
            //ToDo: support onError collection
            List<IDictionary<string, object>> result = new List<IDictionary<string, object>>();
          
            if (transaction == null)
            {
                adapter.InTransaction(currentTransaction =>
                {
                    result = (List<IDictionary<string, object>>) Insert(adapter, tableName, dataList, currentTransaction, onError, resultRequired);
                });
                return result;
            }


            var table = adapter.GetSchema().FindTable(tableName);
            var tableColumns = table.Columns.Select(c => (FbColumn)c).ToArray();
            var nameToFbColumns = tableColumns.ToDictionary(c => c.HomogenizedName, c => c);

            var insertContext = CreateInsertSqlContext(table.QualifiedName, tableColumns);

            var queryBuilder = new FbBulkInsertQueryBuilder(resultRequired, insertContext.ReturnsExecuteBlockSql);
            var insertSqlProvider = new FbBulkInsertSqlProvider();
            var currentColumns = new List<InsertColumn>();

            foreach (var data in dataList)
            {
                var insertData = data.Where(p => nameToFbColumns.ContainsKey(p.Key.Homogenize())).Select(kv => new InsertColumn
                {
                    Value = kv.Value,
                    Column =  nameToFbColumns[kv.Key.Homogenize()]
                }).ToArray();

                ExecuteBlockInsertSql insertSql = insertSqlProvider.GetInsertSql(insertContext, insertData, resultRequired);

                if (insertContext.SkipCommandParameters && !CanInsertInExecuteBlock(insertSql.InsertSql, queryBuilder))
                {
                    insertSql = insertSqlProvider.GetInsertSql(insertContext, insertData, resultRequired, skipCommandParameters: false); 
                }

                if (queryBuilder.CanAddQuery(insertSql))
                {
                    queryBuilder.AddQuery(insertSql);
                    if (!insertContext.SkipCommandParameters) currentColumns.AddRange(insertData);
                }
                else
                {
                    var subResult = CreateAndExecuteInsertCommand(transaction, currentColumns, queryBuilder.GetSql(), resultRequired);
                    if (resultRequired) result.AddRange(subResult);
                    currentColumns.Clear();

                    queryBuilder = new FbBulkInsertQueryBuilder(resultRequired, insertContext.ReturnsExecuteBlockSql);
                    queryBuilder.AddQuery(insertSql);
                    if (!insertContext.SkipCommandParameters) currentColumns.AddRange(insertData);
                }
            }

            if (queryBuilder.QueryCount > 0)
            {
                var subResult = CreateAndExecuteInsertCommand(transaction, currentColumns, queryBuilder.GetSql(), resultRequired);
                if (resultRequired) result.AddRange(subResult);
            }

            return result;
        }