Skip to content
Permalink
Browse files

Add save documentation

  • Loading branch information...
pleb committed Apr 17, 2019
1 parent 0aaa885 commit ba752fb4115ce52c6980f569debe70aebe28b3d0
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
@@ -26,96 +25,96 @@ public void DeleteByPoco()

// Tell PetaPoco to insert it
DB.Insert(person);

// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);

// Tell PetaPoco to delete it
DB.Delete(person);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
count.ShouldBe(0);
}

[Fact]
public void DeleteByPrimaryKey()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, tell PetaPoco to delete a note with the id of 1
DB.Delete<Note>(note.Id);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteCustomWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, we'll tell PetaPoco how to delete the note
DB.Delete<Note>("WHERE [Id] = @Id", new { note.Id });

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteCustomSqlWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, we'll tell PetaPoco how to delete the note
var sql = new Sql();
sql.Where("[Id] = @Id", new { note.Id });
DB.Delete<Note>(sql);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteAdvanced()
{
@@ -124,17 +123,17 @@ public void DeleteAdvanced()

// Tell PetaPoco to insert it, but to the table SpecificPeople and not People
DB.Insert("SpecificPeople", person);

// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);

// Tell PetaPoco to delete it, but in the table SpecificPeople and not People
DB.Delete("SpecificPeople", "Id", person);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
count.ShouldBe(0);
}
}
}
@@ -1,13 +1,28 @@
using System;
using System.Data;
using System.Data.SqlClient;

namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
public class Note
{
public int Id { get; set; }

[DateTime2ConverterAttribute]
public DateTime CreatedOn { get; set; }

public string Text { get; set; }
}

public class DateTime2ConverterAttribute : ValueConverterAttribute
{
public override object ConvertToDb(object value) =>
new SqlParameter
{
DbType = DbType.DateTime2,
Value = value
};

public override object ConvertFromDb(object value) => value;
}
}
@@ -0,0 +1,68 @@
using System;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
using PetaPoco.Tests.Integration.Documentation.Pocos;
using Shouldly;
using Xunit;

namespace PetaPoco.Tests.Integration.Documentation
{
[Collection("MssqlTests")]
public class Save : BaseDatabase
{
public Save()
: base(new MssqlDBTestProvider())
{
PocoData.FlushCaches();
}

[Fact]
public void Save_Insert()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);

// They are the same
note.Id.ShouldBe(noteFromDb.Id);
note.Text.ShouldBe(noteFromDb.Text);
note.CreatedOn.Ticks.ShouldBe(noteFromDb.CreatedOn.Ticks);
}

[Fact]
public void Save_Update()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);

// Update the note
note.Text += " and this is my update";
DB.Save(note);

// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);

// The note text is the same
note.Text.ShouldBe(noteFromDb.Text);
note.Text.ShouldContain(" and this is my update");
}
}
}
@@ -9,6 +9,10 @@
<Copyright>Collaborating Platypus</Copyright>
<OutputType>Library</OutputType>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<Compile Remove="Databases\MSAccess\**" />
@@ -20,15 +20,15 @@ CREATE TABLE dbo.[People] (
[FullName] NVARCHAR(255),
[Age] BIGINT NOT NULL,
[Height] INT NOT NULL,
[Dob] DATETIME NULL
[Dob] DATETIME2 NULL
)

CREATE TABLE dbo.[Orders] (
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[PersonId] UNIQUEIDENTIFIER FOREIGN KEY REFERENCES dbo.[People](Id),
[PoNumber] NVARCHAR(15) NOT NULL,
[OrderStatus] INT NOT NULL,
[CreatedOn] DATETIME NOT NULL,
[CreatedOn] DATETIME2 NOT NULL,
[CreatedBy] NVARCHAR(255) NOT NULL
)

@@ -45,15 +45,15 @@ CREATE TABLE dbo.[SpecificPeople] (
[FullName] NVARCHAR(255),
[Age] BIGINT NOT NULL,
[Height] INT NOT NULL,
[Dob] DATETIME NULL
[Dob] DATETIME2 NULL
)

CREATE TABLE dbo.[SpecificOrders] (
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[PersonId] UNIQUEIDENTIFIER FOREIGN KEY REFERENCES dbo.[SpecificPeople](Id),
[PoNumber] NVARCHAR(15) NOT NULL,
[OrderStatus] INT NOT NULL,
[CreatedOn] DATETIME NOT NULL,
[CreatedOn] DATETIME2 NOT NULL,
[CreatedBy] NVARCHAR(255) NOT NULL
)

@@ -67,13 +67,13 @@ CREATE TABLE dbo.[SpecificOrderLines] (

CREATE TABLE dbo.[TransactionLogs] (
[Description] NTEXT,
[CreatedOn] DATETIME NOT NULL
[CreatedOn] DATETIME2 NOT NULL
)

CREATE TABLE dbo.[Note] (
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[Text] NTEXT NOT NULL,
[CreatedOn] DATETIME NOT NULL
[CreatedOn] DATETIME2 NOT NULL
)

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'store')

0 comments on commit ba752fb

Please sign in to comment.
You can’t perform that action at this time.