private void PopulatePrimaryKeyValues(IWritePrimitives writer, List<ColumnSymbol> primaryKeyValues, IEnumerable<Column> columns)
        {
            InsertRecordService.Logger.Debug("Entering PopulatePrimaryKeyValues");

            columns = columns.ToList();

            InsertRecordService.Logger.Debug($"columns: {Helper.ToCompositeString(columns)}");

            IEnumerable<PropertyAttribute<PrimaryKeyAttribute>> pkPropertyAttributes =
                this.attributeDecorator.GetPropertyAttributes<PrimaryKeyAttribute>(this.recordReference.RecordType);

            IEnumerable<ColumnSymbol> result = pkPropertyAttributes.Select(pa =>
            {
                string columnName = Helper.GetColumnName(pa.PropertyInfo, this.attributeDecorator);

                Column sourceColumn = columns.FirstOrDefault(c => c.Name.Equals(columnName, StringComparison.Ordinal));

                if (sourceColumn == null)
                {
                    if (pa.Attribute.KeyType != PrimaryKeyAttribute.KeyTypeEnum.Auto)
                    {
                        throw new PopulatePrimaryKeyException(Messages.ColumnNotInInputList, pa.PropertyInfo);
                    }

                    if (
                        !new[]
                        {typeof (int), typeof (short), typeof (long), typeof (uint), typeof (ushort), typeof (ulong),}
                            .Contains(pa.PropertyInfo.PropertyType.GetUnderLyingType()))
                    {
                        throw new PopulatePrimaryKeyException(Messages.AutoKeyMustBeInteger, pa.PropertyInfo);
                    }

                    sourceColumn = new Column {Name = columnName, Value = writer.SelectIdentity(columnName)};
                }

                var symbol = new ColumnSymbol
                {
                    ColumnName = sourceColumn.Name,

                    TableType = this.recordReference.RecordType,

                    Value = sourceColumn.Value,
                };

                return symbol;
            }).ToList();

            InsertRecordService.Logger.Debug($"Exiting PopulatePrimaryKeyValues. result: {Helper.ToCompositeString(result)}");

            primaryKeyValues.AddRange(result);
        }
        public void WritePrimitives_AddPrimaryKeyValue_KeysNone_Test()
        {
            // Arrange

            var columns = new Column[0];
            var primaryKeyValues = new List<ColumnSymbol>();

            this.insertRecordService =
                new InsertRecordService(
                    new RecordReference<KeyNoneTable>(
                        Helpers.GetTypeGeneratorMock(
                            new KeyNoneTable()).Object, this.attributeDecorator), this.attributeDecorator, InsertRecordServiceTest.IsKeyReferenceCheckEnforced);

            const string tableName = "ABCD";
            const string catalogueName = "catABC";
            const string schema = "schemaABC";

            // Act

            this.insertRecordService.WritePrimitives(this.writerMock.Object, catalogueName, schema, tableName, columns, primaryKeyValues);

            // Assert

            Assert.IsFalse(primaryKeyValues.Any());
        }
        public virtual IEnumerable<Column> GetRegularColumns(IWritePrimitives writer)
        {
            InsertRecordService.Logger.Debug("Entering GetRegularColumns");

            IEnumerable<Column> result =
                this.recordReference.RecordType.GetPropertiesHelper()
                    .Where(p =>
                    {
                        PrimaryKeyAttribute pka;

                        bool filter =

                            this.attributeDecorator.GetSingleAttribute<ForeignKeyAttribute>(p) == null

                            &&

                            ((pka = this.attributeDecorator.GetSingleAttribute<PrimaryKeyAttribute>(p)) == null ||
                             pka.KeyType != PrimaryKeyAttribute.KeyTypeEnum.Auto);

                        return filter;
                    })
                    .Select(
                        p =>
                        {
                            string columnName = Helper.GetColumnName(p, this.attributeDecorator);

                            var column = new Column
                            {
                                Name = columnName,

                                Value =

                                    p.PropertyType.IsGuid() && !this.recordReference.IsExplicitlySet(p)

                                        ? writer.WriteGuid(columnName)

                                        : p.GetValue(this.recordReference.RecordObject)
                            };

                            return column;
                        }
                    ).ToList();

            InsertRecordService.Logger.Debug($"Exiting GetRegularColumns. result: {Helper.ToCompositeString(result)}");

            return result;
        }