From: <cha...@us...> - 2008-08-12 18:47:27
|
Revision: 3699 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3699&view=rev Author: chadly69 Date: 2008-08-12 18:47:36 +0000 (Tue, 12 Aug 2008) Log Message: ----------- Adding NHibernate.Linq test model (Animal hierarchy) and basic test setup classes. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/ExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq.Test/App.config trunk/nhibernate/src/NHibernate.Linq.Test/BaseTest.cs trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs trunk/nhibernate/src/NHibernate.Linq.Test/Model/ trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml Property Changed: ---------------- trunk/nhibernate/src/NHibernate.Linq.Test/ Modified: trunk/nhibernate/src/NHibernate.Linq/ExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/ExpressionVisitor.cs 2008-08-12 15:40:30 UTC (rev 3698) +++ trunk/nhibernate/src/NHibernate.Linq/ExpressionVisitor.cs 2008-08-12 18:47:36 UTC (rev 3699) @@ -5,7 +5,7 @@ using System.Linq.Expressions; using System.Reflection; -namespace NHibernate.Linq.Visitors +namespace NHibernate.Linq { /// <summary> /// Provides virtual methods that can be used by subclasses to parse an expression tree. Property changes on: trunk/nhibernate/src/NHibernate.Linq.Test ___________________________________________________________________ Modified: svn:ignore - [Bb]in obj [Dd]ebug [Rr]elease *.user *.aps *.eto + [Bb]in obj [Dd]ebug [Rr]elease *.user *.aps *.eto hibernate.cfg.xml Added: trunk/nhibernate/src/NHibernate.Linq.Test/App.config =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/App.config (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/App.config 2008-08-12 18:47:36 UTC (rev 3699) @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8" ?> +<configuration> + <configSections> + <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> + </configSections> + + <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > + <session-factory name="NHibernate.Linq.Test"> + <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> + <property name="connection.connection_string"> + Server=.\SQLEXPRESS;initial catalog=linq2nhibernate;Integrated Security=true + </property> + <property name="adonet.batch_size">10</property> + <property name="show_sql">false</property> + <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> + <property name="use_outer_join">true</property> + <property name="command_timeout">444</property> + <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> + <mapping assembly="NHibernate.Linq.Test" /> + </session-factory> + </hibernate-configuration> +</configuration> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq.Test/BaseTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/BaseTest.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/BaseTest.cs 2008-08-12 18:47:36 UTC (rev 3699) @@ -0,0 +1,31 @@ +using System.Configuration; +using System.Data; +using System.Data.SqlClient; +using NUnit.Framework; + +namespace NHibernate.Linq.Test +{ + public class BaseTest + { + protected ISession session; + + static BaseTest() + { + GlobalSetup.SetupNHibernate(); + } + + [SetUp] + public virtual void Setup() + { + session = GlobalSetup.CreateSession(); + } + + [TearDown] + public virtual void TearDown() + { + session.Connection.Dispose(); + session.Dispose(); + session = null; + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs 2008-08-12 18:47:36 UTC (rev 3699) @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Data; +using NHibernate; +using NUnit.Framework; +using NHibernate.Cfg; +using NHibernate.Tool.hbm2ddl; + +namespace NHibernate.Linq.Test +{ + public static class GlobalSetup + { + private static ISessionFactory factory; + + public static void SetupNHibernate() + { + Configuration cfg = new Configuration().Configure(); + new SchemaExport(cfg).Execute(false, true, false, true); + factory = cfg.BuildSessionFactory(); + } + + public static ISession CreateSession() + { + return factory.OpenSession(); + } + + public static ISession CreateSession(IDbConnection con) + { + return factory.OpenSession(con); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs 2008-08-12 18:47:36 UTC (rev 3699) @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using Iesi.Collections.Generic; + +namespace NHibernate.Linq.Test.Model +{ + public class Animal + { + public virtual int Id { get; set; } + public virtual string Description { get; set; } + public virtual double BodyWeight { get; set; } + public virtual Animal Mother { get; set; } + public virtual Animal Father { get; set; } + public virtual Zoo Zoo { get; set; } + public virtual string SerialNumber { get; set; } + public virtual ISet<Animal> Offspring { get; set; } + } + + public class Reptile : Animal + { + public virtual double BodyTemperature { get; set; } + } + + public class Lizard : Reptile { } + + public class Mammal : Animal + { + public virtual bool Pregnant { get; set; } + public virtual DateTime? BirthDate { get; set; } + } + + public class DomesticAnimal : Mammal + { + public virtual Human Owner { get; set; } + } + + public class Cat : DomesticAnimal { } + + public class Dog : DomesticAnimal { } + + public class Human : Mammal + { + public virtual HumanName Name { get; set; } + public virtual string NickName { get; set; } + public virtual double Height { get; set; } + public virtual int IntValue { get; set; } + public virtual float FloatValue { get; set; } + public virtual decimal DecimalValue { get; set; } + public virtual Int64 Int64Value { get; set; } + public virtual IList<Human> Friends { get; set; } + public virtual IDictionary<string, Human> Family { get; set; } + public virtual IList<DomesticAnimal> Pets { get; set; } + public virtual ISet<string> NickNames { get; set; } + public virtual IDictionary<string, Address> Addresses { get; set; } + } + + public struct Address + { + public string Street { get; set; } + public string City { get; set; } + public string PostalCode { get; set; } + public string Country { get; set; } + public StateProvince StateProvince { get; set; } + } + + public struct HumanName + { + public string First { get; set; } + public string Initial { get; set; } + public string Last { get; set; } + } + + public class User + { + public virtual int Id { get; set; } + public virtual Human Human { get; set; } + public virtual string UserName { get; set; } + public virtual IList<string> Permissions { get; set; } + } + + public class Zoo + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual ClassificationType Classification { get; set; } + public virtual IDictionary<string, Mammal> Mammals { get; set; } + public virtual IDictionary<string, Animal> Animals { get; set; } + public virtual Address Address { get; set; } + } + + public enum ClassificationType { Large, Medium, Small } + + public class PettingZoo : Zoo { } + + public class StateProvince + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual string IsoCode { get; set; } + } + + public class Joiner + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual string JoinedName { get; set; } + } +} Added: trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml 2008-08-12 18:47:36 UTC (rev 3699) @@ -0,0 +1,170 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate.Linq.Test.Model" assembly="NHibernate.Linq.Test"> + <class name="Animal"> + <id name="Id"> + <generator class="native"/> + </id> + + <property name="Description"/> + <property name="BodyWeight" column="body_weight"/> + <many-to-one name="Mother" column="mother_id"/> + <many-to-one name="Father" column="father_id"/> + <many-to-one name="Zoo" column="zoo_id"/> + <property name="SerialNumber"/> + + <set name="Offspring" order-by="father_id"> + <key column="mother_id"/> + <one-to-many class="Animal"/> + </set> + + <joined-subclass name="Reptile"> + <key column="Animal"/> + <property name="BodyTemperature"/> + + <joined-subclass name="Lizard"> + <key column="Reptile"/> + </joined-subclass> + </joined-subclass> + + <joined-subclass name="Mammal"> + <key column="Animal"/> + <property name="Pregnant"/> + <property name="BirthDate" type="DateTime"/> + + <joined-subclass name="DomesticAnimal"> + <key column="Mammal"/> + <many-to-one name="Owner"/> + + <joined-subclass name="Cat"> + <key column="mammal"/> + </joined-subclass> + + <joined-subclass name="Dog"> + <key column="mammal"/> + </joined-subclass> + </joined-subclass> + + <joined-subclass name="Human"> + <key column="Mammal"/> + + <component name="Name"> + <property name="First" column="name_first"/> + <property name="Initial" column="name_initial"/> + <property name="Last" column="name_last"/> + </component> + + <property name="NickName"/> + <property name="Height"/> + + <property name="IntValue"/> + <property name="FloatValue"/> + <property name="DecimalValue"/> + <property name="Int64Value"/> + + <bag name="Friends"> + <key column="Human1"/> + <many-to-many column="Human2" class="Human"/> + </bag> + + <map name="Family"> + <key column="Human1"/> + <map-key column="Relationship" type="AnsiString"/> + <many-to-many column="Human2" class="Human"/> + </map> + + <bag name="Pets" inverse="true"> + <key column="Owner"/> + <one-to-many class="DomesticAnimal"/> + </bag> + + <set name="NickNames" lazy="false" table="human_nick_names" sort="natural"> + <key column="Human"/> + <element column="nick_name" type="string" not-null="true" /> + </set> + + <map name="Addresses" table="addresses"> + <key column="Human"/> + <map-key type="string" column="type" /> + + <composite-element class="Address"> + <property name="Street"/> + <property name="City"/> + <property name="PostalCode"/> + <property name="Country"/> + <many-to-one name="StateProvince" column="state_prov_id" class="StateProvince" /> + </composite-element> + </map> + </joined-subclass> + </joined-subclass> + </class> + + <class name="User" table="User"> + <id name="Id"> + <generator class="foreign"> + <param name="property">Human</param> + </generator> + </id> + + <property name="UserName" /> + <one-to-one name="Human" constrained="true" /> + + <list name="Permissions"> + <key column="UserId" /> + <list-index column="PermissionId" /> + <element type="string" column="PermissionName" /> + </list> + </class> + + <class name="Zoo" discriminator-value="Z"> + <id name="Id"> + <generator class="native"/> + </id> + <discriminator column="ZooType" type="character"/> + + <property name="Name" type="String"/> + <property name="Classification" /> + + <map name="Mammals"> + <key column="MammalZoo_id"/> + <index type="string" column="Name"/> + <one-to-many class="Mammal"/> + </map> + + <map name="Animals" inverse="true"> + <key column="zoo_id"/> + <index type="string" column="SerialNumber"/> + <one-to-many class="Animal"/> + </map> + + <component name="Address" class="Address"> + <property name="Street"/> + <property name="City"/> + <property name="PostalCode"/> + <property name="Country"/> + <many-to-one name="StateProvince" column="state_prov_id" class="StateProvince" /> + </component> + + <subclass name="PettingZoo" discriminator-value="P"/> + </class> + + <class name="StateProvince"> + <id name="Id"> + <generator class="native"/> + </id> + + <property name="Name"/> + <property name="IsoCode"/> + </class> + + <class name="Joiner"> + <id name="Id"> + <generator class="native"/> + </id> + + <property name="Name"/> + <join table="JOINED"> + <key column="Id"/> + <property name="JoinedName"/> + </join> + </class> +</hibernate-mapping> \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-12 15:40:30 UTC (rev 3698) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-12 18:47:36 UTC (rev 3699) @@ -32,6 +32,14 @@ <TreatWarningsAsErrors>true</TreatWarningsAsErrors> </PropertyGroup> <ItemGroup> + <Reference Include="Iesi.Collections, Version=1.0.0.3, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\lib\net\2.0\Iesi.Collections.dll</HintPath> + </Reference> + <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\lib\net\2.0\log4net.dll</HintPath> + </Reference> <Reference Include="nunit.core, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\..\lib\net\2.0\nunit.core.dll</HintPath> @@ -57,6 +65,17 @@ <Name>NHibernate-3.5</Name> </ProjectReference> </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="Model\Animal.hbm.xml" /> + </ItemGroup> + <ItemGroup> + <None Include="App.config" /> + </ItemGroup> + <ItemGroup> + <Compile Include="BaseTest.cs" /> + <Compile Include="GlobalSetup.cs" /> + <Compile Include="Model\Animal.cs" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cha...@us...> - 2008-08-12 19:50:31
|
Revision: 3700 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3700&view=rev Author: chadly69 Date: 2008-08-12 19:50:39 +0000 (Tue, 12 Aug 2008) Log Message: ----------- Copied Query and QueryProvider base classes from previous linq implementation and created beginnings of NHibernateQueryProvider. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/NHibernateExtensions.cs trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs trunk/nhibernate/src/NHibernate.Linq/Query.cs trunk/nhibernate/src/NHibernate.Linq/QueryProvider.cs trunk/nhibernate/src/NHibernate.Linq/Util/ trunk/nhibernate/src/NHibernate.Linq/Util/TypeSystem.cs trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-12 18:47:36 UTC (rev 3699) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-12 19:50:39 UTC (rev 3700) @@ -48,6 +48,10 @@ <ItemGroup> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> + <Compile Include="NHibernateQueryProvider.cs" /> + <Compile Include="Query.cs" /> + <Compile Include="QueryProvider.cs" /> + <Compile Include="Util\TypeSystem.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernateExtensions.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateExtensions.cs 2008-08-12 18:47:36 UTC (rev 3699) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateExtensions.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -16,7 +16,7 @@ /// <returns>An <see cref="T:NHibernate.Linq.NHibernateQueryProvider"/> used to evaluate an expression tree.</returns> public static IQueryable<T> Linq<T>(this ISession session) { - throw new NotImplementedException(); + return new Query<T>(new NHibernateQueryProvider(session)); } } } \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -0,0 +1,37 @@ +using System; +using System.Linq.Expressions; +using NHibernate.Linq.Util; + +namespace NHibernate.Linq +{ + public class NHibernateQueryProvider : QueryProvider + { + private readonly ISession _session; + + public NHibernateQueryProvider(ISession session) + { + if (session == null) throw new ArgumentNullException("session"); + _session = session; + } + + public override object Execute(Expression expression) + { + throw new NotImplementedException(); + + /* iteratively process expression tree here converting to NH tree */ + + //expression = Evaluator.PartialEval(expression); + //expression = new BinaryBooleanReducer().Visit(expression); + //expression = AssociationVisitor.RewriteWithAssociations(_session.SessionFactory, expression); + //expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression); + //expression = new PropertyToMethodVisitor().Visit(expression); + //expression = new BinaryExpressionOrderer().Visit(expression); + + //once tree is converted to NH tree, pass it to NHibernateQueryTranslator + //which will convert the tree to an NHibernate.SqlCommand.SqlString + + //NHibernateQueryTranslator translator = new NHibernateQueryTranslator(_session); + //return translator.Translate(expression,this.queryOptions); + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Query.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Query.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Query.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -0,0 +1,62 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections; +using System.Collections.Generic; + +namespace NHibernate.Linq +{ + ///<summary> + /// Generic IQueryable base class. + /// </summary> + public class Query<T> : IQueryable<T> + { + private readonly QueryProvider provider; + private readonly Expression expression; + + public Query(QueryProvider provider) + { + if (provider == null) throw new ArgumentNullException("provider"); + + this.provider = provider; + this.expression = Expression.Constant(this); + } + + public Query(QueryProvider provider, Expression expression) + { + if (provider == null) throw new ArgumentNullException("provider"); + if (expression == null) throw new ArgumentNullException("expression"); + + if (!typeof(IQueryable<T>).IsAssignableFrom(expression.Type)) + throw new ArgumentOutOfRangeException("expression"); + + this.provider = provider; + this.expression = expression; + } + + Expression IQueryable.Expression + { + get { return this.expression; } + } + + System.Type IQueryable.ElementType + { + get { return typeof(T); } + } + + IQueryProvider IQueryable.Provider + { + get { return this.provider; } + } + + public IEnumerator<T> GetEnumerator() + { + return ((IEnumerable<T>)this.provider.Execute(this.expression)).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)this.provider.Execute(this.expression)).GetEnumerator(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq/QueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/QueryProvider.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/QueryProvider.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; +using NHibernate.Linq.Util; + +namespace NHibernate.Linq +{ + /// <summary> + /// Generic IQueryProvider base class. + /// </summary> + public abstract class QueryProvider : IQueryProvider + { + IQueryable<T> IQueryProvider.CreateQuery<T>(Expression expression) + { + return new Query<T>(this, expression); + } + + IQueryable IQueryProvider.CreateQuery(Expression expression) + { + System.Type elementType = TypeSystem.GetElementType(expression.Type); + return (IQueryable)Activator.CreateInstance(typeof(Query<>).MakeGenericType(elementType), new object[] { this, expression }); + } + + T IQueryProvider.Execute<T>(Expression expression) + { + return (T)this.Execute(expression); + } + + object IQueryProvider.Execute(Expression expression) + { + return this.Execute(expression); + } + + public abstract object Execute(Expression expression); + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Util/TypeSystem.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Util/TypeSystem.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Util/TypeSystem.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -0,0 +1,58 @@ +using System; +using System.Diagnostics; +using System.Collections.Generic; +using System.Linq; + +namespace NHibernate.Linq.Util +{ + /// <remarks> + /// Copied from internal class of same name in System.Core assembly. + /// </remarks> + [DebuggerStepThrough, DebuggerNonUserCode] + public static class TypeSystem + { + public static System.Type GetElementType(System.Type seqType) + { + System.Type ienum = FindIEnumerable(seqType); + if (ienum == null) return seqType; + return ienum.GetGenericArguments()[0]; + } + + private static System.Type FindIEnumerable(System.Type seqType) + { + if (seqType == null || seqType == typeof(string)) + return null; + + if (seqType.IsArray) + return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType()); + + if (seqType.IsGenericType) + { + foreach (System.Type arg in seqType.GetGenericArguments()) + { + System.Type ienum = typeof(IEnumerable<>).MakeGenericType(arg); + if (ienum.IsAssignableFrom(seqType)) + { + return ienum; + } + } + } + + System.Type[] ifaces = seqType.GetInterfaces(); + if (ifaces != null && ifaces.Length > 0) + { + foreach (System.Type iface in ifaces) + { + System.Type ienum = FindIEnumerable(iface); + if (ienum != null) return ienum; + } + } + + if (seqType.BaseType != null && seqType.BaseType != typeof(object)) + { + return FindIEnumerable(seqType.BaseType); + } + return null; + } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml 2008-08-12 18:47:36 UTC (rev 3699) +++ trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.hbm.xml 2008-08-12 19:50:39 UTC (rev 3700) @@ -12,7 +12,7 @@ <many-to-one name="Zoo" column="zoo_id"/> <property name="SerialNumber"/> - <set name="Offspring" order-by="father_id"> + <set name="Offspring"> <key column="mother_id"/> <one-to-many class="Animal"/> </set> @@ -66,11 +66,11 @@ <many-to-many column="Human2" class="Human"/> </bag> - <map name="Family"> + <!--<map name="Family"> <key column="Human1"/> <map-key column="Relationship" type="AnsiString"/> <many-to-many column="Human2" class="Human"/> - </map> + </map>--> <bag name="Pets" inverse="true"> <key column="Owner"/> @@ -82,7 +82,7 @@ <element column="nick_name" type="string" not-null="true" /> </set> - <map name="Addresses" table="addresses"> + <!--<map name="Addresses" table="addresses"> <key column="Human"/> <map-key type="string" column="type" /> @@ -93,12 +93,12 @@ <property name="Country"/> <many-to-one name="StateProvince" column="state_prov_id" class="StateProvince" /> </composite-element> - </map> + </map>--> </joined-subclass> </joined-subclass> </class> - <class name="User" table="User"> + <class name="User" table="`User`"> <id name="Id"> <generator class="foreign"> <param name="property">Human</param> @@ -124,7 +124,7 @@ <property name="Name" type="String"/> <property name="Classification" /> - <map name="Mammals"> + <!--<map name="Mammals"> <key column="MammalZoo_id"/> <index type="string" column="Name"/> <one-to-many class="Mammal"/> @@ -134,7 +134,7 @@ <key column="zoo_id"/> <index type="string" column="SerialNumber"/> <one-to-many class="Animal"/> - </map> + </map>--> <component name="Address" class="Address"> <property name="Street"/> Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-12 18:47:36 UTC (rev 3699) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-12 19:50:39 UTC (rev 3700) @@ -75,6 +75,7 @@ <Compile Include="BaseTest.cs" /> <Compile Include="GlobalSetup.cs" /> <Compile Include="Model\Animal.cs" /> + <Compile Include="SelectTest.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Added: trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs 2008-08-12 19:50:39 UTC (rev 3700) @@ -0,0 +1,19 @@ +using System; +using System.Linq; +using NUnit.Framework; +using NHibernate.Linq.Test.Model; + +namespace NHibernate.Linq.Test +{ + [TestFixture] + public class SelectTest : BaseTest + { + [Test] + [Ignore("this doesn't work yet")] + public void CanSelectAnimals() + { + var animals = session.Linq<Animal>(); + Assert.IsNotNull(animals); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-08-14 17:04:27
|
Revision: 3702 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3702&view=rev Author: tehlike Date: 2008-08-14 17:04:36 +0000 (Thu, 14 Aug 2008) Log Message: ----------- Added logical expression reducer. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Visitors/ trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/ trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/LogicalExpressionSimplifierTests.cs Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-13 14:59:18 UTC (rev 3701) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-14 17:04:36 UTC (rev 3702) @@ -3,7 +3,7 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>9.0.21022</ProductVersion> + <ProductVersion>9.0.30729</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{3073F5DA-5D57-4E12-9F95-8516ED346E67}</ProjectGuid> <OutputType>Library</OutputType> @@ -46,6 +46,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> <Compile Include="NHibernateQueryProvider.cs" /> Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs 2008-08-14 17:04:36 UTC (rev 3702) @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Visitors +{ + /// <summary> + /// Reduces the expression using logical simplification + /// Please note that it doesn't evaluate the binary expressions with constant operands + /// </summary> + + //May be used for animal.Offspring.Any() == true + //Eventhough it is almost unnecessary to reduce not(not(true)) kind of expression, but simplified expressions are gold. + public class LogicalExpressionReducer:ExpressionVisitor + { + protected override Expression VisitBinary(BinaryExpression expr) + { + bool modified; + Expression e = ProcessBinaryExpression(expr.Left, expr.Right, expr.NodeType, out modified); + if (modified) + return Visit(e); + e = ProcessBinaryExpression(expr.Right, expr.Left, expr.NodeType, out modified); + if(modified) + return Visit(e); + return expr; + + } + + protected override Expression VisitUnary(UnaryExpression u) + { + switch(u.NodeType) + { + case ExpressionType.Not: + if (u.Operand.NodeType == ExpressionType.Not) + return Visit(((UnaryExpression) (u.Operand)).Operand); + else if (u.Operand.NodeType == ExpressionType.Equal) + { + var binaryExpression = u.Operand as BinaryExpression; + return Visit(Expression.NotEqual(binaryExpression.Left, binaryExpression.Right)); + } + else if (u.Operand.NodeType == ExpressionType.Equal) + { + var binaryExpression = u.Operand as BinaryExpression; + return Visit(Expression.Equal(binaryExpression.Left, binaryExpression.Right)); + } + else if(u.Operand.NodeType==ExpressionType.Constant) + { + var constantExpression = u.Operand as ConstantExpression; + return Visit(Expression.Constant(!((bool)constantExpression.Value))); + } + break; + } + return u; + } + + private Expression ProcessBinaryExpression(Expression exprToCompare, Expression exprToReturn, + ExpressionType nodeType, out bool modified) + { + modified = false; + + var visitor = new BooleanConstantFinder(); + visitor.Visit(exprToCompare); + + if (!visitor.Constant.HasValue) + return null; + + bool constantValue = visitor.Constant.Value; + switch (nodeType) + { + case ExpressionType.Equal: + modified = true; + return constantValue ? exprToReturn : Expression.Not(exprToReturn); + case ExpressionType.NotEqual: + modified = true; + return constantValue ? Expression.Not(exprToReturn) : exprToReturn; + case ExpressionType.Or: + case ExpressionType.OrElse: + modified = true; + return constantValue ? Expression.Constant(true) : exprToReturn; + case ExpressionType.And: + case ExpressionType.AndAlso: + modified = true; + return constantValue ? exprToReturn : Expression.Constant(false); + default: + return null; + } + + } + + class BooleanConstantFinder : ExpressionVisitor + { + private bool isNestedBinaryExpression; + + public bool? Constant { get; private set; } + + protected override Expression VisitConstant(ConstantExpression c) + { + if (c.Type == typeof(bool) && !isNestedBinaryExpression) + Constant = (bool)c.Value; + return c; + } + + protected override Expression VisitBinary(BinaryExpression b) + { + isNestedBinaryExpression = true; + return b; + } + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-13 14:59:18 UTC (rev 3701) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-08-14 17:04:36 UTC (rev 3702) @@ -3,7 +3,7 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>9.0.21022</ProductVersion> + <ProductVersion>9.0.30729</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{FEAC164E-DE15-418A-8A70-35085B33C548}</ProjectGuid> <OutputType>Library</OutputType> @@ -76,6 +76,7 @@ <Compile Include="GlobalSetup.cs" /> <Compile Include="Model\Animal.cs" /> <Compile Include="SelectTest.cs" /> + <Compile Include="VisitorTests\LogicalExpressionSimplifierTests.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Added: trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/LogicalExpressionSimplifierTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/LogicalExpressionSimplifierTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/LogicalExpressionSimplifierTests.cs 2008-08-14 17:04:36 UTC (rev 3702) @@ -0,0 +1,207 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Criterion; +using NHibernate.Linq.Visitors; +using NUnit.Framework; +using Expression=System.Linq.Expressions.Expression; + +namespace NHibernate.Linq.Test.VisitorTests +{ + [TestFixture] + public class LogicalExpressionSimplifierTests + { + [Test] + public void CanReduceAndAlsoWithFalse() + { + var left = Expression.Constant(false); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.AndAlso(left, right); + var ex2 = Expression.AndAlso(right, left); + var reduced1 = Reduce(ex1) as ConstantExpression; + var reduced2 = Reduce(ex2) as ConstantExpression; + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.IsFalse((bool)reduced1.Value); + Assert.IsFalse((bool)reduced2.Value); + + Console.WriteLine("Original expressions\n"); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions\n"); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + + [Test] + public void CanReduceOrWithTrue() + { + var left = Expression.Constant(true); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.OrElse(left, right); + var ex2 = Expression.OrElse(right, left); + var reduced1 = Reduce(ex1) as ConstantExpression; + var reduced2 = Reduce(ex2) as ConstantExpression; + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.IsTrue((bool)reduced1.Value); + Assert.IsTrue((bool)reduced2.Value); + + Console.WriteLine("Original expressions"); + Console.WriteLine(""); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions"); + Console.WriteLine(""); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + + [Test] + public void CanReduceEqualWithTrue() + { + var left = Expression.Constant(true); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.Equal(left, right); + var ex2 = Expression.Equal(right, left); + var reduced1 = Reduce(ex1); + var reduced2 = Reduce(ex2); + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.AreEqual(reduced1,right); + Assert.AreEqual(reduced2, right); + + Console.WriteLine("Original expressions"); + Console.WriteLine(""); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions"); + Console.WriteLine(""); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + [Test] + public void CanReduceEqualWithFalse() + { + var left = Expression.Constant(false); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.Equal(left, right); + var ex2 = Expression.Equal(right, left); + var reduced1 = Reduce(ex1) as UnaryExpression; + var reduced2 = Reduce(ex2) as UnaryExpression; + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.AreEqual(ExpressionType.Not, reduced1.NodeType); + Assert.AreEqual(ExpressionType.Not, reduced2.NodeType); + Assert.AreEqual(right,reduced1.Operand); + Assert.AreEqual(right, reduced2.Operand); + + Console.WriteLine("Original expressions"); + Console.WriteLine(""); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions"); + Console.WriteLine(""); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + + [Test] + public void CanReduceNotEqualWithTrue() + { + var left = Expression.Constant(true); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.NotEqual(left, right); + var ex2 = Expression.NotEqual(right, left); + var reduced1 = Reduce(ex1) as UnaryExpression; + var reduced2 = Reduce(ex2) as UnaryExpression; + + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.AreEqual(ExpressionType.Not, reduced1.NodeType); + Assert.AreEqual(ExpressionType.Not, reduced2.NodeType); + Assert.AreEqual(right, reduced1.Operand); + Assert.AreEqual(right, reduced2.Operand); + + Console.WriteLine("Original expressions"); + Console.WriteLine(""); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions"); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + [Test] + public void CanReduceNotEqualWithFalse() + { + var left = Expression.Constant(false); + var right = Expression.GreaterThan(Expression.Constant(3), Expression.Constant(1)); + var ex1 = Expression.NotEqual(left, right); + var ex2 = Expression.NotEqual(right, left); + var reduced1 = Reduce(ex1); + var reduced2 = Reduce(ex2); + Assert.IsNotNull(reduced1); + Assert.IsNotNull(reduced2); + Assert.AreEqual(reduced1, right); + Assert.AreEqual(reduced2, right); + + Console.WriteLine("Original expressions\n"); + Console.WriteLine(""); + Console.WriteLine(ex1.ToString()); + Console.WriteLine(ex2.ToString()); + Console.WriteLine(""); + Console.WriteLine("Reduced expressions\n"); + Console.WriteLine(""); + Console.WriteLine(reduced1.ToString()); + Console.WriteLine(reduced2.ToString()); + } + + + [Test] + public void CanReduceNested() + { + var trueExpression = Expression.Constant(true); + var falseExpression = Expression.Constant(false); + var testExpression = Expression.Equal(Expression.Equal(Expression.Equal(falseExpression, falseExpression), falseExpression),falseExpression); + var reduced = Reduce(testExpression) as ConstantExpression; + Assert.IsNotNull(reduced); + Assert.IsTrue((bool)reduced.Value); + + Console.WriteLine("Original expressions\n"); + Console.WriteLine(testExpression.ToString()); + + Console.WriteLine("Reduced expressions\n"); + Console.WriteLine(reduced.ToString()); + } + [Test] + public void CanReduceNested2() + { + var trueExpression = Expression.Constant(true); + var falseExpression = Expression.Constant(false); + var testExpression = Expression.Equal(Expression.Equal(Expression.NotEqual(trueExpression, falseExpression), falseExpression), falseExpression); + var reduced = Reduce(testExpression) as ConstantExpression; + Assert.IsNotNull(reduced); + Assert.IsTrue((bool)reduced.Value); + + Console.WriteLine("Original expressions\n"); + Console.WriteLine(testExpression.ToString()); + + Console.WriteLine("Reduced expressions\n"); + Console.WriteLine(reduced.ToString()); + } + + protected Expression Reduce(Expression expr) + { + var reducer = new LogicalExpressionReducer(); + return reducer.Visit(expr); + } + + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-08-15 11:25:06
|
Revision: 3705 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3705&view=rev Author: tehlike Date: 2008-08-15 11:25:15 +0000 (Fri, 15 Aug 2008) Log Message: ----------- -Copied Evaluator from Contrib and renamed it to LocalVariableExpressionReducer Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs trunk/nhibernate/src/NHibernate.Linq/Query.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Util/Guard.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/LocalVariableExpressionReducer.cs Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-15 05:37:47 UTC (rev 3704) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-15 11:25:15 UTC (rev 3705) @@ -46,6 +46,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Compile Include="Util\Guard.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> @@ -53,6 +54,7 @@ <Compile Include="Query.cs" /> <Compile Include="QueryProvider.cs" /> <Compile Include="Util\TypeSystem.cs" /> + <Compile Include="Visitors\LocalVariableExpressionReducer.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-08-15 05:37:47 UTC (rev 3704) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-08-15 11:25:15 UTC (rev 3705) @@ -1,17 +1,19 @@ using System; -using System.Linq.Expressions; +using NHibernate.Criterion; using NHibernate.Linq.Util; +using Expression=System.Linq.Expressions.Expression; +using NHibernate.Linq.Visitors; namespace NHibernate.Linq { public class NHibernateQueryProvider : QueryProvider { - private readonly ISession _session; + private readonly ISession session; public NHibernateQueryProvider(ISession session) { - if (session == null) throw new ArgumentNullException("session"); - _session = session; + Guard.AgainstNull(session,"session"); + this.session = session; } public override object Execute(Expression expression) @@ -20,9 +22,9 @@ /* iteratively process expression tree here converting to NH tree */ - //expression = Evaluator.PartialEval(expression); - //expression = new BinaryBooleanReducer().Visit(expression); - //expression = AssociationVisitor.RewriteWithAssociations(_session.SessionFactory, expression); + //expression = LocalVariableExpressionReducer.Reduce(expression); + //expression = LogicalExpressionReducer.Reduce(expression); + //expression = AssociationVisitor.RewriteWithAssociations(session.SessionFactory, expression); //expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression); //expression = new PropertyToMethodVisitor().Visit(expression); //expression = new BinaryExpressionOrderer().Visit(expression); @@ -30,7 +32,7 @@ //once tree is converted to NH tree, pass it to NHibernateQueryTranslator //which will convert the tree to an NHibernate.SqlCommand.SqlString - //NHibernateQueryTranslator translator = new NHibernateQueryTranslator(_session); + //NHibernateQueryTranslator translator = new NHibernateQueryTranslator(session); //return translator.Translate(expression,this.queryOptions); } } Modified: trunk/nhibernate/src/NHibernate.Linq/Query.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Query.cs 2008-08-15 05:37:47 UTC (rev 3704) +++ trunk/nhibernate/src/NHibernate.Linq/Query.cs 2008-08-15 11:25:15 UTC (rev 3705) @@ -3,6 +3,7 @@ using System.Linq.Expressions; using System.Collections; using System.Collections.Generic; +using NHibernate.Linq.Util; namespace NHibernate.Linq { @@ -16,7 +17,7 @@ public Query(QueryProvider provider) { - if (provider == null) throw new ArgumentNullException("provider"); + Guard.AgainstNull(provider,"provider"); this.provider = provider; this.expression = Expression.Constant(this); @@ -24,9 +25,10 @@ public Query(QueryProvider provider, Expression expression) { - if (provider == null) throw new ArgumentNullException("provider"); - if (expression == null) throw new ArgumentNullException("expression"); + Guard.AgainstNull(provider,"provider"); + Guard.AgainstNull(expression,"expression"); + if (!typeof(IQueryable<T>).IsAssignableFrom(expression.Type)) throw new ArgumentOutOfRangeException("expression"); Added: trunk/nhibernate/src/NHibernate.Linq/Util/Guard.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Util/Guard.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Util/Guard.cs 2008-08-15 11:25:15 UTC (rev 3705) @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Util +{ + public static class Guard + { + public static void AgainstNull(object obj,string parameterName) + { + if (obj == null) + throw new ArgumentNullException(parameterName); + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/LocalVariableExpressionReducer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/LocalVariableExpressionReducer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/LocalVariableExpressionReducer.cs 2008-08-15 11:25:15 UTC (rev 3705) @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Visitors +{ + /// <summary> + /// Evaluates the local references before they are translated into sql. + /// </summary> + public static class LocalVariableExpressionReducer + { + /// <summary> + /// Performs evaluation & replacement of independent sub-trees + /// </summary> + /// <param name="expression">The root of the expression tree.</param> + /// <param name="fnCanBeEvaluated">A function that decides whether a given expression node can be part of the local function.</param> + /// <returns>A new tree with sub-trees evaluated and replaced.</returns> + public static Expression Reduce(Expression expression, Func<Expression, bool> fnCanBeEvaluated) + { + return new SubtreeEvaluator(new Nominator(fnCanBeEvaluated).Nominate(expression)).Eval(expression); + } + + /// <summary> + /// Performs evaluation & replacement of independent sub-trees + /// </summary> + /// <param name="expression">The root of the expression tree.</param> + /// <returns>A new tree with sub-trees evaluated and replaced.</returns> + public static Expression Reduce(Expression expression) + { + return Reduce(expression, LocalVariableExpressionReducer.CanBeEvaluatedLocally); + } + + private static bool CanBeEvaluatedLocally(Expression expression) + { + if (expression.NodeType == ExpressionType.Constant) + { + return !(((ConstantExpression)expression).Value is IQueryable); + } + + return expression.NodeType != ExpressionType.Parameter; + } + + /// <summary> + /// Evaluates & replaces sub-trees when first candidate is reached (top-down) + /// </summary> + class SubtreeEvaluator : ExpressionVisitor + { + HashSet<Expression> candidates; + + internal SubtreeEvaluator(HashSet<Expression> candidates) + { + this.candidates = candidates; + } + + internal Expression Eval(Expression exp) + { + return this.Visit(exp); + } + + public override Expression Visit(Expression exp) + { + if (exp == null) + { + return null; + } + if (this.candidates.Contains(exp)) + { + return this.Evaluate(exp); + } + return base.Visit(exp); + } + + private Expression Evaluate(Expression e) + { + if (e.NodeType == ExpressionType.Constant) + { + return e; + } + LambdaExpression lambda = Expression.Lambda(e); + Delegate fn = lambda.Compile(); + return Expression.Constant(fn.DynamicInvoke(null), e.Type); + } + } + + /// <summary> + /// Performs bottom-up analysis to determine which nodes can possibly + /// be part of an evaluated sub-tree. + /// </summary> + class Nominator : ExpressionVisitor + { + Func<Expression, bool> fnCanBeEvaluated; + HashSet<Expression> candidates; + bool cannotBeEvaluated; + + internal Nominator(Func<Expression, bool> fnCanBeEvaluated) + { + this.fnCanBeEvaluated = fnCanBeEvaluated; + } + + internal HashSet<Expression> Nominate(Expression expression) + { + this.candidates = new HashSet<Expression>(); + this.Visit(expression); + return this.candidates; + } + + public override Expression Visit(Expression expression) + { + if (expression != null) + { + bool saveCannotBeEvaluated = this.cannotBeEvaluated; + this.cannotBeEvaluated = false; + base.Visit(expression); + if (!this.cannotBeEvaluated) + { + if (this.fnCanBeEvaluated(expression)) + { + this.candidates.Add(expression); + } + else + { + this.cannotBeEvaluated = true; + } + } + this.cannotBeEvaluated |= saveCannotBeEvaluated; + } + return expression; + } + } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs 2008-08-15 05:37:47 UTC (rev 3704) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs 2008-08-15 11:25:15 UTC (rev 3705) @@ -12,9 +12,14 @@ /// </summary> //May be used for animal.Offspring.Any() == true - //Eventhough it is almost unnecessary to reduce not(not(true)) kind of expression, but simplified expressions are gold. + //Eventhough it is almost unnecessary to reduce not(not(true)) kind of expression(sql servers can easily optimize them, + //simplified expressions is gold for debugging. public class LogicalExpressionReducer:ExpressionVisitor { + public static Expression Reduce(Expression expr) + { + return new LogicalExpressionReducer().Visit(expr); + } protected override Expression VisitBinary(BinaryExpression expr) { bool modified; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-08-16 23:15:07
|
Revision: 3712 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3712&view=rev Author: tehlike Date: 2008-08-16 23:15:16 +0000 (Sat, 16 Aug 2008) Log Message: ----------- Initial SqlExpression tree hierarchy. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/ trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Metadata; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// An entity is where we want to execute our query on, in RDBMS case it is a Table. + /// May also be used as from clause of a select expression + /// </summary> + public class EntityExpression:SqlExpression + { + public EntityExpression(System.Type type, string alias) + : base(SqlExpressionType.Entity, type) + { + this.Alias = alias; + } + + public string Alias { get; protected set; } + + public override string ToString() + { + return string.Format("(({0}) as {1}", Type.ToString(), this.Alias); + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs ___________________________________________________________________ Added: svn:mergeinfo + Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + public class LimitFragment + { + public int? Skip { get; set; } + public int? Take { get; set; } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// A member access expression is produced where a member of an object is accessed(can be field or Property) + /// </summary> + public class MemberAccessExpression:SqlExpression + { + public MemberAccessExpression(System.Type type, SqlExpression expression, string name) + : base(SqlExpressionType.MemberAccess, type) + { + this.Name = name; + this.Expression = expression; + } + + public string Name { get; protected set; } + public SqlExpression Expression { get; protected set; } + public override string ToString() + { + return string.Format("({0}.{1})", this.Expression, ".", this.Name); + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// The direction of the ordering, default is Ascending. + /// </summary> + public enum OrderByDirection + { + Ascending, + Descending + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// An order by expression is an expression that the result is ordered. + /// This class holds reference to the actual expression(can be a projection, property, or an aggregate) + /// </summary> + public class OrderByFragment + { + public OrderByFragment(Expression orderBy,OrderByDirection direction) + { + this.OrderBy = orderBy; + this.Direction = direction; + } + + public Expression OrderBy { get; protected set; } + public OrderByDirection Direction { get;protected set; } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs ___________________________________________________________________ Added: svn:mergeinfo + Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// Projection expression + /// </summary> + public class ProjectionExpression:SqlExpression + { + public ProjectionExpression(System.Type type,LambdaExpression lambda):base(SqlExpressionType.Projection,type) + { + this.Projector = lambda; + } + + public LambdaExpression Projector { get; protected set; } + public override string ToString() + { + return string.Format("({0})", this.Projector.ToString()); + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Linq.Expressions; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// A select expression is executed against the database to retrieve data. + /// </summary> + public class SelectExpression : SqlExpression + { + public SelectExpression(System.Type type, SqlExpression from, SqlExpression where, IList<OrderByFragment> orderBys) + : base(SqlExpressionType.Select, type) + { + this.Where = where; + this.OrderBys = orderBys; + this.From = from; + } + + public SqlExpression Where { get; protected set; } + public SqlExpression From { get; protected set; } + public IList<OrderByFragment> OrderBys { get; protected set; } + public LimitFragment Limit { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + public class SelectFragment + { + public SelectFragment(string alias,SqlExpression expression) + { + this.Alias = alias; + this.Expression = expression; + } + + public string Alias { get; protected set; } + public SqlExpression Expression { get; protected set; } + public override string ToString() + { + return string.Format("({0} as {1})", Expression.ToString(),Alias); + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// Base expression for transformed nodes. + /// </summary> + public abstract class SqlExpression:Expression + { + /// <summary> + /// Base class for the modified nodes. + /// </summary> + /// <param name="nodeType"></param> + /// <param name="type"></param> + protected SqlExpression(SqlExpressionType nodeType, System.Type type) + : base((ExpressionType)nodeType, type) + { + + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs 2008-08-16 23:15:16 UTC (rev 3712) @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + public enum SqlExpressionType + { + Entity=100, + Select, + MemberAccess, + Projection, + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-16 19:11:04 UTC (rev 3711) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-16 23:15:16 UTC (rev 3712) @@ -46,6 +46,16 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Compile Include="Expressions\LimitFragment.cs" /> + <Compile Include="Expressions\MemberAccessExpression.cs" /> + <Compile Include="Expressions\OrderByDirection.cs" /> + <Compile Include="Expressions\OrderByFragment.cs" /> + <Compile Include="Expressions\ProjectionExpression.cs" /> + <Compile Include="Expressions\SelectExpression.cs" /> + <Compile Include="Expressions\SelectFragment.cs" /> + <Compile Include="Expressions\SqlExpression.cs" /> + <Compile Include="Expressions\SqlExpressionType.cs" /> + <Compile Include="Expressions\EntityExpression.cs" /> <Compile Include="Util\Guard.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-08-17 17:50:12
|
Revision: 3713 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3713&view=rev Author: tehlike Date: 2008-08-17 17:50:20 +0000 (Sun, 17 Aug 2008) Log Message: ----------- Removed order by and limit fragment classes in order to make sure we work incrementally. Added NHibernateExpressionVisitor class which can visit nhibernate specific nodes. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityChecker.cs trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -9,7 +9,7 @@ { /// <summary> /// An entity is where we want to execute our query on, in RDBMS case it is a Table. - /// May also be used as from clause of a select expression + /// May also be used in from clause of a select expression /// </summary> public class EntityExpression:SqlExpression { Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/LimitFragment.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - public class LimitFragment - { - public int? Skip { get; set; } - public int? Take { get; set; } - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/MemberAccessExpression.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - /// <summary> - /// A member access expression is produced where a member of an object is accessed(can be field or Property) - /// </summary> - public class MemberAccessExpression:SqlExpression - { - public MemberAccessExpression(System.Type type, SqlExpression expression, string name) - : base(SqlExpressionType.MemberAccess, type) - { - this.Name = name; - this.Expression = expression; - } - - public string Name { get; protected set; } - public SqlExpression Expression { get; protected set; } - public override string ToString() - { - return string.Format("({0}.{1})", this.Expression, ".", this.Name); - } - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByDirection.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - /// <summary> - /// The direction of the ordering, default is Ascending. - /// </summary> - public enum OrderByDirection - { - Ascending, - Descending - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderByFragment.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - /// <summary> - /// An order by expression is an expression that the result is ordered. - /// This class holds reference to the actual expression(can be a projection, property, or an aggregate) - /// </summary> - public class OrderByFragment - { - public OrderByFragment(Expression orderBy,OrderByDirection direction) - { - this.OrderBy = orderBy; - this.Direction = direction; - } - - public Expression OrderBy { get; protected set; } - public OrderByDirection Direction { get;protected set; } - } -} Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -11,12 +11,14 @@ /// </summary> public class ProjectionExpression:SqlExpression { - public ProjectionExpression(System.Type type,LambdaExpression lambda):base(SqlExpressionType.Projection,type) + public ProjectionExpression(SelectExpression source, Expression lambda) + : base(SqlExpressionType.Projection, lambda.Type) { this.Projector = lambda; } - public LambdaExpression Projector { get; protected set; } + public Expression Projector { get; protected set; } + public SelectExpression Source { get; set; } public override string ToString() { return string.Format("({0})", this.Projector.ToString()); Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -11,17 +11,16 @@ /// </summary> public class SelectExpression : SqlExpression { - public SelectExpression(System.Type type, SqlExpression from, SqlExpression where, IList<OrderByFragment> orderBys) + public SelectExpression(System.Type type, string alias, Expression from, Expression where) : base(SqlExpressionType.Select, type) { this.Where = where; - this.OrderBys = orderBys; this.From = from; + this.Alias = alias; } - public SqlExpression Where { get; protected set; } - public SqlExpression From { get; protected set; } - public IList<OrderByFragment> OrderBys { get; protected set; } - public LimitFragment Limit { get; set; } + public string Alias { get; set; } + public Expression Where { get; protected set; } + public Expression From { get; protected set; } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -9,7 +9,6 @@ { Entity=100, Select, - MemberAccess, Projection, } } Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-17 17:50:20 UTC (rev 3713) @@ -46,10 +46,6 @@ </ProjectReference> </ItemGroup> <ItemGroup> - <Compile Include="Expressions\LimitFragment.cs" /> - <Compile Include="Expressions\MemberAccessExpression.cs" /> - <Compile Include="Expressions\OrderByDirection.cs" /> - <Compile Include="Expressions\OrderByFragment.cs" /> <Compile Include="Expressions\ProjectionExpression.cs" /> <Compile Include="Expressions\SelectExpression.cs" /> <Compile Include="Expressions\SelectFragment.cs" /> @@ -57,6 +53,7 @@ <Compile Include="Expressions\SqlExpressionType.cs" /> <Compile Include="Expressions\EntityExpression.cs" /> <Compile Include="Util\Guard.cs" /> + <Compile Include="Util\LinqUtil.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> @@ -65,6 +62,9 @@ <Compile Include="QueryProvider.cs" /> <Compile Include="Util\TypeSystem.cs" /> <Compile Include="Visitors\LocalVariableExpressionReducer.cs" /> + <Compile Include="Visitors\NhibernateExpressionTransformer.cs" /> + <Compile Include="Visitors\NHibernateExpressionVisitor.cs" /> + <Compile Include="Visitors\QueryTransformer.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Added: trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Util +{ + public static class LinqUtil + { + public static Expression StripQuotes(Expression expression) + { + while (expression.NodeType == ExpressionType.Quote) + { + expression=((UnaryExpression)expression).Operand; + } + return expression; + } + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Linq.Expressions; + +namespace NHibernate.Linq.Visitors +{ + /// <summary> + /// A visitor for nhibernate specific expression + /// </summary> + public class NHibernateExpressionVisitor:ExpressionVisitor + { + public override Expression Visit(Expression exp) + { + if(exp==null) + return null; + switch((SqlExpressionType)exp.NodeType) + { + case SqlExpressionType.Entity: + return this.VisitEntity((EntityExpression)exp); + case SqlExpressionType.Select: + return this.VisitSelect((SelectExpression)exp); + case SqlExpressionType.Projection: + return this.VisitProjection((ProjectionExpression)exp); + default: + return base.Visit(exp); + } + } + + protected virtual Expression VisitProjection(ProjectionExpression projection) + { + SelectExpression source = (SelectExpression)this.Visit(projection.Source); + Expression projector = this.Visit(projection.Projector); + if (source != projection.Source || projector != projection.Projector) + { + return new ProjectionExpression(source, projector); + } + else + return projection; + } + protected virtual Expression VisitSource(Expression source) + { + return this.Visit(source); + } + protected virtual Expression VisitSelect(SelectExpression select) + { + Expression from = this.VisitSource(select.From); + Expression where = this.Visit(select.Where); + + if (from != select.From || where != select.Where) + { + return new SelectExpression(select.Type, select.Alias, from, where); + } + else + return select; + } + protected virtual Expression VisitEntity(EntityExpression expr) + { + return this.Visit(expr); + } + + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Visitors +{ + public class NhibernateExpressionTransformer:ExpressionVisitor + { + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Visitors +{ + public class QueryTransformer:ExpressionVisitor + { + } +} Modified: trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityChecker.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityChecker.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityChecker.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -8,35 +8,35 @@ { public static class ExpressionEqualityChecker { - public static bool ExpressionEquals(this Expression source,Expression toBeCompared) + public static bool ExpressionEquals(this Expression source, Expression toBeCompared) { - if(object.Equals(source,toBeCompared)) + if (object.Equals(source, toBeCompared)) return true; else if ((source != null && toBeCompared == null) || (source == null && toBeCompared != null)) { return false; } - else if (source.NodeType == toBeCompared.NodeType && source.Type==toBeCompared.Type) + else if (source.NodeType == toBeCompared.NodeType && source.Type == toBeCompared.Type) { - System.Type sourceType = source.GetType(); - var properties = sourceType.GetProperties(); - bool value = true; + System.Type sourceType = source.GetType(); + var properties = sourceType.GetProperties(); + bool value = true; - for (int i = 0; i < properties.Length;i++ ) + for (int i = 0; i < properties.Length && value; i++) + { + var info = properties[i]; + var leftValue = info.GetValue(source, null); + var rightValue = info.GetValue(toBeCompared, null); + if (leftValue is Expression) { - var info = properties[i]; - var leftValue = info.GetValue(source, null); - var rightValue = info.GetValue(toBeCompared, null); - if (leftValue is Expression) - { - value &= ExpressionEquals((Expression)leftValue, (Expression)rightValue); - } - else - value &= object.Equals(leftValue,rightValue); + value &= ExpressionEquals((Expression)leftValue, (Expression)rightValue); } - return value; + else + value &= object.Equals(leftValue, rightValue); + } + return value; } - else + else return false; } } Modified: trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs 2008-08-16 23:15:16 UTC (rev 3712) +++ trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs 2008-08-17 17:50:20 UTC (rev 3713) @@ -2,10 +2,11 @@ using System.Collections.Generic; using System.Data; using NHibernate; +using NHibernate.Linq.Test.Model; using NUnit.Framework; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; - +using M = NHibernate.Linq.Test.Model; namespace NHibernate.Linq.Test { public static class GlobalSetup @@ -19,14 +20,14 @@ factory = cfg.BuildSessionFactory(); } - public static ISession CreateSession() + public static void GenerateTestData() { - return factory.OpenSession(); + } - public static ISession CreateSession(IDbConnection con) + public static ISession CreateSession() { - return factory.OpenSession(con); + return factory.OpenSession(); } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-08-28 20:57:11
|
Revision: 3735 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3735&view=rev Author: tehlike Date: 2008-08-28 20:57:17 +0000 (Thu, 28 Aug 2008) Log Message: ----------- Initial NHExpression -> SQL translator. No entity initialization is implemented. Modified tree structure. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs trunk/nhibernate/src/NHibernate.Linq/Loaders/ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using NHibernate.Metadata; - -namespace NHibernate.Linq.Expressions -{ - /// <summary> - /// An entity is where we want to execute our query on, in RDBMS case it is a Table. - /// May also be used in from clause of a select expression - /// </summary> - public class EntityExpression:SqlExpression - { - public EntityExpression(System.Type type, string alias) - : base(SqlExpressionType.Entity, type) - { - this.Alias = alias; - } - - public string Alias { get; protected set; } - - public override string ToString() - { - return string.Format("(({0}) as {1}", Type.ToString(), this.Alias); - } - } -} Copied: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpression.cs (from rev 3726, trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// Base expression for transformed nodes. + /// </summary> + public abstract class NHExpression:Expression + { + /// <summary> + /// Base class for the modified nodes. + /// </summary> + /// <param name="nodeType"></param> + /// <param name="type"></param> + protected NHExpression(NHExpressionType nodeType, System.Type type) + : base((ExpressionType)nodeType, type) + { + + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpression.cs ___________________________________________________________________ Added: svn:mergeinfo + Copied: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs (from rev 3726, trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + public enum NHExpressionType + { + QuerySource=100, + Select, + Projection, + Property, + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ProjectionExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -6,22 +6,20 @@ namespace NHibernate.Linq.Expressions { - /// <summary> - /// Projection expression - /// </summary> - public class ProjectionExpression:SqlExpression + public class ProjectionExpression:NHExpression { - public ProjectionExpression(SelectExpression source, Expression lambda) - : base(SqlExpressionType.Projection, lambda.Type) + public ProjectionExpression(SelectExpression source, Expression projector) + : base(NHExpressionType.Projection, projector.Type) { - this.Projector = lambda; + this.Projector = projector; + this.Source = source; } public Expression Projector { get; protected set; } - public SelectExpression Source { get; set; } + public SelectExpression Source { get; protected set; } public override string ToString() { - return string.Format("({0})", this.Projector.ToString()); + return string.Format("({0})", this.Projector); } } } Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Net; +using System.Text; +using NHibernate.Metadata; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class PropertyExpression:NHExpression + { + public PropertyExpression(string name, + System.Type type, Expression expression, IType nhibernateType) + : this(name, type, expression,nhibernateType, NHExpressionType.Property) + { + + } + + + + protected PropertyExpression(string name, System.Type type,Expression expression, IType nhibernateType, NHExpressionType nodeType) + : base(nodeType, type) + { + this.NHibernateType = nhibernateType; + this.Expression = expression; + this.Name = name; + } + + public Expression Expression { get; protected set; } + public IType NHibernateType { get; protected set; } + public string Name { get; protected set; } + + public override string ToString() + { + return this.Expression.ToString() + "." + Name; + } + } +} Copied: trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs (from rev 3726, trunk/nhibernate/src/NHibernate.Linq/Expressions/EntityExpression.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Metadata; + +namespace NHibernate.Linq.Expressions +{ + /// <summary> + /// A QuerySource expression is where we want to execute our query on, in RDBMS case it is a physical Table in general. + /// May also be used in from clause of a select expression + /// </summary> + public class QuerySourceExpression:NHExpression + { + public QuerySourceExpression(IQueryable query) + : base(NHExpressionType.QuerySource, query.GetType()) + { + + this.Query = query; + } + + public IQueryable Query { get; protected set; } + + public override string ToString() + { + return string.Format("(({0})", Type.ToString()); + } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -9,17 +9,19 @@ /// <summary> /// A select expression is executed against the database to retrieve data. /// </summary> - public class SelectExpression : SqlExpression + public class SelectExpression : NHExpression { - public SelectExpression(System.Type type, string alias, Expression from, Expression where) - : base(SqlExpressionType.Select, type) + public SelectExpression(System.Type type, string alias, Expression projection, Expression from, Expression where) + : base(NHExpressionType.Select, type) { this.Where = where; this.From = from; - this.Alias = alias; + this.FromAlias = alias; + this.Projection = projection; } - public string Alias { get; set; } + public Expression Projection { get; protected set; } + public string FromAlias { get; protected set; } public Expression Where { get; protected set; } public Expression From { get; protected set; } } Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectFragment.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - public class SelectFragment - { - public SelectFragment(string alias,SqlExpression expression) - { - this.Alias = alias; - this.Expression = expression; - } - - public string Alias { get; protected set; } - public SqlExpression Expression { get; protected set; } - public override string ToString() - { - return string.Format("({0} as {1})", Expression.ToString(),Alias); - } - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpression.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - /// <summary> - /// Base expression for transformed nodes. - /// </summary> - public abstract class SqlExpression:Expression - { - /// <summary> - /// Base class for the modified nodes. - /// </summary> - /// <param name="nodeType"></param> - /// <param name="type"></param> - protected SqlExpression(SqlExpressionType nodeType, System.Type type) - : base((ExpressionType)nodeType, type) - { - - } - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SqlExpressionType.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Expressions -{ - public enum SqlExpressionType - { - Entity=100, - Select, - Projection, - } -} Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-08-28 20:57:17 UTC (rev 3735) @@ -47,13 +47,14 @@ </ItemGroup> <ItemGroup> <Compile Include="Expressions\ProjectionExpression.cs" /> + <Compile Include="Expressions\PropertyExpression.cs" /> <Compile Include="Expressions\SelectExpression.cs" /> - <Compile Include="Expressions\SelectFragment.cs" /> - <Compile Include="Expressions\SqlExpression.cs" /> - <Compile Include="Expressions\SqlExpressionType.cs" /> - <Compile Include="Expressions\EntityExpression.cs" /> + <Compile Include="Expressions\NHExpression.cs" /> + <Compile Include="Expressions\NHExpressionType.cs" /> + <Compile Include="Expressions\QuerySourceExpression.cs" /> <Compile Include="Util\Guard.cs" /> <Compile Include="Util\LinqUtil.cs" /> + <Compile Include="Visitors\AssociationRewriteVisitor.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> @@ -62,10 +63,14 @@ <Compile Include="QueryProvider.cs" /> <Compile Include="Util\TypeSystem.cs" /> <Compile Include="Visitors\LocalVariableExpressionReducer.cs" /> - <Compile Include="Visitors\NhibernateExpressionTransformer.cs" /> <Compile Include="Visitors\NHibernateExpressionVisitor.cs" /> - <Compile Include="Visitors\QueryTransformer.cs" /> + <Compile Include="Visitors\NHExpressionToSqlQueryTranslator.cs" /> + <Compile Include="Visitors\SqlExpressionToSqlStringVisitor.cs" /> + <Compile Include="Visitors\NHExpressionToSqlExpressionTransformer.cs" /> </ItemGroup> + <ItemGroup> + <Folder Include="Loaders\" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,8 +1,11 @@ using System; using NHibernate.Criterion; using NHibernate.Linq.Util; +using NHibernate.SqlCommand; using Expression=System.Linq.Expressions.Expression; using NHibernate.Linq.Visitors; +using NHibernate.Engine; +using System.Collections.Generic; namespace NHibernate.Linq { @@ -18,12 +21,14 @@ public override object Execute(Expression expression) { + IList<object> parameterList = new List<object>(); + ISessionFactoryImplementor sessionFactory = this.session.SessionFactory as ISessionFactoryImplementor; + expression = LocalVariableExpressionReducer.Reduce(expression); + expression = LogicalExpressionReducer.Reduce(expression); + expression = AssociationRewriteVisitor.Rewrite(expression, sessionFactory); + expression = NHExpressionToSqlExpressionTransformer.Transform(sessionFactory, expression); + SqlString sql=SqlExpressionToSqlStringVisitor.Translate(expression, sessionFactory,parameterList); throw new NotImplementedException(); - - /* iteratively process expression tree here converting to NH tree */ - - //expression = LocalVariableExpressionReducer.Reduce(expression); - //expression = LogicalExpressionReducer.Reduce(expression); //expression = AssociationVisitor.RewriteWithAssociations(session.SessionFactory, expression); //expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression); //expression = new PropertyToMethodVisitor().Visit(expression); @@ -32,8 +37,8 @@ //once tree is converted to NH tree, pass it to NHibernateQueryTranslator //which will convert the tree to an NHibernate.SqlCommand.SqlString - //NHibernateQueryTranslator translator = new NHibernateQueryTranslator(session); - //return translator.Translate(expression,this.queryOptions); + + //return translator.Transform(expression,this.queryOptions); } } } Modified: trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Util/LinqUtil.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -16,5 +16,10 @@ } return expression; } + public static bool IsAnonymousType(System.Type type) + { + return type != null && type.Name.StartsWith("<"); + } + } } Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,72 @@ +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using NHibernate.Linq.Expressions; +using NHibernate.Linq.Util; +using NHibernate.Metadata; +using NHibernate.Type; + +namespace NHibernate.Linq.Visitors +{ + public class AssociationRewriteVisitor:NHibernateExpressionVisitor + { + public static Expression Rewrite(Expression expr,ISessionFactory factory) + { + var visitor=new AssociationRewriteVisitor(factory); + expr=visitor.Visit(expr); + return expr; + } + + public AssociationRewriteVisitor(ISessionFactory factory) + { + this.sessionFactory = factory; + this.aliasOrder = 0; + + } + private readonly ISessionFactory sessionFactory; + + private IClassMetadata GetMetaData(System.Type type) + { + if (!LinqUtil.IsAnonymousType(type)) + { + try + { + return sessionFactory.GetClassMetadata(type); + } + catch (MappingException) + { + + } + } + return null; + } + + private string GetNextAlias() + { + return "source" + (aliasOrder++).ToString(); + } + + private int aliasOrder; + + protected override Expression VisitMemberAccess(MemberExpression expr) + { + expr = (MemberExpression)base.VisitMemberAccess(expr); + IClassMetadata clazz = GetMetaData(expr.Member.DeclaringType); + IType propertyType = clazz.GetPropertyType(expr.Member.Name); + if(propertyType.IsAssociationType||propertyType.IsComponentType) + { + return new PropertyExpression(expr.Member.Name, ((PropertyInfo)expr.Member).PropertyType, base.Visit(expr.Expression), propertyType); + } + else + { + return new PropertyExpression(expr.Member.Name, ((PropertyInfo)expr.Member).PropertyType, base.Visit(expr.Expression), propertyType); + } + } + protected override Expression VisitConstant(ConstantExpression c) + { + if (c.Value is IQueryable) + return new QuerySourceExpression((IQueryable) c.Value); + return c; + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/LogicalExpressionReducer.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -12,7 +12,7 @@ /// </summary> //May be used for animal.Offspring.Any() == true - //Eventhough it is almost unnecessary to reduce not(not(true)) kind of expression(sql servers can easily optimize them, + //Eventhough it is almost unnecessary to reduce not(not(true)) kind of expression(sql servers can easily optimize them, but //simplified expressions is gold for debugging. public class LogicalExpressionReducer:ExpressionVisitor { Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using NHibernate.Impl; +using NHibernate.Linq.Expressions; +using NHibernate.Linq.Util; +using NHibernate.Metadata; +using NHibernate.SqlCommand; +using NHibernate.Type; +using NHibernate.Mapping; +using NHibernate.Engine; +using NHibernate.Persister.Entity; +using IQueryable=System.Linq.IQueryable; + +namespace NHibernate.Linq.Visitors +{ + public class NHExpressionToSqlExpressionTransformer : NHibernateExpressionVisitor + { + public static Expression Transform(ISessionFactory factory,Expression expr) + { + return new NHExpressionToSqlExpressionTransformer(factory as ISessionFactoryImplementor).Visit(expr); + } + + public NHExpressionToSqlExpressionTransformer(ISessionFactoryImplementor sessionFactory) + { + this.sessionFactory = sessionFactory; + } + + private readonly ISessionFactoryImplementor sessionFactory; + + protected override Expression VisitMethodCall(MethodCallExpression m) + { + if(m.Method.DeclaringType==typeof(Enumerable)||m.Method.DeclaringType==typeof(Queryable)) + { + if(m.Method.Name=="Where") + { + return TransformWhereCall(m); + } + else if(m.Method.Name=="Select") + { + return TransformSelectCall(m); + } + } + return base.VisitMethodCall(m); + } + protected Expression TransformSelectCall(MethodCallExpression expr) + { + Expression source = this.Visit(expr.Arguments[0]); + LambdaExpression lambda = LinqUtil.StripQuotes(expr.Arguments[1]) as LambdaExpression; + Expression body = lambda.Body; + ParameterExpression parameter = lambda.Parameters[0]; + string alias = parameter.Name; + System.Type type = lambda.Body.Type; + System.Type resultType = typeof(IQueryable<>).MakeGenericType(type); + return new SelectExpression(resultType, alias, body, source, null); + } + + protected Expression TransformWhereCall(MethodCallExpression expr) + { + Expression source = this.Visit(expr.Arguments[0]); + Expression expression = expr.Arguments[1]; + LambdaExpression lambda = LinqUtil.StripQuotes(expr.Arguments[1]) as LambdaExpression; + Expression body = lambda.Body; + body=this.Visit(lambda.Body); + ParameterExpression parameter = lambda.Parameters[0]; + string alias = parameter.Name; + System.Type type = body.Type; + System.Type resultType = typeof(IQueryable<>).MakeGenericType(type); + return new SelectExpression(resultType, alias, null, source, body); + } + + protected override Expression VisitQuerySource(QuerySourceExpression expr) + { + return expr; + } + + + protected override Expression VisitBinary(BinaryExpression b) + { + switch(b.NodeType) + { + case ExpressionType.NotEqual://NOT EQUAL requires special handling since sql servers need expressions like NOT(x=y) + { + Expression eq = Expression.Equal(b.Left, b.Right); + return Expression.Not(eq); + } + default: + return base.VisitBinary(b); + } + + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs ___________________________________________________________________ Added: svn:mergeinfo + Copied: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs (from rev 3726, trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.SqlCommand; + +namespace NHibernate.Linq.Visitors +{ + public class NHExpressionToSqlQueryTranslator:NHibernateExpressionVisitor + { + protected SqlStringBuilder sqlStringBuilder; + public SqlString Translate(Expression expression) + { + this.sqlStringBuilder = new SqlStringBuilder(); + this.Visit(expression); + return sqlStringBuilder.ToSqlString(); + } + protected override Expression VisitSelect(NHibernate.Linq.Expressions.SelectExpression select) + { + return select; + } + protected override Expression VisitMethodCall(MethodCallExpression m) + { + return base.VisitMethodCall(m); + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -16,14 +16,16 @@ { if(exp==null) return null; - switch((SqlExpressionType)exp.NodeType) + switch((NHExpressionType)exp.NodeType) { - case SqlExpressionType.Entity: - return this.VisitEntity((EntityExpression)exp); - case SqlExpressionType.Select: + case NHExpressionType.QuerySource: + return this.VisitQuerySource((QuerySourceExpression)exp); + case NHExpressionType.Select: return this.VisitSelect((SelectExpression)exp); - case SqlExpressionType.Projection: + case NHExpressionType.Projection: return this.VisitProjection((ProjectionExpression)exp); + case NHExpressionType.Property: + return this.VisitProperty((PropertyExpression)exp); default: return base.Visit(exp); } @@ -42,23 +44,28 @@ } protected virtual Expression VisitSource(Expression source) { - return this.Visit(source); + return source; } + protected virtual Expression VisitProperty(PropertyExpression property) + { + return property; + } + //TODO: modify protected virtual Expression VisitSelect(SelectExpression select) { Expression from = this.VisitSource(select.From); Expression where = this.Visit(select.Where); - + Expression projection = this.Visit(select.Projection); if (from != select.From || where != select.Where) { - return new SelectExpression(select.Type, select.Alias, from, where); + return new SelectExpression(select.Type, select.FromAlias, projection, from, where); } else return select; } - protected virtual Expression VisitEntity(EntityExpression expr) + protected virtual Expression VisitQuerySource(QuerySourceExpression expr) { - return this.Visit(expr); + return expr; } } Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NhibernateExpressionTransformer.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Visitors -{ - public class NhibernateExpressionTransformer:ExpressionVisitor - { - } -} Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/QueryTransformer.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace NHibernate.Linq.Visitors -{ - public class QueryTransformer:ExpressionVisitor - { - } -} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -0,0 +1,206 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Engine; +using NHibernate.Linq.Expressions; +using NHibernate.SqlCommand; +using SelectFragment=NHibernate.SqlCommand.SelectFragment; +using NHibernate.Persister.Entity; +using NHibernate.Type; +using NHibernate.Metadata; + +namespace NHibernate.Linq.Visitors +{ + //TODO: Process the query source by converting into select expression + //TODO: Select expression should be changed so that it can handle columns or projections instead of expression. + public class SqlExpressionToSqlStringVisitor:NHibernateExpressionVisitor + { + public static SqlString Translate(Expression expr,ISessionFactoryImplementor sessionFactory,IList<object> parameterList) + { + + var visitor = new SqlExpressionToSqlStringVisitor(sessionFactory,parameterList); + visitor.Visit(expr); + return visitor.selectBuilder.ToSqlString(); + } + + public SqlExpressionToSqlStringVisitor(ISessionFactoryImplementor sessionFactory,IList<object> parameterList) + { + this.selectBuilder = new SqlStringBuilder(); + this.sessionFactory = sessionFactory; + this.parameterList = parameterList; + } + + private readonly IList<object> parameterList; + private ISessionFactoryImplementor sessionFactory; + private SqlStringBuilder selectBuilder; + protected override Expression VisitUnary(UnaryExpression u) + { + switch(u.NodeType) + { + case ExpressionType.Not: + { + selectBuilder.Add(" NOT ("); + Expression ret=base.VisitUnary(u); + selectBuilder.Add(")"); + return ret; + } + case ExpressionType.Negate: + { + selectBuilder.Add("(-1 * ("); + Expression ret = base.VisitUnary(u); + selectBuilder.Add("))"); + return ret; + } + } + return base.VisitUnary(u); + } + protected override Expression VisitBinary(BinaryExpression b) + { + string op = ""; + + #region operators + + switch (b.NodeType) + { + case ExpressionType.Add: + op = "+"; + break; + case ExpressionType.And: + case ExpressionType.AndAlso: + op = "AND"; + break; + case ExpressionType.Divide: + op = "/"; + break; + case ExpressionType.GreaterThan: + op = ">"; + break; + case ExpressionType.GreaterThanOrEqual: + op = ">="; + break; + case ExpressionType.LessThan: + op = "<"; + break; + case ExpressionType.LessThanOrEqual: + op = "<="; + break; + case ExpressionType.Modulo: + op = "MOD"; + break; + case ExpressionType.Multiply: + op = "*"; + break; + case ExpressionType.Or: + case ExpressionType.OrElse: + op = "OR"; + break; + case ExpressionType.Subtract: + op = "-"; + break; + case ExpressionType.Equal: + op = "="; + break; + case ExpressionType.NotEqual: + op = "!="; + break; + case ExpressionType.Power: + default: + throw new NotImplementedException(); + } + + #endregion + + selectBuilder.Add("("); + this.Visit(b.Left); + selectBuilder.Add(" "); + selectBuilder.Add(op); + selectBuilder.Add(" "); + this.Visit(b.Right); + selectBuilder.Add(")"); + return b; + } + protected override Expression VisitConditional(ConditionalExpression c) + { + return base.VisitConditional(c); + } + + protected override Expression VisitConstant(ConstantExpression c) + { + selectBuilder.AddParameter(); + parameterList.Add(c.Value); + return c; + } + + protected override Expression VisitProperty(PropertyExpression property) + { + ParameterExpression expr = property.Expression as ParameterExpression; + + selectBuilder.Add(string.Format("{0}.{1}", expr.Name, property.Name)); + return property; + } + + protected override Expression VisitSelect(SelectExpression select) + { + selectBuilder.Add("(SELECT "); + if(select.Projection!=null) + this.Visit(select.Projection); + else + selectBuilder.Add(select.FromAlias+".* "); + + if (select.From != null) + { + selectBuilder.Add(" FROM ("); + this.Visit(select.From); + selectBuilder.Add(") AS "); + selectBuilder.Add(select.FromAlias); + } + else + throw new NotImplementedException(); + if(select.Where!=null) + { + selectBuilder.Add(" WHERE "); + this.Visit(select.Where); + } + selectBuilder.Add(")"); + return select; + } + + protected override Expression VisitQuerySource(QuerySourceExpression expr) + { + selectBuilder.Add("SELECT "); + IClassMetadata metadata = sessionFactory.GetClassMetadata(expr.Query.ElementType); + IPropertyMapping mapping = (IPropertyMapping)sessionFactory.GetEntityPersister(metadata.EntityName); + string[] names = metadata.PropertyNames; + bool started = false; + for (int i = 0; i < names.Length;i++ ) + { + string name = names[i]; + IType propertyType = metadata.GetPropertyType(name); + if (!(propertyType.IsComponentType | + propertyType.IsCollectionType | + propertyType.IsAssociationType | + propertyType.IsAnyType)) + { + if(started) + selectBuilder.Add(", "); + started = true; + selectBuilder.Add(mapping.ToColumns(name)[0]); + } + + } + selectBuilder.Add(" FROM "); + selectBuilder.Add(expr.Query.ElementType.Name); + return base.VisitQuerySource(expr); + } + protected override Expression VisitParameter(ParameterExpression p) + { + return base.VisitParameter(p); + } + public override string ToString() + { + return selectBuilder.ToString(); + } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq.Test/GlobalSetup.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -18,11 +18,59 @@ Configuration cfg = new Configuration().Configure(); new SchemaExport(cfg).Execute(false, true, false, true); factory = cfg.BuildSessionFactory(); + GenerateTestData(); } public static void GenerateTestData() { - + ISession session = factory.OpenSession(); + using (ITransaction tx=session.BeginTransaction()) + { + StateProvince province = new StateProvince(); + province.IsoCode = "123456789"; + province.Name = "Sim State"; + + Address zooAddress = new Address(); + zooAddress.City = "Simcity"; + zooAddress.Country = "Sim Country"; + zooAddress.PostalCode = "1234"; + zooAddress.StateProvince = province; + zooAddress.Street = "North Sim street"; + + Zoo zoo1 = new Zoo(); + zoo1.Address = zooAddress; + zoo1.Classification = ClassificationType.Medium; + zoo1.Name = "Jurassic Park"; + + M.Animal animal1 = new Animal(); + animal1.BodyWeight = 10; + animal1.Description = "a persian"; + animal1.SerialNumber = "1000001"; + animal1.Zoo = zoo1; + + M.Animal animal2 = new Animal(); + animal2.BodyWeight = 10; + animal2.Description = "a persian"; + animal2.SerialNumber = "1000002"; + animal2.Zoo = zoo1; + + M.Animal child1 = new Animal(); + child1.BodyWeight = 10; + child1.Description = "a persian, wild"; + child1.SerialNumber = "1000003"; + child1.Mother = animal1; + child1.Father = animal2; + child1.Zoo = zoo1; + + animal1.Offspring.Add(child1); + + session.Save(province); + session.Save(zoo1); + session.Save(animal1); + session.Save(animal2); + session.Save(child1); + tx.Commit(); + } } public static ISession CreateSession() Modified: trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs 2008-08-24 15:12:16 UTC (rev 3734) +++ trunk/nhibernate/src/NHibernate.Linq.Test/Model/Animal.cs 2008-08-28 20:57:17 UTC (rev 3735) @@ -6,6 +6,10 @@ { public class Animal { + public Animal() + { + this.Offspring = new HashedSet<Animal>(); + } public virtual int Id { get; set; } public virtual string Description { get; set; } public virtual double BodyWeight { get; set; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-03 21:03:19
|
Revision: 3744 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3744&view=rev Author: tehlike Date: 2008-09-03 21:03:23 +0000 (Wed, 03 Sep 2008) Log Message: ----------- Added QuerySpacesFinderVisitor to support AutoFlush Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs trunk/nhibernate/src/NHibernate.Linq.Test/App.config trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityCheckerTests.cs trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Query/ trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/QuerySpacesFinderVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/QuerySpacesFinderVisitorTests.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Loaders/ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-03 21:03:23 UTC (rev 3744) @@ -52,6 +52,7 @@ <Compile Include="Expressions\NHExpression.cs" /> <Compile Include="Expressions\NHExpressionType.cs" /> <Compile Include="Expressions\QuerySourceExpression.cs" /> + <Compile Include="Query\LinqTranslator.cs" /> <Compile Include="Util\Guard.cs" /> <Compile Include="Util\LinqUtil.cs" /> <Compile Include="Visitors\AssociationRewriteVisitor.cs" /> @@ -65,12 +66,10 @@ <Compile Include="Visitors\LocalVariableExpressionReducer.cs" /> <Compile Include="Visitors\NHibernateExpressionVisitor.cs" /> <Compile Include="Visitors\NHExpressionToSqlQueryTranslator.cs" /> - <Compile Include="Visitors\SqlExpressionToSqlStringVisitor.cs" /> + <Compile Include="Visitors\QuerySpacesFinderVisitor.cs" /> + <Compile Include="Visitors\SqlExpressionTranslator.cs" /> <Compile Include="Visitors\NHExpressionToSqlExpressionTransformer.cs" /> </ItemGroup> - <ItemGroup> - <Folder Include="Loaders\" /> - </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -1,5 +1,4 @@ using System; -using NHibernate.Criterion; using NHibernate.Linq.Util; using NHibernate.SqlCommand; using Expression=System.Linq.Expressions.Expression; @@ -12,22 +11,23 @@ public class NHibernateQueryProvider : QueryProvider { private readonly ISession session; - + private readonly ISessionFactoryImplementor sessionFactory; public NHibernateQueryProvider(ISession session) { Guard.AgainstNull(session,"session"); this.session = session; + this.sessionFactory = this.session.SessionFactory as ISessionFactoryImplementor; } public override object Execute(Expression expression) { IList<object> parameterList = new List<object>(); - ISessionFactoryImplementor sessionFactory = this.session.SessionFactory as ISessionFactoryImplementor; expression = LocalVariableExpressionReducer.Reduce(expression); expression = LogicalExpressionReducer.Reduce(expression); expression = AssociationRewriteVisitor.Rewrite(expression, sessionFactory); expression = NHExpressionToSqlExpressionTransformer.Transform(sessionFactory, expression); SqlString sql=SqlExpressionToSqlStringVisitor.Translate(expression, sessionFactory,parameterList); + Console.WriteLine(sql); throw new NotImplementedException(); //expression = AssociationVisitor.RewriteWithAssociations(session.SessionFactory, expression); //expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression); Property changes on: trunk/nhibernate/src/NHibernate.Linq/Query ___________________________________________________________________ Added: svn:mergeinfo + Added: trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Linq.Expressions; +using NHibernate.Engine; +using NHibernate.Loader; + +namespace NHibernate.Linq.LinqQuery +{ + public class LinqTranslator:OuterJoinLoader + { + public LinqTranslator(Expression expression, + IDictionary<string, IFilter> enabledFilters, + ISessionFactoryImplementor sessionFactory):base(sessionFactory,enabledFilters) + { + this.expression = expression; + this.sessionFactory = sessionFactory; + } + + private readonly Expression expression; + private readonly ISessionFactoryImplementor sessionFactory; + + + + public void Translate() + { + + } + + protected void RenderSql() + { + + } + + } +} Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/QuerySpacesFinderVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/QuerySpacesFinderVisitor.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/QuerySpacesFinderVisitor.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Reflection; +using NHibernate.Engine; +using System.Collections; +using NHibernate.Persister.Collection; +using NHibernate.Linq.Util; +using NHibernate.Metadata; + +namespace NHibernate.Linq.Visitors +{ + /// <summary> + /// A queryspace is a term that is used with NHibernate AutoFlush feature. + /// When an entity(table) is evolved in a query, this entity(table) is added to queryspace. + /// Before nhibernate executes the SQL query, it checks if each item in queryspace has any dirty + /// entity. If there is any, session flush occurs so that query can act on fresh data. + /// This visitor visits nodes of the expression tree, and appends each node corresponding + /// to an entity/collection to the queryspace. + /// </summary> + public class QuerySpacesFinderVisitor:ExpressionVisitor + { + public QuerySpacesFinderVisitor(ISessionFactoryImplementor sessionFactory) + :this(sessionFactory,new List<string>()) + { + + } + public QuerySpacesFinderVisitor(ISessionFactoryImplementor sessionFactory,IList<string> querySpaces) + { + this.QuerySpaces = querySpaces; + this.sessionFactory = sessionFactory; + } + private readonly ISessionFactoryImplementor sessionFactory; + + /// <summary> + /// Query spaces evolved in the query. + /// </summary> + public IList<string> QuerySpaces { get; protected set; } + protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression c) + { + if(c.Value is IQueryable) + { + QuerySpaces.Add(((IQueryable)c.Value).ElementType.ToString()); + } + return base.VisitConstant(c); + } + protected override System.Linq.Expressions.Expression VisitMemberAccess(System.Linq.Expressions.MemberExpression m) + { + if(m.Member.MemberType==MemberTypes.Property) + { + + //TODO: need to find better way to find if a property is a collection/list. + if (TypeSystem.GetElementType(m.Type) == null || TypeSystem.GetElementType(m.Type)==m.Type) + { + if(sessionFactory.GetImplementors(m.Type.ToString()).Length>0) + { + QuerySpaces.Add(m.Type.FullName); + } + } + else + { + ICollectionMetadata metadata = + sessionFactory.GetCollectionMetadata(string.Format("{0}.{1}", m.Member.DeclaringType.FullName, m.Member.Name)); + QuerySpaces.Add(metadata.ElementType.ReturnedClass.FullName); + } + } + return base.VisitMemberAccess(m); + } + } +} Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -1,208 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using NHibernate.Engine; -using NHibernate.Linq.Expressions; -using NHibernate.SqlCommand; -using SelectFragment=NHibernate.SqlCommand.SelectFragment; -using NHibernate.Persister.Entity; -using NHibernate.Type; -using NHibernate.Metadata; - -namespace NHibernate.Linq.Visitors -{ - //TODO: Process the query source by converting into select expression - //TODO: Select expression should be changed so that it can handle columns or projections instead of expression. - public class SqlExpressionToSqlStringVisitor:NHibernateExpressionVisitor - { - public static SqlString Translate(Expression expr,ISessionFactoryImplementor sessionFactory,IList<object> parameterList) - { - - var visitor = new SqlExpressionToSqlStringVisitor(sessionFactory,parameterList); - visitor.Visit(expr); - return visitor.selectBuilder.ToSqlString(); - } - - public SqlExpressionToSqlStringVisitor(ISessionFactoryImplementor sessionFactory,IList<object> parameterList) - { - this.selectBuilder = new SqlStringBuilder(); - this.sessionFactory = sessionFactory; - this.parameterList = parameterList; - } - - private readonly IList<object> parameterList; - private ISessionFactoryImplementor sessionFactory; - private SqlStringBuilder selectBuilder; - protected override Expression VisitUnary(UnaryExpression u) - { - switch(u.NodeType) - { - case ExpressionType.Not: - { - selectBuilder.Add(" NOT ("); - Expression ret=base.VisitUnary(u); - selectBuilder.Add(")"); - return ret; - } - case ExpressionType.Negate: - { - selectBuilder.Add("(-1 * ("); - Expression ret = base.VisitUnary(u); - selectBuilder.Add("))"); - return ret; - } - } - return base.VisitUnary(u); - } - protected override Expression VisitBinary(BinaryExpression b) - { - string op = ""; - - #region operators - - switch (b.NodeType) - { - case ExpressionType.Add: - op = "+"; - break; - case ExpressionType.And: - case ExpressionType.AndAlso: - op = "AND"; - break; - case ExpressionType.Divide: - op = "/"; - break; - case ExpressionType.GreaterThan: - op = ">"; - break; - case ExpressionType.GreaterThanOrEqual: - op = ">="; - break; - case ExpressionType.LessThan: - op = "<"; - break; - case ExpressionType.LessThanOrEqual: - op = "<="; - break; - case ExpressionType.Modulo: - op = "MOD"; - break; - case ExpressionType.Multiply: - op = "*"; - break; - case ExpressionType.Or: - case ExpressionType.OrElse: - op = "OR"; - break; - case ExpressionType.Subtract: - op = "-"; - break; - case ExpressionType.Equal: - op = "="; - break; - case ExpressionType.NotEqual: - op = "!="; - break; - case ExpressionType.Power: - default: - throw new NotImplementedException(); - } - - #endregion - - selectBuilder.Add("("); - this.Visit(b.Left); - selectBuilder.Add(" "); - selectBuilder.Add(op); - selectBuilder.Add(" "); - this.Visit(b.Right); - selectBuilder.Add(")"); - return b; - } - protected override Expression VisitConditional(ConditionalExpression c) - { - return base.VisitConditional(c); - } - - protected override Expression VisitConstant(ConstantExpression c) - { - selectBuilder.AddParameter(); - parameterList.Add(c.Value); - return c; - } - - protected override Expression VisitProperty(PropertyExpression property) - { - ParameterExpression expr = property.Expression as ParameterExpression; - - selectBuilder.Add(string.Format("{0}.{1}", expr.Name, property.Name)); - return property; - } - - protected override Expression VisitSelect(SelectExpression select) - { - selectBuilder.Add("(SELECT "); - if(select.Projection!=null) - this.Visit(select.Projection); - else - selectBuilder.Add(select.FromAlias+".* "); - - if (select.From != null) - { - selectBuilder.Add(" FROM ("); - this.Visit(select.From); - selectBuilder.Add(") AS "); - selectBuilder.Add(select.FromAlias); - } - else - throw new NotImplementedException(); - if(select.Where!=null) - { - selectBuilder.Add(" WHERE "); - this.Visit(select.Where); - } - selectBuilder.Add(")"); - return select; - } - - protected override Expression VisitQuerySource(QuerySourceExpression expr) - { - selectBuilder.Add("SELECT "); - IClassMetadata metadata = sessionFactory.GetClassMetadata(expr.Query.ElementType); - IPropertyMapping mapping = (IPropertyMapping)sessionFactory.GetEntityPersister(metadata.EntityName); - string[] names = metadata.PropertyNames; - bool started = false; - for (int i = 0; i < names.Length;i++ ) - { - string name = names[i]; - IType propertyType = metadata.GetPropertyType(name); - if (!(propertyType.IsComponentType | - propertyType.IsCollectionType | - propertyType.IsAssociationType | - propertyType.IsAnyType)) - { - if(started) - selectBuilder.Add(", "); - started = true; - selectBuilder.Add(mapping.ToColumns(name)[0]); - selectBuilder.Add(" AS "); - selectBuilder.Add(name); - } - - } - selectBuilder.Add(" FROM "); - selectBuilder.Add(expr.Query.ElementType.Name); - return base.VisitQuerySource(expr); - } - protected override Expression VisitParameter(ParameterExpression p) - { - return base.VisitParameter(p); - } - public override string ToString() - { - return selectBuilder.ToString(); - } - } -} Copied: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs (from rev 3737, trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionToSqlStringVisitor.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Engine; +using NHibernate.Linq.Expressions; +using NHibernate.SqlCommand; +using NHibernate.Persister.Entity; +using NHibernate.Type; +using NHibernate.Metadata; + +namespace NHibernate.Linq.Visitors +{ + //TODO: Process the query source by converting into select expression + //TODO: Select expression should be changed so that it can handle columns or projections instead of expression. + public class SqlExpressionToSqlStringVisitor:NHibernateExpressionVisitor + { + public static SqlString Translate(Expression expr,ISessionFactoryImplementor sessionFactory,IList<object> parameterList) + { + + var visitor = new SqlExpressionToSqlStringVisitor(sessionFactory,parameterList); + visitor.Visit(expr); + return visitor.sqlStringBuilder.ToSqlString(); + } + + public SqlExpressionToSqlStringVisitor(ISessionFactoryImplementor sessionFactory,IList<object> parameterList) + { + this.sqlStringBuilder = new SqlStringBuilder(); + this.sessionFactory = sessionFactory; + this.parameterList = parameterList; + + } + + private readonly IList<object> parameterList; + private readonly ISessionFactoryImplementor sessionFactory; + private SqlStringBuilder sqlStringBuilder; + + protected override Expression VisitUnary(UnaryExpression u) + { + switch(u.NodeType) + { + case ExpressionType.Not: + { + sqlStringBuilder.Add(" NOT ("); + Expression ret=base.VisitUnary(u); + sqlStringBuilder.Add(")"); + return ret; + } + case ExpressionType.Negate: + { + sqlStringBuilder.Add("(-1 * ("); + Expression ret = base.VisitUnary(u); + sqlStringBuilder.Add("))"); + return ret; + } + } + return base.VisitUnary(u); + } + protected override Expression VisitBinary(BinaryExpression b) + { + string op = ""; + + #region operators + + switch (b.NodeType) + { + case ExpressionType.Add: + op = "+"; + break; + case ExpressionType.And: + case ExpressionType.AndAlso: + op = "AND"; + break; + case ExpressionType.Divide: + op = "/"; + break; + case ExpressionType.GreaterThan: + op = ">"; + break; + case ExpressionType.GreaterThanOrEqual: + op = ">="; + break; + case ExpressionType.LessThan: + op = "<"; + break; + case ExpressionType.LessThanOrEqual: + op = "<="; + break; + case ExpressionType.Modulo: + op = "MOD"; + break; + case ExpressionType.Multiply: + op = "*"; + break; + case ExpressionType.Or: + case ExpressionType.OrElse: + op = "OR"; + break; + case ExpressionType.Subtract: + op = "-"; + break; + case ExpressionType.Equal: + op = "="; + break; + case ExpressionType.NotEqual: + op = "!="; + break; + case ExpressionType.Power: + default: + throw new NotImplementedException(); + } + + #endregion + + sqlStringBuilder.Add("("); + this.Visit(b.Left); + sqlStringBuilder.Add(" "); + sqlStringBuilder.Add(op); + sqlStringBuilder.Add(" "); + this.Visit(b.Right); + sqlStringBuilder.Add(")"); + return b; + } + + protected override Expression VisitConstant(ConstantExpression c) + { + sqlStringBuilder.AddParameter(); + parameterList.Add(c.Value); + return c; + } + + protected override Expression VisitProperty(PropertyExpression property) + { + ParameterExpression expr = property.Expression as ParameterExpression; + + sqlStringBuilder.Add(string.Format("{0}.{1}", expr.Name, property.Name)); + return property; + } + + protected override Expression VisitSelect(SelectExpression select) + { + SqlSelectBuilder selectBuilder=new SqlSelectBuilder(this.sessionFactory); + SqlString selectString = new SqlString("*"); + SqlString fromString = SqlExpressionToSqlStringVisitor.Translate(select.From, sessionFactory, parameterList); + fromString=new SqlString("(",fromString,")"); + SqlString whereString = SqlExpressionToSqlStringVisitor.Translate(select.Where, sessionFactory, parameterList); + SqlString outerJoinsAfterFrom=new SqlString(); + SqlString outerJoinsAfterWhere=new SqlString(); + selectBuilder.SetFromClause(fromString); + selectBuilder.SetWhereClause(whereString); + selectBuilder.SetSelectClause(selectString); + selectBuilder.SetOuterJoins(outerJoinsAfterFrom, outerJoinsAfterWhere); + this.sqlStringBuilder.Add(selectBuilder.ToSqlString()); + return select; + } + + protected override Expression VisitQuerySource(QuerySourceExpression expr) + { + + sqlStringBuilder.Add("SELECT "); + IClassMetadata metadata = sessionFactory.GetClassMetadata(expr.Query.ElementType); + IPropertyMapping mapping = (IPropertyMapping)sessionFactory.GetEntityPersister(metadata.EntityName); + string[] names = metadata.PropertyNames; + + + + + + + bool started = false; + for (int i = 0; i < names.Length;i++ ) + { + string name = names[i]; + IType propertyType = metadata.GetPropertyType(name); + if (!(propertyType.IsComponentType | + propertyType.IsCollectionType | + propertyType.IsAssociationType | + propertyType.IsAnyType)) + { + if(started) + sqlStringBuilder.Add(", "); + started = true; + sqlStringBuilder.Add(mapping.ToColumns(name)[0]); + sqlStringBuilder.Add(" AS "); + sqlStringBuilder.Add(name); + } + + } + sqlStringBuilder.Add(" FROM "); + sqlStringBuilder.Add(expr.Query.ElementType.Name); + return base.VisitQuerySource(expr); + } + public override string ToString() + { + return sqlStringBuilder.ToString(); + } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq.Test/App.config =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/App.config 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq.Test/App.config 2008-09-03 21:03:23 UTC (rev 3744) @@ -8,10 +8,10 @@ <session-factory name="NHibernate.Linq.Test"> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> - Server=.\SQLEXPRESS;initial catalog=linq2nhibernate;Integrated Security=true + Server=.;initial catalog=linq2nhibernate;Integrated Security=true </property> <property name="adonet.batch_size">10</property> - <property name="show_sql">false</property> + <property name="show_sql">true</property> <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">444</property> Modified: trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityCheckerTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityCheckerTests.cs 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq.Test/ExpressionEqualityCheckerTests.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -8,7 +8,7 @@ namespace NHibernate.Linq.Test { [TestFixture] - public class ExpressionEqualityCheckerTests + public class ExpressionEqualityCheckerTests:BaseTest { [Test] public void SimpleExpressionsProducesCorrectResult() Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-09-03 21:03:23 UTC (rev 3744) @@ -79,6 +79,7 @@ <Compile Include="Model\Animal.cs" /> <Compile Include="SelectTest.cs" /> <Compile Include="VisitorTests\LogicalExpressionSimplifierTests.cs" /> + <Compile Include="VisitorTests\QuerySpacesFinderVisitorTests.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Modified: trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs 2008-09-03 20:57:21 UTC (rev 3743) +++ trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -13,7 +13,7 @@ [Ignore("this doesn't work yet")] public void CanSelectAnimals() { - var animals = session.Linq<Animal>(); + var animals = session.Linq<Animal>().ToList(); Assert.IsNotNull(animals); } } Added: trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/QuerySpacesFinderVisitorTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/QuerySpacesFinderVisitorTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/QuerySpacesFinderVisitorTests.cs 2008-09-03 21:03:23 UTC (rev 3744) @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NHibernate.Linq.Test.Model; +using NHibernate.Linq.Visitors; +using NUnit.Framework; +using System.Collections; +using NHibernate.Engine; + +namespace NHibernate.Linq.Test.VisitorTests +{ + [TestFixture] + public class QuerySpacesFinderVisitorTests:BaseTest + { + + public override void Setup() + { + base.Setup(); + visitor = new QuerySpacesFinderVisitor(session.SessionFactory as ISessionFactoryImplementor); + } + private QuerySpacesFinderVisitor visitor; + + + [NUnit.Framework.Test] + public void CanFindRoot() + { + + var results = from x in session.Linq<Animal>() + select x; + + visitor.Visit(results.Expression); + Assert.Contains(typeof (Animal).FullName, (ICollection)visitor.QuerySpaces); + } + + + [NUnit.Framework.Test] + public void CanFindProperty() + { + + var results = from x in session.Linq<Animal>() + where x.Zoo.Name=="My Zoo" + select x; + + visitor.Visit(results.Expression); + Assert.Contains(typeof(Animal).FullName, (ICollection)visitor.QuerySpaces); + Assert.Contains(typeof(Zoo).FullName, (ICollection)visitor.QuerySpaces); + Assert.That(!visitor.QuerySpaces.Contains(typeof(string).FullName)); + } + + + [NUnit.Framework.Test] + public void CanFindNestedProperty() + { + + var results = from x in session.Linq<Human>() + where x.Name.First=="Osman" && x.Pets.Count>0 + select x; + visitor.Visit(results.Expression); + Assert.Contains(typeof(Human).FullName, (ICollection)visitor.QuerySpaces); + Assert.Contains(typeof(DomesticAnimal).FullName, (ICollection)visitor.QuerySpaces); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-20 12:08:17
|
Revision: 3773 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3773&view=rev Author: tehlike Date: 2008-09-20 11:33:47 +0000 (Sat, 20 Sep 2008) Log Message: ----------- ParameterReplacer renamed to Replacer. It can be used to replace other types of expressions as well. Moving codes in QueryProvider to LinqTranslator Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/WhereExpressionCombiner.cs trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/OrderTransformerTests.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Visitors/Replacer.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/ParameterReplacer.cs Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderExpression.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OrderExpression.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -2,10 +2,10 @@ namespace NHibernate.Linq.Expressions { - public class OrderExpression : Expression + public class OrderExpression : NHExpression { public OrderExpression(Expression source, LambdaExpression selector, OrderType orderType, System.Type type) - : base((ExpressionType) NHExpressionType.Order, type) + : base(NHExpressionType.Order, type) { Source = source; Selector = selector; Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,4 +1,5 @@ -using System.Linq; +using NHibernate.Persister.Entity; +using IQueryable=System.Linq.IQueryable; namespace NHibernate.Linq.Expressions { @@ -8,17 +9,16 @@ /// </summary> public class QuerySourceExpression : NHExpression { - public QuerySourceExpression(IQueryable query) - : base(NHExpressionType.QuerySource, query.GetType()) + public QuerySourceExpression(System.Type type, IOuterJoinLoadable entityPersister) + : base(NHExpressionType.QuerySource, type) { - Query = query; + this.Persister = entityPersister; } + public IEntityPersister Persister { get; protected set; } - public IQueryable Query { get; protected set; } - public override string ToString() { - return string.Format("(({0})", Type); + return string.Format("({0})", Persister.EntityName); } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SelectExpression.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,4 +1,6 @@ -using System.Linq.Expressions; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq.Expressions; namespace NHibernate.Linq.Expressions { @@ -7,18 +9,21 @@ /// </summary> public class SelectExpression : NHExpression { - public SelectExpression(System.Type type, string alias, Expression projection, Expression from, Expression where) + public SelectExpression(System.Type type, string alias, Expression projection, + Expression from, Expression where, ReadOnlyCollection<Expression> orderBys) : base(NHExpressionType.Select, type) { Where = where; From = from; FromAlias = alias; Projection = projection; + OrderBys = orderBys; } public Expression Projection { get; protected set; } public string FromAlias { get; protected set; } public Expression Where { get; protected set; } + public ReadOnlyCollection<Expression> OrderBys { get; protected set; } public Expression From { get; protected set; } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 11:33:47 UTC (rev 3773) @@ -20,7 +20,7 @@ <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> - <TreatWarningsAsErrors>true</TreatWarningsAsErrors> + <TreatWarningsAsErrors>false</TreatWarningsAsErrors> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>none</DebugType> @@ -32,6 +32,10 @@ <TreatWarningsAsErrors>true</TreatWarningsAsErrors> </PropertyGroup> <ItemGroup> + <Reference Include="Iesi.Collections, Version=1.0.0.3, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\lib\net\2.0\Iesi.Collections.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core"> <RequiredTargetFramework>3.5</RequiredTargetFramework> @@ -67,9 +71,8 @@ <Compile Include="Util\TypeSystem.cs" /> <Compile Include="Visitors\LocalVariableExpressionReducer.cs" /> <Compile Include="Visitors\NHibernateExpressionVisitor.cs" /> - <Compile Include="Visitors\NHExpressionToSqlQueryTranslator.cs" /> <Compile Include="Visitors\OrderByTransformer.cs" /> - <Compile Include="Visitors\ParameterReplacer.cs" /> + <Compile Include="Visitors\Replacer.cs" /> <Compile Include="Visitors\QuerySpacesFinderVisitor.cs" /> <Compile Include="Visitors\SqlExpressionTranslator.cs" /> <Compile Include="Visitors\NHExpressionToSqlExpressionTransformer.cs" /> Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernateQueryProvider.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq.Expressions; using NHibernate.Engine; +using NHibernate.Linq.Query; using NHibernate.Linq.Util; using NHibernate.Linq.Visitors; using NHibernate.SqlCommand; @@ -22,24 +23,9 @@ public override object Execute(Expression expression) { - IList<object> parameterList = new List<object>(); - expression = LocalVariableExpressionReducer.Reduce(expression); - expression = LogicalExpressionReducer.Reduce(expression); - expression = AssociationRewriteVisitor.Rewrite(expression, sessionFactory); - expression = NHExpressionToSqlExpressionTransformer.Transform(sessionFactory, expression); - SqlString sql = SqlExpressionToSqlStringVisitor.Translate(expression, sessionFactory, parameterList); - Console.WriteLine(sql); - throw new NotImplementedException(); - //expression = AssociationVisitor.RewriteWithAssociations(session.SessionFactory, expression); - //expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression); - //expression = new PropertyToMethodVisitor().Visit(expression); - //expression = new BinaryExpressionOrderer().Visit(expression); - - //once tree is converted to NH tree, pass it to NHibernateQueryTranslator - //which will convert the tree to an NHibernate.SqlCommand.SqlString - - - //return translator.Transform(expression,this.queryOptions); + LinqTranslator translator = new LinqTranslator(expression, sessionFactory); + translator.Translate(); + return translator.List(session as ISessionImplementor); } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,30 +1,85 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; using System.Linq.Expressions; +using Iesi.Collections.Generic; using NHibernate.Engine; +using NHibernate.Linq.Visitors; using NHibernate.Loader; +using NHibernate.Mapping; +using NHibernate.Persister.Entity; +using NHibernate.SqlCommand; +using NHibernate.Type; +using System.Linq; -namespace NHibernate.Linq.LinqQuery +namespace NHibernate.Linq.Query { - public class LinqTranslator : OuterJoinLoader + public class LinqTranslator : BasicLoader { private readonly Expression expression; private readonly ISessionFactoryImplementor sessionFactory; + private readonly IList<object> parameterList; - public LinqTranslator(Expression expression, - IDictionary<string, IFilter> enabledFilters, - ISessionFactoryImplementor sessionFactory) : base(sessionFactory, enabledFilters) + + public LinqTranslator(Expression expression,ISessionFactoryImplementor sessionFactory) + : base(sessionFactory) + { this.expression = expression; this.sessionFactory = sessionFactory; + this.parameterList = new List<object>(); } + public virtual IType[] ReturnTypes { get; protected set; } public void Translate() { + /*Expression modified = LocalVariableExpressionReducer.Reduce(expression); + modified = LogicalExpressionReducer.Reduce(modified); + modified = AssociationRewriteVisitor.Rewrite(modified, sessionFactory); + modified = NHExpressionToSqlExpressionTransformer.Transform(sessionFactory, modified); + sqlString = SqlExpressionToSqlStringVisitor.Translate(modified, sessionFactory, parameterList);*/ + } + public IList List(ISessionImplementor sessionImplementor) + { + throw new NotImplementedException(); + } + protected override string[] Suffixes + { + get { return suffixes; } + } + private string[] suffixes; - protected void RenderSql() + protected override string[] CollectionSuffixes { + get { return collectionSuffixes; } + } + private string[] collectionSuffixes; + + protected override SqlString SqlString + { + get { return sqlString; } + + } + private SqlString sqlString; + + protected override ILoadable[] EntityPersisters + { + get { return entityPersisters; } + } + private ILoadable[] entityPersisters; + + protected override LockMode[] GetLockModes(IDictionary<string, LockMode> lockModes) + { + return new LockMode[] { LockMode.Read }; + } + + public virtual ISet<string> QuerySpaces + { + get; + protected set; + } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,25 +1,27 @@ -using System.Linq; -using System.Linq.Expressions; +using System.Linq.Expressions; using System.Reflection; +using NHibernate.Engine; using NHibernate.Linq.Expressions; using NHibernate.Linq.Util; using NHibernate.Metadata; +using NHibernate.Persister.Entity; using NHibernate.Type; +using IQueryable=System.Linq.IQueryable; namespace NHibernate.Linq.Visitors { public class AssociationRewriteVisitor : NHibernateExpressionVisitor { - private readonly ISessionFactory sessionFactory; + private readonly ISessionFactoryImplementor sessionFactory; private int aliasOrder; - public AssociationRewriteVisitor(ISessionFactory factory) + public AssociationRewriteVisitor(ISessionFactoryImplementor factory) { sessionFactory = factory; aliasOrder = 0; } - public static Expression Rewrite(Expression expr, ISessionFactory factory) + public static Expression Rewrite(Expression expr, ISessionFactoryImplementor factory) { var visitor = new AssociationRewriteVisitor(factory); expr = visitor.Visit(expr); @@ -66,7 +68,13 @@ protected override Expression VisitConstant(ConstantExpression c) { if (c.Value is IQueryable) - return new QuerySourceExpression((IQueryable) c.Value); + { + var type = c.Value.GetType(); + var elementType = ((IQueryable) c.Value).ElementType; + var loadable = sessionFactory.GetEntityPersister(elementType.FullName) as IOuterJoinLoadable; + return new QuerySourceExpression(type, loadable); + } + return c; } } Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlExpressionTransformer.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -45,7 +45,7 @@ string alias = parameter.Name; System.Type type = lambda.Body.Type; System.Type resultType = typeof (IQueryable<>).MakeGenericType(type); - return new SelectExpression(resultType, alias, body, source, null); + return new SelectExpression(resultType, alias, body, source,null,null); } protected Expression TransformWhereCall(MethodCallExpression expr) @@ -59,7 +59,7 @@ string alias = parameter.Name; System.Type type = body.Type; System.Type resultType = typeof (IQueryable<>).MakeGenericType(type); - return new SelectExpression(resultType, alias, null, source, body); + return new SelectExpression(resultType, alias, null, source, body,null); } protected override Expression VisitQuerySource(QuerySourceExpression expr) @@ -67,7 +67,6 @@ return expr; } - protected override Expression VisitBinary(BinaryExpression b) { switch (b.NodeType) Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHExpressionToSqlQueryTranslator.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,28 +0,0 @@ -using System.Linq.Expressions; -using NHibernate.Linq.Expressions; -using NHibernate.SqlCommand; - -namespace NHibernate.Linq.Visitors -{ - public class NHExpressionToSqlQueryTranslator : NHibernateExpressionVisitor - { - protected SqlStringBuilder sqlStringBuilder; - - public SqlString Translate(Expression expression) - { - sqlStringBuilder = new SqlStringBuilder(); - Visit(expression); - return sqlStringBuilder.ToSqlString(); - } - - protected override Expression VisitSelect(SelectExpression select) - { - return select; - } - - protected override Expression VisitMethodCall(MethodCallExpression m) - { - return base.VisitMethodCall(m); - } - } -} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,6 +1,7 @@ -using System.Linq.Expressions; +using System.Collections.ObjectModel; +using System.Linq.Expressions; using NHibernate.Linq.Expressions; - +using System.Linq; namespace NHibernate.Linq.Visitors { /// <summary> @@ -55,11 +56,12 @@ Expression from = VisitSource(select.From); Expression where = Visit(select.Where); Expression projection = Visit(select.Projection); + ReadOnlyCollection<Expression> orderbys = VisitList(select.OrderBys); if (from != select.From || where != select.Where) { - return new SelectExpression(select.Type, select.FromAlias, projection, from, where); + return new SelectExpression(select.Type, select.FromAlias, projection, from, where, orderbys); } - else + else return select; } Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/ParameterReplacer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/ParameterReplacer.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/ParameterReplacer.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,22 +0,0 @@ -using System.Collections.Generic; -using System.Linq.Expressions; - -namespace NHibernate.Linq.Visitors -{ - public class ParameterReplacer : ExpressionVisitor - { - private readonly Dictionary<ParameterExpression, ParameterExpression> parameterReplacements; - - public ParameterReplacer(Dictionary<ParameterExpression, ParameterExpression> parameterReplacements) - { - this.parameterReplacements = parameterReplacements; - } - - protected override Expression VisitParameter(ParameterExpression p) - { - if (parameterReplacements.ContainsKey(p)) - return parameterReplacements[p]; - return p; - } - } -} \ No newline at end of file Copied: trunk/nhibernate/src/NHibernate.Linq/Visitors/Replacer.cs (from rev 3770, trunk/nhibernate/src/NHibernate.Linq/Visitors/ParameterReplacer.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/Replacer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/Replacer.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Linq.Expressions; + +namespace NHibernate.Linq.Visitors +{ + public class Replacer : ExpressionVisitor + { + private readonly Dictionary<Expression, Expression> replacements; + + public Replacer(Dictionary<Expression, Expression> replacements) + { + this.replacements = replacements; + } + public override Expression Visit(Expression exp) + { + if (replacements.ContainsKey(exp)) + return replacements[exp]; + return base.Visit(exp); + } + } +} \ No newline at end of file Property changes on: trunk/nhibernate/src/NHibernate.Linq/Visitors/Replacer.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -123,7 +123,7 @@ protected override Expression VisitConstant(ConstantExpression c) { - sqlStringBuilder.AddParameter(); + sqlStringBuilder.Add(Parameter.Placeholder); parameterList.Add(c.Value); return c; } @@ -138,52 +138,10 @@ protected override Expression VisitSelect(SelectExpression select) { - var selectBuilder = new SqlSelectBuilder(sessionFactory); - var selectString = new SqlString("*"); - SqlString fromString = Translate(select.From, sessionFactory, parameterList); - fromString = new SqlString("(", fromString, ")"); - SqlString whereString = Translate(select.Where, sessionFactory, parameterList); - var outerJoinsAfterFrom = new SqlString(); - var outerJoinsAfterWhere = new SqlString(); - selectBuilder.SetFromClause(fromString); - selectBuilder.SetWhereClause(whereString); - selectBuilder.SetSelectClause(selectString); - selectBuilder.SetOuterJoins(outerJoinsAfterFrom, outerJoinsAfterWhere); - sqlStringBuilder.Add(selectBuilder.ToSqlString()); - return select; + throw new NotImplementedException(); } - protected override Expression VisitQuerySource(QuerySourceExpression expr) - { - sqlStringBuilder.Add("SELECT "); - IClassMetadata metadata = sessionFactory.GetClassMetadata(expr.Query.ElementType); - var mapping = (IPropertyMapping) sessionFactory.GetEntityPersister(metadata.EntityName); - string[] names = metadata.PropertyNames; - - bool started = false; - for (int i = 0; i < names.Length; i++) - { - string name = names[i]; - IType propertyType = metadata.GetPropertyType(name); - if (!(propertyType.IsComponentType | - propertyType.IsCollectionType | - propertyType.IsAssociationType | - propertyType.IsAnyType)) - { - if (started) - sqlStringBuilder.Add(", "); - started = true; - sqlStringBuilder.Add(mapping.ToColumns(name)[0]); - sqlStringBuilder.Add(" AS "); - sqlStringBuilder.Add(name); - } - } - sqlStringBuilder.Add(" FROM "); - sqlStringBuilder.Add(expr.Query.ElementType.Name); - return base.VisitQuerySource(expr); - } - public override string ToString() { return sqlStringBuilder.ToString(); Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/WhereExpressionCombiner.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/WhereExpressionCombiner.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/WhereExpressionCombiner.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -14,7 +14,7 @@ if (IsWhereCall(m)) { var stack = new Stack<Expression>(); - var parameterReplacements = new Dictionary<ParameterExpression, ParameterExpression>(); + var parameterReplacements = new Dictionary<Expression, Expression>(); Expression source = m.Arguments[0]; var outerLambda = LinqUtil.StripQuotes(m.Arguments[1]) as LambdaExpression; @@ -38,7 +38,7 @@ } source = Visit(source); LambdaExpression resultingLambda = Expression.Lambda(expr, replacementParameter); - var parameterReplacer = new ParameterReplacer(parameterReplacements); + var parameterReplacer = new Replacer(parameterReplacements); resultingLambda = parameterReplacer.Visit(resultingLambda) as LambdaExpression; return Expression.Call(null, m.Method, source, resultingLambda); } Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-09-20 11:33:47 UTC (rev 3773) @@ -20,7 +20,7 @@ <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> - <TreatWarningsAsErrors>true</TreatWarningsAsErrors> + <TreatWarningsAsErrors>false</TreatWarningsAsErrors> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>none</DebugType> Modified: trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/OrderTransformerTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/OrderTransformerTests.cs 2008-09-19 23:34:29 UTC (rev 3772) +++ trunk/nhibernate/src/NHibernate.Linq.Test/VisitorTests/OrderTransformerTests.cs 2008-09-20 11:33:47 UTC (rev 3773) @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Linq.Expressions; using NHibernate.Linq.Test.Model; using NHibernate.Linq.Visitors; @@ -17,6 +18,7 @@ select a; OrderByTransformer transformer = new OrderByTransformer(); Expression expr=transformer.Visit(query.Expression); + Console.WriteLine(expr); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-20 13:24:01
|
Revision: 3775 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3775&view=rev Author: tehlike Date: 2008-09-20 13:23:41 +0000 (Sat, 20 Sep 2008) Log Message: ----------- adding SimplePropertyExpression and ComponentPropertyExpression Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class ComponentPropertyExpression : PropertyExpression + { + public ComponentPropertyExpression(string name, string[] columns, System.Type type, Expression source, + IType nhibernateType) + : base(name, type, source, nhibernateType) + { + this.Columns = columns; + } + + public string[] Columns { get; protected set; } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs 2008-09-20 11:38:34 UTC (rev 3774) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -3,31 +3,30 @@ namespace NHibernate.Linq.Expressions { - public class PropertyExpression : NHExpression + public abstract class PropertyExpression : NHExpression { - public PropertyExpression(string name, - System.Type type, Expression expression, IType nhibernateType) - : this(name, type, expression, nhibernateType, NHExpressionType.Property) + public PropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) + : this(name, type, source, nhibernateType, NHExpressionType.Property) { } - protected PropertyExpression(string name, System.Type type, Expression expression, IType nhibernateType, + protected PropertyExpression(string name, System.Type type, Expression source, IType nhibernateType, NHExpressionType nodeType) : base(nodeType, type) { NHibernateType = nhibernateType; - Expression = expression; + Source = source; Name = name; } - public Expression Expression { get; protected set; } + public Expression Source { get; protected set; } public IType NHibernateType { get; protected set; } public string Name { get; protected set; } public override string ToString() { - return Expression + "." + Name; + return string.Format("({0}).Property({1})", Source, this.Name); } } } \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class SimplePropertyExpression:PropertyExpression + { + public SimplePropertyExpression(string name,string column, System.Type type, Expression source, IType nhibernateType) + : base(name, type, source, nhibernateType) + { + this.Column = column; + } + + public string Column { get; protected set; } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 11:38:34 UTC (rev 3774) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 13:23:41 UTC (rev 3775) @@ -50,6 +50,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Compile Include="Expressions\ComponentPropertyExpression.cs" /> <Compile Include="Expressions\OrderExpression.cs" /> <Compile Include="Expressions\OrderType.cs" /> <Compile Include="Expressions\ProjectionExpression.cs" /> @@ -58,6 +59,7 @@ <Compile Include="Expressions\NHExpression.cs" /> <Compile Include="Expressions\NHExpressionType.cs" /> <Compile Include="Expressions\QuerySourceExpression.cs" /> + <Compile Include="Expressions\SimplePropertyExpression.cs" /> <Compile Include="Query\LinqTranslator.cs" /> <Compile Include="Util\Guard.cs" /> <Compile Include="Util\LinqUtil.cs" /> Modified: trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs 2008-09-20 11:38:34 UTC (rev 3774) +++ trunk/nhibernate/src/NHibernate.Linq/Query/LinqTranslator.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -34,11 +34,12 @@ public void Translate() { - /*Expression modified = LocalVariableExpressionReducer.Reduce(expression); + Expression modified = LocalVariableExpressionReducer.Reduce(expression); modified = LogicalExpressionReducer.Reduce(modified); modified = AssociationRewriteVisitor.Rewrite(modified, sessionFactory); modified = NHExpressionToSqlExpressionTransformer.Transform(sessionFactory, modified); - sqlString = SqlExpressionToSqlStringVisitor.Translate(modified, sessionFactory, parameterList);*/ + sqlString = SqlExpressionToSqlStringVisitor.Translate(modified, sessionFactory, parameterList); + throw new NotImplementedException(); } public IList List(ISessionImplementor sessionImplementor) Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 11:38:34 UTC (rev 3774) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using System; +using System.Linq.Expressions; using System.Reflection; using NHibernate.Engine; using NHibernate.Linq.Expressions; @@ -52,15 +53,28 @@ { expr = (MemberExpression) base.VisitMemberAccess(expr); IClassMetadata clazz = GetMetaData(expr.Member.DeclaringType); + IPropertyMapping mapping = sessionFactory.GetEntityPersister(expr.Type.FullName) as IPropertyMapping; IType propertyType = clazz.GetPropertyType(expr.Member.Name); - if (propertyType.IsAssociationType || propertyType.IsComponentType) + string propertyName = expr.Member.Name; + if (propertyType.IsComponentType) { - return new PropertyExpression(expr.Member.Name, ((PropertyInfo) expr.Member).PropertyType, - base.Visit(expr.Expression), propertyType); + Expression source = base.Visit(expr.Expression); + return new ComponentPropertyExpression(propertyName, mapping.ToColumns(propertyName), + ((PropertyInfo) expr.Member).PropertyType, + source, propertyType); } - else + else if(propertyType.IsAssociationType) { - return new PropertyExpression(expr.Member.Name, ((PropertyInfo) expr.Member).PropertyType, + throw new NotImplementedException(); + } + else if(propertyType.IsCollectionType) + { + throw new NotImplementedException(); + } + + else//Assume simple property + { + return new SimplePropertyExpression(expr.Member.Name,mapping.ToColumns(propertyName)[0], ((PropertyInfo) expr.Member).PropertyType, base.Visit(expr.Expression), propertyType); } } Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-20 11:38:34 UTC (rev 3774) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-20 13:23:41 UTC (rev 3775) @@ -130,7 +130,7 @@ protected override Expression VisitProperty(PropertyExpression property) { - var expr = property.Expression as ParameterExpression; + var expr = property.Source as ParameterExpression; sqlStringBuilder.Add(string.Format("{0}.{1}", expr.Name, property.Name)); return property; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-20 16:10:33
|
Revision: 3776 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3776&view=rev Author: tehlike Date: 2008-09-20 16:10:08 +0000 (Sat, 20 Sep 2008) Log Message: ----------- Adding type specific properties to expressions. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class CollectionPropertyExpression : PropertyExpression + { + public CollectionPropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) + : base(name, type, source, nhibernateType) + { + } + + public CollectionType CollectionType { get { return base.NHibernateType as CollectionType; } } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs 2008-09-20 13:23:41 UTC (rev 3775) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) @@ -17,5 +17,6 @@ } public string[] Columns { get; protected set; } + public ComponentType ComponentType { get { base.NHibernateType as ComponentType; } } } } Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs 2008-09-20 13:23:41 UTC (rev 3775) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/QuerySourceExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) @@ -14,8 +14,8 @@ { this.Persister = entityPersister; } - public IEntityPersister Persister { get; protected set; } - + public IOuterJoinLoadable Persister { get; protected set; } + public override string ToString() { return string.Format("({0})", Persister.EntityName); Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 13:23:41 UTC (rev 3775) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 16:10:08 UTC (rev 3776) @@ -50,6 +50,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Compile Include="Expressions\CollectionPropertyExpression.cs" /> <Compile Include="Expressions\ComponentPropertyExpression.cs" /> <Compile Include="Expressions\OrderExpression.cs" /> <Compile Include="Expressions\OrderType.cs" /> Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 13:23:41 UTC (rev 3775) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 16:10:08 UTC (rev 3776) @@ -44,14 +44,10 @@ return null; } - private string GetNextAlias() - { - return "source" + (aliasOrder++); - } - protected override Expression VisitMemberAccess(MemberExpression expr) { expr = (MemberExpression) base.VisitMemberAccess(expr); + System.Type type = TypeSystem.GetElementType(expr.Type); IClassMetadata clazz = GetMetaData(expr.Member.DeclaringType); IPropertyMapping mapping = sessionFactory.GetEntityPersister(expr.Type.FullName) as IPropertyMapping; IType propertyType = clazz.GetPropertyType(expr.Member.Name); @@ -65,13 +61,12 @@ } else if(propertyType.IsAssociationType) { - throw new NotImplementedException(); + throw new NotImplementedException("Queries on associations are not yet supported"); } else if(propertyType.IsCollectionType) { - throw new NotImplementedException(); + throw new NotImplementedException("Queries on collections are not yet supported"); } - else//Assume simple property { return new SimplePropertyExpression(expr.Member.Name,mapping.ToColumns(propertyName)[0], ((PropertyInfo) expr.Member).PropertyType, Modified: trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs 2008-09-20 13:23:41 UTC (rev 3775) +++ trunk/nhibernate/src/NHibernate.Linq.Test/SelectTest.cs 2008-09-20 16:10:08 UTC (rev 3776) @@ -13,8 +13,9 @@ [Ignore("this doesn't work yet")] public void CanSelectAnimals() { - var animals = session.Linq<Animal>().ToList(); - Assert.IsNotNull(animals); + var animals = from f in session.Linq<Animal>() + select f; + animals.ToList(); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-20 16:51:00
|
Revision: 3777 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3777&view=rev Author: tehlike Date: 2008-09-20 16:50:54 +0000 (Sat, 20 Sep 2008) Log Message: ----------- Minor modifications to PropertyExpressions Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -10,7 +10,7 @@ public class CollectionPropertyExpression : PropertyExpression { public CollectionPropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) - : base(name, type, source, nhibernateType) + : base(name,NHExpressionType.CollectionProperty, type, source, nhibernateType) { } Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/ComponentPropertyExpression.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -11,12 +11,12 @@ { public ComponentPropertyExpression(string name, string[] columns, System.Type type, Expression source, IType nhibernateType) - : base(name, type, source, nhibernateType) + : base(name,NHExpressionType.ComponentProperty, type, source, nhibernateType) { this.Columns = columns; } public string[] Columns { get; protected set; } - public ComponentType ComponentType { get { base.NHibernateType as ComponentType; } } + public ComponentType ComponentType { get { return base.NHibernateType as ComponentType; } } } } Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -5,7 +5,9 @@ QuerySource = 100, Select, Projection, - Property, + SimpleProperty, + ComponentProperty, + CollectionProperty, Order, } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/PropertyExpression.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -5,14 +5,8 @@ { public abstract class PropertyExpression : NHExpression { - public PropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) - : this(name, type, source, nhibernateType, NHExpressionType.Property) - { - } - - protected PropertyExpression(string name, System.Type type, Expression source, IType nhibernateType, - NHExpressionType nodeType) + protected PropertyExpression(string name,NHExpressionType nodeType, System.Type type, Expression source, IType nhibernateType) : base(nodeType, type) { NHibernateType = nhibernateType; Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/SimplePropertyExpression.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -10,7 +10,7 @@ public class SimplePropertyExpression:PropertyExpression { public SimplePropertyExpression(string name,string column, System.Type type, Expression source, IType nhibernateType) - : base(name, type, source, nhibernateType) + : base(name,NHExpressionType.SimpleProperty, type, source, nhibernateType) { this.Column = column; } Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -21,13 +21,18 @@ return VisitSelect((SelectExpression) exp); case NHExpressionType.Projection: return VisitProjection((ProjectionExpression) exp); - case NHExpressionType.Property: - return VisitProperty((PropertyExpression) exp); + case NHExpressionType.SimpleProperty: + return VisitSimpleProperty((SimplePropertyExpression) exp); + case NHExpressionType.CollectionProperty: + return VisitCollectionProperty((CollectionPropertyExpression)exp); + case NHExpressionType.ComponentProperty: + return VisitComponentProperty((ComponentPropertyExpression) exp); default: return base.Visit(exp); } } + protected virtual Expression VisitProjection(ProjectionExpression projection) { var source = (SelectExpression) Visit(projection.Source); @@ -45,11 +50,29 @@ return source; } - protected virtual Expression VisitProperty(PropertyExpression property) + protected virtual Expression VisitSimpleProperty(SimplePropertyExpression property) { + Expression source = Visit(property.Source); + if (source != property.Source) + return new SimplePropertyExpression(property.Name, property.Column, property.Type, source, property.NHibernateType); return property; } + private Expression VisitComponentProperty(ComponentPropertyExpression property) + { + Expression source = Visit(property.Source); + if (source != property.Source) + return new ComponentPropertyExpression(property.Name, property.Columns, property.Type, source, property.NHibernateType); + return property; + } + private Expression VisitCollectionProperty(CollectionPropertyExpression property) + { + Expression source = Visit(property.Source); + if (source != property.Source) + return new CollectionPropertyExpression(property.Name, property.Type, source, property.NHibernateType); + return property; + } + //TODO: modify protected virtual Expression VisitSelect(SelectExpression select) { Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-20 16:10:08 UTC (rev 3776) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SqlExpressionTranslator.cs 2008-09-20 16:50:54 UTC (rev 3777) @@ -128,7 +128,7 @@ return c; } - protected override Expression VisitProperty(PropertyExpression property) + protected override Expression VisitSimpleProperty(SimplePropertyExpression property) { var expr = property.Source as ParameterExpression; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-09-20 22:06:52
|
Revision: 3778 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3778&view=rev Author: tehlike Date: 2008-09-20 22:06:36 +0000 (Sat, 20 Sep 2008) Log Message: ----------- Adding OneToOnePropertyExpression, AssociationPropertyExpression, OneToManyPropertyExpression in order to handle associations easier. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Linq.Expressions; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public abstract class AssociationPropertyExpression:PropertyExpression + { + public AssociationPropertyExpression(string name,NHExpressionType nodeType,System.Type type, + Expression source,IType nhType):base(name,nodeType,type,source,nhType) + { + + } + + public IAssociationType AssociationType { get { return base.NHibernateType as IAssociationType; } } + } +} Deleted: trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs 2008-09-20 16:50:54 UTC (rev 3777) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using NHibernate.Type; - -namespace NHibernate.Linq.Expressions -{ - public class CollectionPropertyExpression : PropertyExpression - { - public CollectionPropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) - : base(name,NHExpressionType.CollectionProperty, type, source, nhibernateType) - { - } - - public CollectionType CollectionType { get { return base.NHibernateType as CollectionType; } } - } -} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-09-20 16:50:54 UTC (rev 3777) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -7,7 +7,8 @@ Projection, SimpleProperty, ComponentProperty, - CollectionProperty, + OneToManyProperty, + OneToOneProperty, Order, } } \ No newline at end of file Copied: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs (from rev 3777, trunk/nhibernate/src/NHibernate.Linq/Expressions/CollectionPropertyExpression.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class OneToManyPropertyExpression : AssociationPropertyExpression + { + public OneToManyPropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) + : base(name,NHExpressionType.OneToManyProperty, type, source, nhibernateType) + { + } + + public CollectionType CollectionType { get { return base.AssociationType as CollectionType; } } + } +} \ No newline at end of file Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs ___________________________________________________________________ Added: svn:mergeinfo + Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using NHibernate.Type; + +namespace NHibernate.Linq.Expressions +{ + public class OneToOnePropertyExpression:AssociationPropertyExpression + { + public OneToOnePropertyExpression(string name, System.Type type, Expression source, IType nhType) + : base(name, NHExpressionType.OneToOneProperty, type, source, nhType) + { + + } + + public OneToOneType OneToOneType { get { return base.AssociationType as OneToOneType; } } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 16:50:54 UTC (rev 3777) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-09-20 22:06:36 UTC (rev 3778) @@ -50,8 +50,10 @@ </ProjectReference> </ItemGroup> <ItemGroup> - <Compile Include="Expressions\CollectionPropertyExpression.cs" /> + <Compile Include="Expressions\AssociationPropertyExpression.cs" /> + <Compile Include="Expressions\OneToManyPropertyExpression.cs" /> <Compile Include="Expressions\ComponentPropertyExpression.cs" /> + <Compile Include="Expressions\OneToOnePropertyExpression.cs" /> <Compile Include="Expressions\OrderExpression.cs" /> <Compile Include="Expressions\OrderType.cs" /> <Compile Include="Expressions\ProjectionExpression.cs" /> @@ -65,6 +67,7 @@ <Compile Include="Util\Guard.cs" /> <Compile Include="Util\LinqUtil.cs" /> <Compile Include="Visitors\AssociationRewriteVisitor.cs" /> + <Compile Include="Visitors\JoinGatherer.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 16:50:54 UTC (rev 3777) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -59,19 +59,18 @@ ((PropertyInfo) expr.Member).PropertyType, source, propertyType); } - else if(propertyType.IsAssociationType) + else if (propertyType is OneToOneType) { - throw new NotImplementedException("Queries on associations are not yet supported"); + Expression source = base.Visit(expr.Expression); + return new OneToOnePropertyExpression(propertyName, expr.Type, source, propertyType); } - else if(propertyType.IsCollectionType) + else if(!propertyType.IsAssociationType)//assume simple property { - throw new NotImplementedException("Queries on collections are not yet supported"); + return new SimplePropertyExpression(expr.Member.Name, mapping.ToColumns(propertyName)[0], + ((PropertyInfo) expr.Member).PropertyType, + base.Visit(expr.Expression), propertyType); } - else//Assume simple property - { - return new SimplePropertyExpression(expr.Member.Name,mapping.ToColumns(propertyName)[0], ((PropertyInfo) expr.Member).PropertyType, - base.Visit(expr.Expression), propertyType); - } + return expr; } protected override Expression VisitConstant(ConstantExpression c) Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NHibernate.Engine; +using NHibernate.Mapping; +using NHibernate.Type; + +namespace NHibernate.Linq.Visitors +{ + public class SelectWhereJoinGatherer:NHibernateExpressionVisitor + { + public override System.Linq.Expressions.Expression Visit(System.Linq.Expressions.Expression exp) + { + return base.Visit(exp); + } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-20 16:50:54 UTC (rev 3777) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-09-20 22:06:36 UTC (rev 3778) @@ -23,8 +23,10 @@ return VisitProjection((ProjectionExpression) exp); case NHExpressionType.SimpleProperty: return VisitSimpleProperty((SimplePropertyExpression) exp); - case NHExpressionType.CollectionProperty: - return VisitCollectionProperty((CollectionPropertyExpression)exp); + case NHExpressionType.OneToManyProperty: + return VisitOneToManyProperty((OneToManyPropertyExpression)exp); + case NHExpressionType.OneToOneProperty: + return VisitOneToOneProperty((OneToOnePropertyExpression)exp); case NHExpressionType.ComponentProperty: return VisitComponentProperty((ComponentPropertyExpression) exp); default: @@ -33,6 +35,8 @@ } + + protected virtual Expression VisitProjection(ProjectionExpression projection) { var source = (SelectExpression) Visit(projection.Source); @@ -65,14 +69,20 @@ return property; } - private Expression VisitCollectionProperty(CollectionPropertyExpression property) + private Expression VisitOneToManyProperty(OneToManyPropertyExpression propertyExpression) { - Expression source = Visit(property.Source); - if (source != property.Source) - return new CollectionPropertyExpression(property.Name, property.Type, source, property.NHibernateType); - return property; + Expression source = Visit(propertyExpression.Source); + if (source != propertyExpression.Source) + return new OneToManyPropertyExpression(propertyExpression.Name, propertyExpression.Type, source, propertyExpression.NHibernateType); + return propertyExpression; } - + private Expression VisitOneToOneProperty(OneToOnePropertyExpression propertyExpression) + { + Expression source = Visit(propertyExpression.Source); + if (source != propertyExpression.Source) + return new OneToOnePropertyExpression(propertyExpression.Name, propertyExpression.Type, source, propertyExpression.NHibernateType); + return propertyExpression; + } //TODO: modify protected virtual Expression VisitSelect(SelectExpression select) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <te...@us...> - 2008-10-05 13:10:11
|
Revision: 3815 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3815&view=rev Author: tehlike Date: 2008-10-05 13:07:29 +0000 (Sun, 05 Oct 2008) Log Message: ----------- -Making some more visit methods virtual. -Initials for SelectJoinGatherer. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Linq/Expressions/QueryParameterExpression.cs trunk/nhibernate/src/NHibernate.Linq/Visitors/SelectJoinGatherer.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/AssociationPropertyExpression.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -7,14 +7,16 @@ namespace NHibernate.Linq.Expressions { - public abstract class AssociationPropertyExpression:PropertyExpression + public abstract class AssociationPropertyExpression : PropertyExpression { - public AssociationPropertyExpression(string name,NHExpressionType nodeType,System.Type type, - Expression source,IType nhType):base(name,nodeType,type,source,nhType) + public AssociationPropertyExpression(string name, string alias, NHExpressionType nodeType, + System.Type type, Expression source, IType nhType) + : base(name, nodeType, type, source, nhType) { - + } public IAssociationType AssociationType { get { return base.NHibernateType as IAssociationType; } } + public string Alias { get; protected set; } } -} +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/NHExpressionType.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -9,6 +9,7 @@ ComponentProperty, OneToManyProperty, OneToOneProperty, + Parameter, Order, } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToManyPropertyExpression.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -9,8 +9,8 @@ { public class OneToManyPropertyExpression : AssociationPropertyExpression { - public OneToManyPropertyExpression(string name, System.Type type, Expression source, IType nhibernateType) - : base(name,NHExpressionType.OneToManyProperty, type, source, nhibernateType) + public OneToManyPropertyExpression(string name, string alias, System.Type type, Expression source, IType nhibernateType) + : base(name,alias,NHExpressionType.OneToManyProperty, type, source, nhibernateType) { } Modified: trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/OneToOnePropertyExpression.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -9,8 +9,8 @@ { public class OneToOnePropertyExpression:AssociationPropertyExpression { - public OneToOnePropertyExpression(string name, System.Type type, Expression source, IType nhType) - : base(name, NHExpressionType.OneToOneProperty, type, source, nhType) + public OneToOnePropertyExpression(string name,string alias, System.Type type, Expression source, IType nhType) + : base(name,alias, NHExpressionType.OneToOneProperty, type, source, nhType) { } Added: trunk/nhibernate/src/NHibernate.Linq/Expressions/QueryParameterExpression.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Expressions/QueryParameterExpression.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Expressions/QueryParameterExpression.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace NHibernate.Linq.Expressions +{ + public class QueryParameterExpression:NHExpression + { + public QueryParameterExpression(object value, int ordinal, System.Type type) + : base(NHExpressionType.Parameter, type) + { + this.Value = value; + this.Ordinal = ordinal; + } + + public object Value { get; protected set; } + public int Ordinal { get; protected set; } + } +} Property changes on: trunk/nhibernate/src/NHibernate.Linq/Expressions/QueryParameterExpression.cs ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/NHibernate.Linq.csproj 2008-10-05 13:07:29 UTC (rev 3815) @@ -44,7 +44,7 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\NHibernate\NHibernate-3.5.csproj"> + <ProjectReference Include="..\NHibernate\NHibernate.csproj"> <Project>{5909BFE7-93CF-4E5F-BE22-6293368AF01D}</Project> <Name>NHibernate-3.5</Name> </ProjectReference> @@ -56,6 +56,7 @@ <Compile Include="Expressions\OneToOnePropertyExpression.cs" /> <Compile Include="Expressions\OrderExpression.cs" /> <Compile Include="Expressions\OrderType.cs" /> + <Compile Include="Expressions\QueryParameterExpression.cs" /> <Compile Include="Expressions\ProjectionExpression.cs" /> <Compile Include="Expressions\PropertyExpression.cs" /> <Compile Include="Expressions\SelectExpression.cs" /> @@ -67,7 +68,6 @@ <Compile Include="Util\Guard.cs" /> <Compile Include="Util\LinqUtil.cs" /> <Compile Include="Visitors\AssociationRewriteVisitor.cs" /> - <Compile Include="Visitors\JoinGatherer.cs" /> <Compile Include="Visitors\LogicalExpressionReducer.cs" /> <Compile Include="ExpressionVisitor.cs" /> <Compile Include="NHibernateExtensions.cs" /> @@ -80,6 +80,7 @@ <Compile Include="Visitors\OrderByTransformer.cs" /> <Compile Include="Visitors\Replacer.cs" /> <Compile Include="Visitors\QuerySpacesFinderVisitor.cs" /> + <Compile Include="Visitors\SelectJoinGatherer.cs" /> <Compile Include="Visitors\SqlExpressionTranslator.cs" /> <Compile Include="Visitors\NHExpressionToSqlExpressionTransformer.cs" /> <Compile Include="Visitors\WhereExpressionCombiner.cs" /> Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/AssociationRewriteVisitor.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -14,12 +14,13 @@ public class AssociationRewriteVisitor : NHibernateExpressionVisitor { private readonly ISessionFactoryImplementor sessionFactory; + private int parameterOrder; private int aliasOrder; - public AssociationRewriteVisitor(ISessionFactoryImplementor factory) { - sessionFactory = factory; - aliasOrder = 0; + this.sessionFactory = factory; + this.aliasOrder = 0; + this.parameterOrder = 0; } public static Expression Rewrite(Expression expr, ISessionFactoryImplementor factory) @@ -62,7 +63,7 @@ else if (propertyType is OneToOneType) { Expression source = base.Visit(expr.Expression); - return new OneToOnePropertyExpression(propertyName, expr.Type, source, propertyType); + return new OneToOnePropertyExpression(propertyName,GenerateAliasString(), expr.Type, source, propertyType); } else if(!propertyType.IsAssociationType)//assume simple property { @@ -82,8 +83,12 @@ var loadable = sessionFactory.GetEntityPersister(elementType.FullName) as IOuterJoinLoadable; return new QuerySourceExpression(type, loadable); } - - return c; + else + return new QueryParameterExpression(c.Type, parameterOrder++, c.Type); } + private string GenerateAliasString() + { + return string.Format("a_{0}", aliasOrder++); + } } } \ No newline at end of file Deleted: trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/JoinGatherer.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using NHibernate.Engine; -using NHibernate.Mapping; -using NHibernate.Type; - -namespace NHibernate.Linq.Visitors -{ - public class SelectWhereJoinGatherer:NHibernateExpressionVisitor - { - public override System.Linq.Expressions.Expression Visit(System.Linq.Expressions.Expression exp) - { - return base.Visit(exp); - } - } -} Modified: trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/NHibernateExpressionVisitor.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -61,7 +61,7 @@ return new SimplePropertyExpression(property.Name, property.Column, property.Type, source, property.NHibernateType); return property; } - private Expression VisitComponentProperty(ComponentPropertyExpression property) + protected virtual Expression VisitComponentProperty(ComponentPropertyExpression property) { Expression source = Visit(property.Source); if (source != property.Source) @@ -69,18 +69,19 @@ return property; } - private Expression VisitOneToManyProperty(OneToManyPropertyExpression propertyExpression) + protected virtual Expression VisitOneToManyProperty(OneToManyPropertyExpression propertyExpression) { Expression source = Visit(propertyExpression.Source); + if (source != propertyExpression.Source) - return new OneToManyPropertyExpression(propertyExpression.Name, propertyExpression.Type, source, propertyExpression.NHibernateType); + return new OneToManyPropertyExpression(propertyExpression.Name, propertyExpression.Alias, propertyExpression.Type, source, propertyExpression.NHibernateType); return propertyExpression; } - private Expression VisitOneToOneProperty(OneToOnePropertyExpression propertyExpression) + protected virtual Expression VisitOneToOneProperty(OneToOnePropertyExpression propertyExpression) { Expression source = Visit(propertyExpression.Source); if (source != propertyExpression.Source) - return new OneToOnePropertyExpression(propertyExpression.Name, propertyExpression.Type, source, propertyExpression.NHibernateType); + return new OneToOnePropertyExpression(propertyExpression.Name, propertyExpression.Alias, propertyExpression.Type, source, propertyExpression.NHibernateType); return propertyExpression; } //TODO: modify Added: trunk/nhibernate/src/NHibernate.Linq/Visitors/SelectJoinGatherer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Linq/Visitors/SelectJoinGatherer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Linq/Visitors/SelectJoinGatherer.cs 2008-10-05 13:07:29 UTC (rev 3815) @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NHibernate.Engine; + +namespace NHibernate.Linq.Visitors +{ + public class SelectJoinGatherer:NHibernateExpressionVisitor + { + public SelectJoinGatherer(ISessionFactoryImplementor sessionFactory) + { + this.SessionFactory = sessionFactory; + this.JoinSequence=new JoinSequence(sessionFactory); + } + + public ISessionFactoryImplementor SessionFactory { get; protected set; } + public JoinSequence JoinSequence { get; protected set; } + } +} Modified: trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-10-04 19:27:55 UTC (rev 3814) +++ trunk/nhibernate/src/NHibernate.Linq.Test/NHibernate.Linq.Test.csproj 2008-10-05 13:07:29 UTC (rev 3815) @@ -60,7 +60,7 @@ <Project>{3073F5DA-5D57-4E12-9F95-8516ED346E67}</Project> <Name>NHibernate.Linq</Name> </ProjectReference> - <ProjectReference Include="..\NHibernate\NHibernate-3.5.csproj"> + <ProjectReference Include="..\NHibernate\NHibernate.csproj"> <Project>{5909BFE7-93CF-4E5F-BE22-6293368AF01D}</Project> <Name>NHibernate-3.5</Name> </ProjectReference> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |