forked from Shamar/Epic.NET
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from zhelgadis/select_modeling
Added Selection and Projection classes
- Loading branch information
Showing
14 changed files
with
724 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,189 @@ | |||
// | |||
// Projection.cs | |||
// | |||
// Author: | |||
// Marco Veglio <m.veglio@gmail.com> | |||
// | |||
// Copyright (c) 2010-2012 Giacomo Tesio | |||
// | |||
// This file is part of Epic.NET. | |||
// | |||
// Epic.NET is free software: you can redistribute it and/or modify | |||
// it under the terms of the GNU Affero General Public License as published by | |||
// the Free Software Foundation, either version 3 of the License, or | |||
// (at your option) any later version. | |||
// | |||
// Epic.NET 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 Affero General Public License for more details. | |||
// | |||
// You should have received a copy of the GNU Affero General Public License | |||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
// | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
|
|||
|
|||
namespace Epic.Query.Relational.Operations | |||
{ | |||
/// <summary> | |||
/// This class models the Projection operation, which extracts a given set of fields from a given | |||
/// <see cref="Relation"/>. | |||
/// </summary> | |||
[Serializable] | |||
public sealed class Projection: Relation, IEquatable<Projection> | |||
{ | |||
private readonly Relation relation; | |||
private readonly IEnumerable<RelationAttribute> attributes; | |||
|
|||
/// <summary> | |||
/// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Projection"/> class. | |||
/// </summary> | |||
/// <param name='relation'> | |||
/// The <see cref="Relation"/> used as source for the projection. | |||
/// </param> | |||
/// <param name='attributes'> | |||
/// The collection of <see cref="RelationAttributes"/> extracted | |||
/// </param> | |||
/// <param name='name'> | |||
/// A user-defined name used to identify the Projection relation. | |||
/// </param> | |||
public Projection (Relation relation, IEnumerable<RelationAttribute> attributes, string name): | |||
base(RelationType.Projection, name) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == attributes) throw new ArgumentNullException("attributes"); | |||
if (null == name) throw new ArgumentNullException("name"); | |||
this.relation = relation; | |||
this.attributes = attributes; | |||
} | |||
|
|||
/// <summary> | |||
/// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Projection"/> class. | |||
/// </summary> | |||
/// <param name='relation'> | |||
/// The <see cref="Relation"/> used as source for the projection. | |||
/// </param> | |||
/// <param name='attributes'> | |||
/// The collection of <see cref="RelationAttributes"/> extracted | |||
/// </param> | |||
public Projection(Relation relation, IEnumerable<RelationAttribute> attributes): | |||
base(RelationType.Projection, getDefaultName(relation, attributes)) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == attributes) throw new ArgumentNullException("attributes"); | |||
this.relation = relation; | |||
this.attributes = attributes; | |||
} | |||
|
|||
/// <summary> | |||
/// Gets the <see cref="Relation"/> used as source for the projection. | |||
/// </summary> | |||
/// <value> | |||
/// The table. | |||
/// </value> | |||
public Relation Relation { get { return this.relation; } } | |||
|
|||
/// <summary> | |||
/// Gets the attributes extracted from the given table. | |||
/// </summary> | |||
/// <value> | |||
/// The attributes. | |||
/// </value> | |||
public IEnumerable<RelationAttribute> Attributes { get { return this.attributes; } } | |||
|
|||
/// <summary> | |||
/// Determines whether the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current <see cref="Epic.Query.Relational.Operations.Projection"/>. | |||
/// </summary> | |||
/// <param name='other'> | |||
/// The <see cref="Epic.Query.Relational.Relation"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Projection"/>. | |||
/// </param> | |||
/// <returns> | |||
/// <c>true</c> if the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current | |||
/// <see cref="Epic.Query.Relational.Operations.Projection"/>; otherwise, <c>false</c>. | |||
/// </returns> | |||
public override bool Equals (Relation other) | |||
{ | |||
return this.Equals (other as Projection); | |||
} | |||
|
|||
/// <summary> | |||
/// Determines whether the specified <see cref="Epic.Query.Relational.Operations.Projection"/> is equal to the | |||
/// current <see cref="Epic.Query.Relational.Operations.Projection"/>. | |||
/// </summary> | |||
/// <param name='other'> | |||
/// The <see cref="Epic.Query.Relational.Operations.Projection"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Projection"/>. | |||
/// </param> | |||
/// <returns> | |||
/// <c>true</c> if the specified <see cref="Epic.Query.Relational.Operations.Projection"/> is equal to the | |||
/// current <see cref="Epic.Query.Relational.Operations.Projection"/>; otherwise, <c>false</c>. | |||
/// </returns> | |||
public bool Equals(Projection other) | |||
{ | |||
if (null == other) return false; | |||
return this.Relation.Equals (other.Relation) && this.Attributes.SequenceEqual(other.Attributes); | |||
} | |||
|
|||
/// <summary> | |||
/// Accept the specified visitor and context. | |||
/// </summary> | |||
/// <param name='visitor'> | |||
/// Visitor. | |||
/// </param> | |||
/// <param name='context'> | |||
/// Context. | |||
/// </param> | |||
/// <typeparam name='TResult'> | |||
/// The 1st type parameter. | |||
/// </typeparam> | |||
public override TResult Accept<TResult> (IVisitor<TResult> visitor, IVisitContext context) | |||
{ | |||
return AcceptMe (this, visitor, context); | |||
} | |||
|
|||
/// <summary> | |||
/// Serves as a hash function for a <see cref="Epic.Query.Relational.Operations.Projection"/> object. | |||
/// </summary> | |||
/// <returns> | |||
/// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a | |||
/// hash table. | |||
/// </returns> | |||
public override int GetHashCode () | |||
{ | |||
int hash = this.Relation.GetHashCode (); | |||
foreach (RelationAttribute attribute in this.Attributes) | |||
hash ^= attribute.GetHashCode (); | |||
return hash; | |||
} | |||
|
|||
/// <summary> | |||
/// Gets the default name for the projection. | |||
/// </summary> | |||
/// <returns> | |||
/// The default name. | |||
/// </returns> | |||
/// <param name='relation'> | |||
/// the <see cref="Relation"/> used as source for the projection. | |||
/// </param> | |||
/// <param name='attributes'> | |||
/// The attributes to be extracted from the relation. | |||
/// </param> | |||
private static string getDefaultName (Relation relation, IEnumerable<RelationAttribute> attributes) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == attributes) throw new ArgumentNullException("attributes"); | |||
StringBuilder sb = new StringBuilder(); | |||
sb.Append("Take "); | |||
foreach (RelationAttribute attribute in attributes) | |||
sb.Append (attribute.Name).Append(","); | |||
sb.Append (" from "); | |||
sb.Append (relation.Name); | |||
return sb.ToString (); | |||
} | |||
|
|||
} | |||
} | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,176 @@ | |||
// | |||
// Select.cs | |||
// | |||
// Author: | |||
// Marco Veglio <m.veglio@gmail.com> | |||
// | |||
// Copyright (c) 2010-2012 Giacomo Tesio | |||
// | |||
// This file is part of Epic.NET. | |||
// | |||
// Epic.NET is free software: you can redistribute it and/or modify | |||
// it under the terms of the GNU Affero General Public License as published by | |||
// the Free Software Foundation, either version 3 of the License, or | |||
// (at your option) any later version. | |||
// | |||
// Epic.NET 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 Affero General Public License for more details. | |||
// | |||
// You should have received a copy of the GNU Affero General Public License | |||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
// | |||
using System; | |||
using Epic.Query.Relational.Predicates; | |||
|
|||
namespace Epic.Query.Relational.Operations | |||
{ | |||
|
|||
/// <summary> | |||
/// This class models the Select operation which extracts those record from a given table matching | |||
/// a given <see cref="Predicate"/> | |||
/// </summary> | |||
[Serializable] | |||
public sealed class Selection: Relation, IEquatable<Selection> | |||
{ | |||
private readonly Predicate condition; | |||
private readonly Relation relation; | |||
|
|||
/// <summary> | |||
/// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Selection"/> class. | |||
/// </summary> | |||
/// <param name='relation'> | |||
/// The <see cref="Relation"/> used as source for the selection. | |||
/// </param> | |||
/// <param name='condition'> | |||
/// The <see cref="Predicate"/> against which the records are matched. | |||
/// </param> | |||
public Selection (Relation relation, Predicate condition): | |||
base(RelationType.Selection, getDefaultName (relation, condition)) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == condition) throw new ArgumentNullException("condition"); | |||
this.condition = condition; | |||
this.relation = relation; | |||
} | |||
|
|||
/// <summary> | |||
/// Initializes a new instance of the <see cref="Epic.Query.Relational.Operations.Selection"/> class. | |||
/// </summary> | |||
/// <param name='relation'> | |||
/// The table used as source for the selection. | |||
/// </param> | |||
/// <param name='condition'> | |||
/// The <see cref="Predicate"/> against which the records are matched. | |||
/// </param> | |||
/// <param name='name'> | |||
/// A user-defined name to identify the Selection relation. | |||
/// </param> | |||
public Selection (Relation relation, Predicate condition, string name): base(RelationType.Selection, name) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == condition) throw new ArgumentNullException("condition"); | |||
if (null == name) throw new ArgumentNullException("name"); | |||
this.condition = condition; | |||
this.relation = relation; | |||
} | |||
|
|||
/// <summary> | |||
/// Gets the condition against which the table's records are matched. | |||
/// </summary> | |||
/// <value> | |||
/// The condition. | |||
/// </value> | |||
public Predicate Condition { get { return this.condition; } } | |||
|
|||
/// <summary> | |||
/// Gets the <see cref="Relation"/> used as source for the selection. | |||
/// </summary> | |||
/// <value> | |||
/// The table. | |||
/// </value> | |||
public Relation Relation { get { return this.relation; } } | |||
|
|||
/// <summary> | |||
/// Determines whether the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current <see cref="Epic.Query.Relational.Operations.Selection"/>. | |||
/// </summary> | |||
/// <param name='other'> | |||
/// The <see cref="Epic.Query.Relational.Relation"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Selection"/>. | |||
/// </param> | |||
/// <returns> | |||
/// <c>true</c> if the specified <see cref="Epic.Query.Relational.Relation"/> is equal to the current | |||
/// <see cref="Epic.Query.Relational.Operations.Selection"/>; otherwise, <c>false</c>. | |||
/// </returns> | |||
public override bool Equals (Relation other) | |||
{ | |||
return this.Equals (other as Selection); | |||
} | |||
|
|||
/// <summary> | |||
/// Determines whether the specified <see cref="Epic.Query.Relational.Operations.Selection"/> is equal to the | |||
/// current <see cref="Epic.Query.Relational.Operations.Selection"/>. | |||
/// </summary> | |||
/// <param name='other'> | |||
/// The <see cref="Epic.Query.Relational.Operations.Selection"/> to compare with the current <see cref="Epic.Query.Relational.Operations.Selection"/>. | |||
/// </param> | |||
/// <returns> | |||
/// <c>true</c> if the specified <see cref="Epic.Query.Relational.Operations.Selection"/> is equal to the current | |||
/// <see cref="Epic.Query.Relational.Operations.Selection"/>; otherwise, <c>false</c>. | |||
/// </returns> | |||
public bool Equals(Selection other) | |||
{ | |||
if (null == other) return false; | |||
return this.Relation.Equals (other.Relation) && this.Condition.Equals (other.Condition); | |||
} | |||
|
|||
/// <summary> | |||
/// Serves as a hash function for a <see cref="Epic.Query.Relational.Operations.Selection"/> object. | |||
/// </summary> | |||
/// <returns> | |||
/// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a | |||
/// hash table. | |||
/// </returns> | |||
public override int GetHashCode () | |||
{ | |||
return this.Relation.GetHashCode () ^ this.Condition.GetHashCode (); | |||
} | |||
|
|||
/// <summary> | |||
/// Accept the specified visitor and context. | |||
/// </summary> | |||
/// <param name='visitor'> | |||
/// Visitor. | |||
/// </param> | |||
/// <param name='context'> | |||
/// Context. | |||
/// </param> | |||
/// <typeparam name='TResult'> | |||
/// The 1st type parameter. | |||
/// </typeparam> | |||
public override TResult Accept<TResult> (IVisitor<TResult> visitor, IVisitContext context) | |||
{ | |||
return AcceptMe (this, visitor, context); | |||
} | |||
|
|||
/// <summary> | |||
/// Gets the default name for the selection. | |||
/// </summary> | |||
/// <returns> | |||
/// The default name. | |||
/// </returns> | |||
/// <param name='relation'> | |||
/// The <see cref="Relation"/> used as source for the selection. | |||
/// </param> | |||
/// <param name='predicate'> | |||
/// The condition against which the records are matched. | |||
/// </param> | |||
private static string getDefaultName(Relation relation, Predicate predicate) | |||
{ | |||
if (null == relation) throw new ArgumentNullException("relation"); | |||
if (null == predicate) throw new ArgumentNullException("predicate"); | |||
return string.Format ("SELECT * from {0} where {1}", relation.Name, predicate.ToString ()); | |||
} | |||
} | |||
} | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.