Skip to content

Commit

Permalink
Initial import of source
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed May 30, 2012
0 parents commit abbf788
Show file tree
Hide file tree
Showing 24 changed files with 1,338 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
@@ -0,0 +1,16 @@
build/
*.suo
*.user
bin
Bin
obj
_ReSharper*
*.csproj.user
*.resharper.user
*.suo
*.cache
TestResult.xml
*.orig
.hg/
.hgignore
packages/
6 changes: 6 additions & 0 deletions .nuget/NuGet.Config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
Binary file added .nuget/NuGet.exe
Binary file not shown.
71 changes: 71 additions & 0 deletions .nuget/NuGet.targets
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>

<!-- Windows specific commands -->
<NuGetToolsPath Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
<PackagesConfig Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
<PackagesDir Condition=" '$(OS)' == 'Windows_NT'">$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>

<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
<NuGetToolsPath Condition=" '$(OS)' != 'Windows_NT'">$(SolutionDir).nuget</NuGetToolsPath>
<PackagesConfig Condition=" '$(OS)' != 'Windows_NT' ">packages.config</PackagesConfig>
<PackagesDir Condition=" '$(OS)' != 'Windows_NT'">$(SolutionDir)packages</PackagesDir>

<!-- NuGet command -->
<NuGetExePath>$(NuGetToolsPath)\nuget.exe</NuGetExePath>
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>

<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

<!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
<PackageSources>""</PackageSources>

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition="$(RestorePackages) == ''">false</RestorePackages>

<!-- Property that enables building a package from a project -->
<BuildPackage Condition="$(BuildPackage) == ''">false</BuildPackage>

<!-- Commands -->
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source $(PackageSources) -o "$(PackagesDir)"</RestoreCommand>
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="!Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
Condition=" '$(OS)' != 'Windows_NT' " />

<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true"
Condition=" '$(OS)' == 'Windows_NT' " />
</Target>
</Project>
40 changes: 40 additions & 0 deletions README.txt
@@ -0,0 +1,40 @@
A lightweight wrapper around ADO.NET that allows you to map SQL statements to objects.

This is based around WebMatrix.Data.StronglyTyped
(https://github.com/JeremySkinner/WebMatrix.Data.StronglyTyped)
but without the dependency on WebMatrix.Data

Note that this requires NuGet Package Restore in order to use.

Examples:

[Table("Users")]
public class User {
public int Id { get; set; }
public string Name { get; set; }
}

// Map query into a list of User objects.
using(var db = Connection.Open("ConnectionStringName")) {
var results = db.Query<User>("select * from Users").ToList();
}

//Find record by PK
using (var db = Connection.Open("ConnectionStringName")) {
//Assumes PK property is called ID, but this can be overriden
User result = db.FindById<User>(100);
}

//Insert record
using(var db = Connection.Open("ConnectionStringName")) {
db.Insert(new User { Name = "Foo" });
}

// Update record
using (var db = Connection.Open("ConnectionStringName")) {
var user = new User { Name = "Foo" };
db.Insert(user);

user.Name = "Bar";
db.Update(user);
}
26 changes: 26 additions & 0 deletions SimpleQuery.sln
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleQuery", "src\SimpleQuery\SimpleQuery.csproj", "{F63604CB-832F-465F-9FE5-05766EEF1FC6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleQuery.Tests", "src\SimpleQuery.Tests\SimpleQuery.Tests.csproj", "{04BEEC17-023B-4EFA-8B75-66F7DC8FB5EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F63604CB-832F-465F-9FE5-05766EEF1FC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F63604CB-832F-465F-9FE5-05766EEF1FC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F63604CB-832F-465F-9FE5-05766EEF1FC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F63604CB-832F-465F-9FE5-05766EEF1FC6}.Release|Any CPU.Build.0 = Release|Any CPU
{04BEEC17-023B-4EFA-8B75-66F7DC8FB5EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04BEEC17-023B-4EFA-8B75-66F7DC8FB5EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04BEEC17-023B-4EFA-8B75-66F7DC8FB5EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04BEEC17-023B-4EFA-8B75-66F7DC8FB5EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions src/SimpleQuery.Tests/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=Test.sdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
</configuration>
35 changes: 35 additions & 0 deletions src/SimpleQuery.Tests/BaseTest.cs
@@ -0,0 +1,35 @@
using System.Configuration;
using System.Data.SqlServerCe;
using System.IO;
using NUnit.Framework;

namespace SimpleQuery.Tests {
public abstract class BaseTest {
[TestFixtureSetUp]
public void TestFixtureSetup() {
// Initialize the database.

if (File.Exists("Test.sdf")) {
File.Delete("Test.sdf");
}

using (var engine = new SqlCeEngine(ConfigurationManager.ConnectionStrings["Test"].ConnectionString)) {
engine.CreateDatabase();
}

using (var conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString)) {
var cmd = conn.CreateCommand();
conn.Open();

cmd.CommandText = "create table Users (Id int identity, Name nvarchar(250))";
cmd.ExecuteNonQuery();

cmd.CommandText = "create table ManualIdUser (Id int, Name nvarchar(250))";
cmd.ExecuteNonQuery();

cmd.CommandText = "create table CompositeKeyUser (Id int not null, Id2 nvarchar(250) not null, Name nvarchar(250), primary key (Id, Id2)) ";
cmd.ExecuteNonQuery();
}
}
}
}
125 changes: 125 additions & 0 deletions src/SimpleQuery.Tests/ExecuteTests.cs
@@ -0,0 +1,125 @@
namespace SimpleQuery.Tests {
using System;
using System.Data.SqlServerCe;
using System.IO;
using NUnit.Framework;
using System.Linq;
using Should;

[TestFixture]
public class ExecuteTests : BaseTest {

[SetUp]
public void Setup() {
using (var db = Connection.Open("Test")) {
db.Log = Console.Out;
db.Execute("delete from Users");
}
}

[Test]
public void Inserts_record() {
using(var db = Connection.Open("Test")) {
db.Log = Console.Out;

db.Insert(new User { Name = "Foo" });

var result = db.Query<User>("select * from users").Single();
result.Id.ShouldEqual(1);
result.Name.ShouldEqual("Foo");
}
}
[Test]
public void Inserts_record_with_aliased_column() {
using(var db = Connection.Open("Test")) {
db.Log = Console.Out;

db.Insert(new UserWithAliasedProperty { OtherName = "FooAliased" });

var result = db.Query<User>("select * from users").Single();
result.Name.ShouldEqual("FooAliased");
}
}

[Test]
public void Updates_record() {
using (var db = Connection.Open("Test")) {
db.Log = Console.Out;

var user = new User { Name = "Foo" };
db.Insert(user);
user.Name = "Bar";
db.Update(user);

var result = db.Query<User>("select * from users").Single();
result.Name.ShouldEqual("Bar");
}
}


[Test]
public void Inserts_with_store_generated_id() {
using(var db = Connection.Open("Test")) {
db.Log = Console.Out;

var user = new User{ Name = "Foo" };
db.Insert(user);

user.Id.ShouldNotEqual(0);
}
}

[Test]
public void Inserts_with_store_generated_id_when_id_is_implicit() {
using (var db = Connection.Open("Test")) {
db.Log = Console.Out;

var user = new UserWithImplicitId { Name = "Foo" };
db.Insert(user);

user.Id.ShouldNotEqual(0);
}
}

[Test]
public void Saves_with_non_generated_key() {
using (var db = Connection.Open("Test")) {
db.Log = Console.Out;
var user = new ManualIdUser {Id = 5, Name = "foo"};
db.Insert(user);
user.Id.ShouldEqual(5);

var result = db.FindById<ManualIdUser>(5);
result.Id.ShouldEqual(5);
}
}

public class ManualIdUser {
[Key(Generated = false)]
public int Id { get; set; }
public string Name { get; set; }
}


[Table("Users")]
public class User {
[Key]
public int Id { get; set; }
public string Name { get; set; }
}

[Table("Users")]
public class UserWithImplicitId {
public int Id { get; set; }
public string Name { get; set; }
}

[Table("Users")]
public class UserWithAliasedProperty {
public int Id { get; set; }
[Column("Name")]
public string OtherName { get; set; }
}
}

}
36 changes: 36 additions & 0 deletions src/SimpleQuery.Tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SimpleQuery.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimpleQuery.Tests")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c9a77f96-9302-4a8d-ad66-e13d2f63c5b5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit abbf788

Please sign in to comment.