/// <summary> /// Validates whether the values match the row, returning a boolean representing the result /// </summary> /// <param name="expectedData">The expected data to match. Respects <see cref="Comparisons.Abstract.IComparison"/> objects</param> /// <returns>Returns whether the values match the current row</returns> internal bool ValidateValuesMatch(DataSetRow expectedData) { expectedData = expectedData ?? new DataSetRow(); return(expectedData.All(p => ValidateValue(p.Key, p.Value))); }
/// <inheritdoc/> public async Task <DataSetRow> InsertDataAsync(string tableName, DataSetRow defaultData, DataSetRow overrideData) { tableName = DelimitTableName(tableName); DataSetRow data = defaultData.Copy(); if (overrideData != null) { data.Apply(overrideData); } if (data.All(p => p.Value == null)) { (int?defaultIdentityValue, string defaultIdentityColumnName) = await InsertDefaultWithIdentity(tableName); if (!defaultIdentityValue.HasValue) { return(new DataSetRow()); } return(new DataSetRow { [defaultIdentityColumnName] = defaultIdentityValue }); } string command = $@" DECLARE @HasIdentityDBConfirmProperty BIT = 0; DECLARE @IdentityColumnNameUpdatedDBConfirmProperty BIT = 0; DECLARE @IdentityColumnNameDBConfirmProperty NVARCHAR(255) = ''; IF NOT EXISTS( SELECT * FROM sys.tables WHERE '[' + OBJECT_SCHEMA_NAME(object_id) + '].[' + OBJECT_NAME(object_id) + ']' = @TableNameDBConfirmProperty ) BEGIN DECLARE @ErrorDBConfirmProperty NVARCHAR(255) = 'The table cannot be found. Table Name: ' + @TableNameDBConfirmProperty; ;THROW 51000, @ErrorDBConfirmProperty, 1; END IF OBJECTPROPERTY(OBJECT_ID(@TableNameDBConfirmProperty), 'TableHasIdentity') = 1 BEGIN SELECT TOP (1) @HasIdentityDBConfirmProperty = 1 ,@IdentityColumnNameDBConfirmProperty = name FROM sys.identity_columns WHERE '[' + OBJECT_SCHEMA_NAME(object_id) + '].[' + OBJECT_NAME(object_id) + ']' = @TableNameDBConfirmProperty END IF (@HasIdentityDBConfirmProperty = 1) BEGIN DECLARE @Columns TABLE (ColumnName NVARCHAR(255)) INSERT INTO @Columns VALUES ({ string.Join("),(", data.Select(p => $"'{DelimitName(p.Key)}'")) }) IF (EXISTS(SELECT * FROM @Columns WHERE ColumnName = '[' + @IdentityColumnNameDBConfirmProperty + ']')) BEGIN SET @IdentityColumnNameUpdatedDBConfirmProperty = 1; END IF (@IdentityColumnNameUpdatedDBConfirmProperty = 1) BEGIN SET IDENTITY_INSERT {tableName} ON; END END INSERT INTO {tableName} ( { string.Join(",", data.Select(p => DelimitName(p.Key))) } ) VALUES ( { string.Join(",", data.Select(p => $"@{p.Key}")) } ); IF (@HasIdentityDBConfirmProperty = 1) BEGIN IF (@IdentityColumnNameUpdatedDBConfirmProperty = 1) BEGIN SET IDENTITY_INSERT {tableName} OFF; END ELSE BEGIN SELECT SCOPE_IDENTITY() AS IdentityValue ,@IdentityColumnNameDBConfirmProperty AS IdentityColumnName END END"; try { IList <SqlParameter> parameters = data.ToSqlParameters(tableName); parameters.Add(new SqlParameter { ParameterName = "TableNameDBConfirmProperty", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = tableName }); QueryResult results = await ExecuteCommandAsync(command, parameters); if (results.TotalRows == 0) { return(data); } int insertedIdentityValue = Convert.ToInt32(results.RawData.Rows[0]["IdentityValue"]); string insertedIdentityColumnName = results.RawData.Rows[0]["IdentityColumnName"].ToString(); data[insertedIdentityColumnName] = insertedIdentityValue; return(data); } catch (RequiredPlaceholderIsNullException ex) { _testFramework.Fail(ex.Message); return(null); } }