/// <summary> /// Generates a collection of sql statements to delete the business /// object from the database /// </summary> /// <returns>Returns a sql statement collection</returns> public IEnumerable <ISqlStatement> Generate() { var statements = new List <ISqlStatement>(); //AddRelationshipDeleteStatements(statementCollection); SqlStatement deleteSql = new SqlStatement(_connection); deleteSql.Statement = new StringBuilder( @"DELETE FROM " + _connection.SqlFormatter.DelimitTable(StatementGeneratorUtils.GetTableName(_bo)) + " WHERE " + StatementGeneratorUtils.PersistedDatabaseWhereClause((BOKey)_bo.ID, deleteSql)); statements.Add(deleteSql); IClassDef currentClassDef = _bo.ClassDef; while (currentClassDef.IsUsingClassTableInheritance()) { while (currentClassDef.SuperClassClassDef.SuperClassDef != null && currentClassDef.SuperClassClassDef.SuperClassDef.ORMapping == ORMapping.SingleTableInheritance) { currentClassDef = currentClassDef.SuperClassClassDef; } deleteSql = new SqlStatement(_connection); deleteSql.Statement.Append( "DELETE FROM " + _connection.SqlFormatter.DelimitTable(currentClassDef.SuperClassClassDef.TableName) + " WHERE " + StatementGeneratorUtils.PersistedDatabaseWhereClause(BOPrimaryKey.GetSuperClassKey((ClassDef)currentClassDef, _bo), deleteSql)); statements.Add(deleteSql); currentClassDef = currentClassDef.SuperClassClassDef; } return(statements); }
/// <summary> /// Generates an "update" sql statement for the properties in the /// business object /// </summary> /// <param name="tableName">The table name</param> /// <param name="propsToInclude">A collection of properties to update, /// if the previous include-all boolean was not set to true</param> /// <param name="isSuperClassStatement">Whether a super-class is involved</param> /// <param name="currentClassDef">The current class definition</param> private void GenerateSingleUpdateStatement(string tableName, IBOPropCol propsToInclude, bool isSuperClassStatement, ClassDef currentClassDef) { _updateSql = new SqlStatement(_connection); _updateSql.Statement.Append( @"UPDATE " + _connection.SqlFormatter.DelimitTable(tableName) + " SET "); int includedProps = 0; foreach (BOProp prop in _bo.Props.SortedValues) { if (propsToInclude.Contains(prop.PropertyName)) { PrimaryKeyDef primaryKeyDef = (PrimaryKeyDef)_bo.ClassDef.PrimaryKeyDef ?? (PrimaryKeyDef)_bo.ID.KeyDef; if (prop.IsDirty && ((primaryKeyDef.IsGuidObjectID && !primaryKeyDef.Contains(prop.PropertyName)) || !primaryKeyDef.IsGuidObjectID)) { includedProps++; _updateSql.Statement.Append(_connection.SqlFormatter.DelimitField(prop.DatabaseFieldName)); _updateSql.Statement.Append(" = "); //prop.PropDef.GetDataMapper().GetDatabaseValue(prop.Value); _updateSql.AddParameterToStatement(prop.Value); _updateSql.Statement.Append(", "); } } } _updateSql.Statement.Remove(_updateSql.Statement.Length - 2, 2); //remove the last ", " if (isSuperClassStatement) { _updateSql.Statement.Append(" WHERE " + StatementGeneratorUtils.PersistedDatabaseWhereClause(BOPrimaryKey.GetSuperClassKey(currentClassDef, _bo), _updateSql)); } else { _updateSql.Statement.Append(" WHERE " + StatementGeneratorUtils.PersistedDatabaseWhereClause((BOKey)_bo.ID, _updateSql)); } if (includedProps > 0) { _statements.Add(_updateSql); } }