Commits

malweka committed 06f33aa

sql procedure

  • Participants
  • Parent commits b25746b

Comments (0)

Files changed (5)

File src/Goliath.Data.Console/Goliath.Data.Console.csproj

     </SccProvider>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <PlatformTarget>AnyCPU</PlatformTarget>
+    <PlatformTarget>x86</PlatformTarget>
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>

File src/Goliath.Data.Tests/Goliath.Data.Tests.csproj

     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
     <Compile Include="MapConfigTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="ConnectionProviderTests.cs" />
+    <Compile Include="SqlProcedureCollectionTests.cs" />
     <Compile Include="UserProvidedConnectionProviderTests.cs" />
   </ItemGroup>
   <ItemGroup>

File src/Goliath.Data.Tests/SqlProcedureCollectionTests.cs

+using System;
+using System.Collections.Generic;
+using System.Text;
+using Gallio.Framework;
+using MbUnit.Framework;
+using MbUnit.Framework.ContractVerifiers;
+
+namespace Goliath.Data.Tests
+{
+    using Mapping;
+
+    [TestFixture]
+    public class SqlProcedureCollectionTests
+    {
+
+        [Test]
+        public void Add_EntityMap_should_build_correct_name()
+        {
+            SqlProcedureCollection col = new SqlProcedureCollection();
+            EntityMap map = new EntityMap("Faker", "tb_faker");
+            map.AssemblyName="faker.dll";
+            map.Namespace="Test";
+            col.Add(map, ProcedureType.Insert, "TEST");
+
+            var proc = col[0];
+            Assert.AreEqual("Test.Faker_Insert", proc.Name);
+        }
+
+        [Test]
+        public void Add_with_name_db_name_respect_provided_name()
+        {
+            SqlProcedureCollection col = new SqlProcedureCollection();
+            col.Add("fake_proc", "fake_proc_dbname", ProcedureType.ExecuteNonQuery, "TEST", SupportedRdbms.All);
+            var proc = col[0];
+
+            Assert.AreEqual("fake_proc", proc.Name);
+            Assert.AreEqual("fake_proc_dbname", proc.DbName);
+        }
+
+        [Test]
+        public void TryGetValue_get_proc_for_type_where_platform_is_supported_return_proc()
+        {
+            SqlProcedureCollection col = new SqlProcedureCollection(SupportedRdbms.Mssql2008);
+            Type type = typeof(WebZoo.Data.Animal);
+
+            EntityMap map = new EntityMap(type.Name, "tb_faker");
+            map.AssemblyName = "faker.dll";
+            map.Namespace = type.Namespace;
+
+            col.Add(map, "fake", ProcedureType.Insert, "TEST", SupportedRdbms.MssqlAll);
+
+            SqlProcedure proc;
+            Assert.IsTrue(col.TryGetValue(type, ProcedureType.Insert, out proc));
+            Assert.IsNotNull(proc);
+        }
+
+        [Test]
+        public void TryGetValue_get_proc_for_type_where_platform_is_not_supported_return_null()
+        {
+            SqlProcedureCollection col = new SqlProcedureCollection(SupportedRdbms.Postgresql9);
+            Type type = typeof(WebZoo.Data.Animal);
+
+            EntityMap map = new EntityMap(type.Name, "tb_faker");
+            map.AssemblyName = "faker.dll";
+            map.Namespace = type.Namespace;
+
+            col.Add(map, "fake", ProcedureType.Insert, "TEST", SupportedRdbms.MssqlAll);
+
+            SqlProcedure proc;
+            Assert.IsFalse(col.TryGetValue(type, ProcedureType.Insert, out proc));
+            Assert.IsNull(proc);
+        }
+    }
+}

File src/Goliath.Data/KeyedCollectionBase.cs

         /// <param name="key">The key.</param>
         /// <param name="val">The val.</param>
         /// <returns></returns>
-        public bool TryGetValue(S key, out T val)
+        public virtual bool TryGetValue(S key, out T val)
         {
             bool isFound = false;
             val = default(T);

File src/Goliath.Data/Mapping/Models/SqlProcedureCollection.cs

     /// </summary>
     public class SqlProcedureCollection : KeyedCollectionBase<string, SqlProcedure>
     {
-        internal SupportedRdbms Platform{get;set;}
+        internal SupportedRdbms Platform { get; set; }
 
         /// <summary>
         /// Initializes a new instance of the <see cref="SqlProcedureCollection"/> class.
         /// </summary>
         public SqlProcedureCollection()
+            : this(SupportedRdbms.Sqlite3)
         {
-            Platform = SupportedRdbms.All;
         }
 
-        internal static string BuildProcedureName(EntityMap map, ProcedureType type)
+        /// <summary>
+        /// Initializes a new instance of the <see cref="SqlProcedureCollection"/> class.
+        /// </summary>
+        /// <param name="platform">The platform.</param>
+        public SqlProcedureCollection(SupportedRdbms platform)
         {
-            return string.Format("{0}_{1}", map.FullName, type); ;
+            Platform = platform;
         }
 
-        internal static string BuildProcedureName(Type type, ProcedureType procType)
-        {
-            return string.Format("{0}_{1}", type.FullName, procType); ;
-        }
 
         /// <summary>
         /// Adds the specified map.
         /// <param name="map">The map.</param>
         /// <param name="type">The type.</param>
         /// <param name="body">The body.</param>
-        public void Add(EntityMap map, ProcedureType type, string body)
+        public SqlProcedure Add(EntityMap map, ProcedureType type, string body)
         {
             string procName = BuildProcedureName(map, type);
-            Add(procName, procName, type, body);
+            return Add(procName, procName, type, body, Platform);
         }
 
         /// <summary>
         /// <param name="dbName">Name of the db.</param>
         /// <param name="type">The type.</param>
         /// <param name="body">The body.</param>
-        public void Add(EntityMap map, string dbName, ProcedureType type, string body)
+        public SqlProcedure Add(EntityMap map, string dbName, ProcedureType type, string body)
+        {
+            return Add(map, dbName, type, body, Platform);
+        }
+
+        /// <summary>
+        /// Adds the specified map.
+        /// </summary>
+        /// <param name="map">The map.</param>
+        /// <param name="dbName">Name of the db.</param>
+        /// <param name="type">The type.</param>
+        /// <param name="body">The body.</param>
+        /// <param name="supportedRdbms">The supported RDBMS.</param>
+        /// <returns></returns>
+        public SqlProcedure Add(EntityMap map, string dbName, ProcedureType type, string body, SupportedRdbms supportedRdbms)
         {
             string procName = BuildProcedureName(map, type);
-            Add(procName, dbName, type, body);
+            return Add(procName, dbName, type, body, supportedRdbms);
         }
 
         /// <summary>
         /// <param name="dbName">Name of the db.</param>
         /// <param name="type">The type.</param>
         /// <param name="body">The body.</param>
-        public void Add(string procedureName, string dbName, ProcedureType type, string body)
+        /// <param name="supportedRdbms">The supported RDBMS.</param>
+        /// <returns></returns>
+        public SqlProcedure Add(string procedureName, string dbName, ProcedureType type, string body, SupportedRdbms supportedRdbms)
         {
-            SqlProcedure proc = new SqlProcedure(procedureName, dbName, type) { Body = body };
+            SqlProcedure proc = new SqlProcedure(procedureName, dbName, type) { Body = body, CanRunOn = supportedRdbms };
             Add(proc);
+            return proc;
         }
 
         /// <summary>
             var procName = BuildProcedureName(map, procType);
             return TryGetValue(procName, out val);
         }
+
+        /// <summary>
+        /// Tries the get value.
+        /// </summary>
+        /// <param name="key">The key.</param>
+        /// <param name="val">The val.</param>
+        /// <returns></returns>
+        public override bool TryGetValue(string key, out SqlProcedure val)
+        {
+            if (base.TryGetValue(key, out val))
+            {
+                if ((val.CanRunOn & Platform) == Platform)
+                {
+                    return true;
+                }
+            }
+
+            val = null;
+            return false;
+              
+        }
+
+        internal static string BuildProcedureName(EntityMap map, ProcedureType type)
+        {
+            return string.Format("{0}_{1}", map.FullName, type); ;
+        }
+
+        internal static string BuildProcedureName(Type type, ProcedureType procType)
+        {
+            return string.Format("{0}_{1}", type.FullName, procType); ;
+        }
     }
 }