public void Test_Generic_BuildQueryWithOutputs() { RunTestOnAllDBTypes(delegate(DataBaseType type) { var objectToSql = new Services.ObjectToSql(type); var sql = string.Empty; if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { EnsureExpectedExceptionIsThrown <NotImplementedException>(() => objectToSql.BuildQueryWithOutputs <EmployeeWithPrimaryKeySqlColumn>(ActionType, "Employee", a => a.PrimaryKey) ); return; } else { sql = objectToSql.BuildQueryWithOutputs <EmployeeWithPrimaryKeySqlColumn>(ActionType, "Employee", a => a.PrimaryKey); } var value = $""; switch (type) { case DataBaseType.SqlServer: value = $"INSERT INTO Employee ([FirstName],[LastName],[PrimaryKey]) OUTPUT INSERTED.[PrimaryKey] {Environment.NewLine} VALUES (@FirstName,@LastName,@PrimaryKey)"; break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } Assert.AreEqual(sql, value); }); }
public void Test_Generic_BuildQueryWithOutputs_Uses_MappedColumn_Name_Instead_Of_PropertyName() { RunTestOnAllDBTypes(delegate(DataBaseType type) { var objectToSql = new Services.ObjectToSql(type); var sql = string.Empty; if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { EnsureExpectedExceptionIsThrown <NotImplementedException>(() => objectToSql.BuildQueryWithOutputs <EmployeeWithMappedColumnSqlColumn>(ActionType, "Employee", e => e.FirstName) ); return; } else { sql = objectToSql.BuildQueryWithOutputs <EmployeeWithMappedColumnSqlColumn>(ActionType, "Employee", e => e.FirstName); } var expected = ""; switch (type) { case DataBaseType.SqlServer: expected = $"INSERT INTO Employee ([FirstName2],[LastName]) OUTPUT INSERTED.[FirstName2] {Environment.NewLine} VALUES (@FirstName,@LastName)"; break; case DataBaseType.MySql: break; case DataBaseType.Sqlite: expected = $"INSERT INTO Employee ([FirstName2],[LastName]) OUTPUT INSERTED.[FirstName2] {Environment.NewLine} VALUES (@FirstName,@LastName)"; break; case DataBaseType.Oracle: break; case DataBaseType.Oledb: break; case DataBaseType.Access95: break; case DataBaseType.Odbc: break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } Assert.AreEqual(sql, expected); }); }
public void Test_Generic_BuildInsertQueryWithOutputs() { var objectToSql = new Services.ObjectToSql(DataBaseType.SqlServer); var sql = objectToSql.BuildQueryWithOutputs <Employee>(ActionType, "Employee", e => e.FirstName); Assert.AreEqual(sql, $"INSERT INTO Employee ([FirstName],[LastName]) OUTPUT INSERTED.[FirstName] {Environment.NewLine} VALUES (@FirstName,@LastName)"); }
public void Test_Generic_BuildQueryWithOutputs_Uses_MappedColumn_Name_Instead_Of_PropertyName() { RunTestOnAllDBTypes(delegate(DataBaseType type) { var objectToSql = new Services.ObjectToSql(type); var sql = string.Empty; if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { EnsureExpectedExceptionIsThrown <NotImplementedException>(() => objectToSql.BuildQueryWithOutputs <EmployeeWithMappedColumnDataAnnotation>(ActionType, "Employee", e => e.FirstName) ); return; } else { sql = objectToSql.BuildQueryWithOutputs <EmployeeWithMappedColumnDataAnnotation>(ActionType, "Employee", e => e.FirstName); } Assert.AreEqual(sql, $"INSERT INTO Employee ([FirstName2],[LastName]) OUTPUT INSERTED.[FirstName2] {Environment.NewLine} VALUES (@FirstName,@LastName)"); }); }
public void Test_Generic_BuildQueryWithOutputs() { RunTestOnAllDBTypes(delegate(DataBaseType type) { var objectToSql = new Services.ObjectToSql(type); var sql = string.Empty; if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { EnsureExpectedExceptionIsThrown <NotImplementedException>(() => objectToSql.BuildQueryWithOutputs <EmployeeWithIdentityKeySqlColumn>(ActionType, "Employee", a => a.FirstName) ); return; } else { sql = objectToSql.BuildQueryWithOutputs <EmployeeWithIdentityKeySqlColumn>( ActionType, nameof(Employee), a => a.FirstName); } Assert.AreEqual(sql, $"IF EXISTS ( SELECT * FROM Employee WHERE [IdentityKey]=@IdentityKey ) BEGIN UPDATE Employee SET [FirstName]=@FirstName,[LastName]=@LastName OUTPUT DELETED.[FirstName] WHERE [IdentityKey]=@IdentityKeyINSERT INTO Employee ([FirstName],[LastName]) OUTPUT INSERTED.[FirstName] {Environment.NewLine} VALUES (@FirstName,@LastName) END ELSE BEGIN END"); }); }
public void Test_Generic_BuildQueryWithOutputs() { RunTestOnAllDBTypes(delegate(DataBaseType type) { var objectToSql = new Services.ObjectToSql(type); var sql = string.Empty; if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { EnsureExpectedExceptionIsThrown <NotImplementedException>(() => objectToSql.BuildQueryWithOutputs <EmployeeWithPrimaryKeySqlColumn>(ActionType, "Employee", a => a.PrimaryKey) ); return; } else { sql = objectToSql.BuildQueryWithOutputs <EmployeeWithPrimaryKeySqlColumn>( ActionType, nameof(Employee), a => a.PrimaryKey); } Assert.AreEqual(sql, $@"UPDATE Employee SET [FirstName]=@FirstName,[LastName]=@LastName OUTPUT DELETED.[PrimaryKey] WHERE [PrimaryKey]=@PrimaryKey"); }); }
public void Test_Generic_Build_Insert_Query_With_Outputs() { RunTestOnAllDBTypes(delegate(DataBaseType type) { if (type == DataBaseType.Sqlite || type == DataBaseType.MySql) { return; } var objectToSql = new Services.ObjectToSql(type); var sql = objectToSql.BuildQueryWithOutputs <Employee>(ActionType, e => e.FirstName); var expected = ""; switch (type) { case DataBaseType.SqlServer: expected = $"INSERT INTO Employee ([FirstName],[LastName]) OUTPUT INSERTED.[FirstName] {Environment.NewLine} VALUES (@FirstName,@LastName)"; break; case DataBaseType.MySql: break; case DataBaseType.Sqlite: expected = "NOT SUPPORTED"; break; case DataBaseType.Oracle: break; case DataBaseType.Oledb: break; case DataBaseType.Access95: break; case DataBaseType.Odbc: break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } Assert.AreEqual(sql, expected); }); }