/// <summary> /// Generates a collection of sql statements to insert the business /// object's properties into the database /// </summary> /// <returns>Returns a sql statement collection</returns> public IEnumerable <ISqlStatement> Generate() { _statements = new List <ISqlStatement>(); _currentClassDef = _bo.ClassDef; IBOPropCol propsToInclude; string tableName; propsToInclude = GetPropsToInclude(_currentClassDef); tableName = StatementGeneratorUtils.GetTableName(_bo); GenerateSingleInsertStatement(propsToInclude, tableName); if (_bo.ClassDef.IsUsingClassTableInheritance()) { _currentClassDef = (ClassDef)_bo.ClassDef.SuperClassClassDef; while (_currentClassDef.IsUsingClassTableInheritance()) { propsToInclude = GetPropsToInclude(_currentClassDef); tableName = _currentClassDef.TableName; GenerateSingleInsertStatement(propsToInclude, tableName); _currentClassDef = (ClassDef)_currentClassDef.SuperClassClassDef; } propsToInclude = GetPropsToInclude(_currentClassDef); tableName = _currentClassDef.InheritedTableName; GenerateSingleInsertStatement(propsToInclude, tableName); } return(_statements); }
/// <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); } }
/// <summary> /// Generates a collection of sql statements to update the business /// object's properties in the database /// </summary> /// <returns>Returns a sql statement collection</returns> public IEnumerable <ISqlStatement> Generate() { _statements = new List <ISqlStatement>(); IBOPropCol propsToInclude; string tableName; ClassDef currentClassDef = _bo.ClassDef; while (currentClassDef.IsUsingClassTableInheritance()) { var superClassClassDef = (ClassDef)currentClassDef.SuperClassClassDef; propsToInclude = GetPropsToInclude(superClassClassDef); if (propsToInclude.Count > 0) { tableName = superClassClassDef.InheritedTableName; GenerateSingleUpdateStatement(tableName, propsToInclude, true, currentClassDef); } currentClassDef = superClassClassDef; } propsToInclude = GetPropsToInclude(_bo.ClassDef); tableName = StatementGeneratorUtils.GetTableName(_bo); GenerateSingleUpdateStatement(tableName, propsToInclude, false, _bo.ClassDef); return(_statements); }