using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Patcher.DB { abstract class AbstractConstraint { public interface IVisitor { void Visit(ForeignKeyConstraint constraint); void Visit(UniqueConstraint constraint); void Visit(CheckConstraint constraint); } public interface IVisitor { T Visit(ForeignKeyConstraint constraint); T Visit(UniqueConstraint constraint); T Visit(CheckConstraint constraint); } private class Visitor : IVisitor { private Action foreignKeyAction; private Action uniqueAction; private Action checkAction; public Visitor( Action foreignKeyAction ,Action uniqueAction ,Action checkAction ) { this.foreignKeyAction = foreignKeyAction; this.uniqueAction = uniqueAction; this.checkAction = checkAction; } void IVisitor.Visit(ForeignKeyConstraint constraint) { this.foreignKeyAction(constraint); } public void Visit(UniqueConstraint constraint) { this.uniqueAction(constraint); } public void Visit(CheckConstraint constraint) { this.checkAction(constraint); } } private class Visitor : IVisitor { private Func foreignKeyAction; private Func uniqueAction; private Func checkAction; public Visitor( Func foreignKeyAction ,Func uniqueAction ,Func checkAction ) { this.foreignKeyAction = foreignKeyAction; this.uniqueAction = uniqueAction; this.checkAction = checkAction; } T IVisitor.Visit(ForeignKeyConstraint constraint) { return this.foreignKeyAction(constraint); } public T Visit(UniqueConstraint constraint) { return this.uniqueAction(constraint); } public T Visit(CheckConstraint constraint) { return this.checkAction(constraint); } } abstract public void Accept(IVisitor visitor); abstract public T Accept(IVisitor visitor); public void Accept( Action foreignKeyAction ,Action uniqueAction ,Action checkAction ) { this.Accept(new Visitor(foreignKeyAction, uniqueAction, checkAction)); } public T Accept( Func foreignKeyFunc ,Func uniqueFunc ,Func checkFunc ) { return this.Accept(new Visitor(foreignKeyFunc, uniqueFunc, checkFunc)); } public readonly string table; public readonly string name; protected AbstractConstraint(string table, string name) { this.table = table; this.name = name; } } }