Exemplo n.º 1
0
        public IEnumerable <IDictionary <string, object> > Insert(AdoAdapter adapter, string tableName, IEnumerable <IDictionary <string, object> > data, IDbTransaction transaction)
        {
            var table   = adapter.GetSchema().FindTable(tableName);
            var columns = table.Columns.Where(c => !c.IsIdentity).ToList();

            string columnList = string.Join(",", columns.Select(c => c.QuotedName));
            string valueList  = string.Join(",", columns.Select(c => "?"));

            string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";

            var helper = transaction == null
                             ? new BulkInserterHelper(adapter, data, table, columns)
                             : new BulkInserterTransactionHelper(adapter, data, table, columns, transaction);

            var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);

            if (identityColumn != null)
            {
                var identityFunction = adapter.GetIdentityFunction();
                if (!string.IsNullOrWhiteSpace(identityFunction))
                {
                    return(InsertRowsAndReturn(adapter, identityFunction, helper, insertSql, table));
                }
            }

            helper.InsertRowsWithoutFetchBack(insertSql);

            return(null);
        }
Exemplo n.º 2
0
        public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction)
        {
            var table = adapter.GetSchema().FindTable(tableName);
            var columns = table.Columns.Where(c => !c.IsIdentity).ToList();

            string columnList = string.Join(",", columns.Select(c => c.QuotedName));
            string valueList = string.Join(",", columns.Select(c => "?"));

            string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";

            var identityFunction = adapter.GetIdentityFunction();
            if (!string.IsNullOrWhiteSpace(identityFunction))
            {
                var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);

                if (identityColumn != null)
                {
                    var selectSql = "select * from " + table.QualifiedName + " where " + identityColumn.QuotedName +
                                     " = " + identityFunction;
                    if (adapter.ProviderSupportsCompoundStatements)
                    {
                        return InsertRowsWithCompoundStatement(adapter, data, transaction, table, columns, selectSql, insertSql);
                    }

                    return InsertRowsWithSeparateStatements(adapter, data, transaction, table, columns, insertSql, selectSql);
                }
            }

            InsertRowsWithoutFetchBack(adapter, data, table, columns, insertSql);

            return null;
        }
Exemplo n.º 3
0
        public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction, Func<IDictionary<string,object>, Exception, bool> onError, bool resultRequired)
        {
            var table = adapter.GetSchema().FindTable(tableName);
            var columns = table.Columns.Where(c => !c.IsIdentity).ToList();

            string columnList = string.Join(",", columns.Select(c => c.QuotedName));
            string valueList = string.Join(",", columns.Select(c => "?"));

            string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";

            var helper = transaction == null
                             ? new BulkInserterHelper(adapter, data, table, columns)
                             : new BulkInserterTransactionHelper(adapter, data, table, columns, transaction);

            if (resultRequired)
            {
                var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);
                if (identityColumn != null)
                {
                    var identityFunction = adapter.GetIdentityFunction();
                    if (!string.IsNullOrWhiteSpace(identityFunction))
                    {
                        return InsertRowsAndReturn(adapter, identityFunction, helper, insertSql, table, onError);
                    }
                }
            }

            helper.InsertRowsWithoutFetchBack(insertSql, onError);

            return null;
        }
Exemplo n.º 4
0
        public IDictionary <string, object> Insert(string tableName, IEnumerable <KeyValuePair <string, object> > data, bool resultRequired)
        {
            var table = _adapter.GetSchema().FindTable(tableName);

            CheckInsertablePropertiesAreAvailable(table, data);

            var customInserter = _adapter.ProviderHelper.GetCustomProvider <ICustomInserter>(_adapter.ConnectionProvider);

            if (customInserter != null)
            {
                return(customInserter.Insert(_adapter, tableName, data.ToDictionary(), _transaction));
            }

            var dataDictionary = data.Where(kvp => table.HasColumn(kvp.Key) && !table.FindColumn(kvp.Key).IsIdentity)
                                 .ToDictionary(kvp => table.FindColumn(kvp.Key), kvp => kvp.Value);

            string columnList = dataDictionary.Keys.Select(c => c.QuotedName).Aggregate((agg, next) => agg + "," + next);
            string valueList  = dataDictionary.Keys.Select(s => "?").Aggregate((agg, next) => agg + "," + next);

            string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";

            if (resultRequired)
            {
                var identityFunction = _adapter.GetIdentityFunction();
                if (!string.IsNullOrWhiteSpace(identityFunction))
                {
                    var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);

                    if (identityColumn != null)
                    {
                        var selectSql = "select * from " + table.QualifiedName + " where " + identityColumn.QuotedName +
                                        " = " + identityFunction;
                        if (_adapter.ProviderSupportsCompoundStatements)
                        {
                            insertSql += "; " + selectSql;
                            return(ExecuteSingletonQuery(insertSql, dataDictionary.Keys, dataDictionary.Values));
                        }
                        else
                        {
                            return(ExecuteSingletonQuery(insertSql, selectSql, dataDictionary.Keys,
                                                         dataDictionary.Values));
                        }
                    }
                }
            }

            Execute(insertSql, dataDictionary.Keys, dataDictionary.Values);
            return(null);
        }
Exemplo n.º 5
0
        public IDictionary <string, object> Insert(string tableName, IDictionary <string, object> data)
        {
            var customInserter = _adapter.ProviderHelper.GetCustomProvider <ICustomInserter>(_adapter.ConnectionProvider);

            if (customInserter != null)
            {
                return(customInserter.Insert(_adapter, tableName, data));
            }

            var table = _adapter.GetSchema().FindTable(tableName);

            data = data.Where(kvp => !table.FindColumn(kvp.Key).IsIdentity)
                   .ToDictionary();
            string columnList =
                data.Keys.Select(table.FindColumn)
                //.Where(c => !c.IsIdentity)
                .Select(c => c.QuotedName)
                .Aggregate((agg, next) => agg + "," + next);
            string valueList = columnList.Split(',').Select(s => "?").Aggregate((agg, next) => agg + "," + next);

            string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";

            var identityFunction = _adapter.GetIdentityFunction();

            if (!string.IsNullOrWhiteSpace(identityFunction))
            {
                var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);

                if (identityColumn != null)
                {
                    var selectSql = "select * from " + table.QualifiedName + " where " + identityColumn.QuotedName +
                                    " = " + identityFunction;
                    if (_adapter.ProviderSupportsCompoundStatements)
                    {
                        insertSql += "; " + selectSql;
                        return(ExecuteSingletonQuery(insertSql, data.Values.ToArray()));
                    }
                    else
                    {
                        return(ExecuteSingletonQuery(insertSql, selectSql, data.Values.ToArray()));
                    }
                }
            }

            Execute(insertSql, data.Values.ToArray());
            return(null);
        }