// Persist library : Persistence layer
// Copyright (C) 2003 Vincent Daron
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections;
using Persist.Maps;
using Persist.Sql;
namespace Persist.Criteria{
/// <summary>
/// A set of criteria used in a query
/// </summary>
public class CriteriaConditionSet : ICriteriaPart
{
internal class CriteriaPart
{
public enum CriteriaPartKind
{
OR,
AND
};
private ICriteriaPart theCriteriaPart = null;
private CriteriaPartKind theCriteriaPartKind = CriteriaPartKind.AND;
public CriteriaPart(ICriteriaPart part, CriteriaPartKind kind)
{
theCriteriaPart = part;
theCriteriaPartKind = kind;
}
public ICriteriaPart Part
{
get{return theCriteriaPart;}
}
public CriteriaPartKind Kind
{
get{return theCriteriaPartKind;}
}
}
private ArrayList theParts = new ArrayList();
private ClassMap theClassMap = null;
internal CriteriaConditionSet(ClassMap classMap)
{
theClassMap = classMap;
}
/// <summary>
/// Return the number of conditions in the set
/// </summary>
public int Count
{
get
{
return theParts.Count;
}
}
/// <summary>
/// Adds OR criteria condition to this condition.
/// </summary>
/// <param name="orCriteria">Criteria</param>
public void AddOrCriteria(ICriteriaPart orCriteria)
{
theParts.Add(new CriteriaPart(orCriteria, CriteriaPart.CriteriaPartKind.OR));
}
/// <summary>
/// Adds AND criteria condition to this condition.
/// </summary>
/// <param name="andCriteria">Criteria</param>
public void AddAndCriteria(ICriteriaPart andCriteria)
{
theParts.Add(new CriteriaPart(andCriteria, CriteriaPart.CriteriaPartKind.AND));
}
internal void FillSqlStatement(SqlStatement sqlStatement,ICollection parameters)
{
FillSqlStatement(sqlStatement,new Queue(parameters));
}
#region ICriteriaPart Members
/// <summary>
/// Fill SqlStatement.
/// The ConditionSet is not a Criteria, so just open braces, Add Criteria Sets and Close brace
/// </summary>
/// <param name="statement"></param>
/// <param name="parameters"></param>
public void FillSqlStatement(SqlStatement statement, Queue parameters)
{
statement.AddSqlClause("(");
for(int i=0;i < theParts.Count ; i++)
{
CriteriaPart part = theParts[i] as CriteriaPart;
if(i != 0)
{
switch(part.Kind)
{
case CriteriaPart.CriteriaPartKind.AND:
statement.AddSqlClause(" " + theClassMap.RelationalDatabase.ClauseStringAnd + " ");
break;
case CriteriaPart.CriteriaPartKind.OR:
statement.AddSqlClause(" " + theClassMap.RelationalDatabase.ClauseStringOr + " ");
break;
}
}
part.Part.FillSqlStatement(statement, parameters);
}
statement.AddSqlClause(")");
}
#endregion
}
}
|