Commit 0c07035

unknown <mo@.(none)>
2009-10-10 15:32:06
hooking up the missing pieces.
1 parent 3060a6b
product/application/data/DatabaseCommand.cs
@@ -1,11 +1,12 @@
 using System;
+using System.Collections.Generic;
 using System.Data;
 
 namespace simple.migrations.Data
 {
     public interface DatabaseCommand : IDisposable
     {
-        DataTable run(string sql);
+        IEnumerable<DataRow> run(string sql);
         void run(SqlFile sql);
     }
 }
\ No newline at end of file
product/application/data/SqlDatabaseCommand.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+
+namespace simple.migrations.Data
+{
+    public class SqlDatabaseCommand : DatabaseCommand
+    {
+        readonly IDbConnection connection;
+
+        public SqlDatabaseCommand(IDbConnection connection)
+        {
+            this.connection = connection;
+        }
+
+        public IEnumerable<DataRow> run(string sql)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void run(SqlFile sql)
+        {
+            using (var command = connection.CreateCommand())
+            {
+                command.CommandText = sql.raw_sql();
+                command.CommandType = CommandType.Text;
+                command.ExecuteNonQuery();
+            }
+        }
+
+        public void Dispose()
+        {
+            connection.Close();
+            connection.Dispose();
+        }
+    }
+}
\ No newline at end of file
product/application/data/SqlDatabaseCommandFactory.cs
@@ -0,0 +1,23 @@
+using System.Data.Common;
+
+namespace simple.migrations.Data
+{
+    public class SqlDatabaseCommandFactory : DatabaseCommandFactory
+    {
+        readonly DbProviderFactory factory;
+        readonly string connection_string;
+
+        public SqlDatabaseCommandFactory(DbProviderFactory factory, string connection_string)
+        {
+            this.factory = factory;
+            this.connection_string = connection_string;
+        }
+
+        public DatabaseCommand create()
+        {
+            var connection = factory.CreateConnection();
+            connection.ConnectionString = connection_string;
+            return new SqlDatabaseCommand(connection);
+        }
+    }
+}
\ No newline at end of file
product/application/data/SqlDatabaseGateway.cs
@@ -1,4 +1,3 @@
-using System.Data;
 using System.Linq;
 using simple.migrations.utility;
 
@@ -17,10 +16,7 @@ namespace simple.migrations.Data
         {
             using (var command = command_factory.create())
             {
-                if (command.run("select * from migration_scripts")
-                    .Rows
-                    .Cast<DataRow>()
-                    .Any(x => !file.is_greater_than(x["version"].convert_to<int>()))) return;
+                if (command.run("select * from migration_scripts").Any(x => !file.is_greater_than(x["version"].convert_to<int>()))) return;
 
                 command.run(file);
             }
product/application/data/SqlDatabaseGatewayFactory.cs
@@ -0,0 +1,12 @@
+using System.Data.Common;
+
+namespace simple.migrations.Data
+{
+    public class SqlDatabaseGatewayFactory : DatabaseGatewayFactory
+    {
+        public DatabaseGateway gateway_to(string connection_string, string database_provider)
+        {
+            return new SqlDatabaseGateway(new SqlDatabaseCommandFactory(DbProviderFactories.GetFactory(database_provider), connection_string));
+        }
+    }
+}
\ No newline at end of file
product/application/data/SqlFile.cs
@@ -33,6 +33,11 @@ namespace simple.migrations.Data
             return version().CompareTo(other.version());
         }
 
+        public virtual string raw_sql()
+        {
+            throw new NotImplementedException();
+        }
+
         static public implicit operator SqlFile(string file_path)
         {
             return new SqlFile {path = file_path.ToLower()};
product/application/application.csproj
@@ -56,6 +56,9 @@
     <Compile Include="data\DatabaseCommandFactory.cs" />
     <Compile Include="data\DatabaseGateway.cs" />
     <Compile Include="data\DatabaseGatewayFactory.cs" />
+    <Compile Include="data\SqlDatabaseCommand.cs" />
+    <Compile Include="data\SqlDatabaseCommandFactory.cs" />
+    <Compile Include="data\SqlDatabaseGatewayFactory.cs" />
     <Compile Include="data\SqlFile.cs" />
     <Compile Include="data\SqlDatabaseGateway.cs" />
     <Compile Include="HelpCommand.cs" />
product/application.tests/data/SqlDatabaseGatewayFactorySpecs.cs
@@ -0,0 +1,30 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.harnesses.mbunit;
+using developwithpassion.bdddoc.core;
+using simple.migrations.Data;
+
+namespace tests.data
+{
+    public class SqlDatabaseGatewayFactorySpecs
+    {
+        public abstract class concern : observations_for_a_sut_with_a_contract<DatabaseGatewayFactory, SqlDatabaseGatewayFactory> {}
+
+        [Concern(typeof (SqlDatabaseGatewayFactory))]
+        public class when_creating_a_database_gateway : concern
+        {
+            context c = () => {};
+
+            because b = () =>
+            {
+                result = sut.gateway_to("data source=(local);Integrated Security=SSPI;initial catalog=blah;", "System.Data.SqlClient");
+            };
+
+            it should_return_a_database_gateway = () =>
+            {
+                result.should_not_be_null();
+            };
+
+            static DatabaseGateway result;
+        }
+    }
+}
\ No newline at end of file
product/application.tests/data/SqlDatabaseGatewaySpecs.cs
@@ -26,7 +26,7 @@ namespace tests.data
                 table.Rows.Add(row);
 
                 command_factory.is_told_to(x => x.create()).it_will_return(command);
-                command.is_told_to(x => x.run("select * from migration_scripts")).it_will_return(table);
+                command.is_told_to(x => x.run("select * from migration_scripts")).it_will_return(row);
                 first_script.is_told_to(x => x.is_greater_than(1)).it_will_return(false);
                 second_script.is_told_to(x => x.is_greater_than(1)).it_will_return(true);
             };
product/application.tests/application.tests.csproj
@@ -72,6 +72,7 @@
     <Compile Include="CommandRegistrySpecs.cs" />
     <Compile Include="ConsoleArgumentsSpecs.cs" />
     <Compile Include="ConsoleSpecs.cs" />
+    <Compile Include="data\SqlDatabaseGatewayFactorySpecs.cs" />
     <Compile Include="data\SqlFileSpecs.cs" />
     <Compile Include="data\SqlDatabaseGatewaySpecs.cs" />
     <Compile Include="EmptyTestFixture.cs" />