示例#1
0
        private void ProcessInserts(BindingList <RecordToAdd> recordsToAdd, EntityBatch entityBatch, string tableName)
        {
            for (int i = 0; i < recordsToAdd.Count; i++)
            {
                var recordToAdd = recordsToAdd[i];

                PostgresSqlInsertFactory insertFactory;
                if (entityBatch.EntityDefinition.PrimaryKeyGenerationType == PrimaryKeyGenerationType.Custom)
                {
                    insertFactory = new PostgresSqlInsertFactory(tableName, entityBatch.EntityDefinition.PrimaryKeyColumnNames[0], entityBatch.EntityDefinition.CustomCommand, recordToAdd.FieldValuePairs);
                }
                else
                {
                    insertFactory = new PostgresSqlInsertFactory(tableName, entityBatch.EntityDefinition.PrimaryKeyColumnNames[0], recordToAdd.FieldValuePairs);
                }

                if (entityBatch.EntityDefinition.AutoBindingForeignKeys != null)
                {
                    foreach (var autoBindingForeignKey in entityBatch.EntityDefinition.AutoBindingForeignKeys)
                    {
                        EntityRecord parentRecord    = null;
                        string       primaryKeyValue = null;

                        try
                        {
                            parentRecord = recordToAdd.Parents[autoBindingForeignKey.Relationship.Name];
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Relationship with name '{0}' was not found within parents for entity record '{1}' with primary key field '{2}' to bind to parent field(s) '{3}'.  Data only values for the failed record are: {4}.",
                                                              autoBindingForeignKey.Relationship.Name, entityBatch.EntityDefinition.TechnicalEntityName,
                                                              autoBindingForeignKey.FieldNameToUpdate, StringHelper.GetDelimitedString(autoBindingForeignKey.Relationship.ChildFieldNamesToMatchOn),
                                                              recordToAdd.GetDataOnlyValuesAsText()), ex);
                        }

                        if (parentRecord is EntityRecordWithDataChange)
                        {
                            var parentRecordWithChange = (EntityRecordWithDataChange)parentRecord;

                            if (parentRecordWithChange.HasError)
                            {
                                recordsToAdd[i].HasError     = true;
                                recordsToAdd[i].ErrorMessage = "See parent record for error details.";
                                continue;
                            }
                        }

                        try
                        {
                            primaryKeyValue = parentRecord.PrimaryKeyValues[autoBindingForeignKey.ParentPrimaryKeyColumnIdx];
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Primary key index '{0}' was not found for parent entity record '{1}' with primary key value(s) '{2}' and data only field(s): {3}.",
                                                              autoBindingForeignKey.Relationship.Name, entityBatch.EntityDefinition.TechnicalEntityName,
                                                              StringHelper.GetDelimitedString(parentRecord.PrimaryKeyValues),
                                                              parentRecord.GetDataOnlyValuesAsText()), ex);
                        }

                        insertFactory.AddField(autoBindingForeignKey.FieldNameToUpdate, primaryKeyValue);
                    }
                }

                string recordId = "";

                try
                {
                    recordsToAdd[i].CommandText = insertFactory.GetSQL();

                    recordId = ExecuteCommand(recordsToAdd[i].CommandText).ToString();

                    recordsToAdd[i].PrimaryKeyValues.Add(recordId);
                }
                catch (Exception ex)
                {
                    recordsToAdd[i].HasError = true;

                    recordsToAdd[i].ErrorMessage = ExceptionFormatter.Format(ex);

                    recordsToAdd[i].Exception = ex;
                }
                finally
                {
                    recordsToAdd[i].HasBeenProcessed = true;
                }
            }
        }