User: xtoff
Date: 2010/01/09 10:56 AM

Added:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
  ActiveRecordDetachedQueryGenericTestCase.cs, 
ActiveRecordDetachedQueryTestCase.cs

Modified:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
  Castle.ActiveRecord.Tests-vs2008.csproj, FetchTestCase.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/
  Blog.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/
  Blog.cs, Post.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/
  ActiveRecordBase.Generic.cs, ActiveRecordBase.cs, 
ActiveRecordMediator.Generic.cs, ActiveRecordMediator.cs

Log:
 - applied patch from Dario Quintana (with changes) for - AR-ISSUE-192 - 
ActiveRecordBase/ <T> and ActiveRecordMediator / <T> with IDetachedQuery Support

File Changes:

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
=============================================================

File [added]: ActiveRecordDetachedQueryGenericTestCase.cs
Delta lines: +98 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordDetachedQueryTestCase.cs
                               (rev 0)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordDetachedQueryTestCase.cs
       2010-01-09 17:56:49 UTC (rev 6612)
@@ -0,0 +1,98 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using NUnit.Framework;
+
+using NHibernate.Impl;
+
+using Castle.ActiveRecord.Tests.Model;
+
+namespace Castle.ActiveRecord.Tests 
+{
+
+       [TestFixture]
+       public class ActiveRecordDetachedQueryTestCase : 
AbstractActiveRecordTest
+       {
+
+               
+               public void FillData()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Blog),typeof(Post));
+                       Recreate();
+
+                       for (int i = 1; i <= 10; i++)
+                       {
+                               var blog = new Blog(i) { Name = "n" + i };
+                               blog.Create();
+                       }
+               }
+
+               [Test]
+               public void Exists()
+               {
+                       FillData();
+
+                       Assert.AreEqual(10, Blog.FindAll(new 
DetachedQuery("from Blog")).Length);
+
+                       for (int i = 1; i <= 10; i++)
+                       {
+                               Assert.AreEqual(true, Blog.Exists(
+                                       new DetachedQuery("from Blog f where 
f.Id=:value").SetInt32("value", i)));
+                       }
+               }
+
+               [Test]
+               public void FindAll()
+               {
+                       FillData();
+
+                       Blog[] list = Blog.FindAll(new DetachedQuery("from Blog 
Order By Id"));
+
+                       Assert.AreEqual(10, list.Length);
+                       Assert.AreEqual(1, list[0].Id);
+                       Assert.AreEqual("n1", list[0].Name);
+                       Assert.AreEqual(10, list[9].Id);
+                       Assert.AreEqual("n10", list[9].Name);
+               }
+
+               [Test]
+               public void FindOne()
+               {
+                       FillData();
+
+                       Blog f = Blog.FindOne(
+                               new DetachedQuery("from Blog f where 
f.Id=:value").SetInt32("value",10));
+
+                       Assert.IsNotNull(f);
+                       Assert.AreEqual(10, f.Id);
+                       Assert.AreEqual("n10", f.Name);
+               }
+
+               [Test]
+               public void SlidedFindAll()
+               {
+                       FillData();
+
+                       Blog[] list = Blog.SlicedFindAll(5, 9, new 
DetachedQuery("from Blog"));
+
+                       Assert.AreEqual(5, list.Length);
+
+                       Assert.AreEqual(6, list[0].Id);
+                       Assert.AreEqual("n6", list[0].Name);
+
+                       Assert.AreEqual(10, list[4].Id);
+                       Assert.AreEqual("n10", list[4].Name);
+               }
+       }
+}

File [added]: ActiveRecordDetachedQueryTestCase.cs
Delta lines: +2 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-09 17:08:31 UTC (rev 6611)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-09 17:56:49 UTC (rev 6612)
@@ -329,6 +329,8 @@
     <None Include="App-net-3.5.config" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ActiveRecordDetachedQueryGenericTestCase.cs" />
+    <Compile Include="ActiveRecordDetachedQueryTestCase.cs" />
     <Compile Include="AnyRelationTestCase.cs" />
     <Compile Include="CompositeNestedClassTestCase.cs" />

File [modified]: Castle.ActiveRecord.Tests-vs2008.csproj
Delta lines: +1 -1
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/FetchTestCase.cs   
2010-01-09 17:08:31 UTC (rev 6611)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/FetchTestCase.cs   
2010-01-09 17:56:49 UTC (rev 6612)
@@ -37,7 +37,7 @@
                        Assert.IsNotNull(blogs);
                        Assert.AreEqual(0, blogs.Length);
 
-                       var blog = new Blog { Name = "Test blog", Author = 
"Eric Bowen" };
+                       var blog = new Blog() { Name = "Test blog", Author = 
"Eric Bowen" };
 
                        blog.Save();

File [modified]: FetchTestCase.cs
Delta lines: +20 -0
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/Blog.cs      
2010-01-09 17:08:31 UTC (rev 6611)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/Blog.cs      
2010-01-09 17:56:49 UTC (rev 6612)
@@ -105,11 +105,21 @@
                        return (Blog[]) 
ActiveRecordMediator.FindAll(typeof(Blog));
                }
 
+        public static Blog[] FindAll(IDetachedQuery dq)
+        {
+            return (Blog[]) FindAll(typeof(Blog), dq);
+        }
+
                public static Blog Find(int id)
                {
                        return (Blog) 
ActiveRecordMediator.FindByPrimaryKey(typeof(Blog), id);
                }
 
+        public static Blog FindOne(IDetachedQuery dq)
+        {
+            return (Blog) FindOne(typeof(Blog), dq);
+        }
+
                public static int FetchCount()
                {
                        return Count(typeof(Blog));
@@ -145,6 +155,16 @@
                        return Exists(typeof(Blog), criteria);
                }
 
+        public static bool Exists(IDetachedQuery dq)
+        {
+            return Exists(typeof(Blog), dq);
+        }
+
+        public static Blog[] SlicedFindAll(int FirstResult, int MaxResult, 
IDetachedQuery dq)
+        {
+            return (Blog[]) SlicedFindAll(typeof(Blog), FirstResult, 
MaxResult, dq);
+        }
+
                public static Blog[] FindByProperty(String property, object 
value)
                {

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/
=================================================================

File [modified]: ActiveRecordBase.Generic.cs
Delta lines: +123 -1
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordBase.cs    
2010-01-09 17:08:31 UTC (rev 6611)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordBase.cs    
2010-01-09 17:56:49 UTC (rev 6612)
@@ -935,7 +935,20 @@
                {
                        return Count(targetType, detachedCriteria) > 0;
                }
-               
+
+               /// <summary>
+               /// Check if there is any records in the db for the target type
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="detachedQuery"></param>
+               /// <returns><c>true</c> if there's at least one row</returns>
+               protected internal static bool Exists(Type targetType, 
IDetachedQuery detachedQuery)
+               {
+                       Array array = SlicedFindAll(targetType, 0, 1, 
detachedQuery);
+
+                       return array.Length > 0;
+               }
+
                #endregion
 
                #region FindAll
@@ -1061,6 +1074,39 @@
                        return FindAll(targetType, null, criteria);
                }
 
+               /// <summary>
+               /// Returns all instances found for the specified type 
according to the criteria
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The <see cref="Array"/> of results.</returns>
+               protected internal static Array FindAll(Type targetType, 
IDetachedQuery detachedQuery)
+               {
+                       EnsureInitialized(targetType);
+
+                       ISession session = holder.CreateSession(targetType);
+                       try
+                       {
+                               IQuery executableQuery = 
detachedQuery.GetExecutableQuery(session);
+                               return SupportingUtils.BuildArray(targetType, 
executableQuery.List());
+                       }
+                       catch (ValidationException)
+                       {
+                               holder.FailSession(session);
+                               throw;
+                       }
+                       catch (Exception exception)
+                       {
+                               holder.FailSession(session);
+                               throw new ActiveRecordException("Could not 
perform FindAll for " + targetType.Name, exception);
+                       }
+                       finally
+                       {
+                               holder.ReleaseSession(session);
+                       }
+               }
+
+
                #endregion
 
                #region FindAllByProperty
@@ -1214,6 +1260,22 @@
                        return FindFirst(targetType, null, criteria);
                }
 
+               /// <summary>
+               /// Searches and returns the first row.
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="detachedQuery">The expression query.</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null.</c></returns>
+               protected internal static object FindFirst(Type targetType, 
IDetachedQuery detachedQuery)
+               {
+                       Array array = SlicedFindAll(targetType, 0, 1, 
detachedQuery);
+                       if ((array != null) && (array.Length > 0))
+                       {
+                               return array.GetValue(0);
+                       }
+                       return null;
+               }
+
                #endregion
 
                #region FindOne
@@ -1258,6 +1320,29 @@
                        return (result.Length == 0) ? null : result.GetValue(0);
                }
 
+               /// <summary>
+               /// Searches and returns a row. If more than one is found,
+               /// throws <see cref="ActiveRecordException"/>
+               /// </summary>
+               /// <param name="targetType">The target type</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null</c></returns>
+               protected internal static object FindOne(Type targetType, 
IDetachedQuery detachedQuery)
+               {
+                       Array array = SlicedFindAll(targetType, 0, 2, 
detachedQuery);
+                       if (array.Length > 1)
+                       {
+                               throw new ActiveRecordException(
+                                       string.Concat(
+                                               new object[] { targetType.Name, 
".FindOne returned ", array.Length, " rows. Expecting one or none" }));
+                       }
+                       if (array.Length != 0)
+                       {
+                               return array.GetValue(0);
+                       }
+                       return null;
+               }
+
                #endregion
 
                #region SlicedFindAll
@@ -1389,6 +1474,43 @@
                        return SlicedFindAll(targetType, firstResult, 
maxResults, null, criteria);
                }
 
+               /// <summary>
+               /// Returns a portion of the query results (sliced)
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="firstResult">The number of the first row to 
retrieve.</param>
+               /// <param name="maxResults">The maximum number of results 
retrieved.</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The sliced query results.</returns>
+               public static Array SlicedFindAll(Type targetType, int 
firstResult, int maxResults, IDetachedQuery detachedQuery)
+               {
+
+                       EnsureInitialized(targetType);
+                       ISession session = holder.CreateSession(targetType);
+                       try
+                       {
+                               IQuery executableQuery = 
detachedQuery.GetExecutableQuery(session);
+                               executableQuery.SetFirstResult(firstResult);
+                               executableQuery.SetMaxResults(maxResults);
+                               return SupportingUtils.BuildArray(targetType, 
executableQuery.List());
+                       }
+                       catch (ValidationException)
+                       {
+                               holder.FailSession(session);
+                               throw;
+                       }
+                       catch (Exception exception)
+                       {
+                               holder.FailSession(session);
+                               throw new ActiveRecordException("Could not 
perform SlicedFindAll for " + targetType.Name, exception);
+                       }
+                       finally
+                       {
+                               holder.ReleaseSession(session);
+                       }
+               }
+
+
                #endregion
 

File [modified]: ActiveRecordBase.cs
Delta lines: +55 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordMediator.Generic.cs
        2010-01-09 17:08:31 UTC (rev 6611)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordMediator.Generic.cs
        2010-01-09 17:56:49 UTC (rev 6612)
@@ -16,6 +16,8 @@
 {
        using System;
        using Framework;
+
+       using NHibernate;
        using NHibernate.Criterion;
 
        /// <summary>
@@ -103,6 +105,16 @@
                /// <summary>
                /// Searches and returns the first row.
                /// </summary>
+               /// <param name="detachedQuery">The expression query.</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null.</c></returns>
+               public static T FindFirst(IDetachedQuery detachedQuery)
+               {
+                       return (T)FindFirst(typeof(T), detachedQuery);
+               }
+
+               /// <summary>
+               /// Searches and returns the first row.
+               /// </summary>
                /// <param name="criterias">The criterias.</param>
                /// <returns>A instance the targetType or <c>null</c></returns>
                public static T FindOne(params ICriterion[] criterias)
@@ -122,6 +134,17 @@
                }
 
                /// <summary>
+               /// Searches and returns a row. If more than one is found,
+               /// throws <see cref="ActiveRecordException"/>
+               /// </summary>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null</c></returns>
+               public static T FindOne(IDetachedQuery detachedQuery)
+               {
+                       return (T)FindOne(typeof(T), detachedQuery);
+               }
+
+               /// <summary>
                /// Finds records based on a property value - automatically 
converts null values to IS NULL style queries. 
                /// </summary>
                /// <param name="property">A property name (not a column 
name)</param>
@@ -185,6 +208,16 @@
                }
 
                /// <summary>
+               /// Returns all instances found for the specified type 
according to the criteria
+               /// </summary>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The <see cref="Array"/> of results.</returns>
+               public static T[] FindAll(IDetachedQuery detachedQuery)
+               {
+                       return (T[])FindAll(typeof(T), detachedQuery);
+               }
+
+               /// <summary>
                /// Returns a portion of the query results (sliced)
                /// </summary>
                public static T[] SlicedFindAll(int firstResult, int 
maxResults, Order[] orders, params ICriterion[] criterias)
@@ -209,6 +242,18 @@
                }
 
                /// <summary>
+               /// Returns a portion of the query results (sliced)
+               /// </summary>
+               /// <param name="firstResult">The number of the first row to 
retrieve.</param>
+               /// <param name="maxResults">The maximum number of results 
retrieved.</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The sliced query results.</returns>
+               public static T[] SlicedFindAll(int firstResult, int 
maxResults, IDetachedQuery detachedQuery)
+               {
+                       return (T[])SlicedFindAll(typeof(T), firstResult, 
maxResults, detachedQuery);
+               }
+
+               /// <summary>
                /// Deletes all entities of <typeparamref name="T"/>.
                /// </summary>
                public static void DeleteAll()
@@ -302,6 +347,16 @@
                }
 
                /// <summary>
+               /// Check if any instance matches the query.
+               /// </summary>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns><c>true</c> if an instance is found; otherwise 
<c>false</c>.</returns>
+               public static bool Exists(IDetachedQuery detachedQuery)
+               {
+                       return Exists(typeof(T), detachedQuery);
+               }
+
+               /// <summary>
                /// Returns the number of records of the specified 
                /// type in the database

File [modified]: ActiveRecordMediator.Generic.cs
Delta lines: +103 -43
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordMediator.cs    
    2010-01-09 17:08:31 UTC (rev 6611)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/ActiveRecordMediator.cs    
    2010-01-09 17:56:49 UTC (rev 6612)
@@ -21,14 +21,14 @@
        using Castle.ActiveRecord.Framework;
 
        /// <summary>
-       /// Allow programmers to use the 
+       /// Allow programmers to use the
        /// ActiveRecord functionality without direct reference
        /// to <see cref="ActiveRecordBase"/>
        /// </summary>
        public class ActiveRecordMediator
        {
                /// <summary>
-               /// Invokes the specified delegate passing a valid 
+               /// Invokes the specified delegate passing a valid
                /// NHibernate session. Used for custom NHibernate queries.
                /// </summary>
                /// <param name="targetType">The target ActiveRecordType</param>
@@ -47,7 +47,7 @@
                /// <param name="id">ID value</param>
                /// <param name="throwOnNotFound"><c>true</c> if you want an 
exception to be thrown
                /// if the object is not found</param>
-               /// <exception cref="ObjectNotFoundException">if 
<c>throwOnNotFound</c> is set to 
+               /// <exception cref="ObjectNotFoundException">if 
<c>throwOnNotFound</c> is set to
                /// <c>true</c> and the row is not found</exception>
                public static object FindByPrimaryKey(Type targetType, object 
id, bool throwOnNotFound)
                {
@@ -111,7 +111,19 @@
                }
 
                /// <summary>
-               /// Searches and returns the a row. If more than one is found, 
+               /// Searches and returns the first row.
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="detachedQuery">The expression query.</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null.</c></returns>
+               public static object FindFirst(Type targetType, IDetachedQuery 
detachedQuery)
+               {
+                       return ActiveRecordBase.FindFirst(targetType, 
detachedQuery);
+               }
+
+
+               /// <summary>
+               /// Searches and returns the a row. If more than one is found,
                /// throws <see cref="ActiveRecordException"/>
                /// </summary>
                /// <param name="targetType">The target type</param>
@@ -123,7 +135,7 @@
                }
 
                /// <summary>
-               /// Searches and returns a row. If more than one is found, 
+               /// Searches and returns a row. If more than one is found,
                /// throws <see cref="ActiveRecordException"/>
                /// </summary>
                /// <param name="targetType">The target type</param>
@@ -135,6 +147,18 @@
                }
 
                /// <summary>
+               /// Searches and returns a row. If more than one is found,
+               /// throws <see cref="ActiveRecordException"/>
+               /// </summary>
+               /// <param name="targetType">The target type</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>A <c>targetType</c> instance or 
<c>null</c></returns>
+               public static object FindOne(Type targetType, IDetachedQuery 
detachedQuery)
+               {
+                       return ActiveRecordBase.FindOne(targetType, 
detachedQuery);
+               }
+
+               /// <summary>
                /// Returns a portion of the query results (sliced)
                /// </summary>
                public static Array SlicedFindAll(Type targetType, int 
firstResult, int maxresults,
@@ -156,7 +180,7 @@
                /// Returns a portion of the query results (sliced)
                /// </summary>
                public static Array SlicedFindAll(Type targetType, int 
firstResult, int maxResults,
-                                                                               
                          Order[] orders, DetachedCriteria criteria)
+                                                 Order[] orders, 
DetachedCriteria criteria)
                {
                        return ActiveRecordBase.SlicedFindAll(targetType, 
firstResult, maxResults, orders, criteria);
                }
@@ -165,12 +189,26 @@
                /// Returns a portion of the query results (sliced)
                /// </summary>
                public static Array SlicedFindAll(Type targetType, int 
firstResult, int maxResults,
-                                                                               
                          DetachedCriteria criteria)
+                                                 DetachedCriteria criteria)
                {
                        return ActiveRecordBase.SlicedFindAll(targetType, 
firstResult, maxResults, criteria);
                }
 
                /// <summary>
+               /// Returns a portion of the query results (sliced)
+               /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="firstResult">The number of the first row to 
retrieve.</param>
+               /// <param name="maxResults">The maximum number of results 
retrieved.</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The sliced query results.</returns>
+               public static Array SlicedFindAll(Type targetType, int 
firstResult, int maxResults,
+                                                 IDetachedQuery detachedQuery)
+               {
+                       return ActiveRecordBase.SlicedFindAll(targetType, 
firstResult, maxResults, detachedQuery);
+               }
+               
+               /// <summary>
                /// Returns all instances found for the specified type.
                /// </summary>
                /// <param name="targetType"></param>
@@ -181,7 +219,7 @@
                }
 
                /// <summary>
-               /// Returns all instances found for the specified type 
+               /// Returns all instances found for the specified type
                /// using sort orders and criterias.
                /// </summary>
                /// <param name="targetType"></param>
@@ -194,7 +232,7 @@
                }
 
                /// <summary>
-               /// Returns all instances found for the specified type 
+               /// Returns all instances found for the specified type
                /// using criterias.
                /// </summary>
                /// <param name="targetType"></param>
@@ -214,8 +252,19 @@
                }
 
                /// <summary>
-               /// Finds records based on a property value - automatically 
converts null values to IS NULL style queries. 
+               /// Returns all instances found for the specified type 
according to the criteria
                /// </summary>
+               /// <param name="targetType">The target type.</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns>The <see cref="Array"/> of results.</returns>
+               public static Array FindAll(Type targetType, IDetachedQuery 
detachedQuery)
+               {
+                       return ActiveRecordBase.FindAll(targetType, 
detachedQuery);
+               }
+
+               /// <summary>
+               /// Finds records based on a property value - automatically 
converts null values to IS NULL style queries.
+               /// </summary>
                /// <param name="targetType">The target type</param>
                /// <param name="property">A property name (not a column 
name)</param>
                /// <param name="value">The value to be equals to</param>
@@ -226,7 +275,7 @@
                }
 
                /// <summary>
-               /// Finds records based on a property value - automatically 
converts null values to IS NULL style queries. 
+               /// Finds records based on a property value - automatically 
converts null values to IS NULL style queries.
                /// </summary>
                /// <param name="targetType">The target type</param>
                /// <param name="orderByColumn">The column name to be ordered 
ASC</param>
@@ -291,7 +340,7 @@
                }
 
                /// <summary>
-               /// Returns the number of records of the specified 
+               /// Returns the number of records of the specified
                /// type in the database
                /// </summary>
                /// <example>
@@ -300,7 +349,7 @@
                /// public class User : ActiveRecordBase
                /// {
                ///   ...
-               ///   
+               ///
                ///   public static int CountUsers()
                ///   {
                ///     return Count(typeof(User));
@@ -316,7 +365,7 @@
                }
 
                /// <summary>
-               /// Returns the number of records of the specified 
+               /// Returns the number of records of the specified
                /// type in the database
                /// </summary>
                /// <example>
@@ -325,7 +374,7 @@
                /// public class User : ActiveRecordBase
                /// {
                ///   ...
-               ///   
+               ///
                ///   public static int CountUsersLocked()
                ///   {
                ///     return Count(typeof(User), "IsLocked = ?", true);
@@ -343,7 +392,7 @@
                }
 
                /// <summary>
-               /// Returns the number of records of the specified 
+               /// Returns the number of records of the specified
                /// type in the database
                /// </summary>
                /// <param name="targetType">The target type.</param>
@@ -354,7 +403,7 @@
                }
 
                /// <summary>
-               /// Returns the number of records of the specified 
+               /// Returns the number of records of the specified
                /// type in the database
                /// </summary>
                /// <param name="targetType">The target type.</param>
@@ -412,7 +461,7 @@
                /// Check if any instance matching the criteria exists in the 
database.
                /// </summary>
                /// <param name="targetType">The target type.</param>
-               /// <param name="detachedCriteria">The criteria 
expression</param>              
+               /// <param name="detachedCriteria">The criteria 
expression</param>
                /// <returns><c>true</c> if an instance is found; otherwise 
<c>false</c>.</returns>
                public static bool Exists(Type targetType, DetachedCriteria 
detachedCriteria)
                {
@@ -420,6 +469,17 @@
                }
 
                /// <summary>
+               /// Check if any instance matches the query.
+               /// </summary>
+               /// <param name="targetType">target Type</param>
+               /// <param name="detachedQuery">The query expression</param>
+               /// <returns><c>true</c> if an instance is found; otherwise 
<c>false</c>.</returns>
+               public static bool Exists(Type targetType, IDetachedQuery 
detachedQuery)
+               {
+                       return ActiveRecordBase.Exists(targetType, 
detachedQuery);
+               }
+
+               /// <summary>
                /// Saves the instance to the database
                /// </summary>
                /// <param name="instance">The ActiveRecord instance to be 
deleted</param>
@@ -442,30 +502,30 @@
                        ActiveRecordBase.SaveAndFlush(instance);
                }
 
-        /// <summary>
-        /// Saves a copy of instance to the database
-        /// </summary>
-        /// <param name="instance">The transient instance to be copied</param>
-        /// <returns>The saved ActiveRecord instance</returns>
-        public static object SaveCopy(object instance)
-        {
-            return ActiveRecordBase.SaveCopy(instance);
-        }
+               /// <summary>
+               /// Saves a copy of instance to the database
+               /// </summary>
+               /// <param name="instance">The transient instance to be 
copied</param>
+               /// <returns>The saved ActiveRecord instance</returns>
+               public static object SaveCopy(object instance)
+               {
+                       return ActiveRecordBase.SaveCopy(instance);
+               }
 
-        /// <summary>
-        /// Saves a copy of the instance to the database and flushes the 
session. If the primary key is unitialized
-        /// it creates the instance on the database. Otherwise it updates it.
-        /// <para>
-        /// If the primary key is assigned, then you must invoke <see 
cref="Create(object)"/>
-        /// or <see cref="Update(object)"/> instead.
-        /// </para>
-        /// </summary>
-        /// <param name="instance">The transient instance to be copied</param>
-        /// <returns>The saved ActiveRecord instance</returns>
-        public static void SaveCopyAndFlush(object instance)
-        {
-            ActiveRecordBase.SaveCopyAndFlush(instance);
-        }
+               /// <summary>
+               /// Saves a copy of the instance to the database and flushes 
the session. If the primary key is unitialized
+               /// it creates the instance on the database. Otherwise it 
updates it.
+               /// <para>
+               /// If the primary key is assigned, then you must invoke <see 
cref="Create(object)"/>
+               /// or <see cref="Update(object)"/> instead.
+               /// </para>
+               /// </summary>
+               /// <param name="instance">The transient instance to be 
copied</param>
+               /// <returns>The saved ActiveRecord instance</returns>
+               public static void SaveCopyAndFlush(object instance)
+               {
+                       ActiveRecordBase.SaveCopyAndFlush(instance);
+               }
 
                /// <summary>
                /// Creates (Saves) a new instance to the database.
@@ -541,8 +601,8 @@
                }
 
                /// <summary>
-               /// From NHibernate documentation: 
-               /// Persist all reachable transient objects, reusing the 
current identifier 
+               /// From NHibernate documentation:
+               /// Persist all reachable transient objects, reusing the 
current identifier
                /// values. Note that this will not trigger the Interceptor of 
the Session.
                /// </summary>

File [modified]: ActiveRecordMediator.cs
Delta lines: +92 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordDetachedQueryGenericTestCase.cs
                                (rev 0)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/ActiveRecordDetachedQueryGenericTestCase.cs
        2010-01-09 17:56:49 UTC (rev 6612)
@@ -0,0 +1,93 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using NUnit.Framework;
+using NHibernate.Impl;
+using Castle.ActiveRecord.Tests.Model.GenericModel;
+
+namespace Castle.ActiveRecord.Tests
+{
+
+       [TestFixture]
+       public class ActiveRecordDetachedQueryGenericTestCase : 
AbstractActiveRecordTest
+       {
+               public void FillData()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Blog), typeof(Post));
+                       Recreate();
+
+                       for (int i = 1; i <= 10; i++)
+                       {
+                               var blog = new Blog(i) { Name = "n" + i };
+                               blog.Create();
+                       }
+               }
+
+               [Test]
+               public void Exists() 
+               {
+                       FillData();
+
+                       Assert.AreEqual(10, Blog.FindAll(new 
DetachedQuery("from Blog")).Length);
+
+                       for (int i = 1; i <= 10; i++)
+                       {
+                               Assert.AreEqual(true, Blog.Exists(
+                                       new DetachedQuery("from Blog f where 
f.Id=:value").SetInt32("value", i)));
+                       }
+               }
+
+               [Test]
+               public void FindAll() {
+                       FillData();
+
+                       Blog[] list = Blog.FindAll(new DetachedQuery("from Blog 
Order By Id"));
+
+                       Assert.AreEqual(10, list.Length);
+                       Assert.AreEqual(1, list[0].Id);
+                       Assert.AreEqual("n1", list[0].Name);
+                       Assert.AreEqual(10, list[9].Id);
+                       Assert.AreEqual("n10", list[9].Name);
+               }
+
+               [Test]
+               public void FindOne()
+               {
+                       FillData();
+
+                       Blog f = Blog.FindOne(
+                               new DetachedQuery("from Blog f where 
f.Id=:value").SetInt32("value", 10));
+
+                       Assert.IsNotNull(f);
+                       Assert.AreEqual(10, f.Id);
+                       Assert.AreEqual("n10", f.Name);
+               }
+
+               [Test]
+               public void SlidedFindAll()
+               {
+                       FillData();
+
+                       Blog[] list = Blog.SlicedFindAll(5, 9, new 
DetachedQuery("from Blog"));
+
+                       Assert.AreEqual(5, list.Length);
+
+                       Assert.AreEqual(6, list[0].Id);
+                       Assert.AreEqual("n6", list[0].Name);
+
+                       Assert.AreEqual(10, list[4].Id);
+                       Assert.AreEqual("n10", list[4].Name);
+               }
+       }

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/
================================================================================

File [modified]: Blog.cs
Delta lines: +11 -47
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/Post.cs 
2010-01-09 17:08:31 UTC (rev 6611)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/Post.cs 
2010-01-09 17:56:49 UTC (rev 6612)
@@ -22,74 +22,38 @@
        [ActiveRecord("PostTable")]
        public class Post : ActiveRecordBase<Post>
        {
-               private int _id;
-               private String _title;
-               private String _contents;
-               private String _category;
-               private DateTime _created;
-               private bool _published;
-               private Blog _blog;
-
                public Post()
                {
                }
 
                public Post(Blog blog, String title, String contents, String 
category)
                {
-                       _blog = blog;
-                       _title = title;
-                       _contents = contents;
-                       _category = category;
+                       Blog = blog;
+                       Title = title;
+                       Contents = contents;
+                       Category = category;
                }
 
                [PrimaryKey]
-               public int Id
-               {
-                       get { return _id; }
-                       set { _id = value; }
-               }
+               public int Id { get; set; }
 
                [Property]
-               public String Title
-               {
-                       get { return _title; }
-                       set { _title = value; }
-               }
+               public string Title { get; set; }
 
                [Property(ColumnType="StringClob")]
-               public String Contents
-               {
-                       get { return _contents; }
-                       set { _contents = value; }
-               }
+               public string Contents { get; set; }
 
                [Property]
-               public String Category
-               {
-                       get { return _category; }
-                       set { _category = value; }
-               }
+               public string Category { get; set; }
 
                [BelongsTo("blogid")]
-               public Blog Blog
-               {
-                       get { return _blog; }
-                       set { _blog = value; }
-               }
+               public Blog Blog { get; set; }
 
                [Property("created")]
-               public DateTime Created
-               {
-                       get { return _created; }
-                       set { _created = value; }
-               }
+               public DateTime Created { get; set; }
 
                [Property("published")]
-               public bool Published
-               {
-                       get { return _published; }
-                       set { _published = value; }
-               }
+               public bool Published { get; set; }
 
                public void SaveWithException()

File [modified]: Post.cs
Delta lines: +0 -0
===================================================================

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/
===================================================================

File [modified]: Blog.cs
Delta lines: +5 -0
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/Blog.cs 
2010-01-09 17:08:31 UTC (rev 6611)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Model/GenericModel/Blog.cs 
2010-01-09 17:56:49 UTC (rev 6612)
@@ -31,6 +31,11 @@
                        Posts = new List<Post>();
                }
 
+               public Blog(int i):this()
+               {
+                       Id = i;
+               }
+
                [PrimaryKey]
                public int Id { get; set; }

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-commits?hl=en.


Reply via email to