Skip to content
Browse files

Added feature to insert records into excel files

  • Loading branch information...
1 parent 134b700 commit 99a47c318bd7f25a74e277b082ee472ad3ce540b @shashankshetty shashankshetty committed Jul 18, 2009
View
BIN lib/ExcelToDTOMapper.DTO.dll
Binary file not shown.
View
2 src/BuildDTOsFromExcel.Tests/EngineTests.cs
@@ -23,7 +23,7 @@ public class When_given_a_list_of_excel_files
[SetUp]
public void SetUp()
{
- ExcelMapperServiceLocator.SetUp();
+ ExcelMapper.Configuration.ExcelMapper.SetUp();
_engine = new Engine(new FileParser(new FileSystemService()), ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());
}
View
12 src/BuildDTOsFromExcel/Program.cs
@@ -1,10 +1,6 @@
using System;
-
using BuildDTOsFromExcel.FileService;
-
using ExcelMapper;
-using ExcelMapper.Configuration;
-
using Microsoft.Practices.ServiceLocation;
namespace BuildDTOsFromExcel
@@ -25,15 +21,17 @@ public static void Main(string[] args)
return;
}
- ExcelMapperServiceLocator.SetUp();
- IEngine buildDtoFromExcelEngine = new Engine(new FileParser(new FileSystemService()), ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());
+ ExcelMapper.Configuration.ExcelMapper.SetUp();
+ IEngine buildDtoFromExcelEngine = new Engine(new FileParser(new FileSystemService()),
+ ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());
Console.WriteLine(buildDtoFromExcelEngine.Run(args));
}
private static void DisplayUsage()
{
- Console.WriteLine("Maps non-empty workSheets in excel files to an assembly containing strongly typed objects");
+ Console.WriteLine(
+ "Maps non-empty workSheets in excel files to an assembly containing strongly typed objects");
Console.WriteLine();
Console.WriteLine("BuildDTOsFromExcel [/Assembly[[:]assemblyName]] [Excel Files]");
Console.WriteLine("/Assembly [Optional] Assembly name");
View
14 src/ExcelMapper.Tests/ExcelToDTOMapperTests.cs
@@ -61,8 +61,8 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
AddProperties();
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
- _excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
- _excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);
@@ -73,8 +73,8 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
public void Should_not_create_dto_if_there_are_no_properties()
{
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
- _excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
- _excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(_classAttributes1)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);
@@ -87,8 +87,8 @@ public void Should_not_create_an_assembly_if_the_compile_fails()
AddProperties();
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
- _excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
- _excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
+ _excelRepository.Expect(x => x.GetDTOClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(false);
@@ -112,7 +112,7 @@ public class SystemTest
[SetUp]
public void SetUp()
{
- ExcelMapperServiceLocator.SetUp();
+ Configuration.ExcelMapper.SetUp();
_excelToDtoMapper = ServiceLocator.Current.GetInstance<IExcelToDTOMapper>();
_assemblyName = String.Format("{0}.dll", TestData.AssemblyName);
}
View
57 src/ExcelMapper.Tests/Repository/ExcelRepositoryTests.cs
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
-
using ExcelMapper.Configuration;
using ExcelMapper.Repository;
using ExcelMapper.Repository.Connection;
using ExcelMapper.Tests.DTO;
-
using NUnit.Framework;
-
using Rhino.Mocks;
-
using RunTimeCodeGenerator.ClassGeneration;
namespace ExcelMapper.Tests.Repository
@@ -63,7 +59,8 @@ public class When_given_a_WorkSheet : ExcelRepositoryTestsBase
public void Should_return_Class_Properties_object_that_includes_all_the_properties_in_the_class()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- ClassAttributes classAttributes = _excelRepository.GetClassAttributes(String.Format("{0}$", _workSheetName));
+ ClassAttributes classAttributes =
+ _excelRepository.GetDTOClassAttributes(String.Format("{0}$", _workSheetName));
Assert.IsNotNull(classAttributes);
Assert.AreEqual(_workSheetName, classAttributes.Name);
Assert.IsTrue(classAttributes.Properties.Count == 4);
@@ -92,20 +89,54 @@ public void Should_map_the_excel_columns_to_the_given_dto_object()
public void Should_return_dto_objects_for_each_row_in_the_Xlsx_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- List<User> expectedCourts = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
- List<User> actualCourts = _excelRepository.Get<User>(_workSheetName).ToList();
- Assert.IsNotNull(actualCourts);
- Assert.AreEqual(expectedCourts.Count, actualCourts.Count);
+ List<User> expectedUsers = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
+ List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
+ Assert.IsNotNull(actualUsers);
+ Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
}
[Test]
public void Should_return_dto_objects_for_each_row_in_the_Xls_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
- List<User> expectedSuits = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
- List<User> actualSuits = _excelRepository.Get<User>(_workSheetName).ToList();
- Assert.IsNotNull(actualSuits);
- Assert.AreEqual(expectedSuits.Count, actualSuits.Count);
+ List<User> expectedUsers = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
+ List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
+ Assert.IsNotNull(actualUsers);
+ Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
+ }
+
+ [Test]
+ public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xls_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
+ List<User> users = new List<User>
+ {
+ new User
+ {
+ Id = 5,
+ FirstName = "John",
+ LastName = "Doe",
+ DateOfBirth = Convert.ToDateTime("1/1/2008")
+ }
+ };
+ _excelRepository.Put(users);
+ }
+
+ [Test]
+ public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xlsx_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ List<User> users = new List<User>
+ {
+ new User
+ {
+ Id = 5,
+ FirstName = "John",
+ LastName = "Doe",
+ DateOfBirth = Convert.ToDateTime("1/1/2008")
+ }
+ };
+ _excelRepository.Put(users);
}
}
}
View
2 ...onfiguration/ExcelMapperServiceLocator.cs → src/ExcelMapper/Configuration/ExcelMapper.cs
@@ -2,7 +2,7 @@
namespace ExcelMapper.Configuration
{
- public class ExcelMapperServiceLocator
+ public class ExcelMapper
{
public static void SetUp()
{
View
4 src/ExcelMapper/ExcelMapper.csproj
@@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>9.0.30729</ProductVersion>
+ <ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CB8A10B6-9E2E-4228-B945-9C375577AF88}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -73,7 +73,7 @@
<Compile Include="Configuration\ExcelMapperRegistry.cs" />
<Compile Include="Configuration\FileConfiguration.cs" />
<Compile Include="Configuration\StructureMapServiceLocator.cs" />
- <Compile Include="Configuration\ExcelMapperServiceLocator.cs" />
+ <Compile Include="Configuration\ExcelMapper.cs" />
<Compile Include="IExcelToDTOMapper.cs" />
<Compile Include="ExcelToDTOMapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
View
2 src/ExcelMapper/ExcelToDTOMapper.cs
@@ -35,7 +35,7 @@ public bool Run(string assemblyName, List<string> files)
_fileConfiguration.FileName = file;
foreach (string workSheet in _excelRepository.GetWorkSheetNames())
{
- ClassAttributes classAttributes = _excelRepository.GetClassAttributes(workSheet);
+ ClassAttributes classAttributes = _excelRepository.GetDTOClassAttributes(workSheet);
classAttributes.Namespace = String.Format("{0}.{1}", assemblyName, Path.GetFileNameWithoutExtension(file));
if (classAttributes.Properties.Count == 0)
View
43 src/ExcelMapper/Repository/ExcelRepository.cs
@@ -4,11 +4,10 @@
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
-
+using System.Text;
using ExcelMapper.Configuration;
using ExcelMapper.Repository.Connection;
using ExcelMapper.Repository.Extensions;
-
using RunTimeCodeGenerator.ClassGeneration;
namespace ExcelMapper.Repository
@@ -39,7 +38,7 @@ public IEnumerable<string> GetWorkSheetNames()
}
}
- public ClassAttributes GetClassAttributes(string workSheet)
+ public ClassAttributes GetDTOClassAttributes(string workSheet)
{
string className = workSheet.Replace("$", "");
ClassAttributes classAttributes = new ClassAttributes(className);
@@ -55,7 +54,8 @@ public ClassAttributes GetClassAttributes(string workSheet)
{
for (int i = 0; i < reader.FieldCount; i++)
{
- classAttributes.Properties.Add(new Property(reader.GetFieldType(i).ToString(), reader.GetName(i)));
+ classAttributes.Properties.Add(new Property(reader.GetFieldType(i).ToString(),
+ reader.GetName(i)));
}
}
}
@@ -73,7 +73,7 @@ public IEnumerable<T> Get<T>(string workSheet)
command.CommandText = String.Format("SELECT * FROM [{0}$]", workSheet);
using (OleDbDataReader reader = command.ExecuteReader())
{
- PropertyInfo[] properties = typeof(T).GetProperties();
+ PropertyInfo[] properties = typeof (T).GetProperties();
while (reader.Read())
{
@@ -90,5 +90,38 @@ public IEnumerable<T> Get<T>(string workSheet)
}
}
}
+
+ private static string ConstructQueryValues(int length)
+ {
+ StringBuilder query = new StringBuilder();
+ for (int i = 0; i < length - 1; i++)
+ {
+ query.Append("?, ");
+ }
+ query.Append("?");
+ return query.ToString();
+ }
+
+ public void Put<T>(IEnumerable<T> values)
+ {
+ PropertyInfo[] properties = typeof (T).GetProperties();
+ string query = String.Format("INSERT INTO [{0}$] VALUES ({1})", typeof (T).Name,
+ ConstructQueryValues(properties.Length));
+ using (OleDbConnection connection = _connectionBuilder.GetConnection(_fileConfiguration.FileName))
+ {
+ using (OleDbCommand command = connection.CreateCommand())
+ {
+ command.CommandText = query;
+ foreach (var value in values)
+ {
+ foreach (var property in properties)
+ {
+ command.Parameters.AddWithValue(property.Name, property.GetValue(value, null));
+ }
+ command.ExecuteNonQuery();
+ }
+ }
+ }
+ }
}
}
View
3 src/ExcelMapper/Repository/IRepository.cs
@@ -7,7 +7,8 @@ namespace ExcelMapper.Repository
public interface IRepository
{
IEnumerable<string> GetWorkSheetNames();
- ClassAttributes GetClassAttributes(string workSheet);
+ ClassAttributes GetDTOClassAttributes(string workSheet);
IEnumerable<T> Get<T>(string workSheet);
+ void Put<T>(IEnumerable<T> values);
}
}
View
2 src/Samples.Tests/Tests.cs
@@ -13,7 +13,7 @@ public class Tests
[Test]
public void Test()
{
- ExcelMapperServiceLocator.SetUp();
+ ExcelMapper.Configuration.ExcelMapper.SetUp();
ServiceLocator.Current.GetInstance<IFileConfiguration>().FileName = "Excel\\Users.xlsx";
IRepository repository = ServiceLocator.Current.GetInstance<IRepository>();

0 comments on commit 99a47c3

Please sign in to comment.
Something went wrong with that request. Please try again.