Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for working with interfaces for create, Insert, and get #692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,368 changes: 4,194 additions & 4,174 deletions src/SQLite.cs

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions src/SQLiteAsync.cs
Original file line number Diff line number Diff line change
@@ -257,7 +257,7 @@ public Task EnableLoadExtensionAsync (bool enabled)
/// Whether the table was created or migrated.
/// </returns>
public Task<CreateTableResult> CreateTableAsync<T> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T : class
{
return WriteAsync (conn => conn.CreateTable<T> (createFlags));
}
@@ -288,8 +288,8 @@ public Task<CreateTableResult> CreateTableAsync (Type ty, CreateFlags createFlag
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T : class
where T2 : class
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2));
}
@@ -304,9 +304,9 @@ public Task<CreateTablesResult> CreateTablesAsync<T, T2> (CreateFlags createFlag
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T : class
where T2 : class
where T3 : class
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3));
}
@@ -321,10 +321,10 @@ public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3> (CreateFlags create
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new()
where T : class
where T2 : class
where T3 : class
where T4 : class
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3), typeof (T4));
}
@@ -339,11 +339,11 @@ public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4> (CreateFlags cr
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4, T5> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new()
where T5 : new()
where T : class
where T2 : class
where T3 : class
where T4 : class
where T5 : class
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3), typeof (T4), typeof (T5));
}
@@ -366,7 +366,7 @@ public Task<CreateTablesResult> CreateTablesAsync (CreateFlags createFlags = Cre
/// Executes a "drop table" on the database. This is non-recoverable.
/// </summary>
public Task<int> DropTableAsync<T> ()
where T : new()
where T : class
{
return WriteAsync (conn => conn.DropTable<T> ());
}
@@ -701,7 +701,7 @@ public Task<int> DeleteAllAsync (TableMapping map)
/// if the object is not found.
/// </returns>
public Task<T> GetAsync<T> (object pk)
where T : new()
where T : class
{
return ReadAsync (conn => conn.Get<T> (pk));
}
@@ -738,7 +738,7 @@ public Task<object> GetAsync (object pk, TableMapping map)
/// if the object is not found.
/// </returns>
public Task<T> GetAsync<T> (Expression<Func<T, bool>> predicate)
where T : new()
where T : class
{
return ReadAsync (conn => conn.Get<T> (predicate));
}
@@ -756,7 +756,7 @@ public Task<T> GetAsync<T> (Expression<Func<T, bool>> predicate)
/// if the object is not found.
/// </returns>
public Task<T> FindAsync<T> (object pk)
where T : new()
where T : class
{
return ReadAsync (conn => conn.Find<T> (pk));
}
@@ -793,7 +793,7 @@ public Task<object> FindAsync (object pk, TableMapping map)
/// if the object is not found.
/// </returns>
public Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate)
where T : new()
where T : class
{
return ReadAsync (conn => conn.Find<T> (predicate));
}
@@ -813,7 +813,7 @@ public Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate)
/// if the object is not found.
/// </returns>
public Task<T> FindWithQueryAsync<T> (string query, params object[] args)
where T : new()
where T : class
{
return ReadAsync (conn => conn.FindWithQuery<T> (query, args));
}
@@ -869,7 +869,7 @@ public Task<TableMapping> GetMappingAsync (Type type, CreateFlags createFlags =
/// methods to set and get properties of objects.
/// </returns>
public Task<TableMapping> GetMappingAsync<T> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T : class
{
return ReadAsync (conn => conn.GetMapping<T> (createFlags));
}
@@ -996,7 +996,7 @@ public Task RunInTransactionAsync (Action<SQLiteConnection> action)
/// queries into native SQL.
/// </returns>
public AsyncTableQuery<T> Table<T> ()
where T : new()
where T : class
{
//
// This isn't async as the underlying connection doesn't go out to the database
@@ -1046,7 +1046,7 @@ public Task<T> ExecuteScalarAsync<T> (string query, params object[] args)
/// An enumerable with one result for each row returned by the query.
/// </returns>
public Task<List<T>> QueryAsync<T> (string query, params object[] args)
where T : new()
where T : class
{
return ReadAsync (conn => conn.Query<T> (query, args));
}
@@ -1094,7 +1094,7 @@ public Task<List<object>> QueryAsync (TableMapping map, string query, params obj
/// connection must remain open for the lifetime of the enumerator.
/// </returns>
public Task<IEnumerable<T>> DeferredQueryAsync<T> (string query, params object[] args)
where T : new()
where T : class
{
return ReadAsync (conn => (IEnumerable<T>)conn.DeferredQuery<T> (query, args).ToList ());
}
@@ -1136,7 +1136,7 @@ public Task<IEnumerable<object>> DeferredQueryAsync (TableMapping map, string qu
/// Query to an asynchronous database connection.
/// </summary>
public class AsyncTableQuery<T>
where T : new()
where T : class
{
TableQuery<T> _innerQuery;

247 changes: 151 additions & 96 deletions tests/CreateTableTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Linq;

#if !__IOS__
using Autofac;
using Autofac.Core;
#endif
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
@@ -13,39 +16,76 @@

namespace SQLite.Tests
{
[TestFixture]
public class CreateTableTest
{
[TestFixture]
public class CreateTableTest
{
#if !__IOS__
private IContainer _container;
#endif
private DefaultContractResolver _contractResolver;

class NoPropObject
{
}
{
}

[Test, ExpectedException]
public void CreateTypeWithNoProps ()
[SetUp]
public void Setup ()
{
var db = new TestDb ();
#if !__IOS__
var cb = new Autofac.ContainerBuilder ();
cb.RegisterType<Account> ().As<IAccount> ();

db.CreateTable<NoPropObject> ();
}
_container = cb.Build ();

[Test]
public void CreateThem ()
{
var db = new TestDb ();

db.CreateTable<Product> ();
db.CreateTable<Order> ();
db.CreateTable<OrderLine> ();
db.CreateTable<OrderHistory> ();

VerifyCreations(db);
_contractResolver = new DefaultContractResolver {
CanCreate = type => _container.IsRegistered (type),
Create = (type, args) => _container.IsRegistered(type) ? _container.Resolve (type) : Activator.CreateInstance(type, args)
};
#else
_contractResolver = new DefaultContractResolver();
#endif
}

[Test]
[TearDown]
public void TearDown ()
{

}

[Test, ExpectedException]
public void CreateTypeWithNoProps ()
{
var db = new TestDb {
Resolver = _contractResolver
};

db.CreateTable<NoPropObject> ();
}

[Test]
public void CreateThem ()
{
var db = new TestDb {
Resolver = _contractResolver
};

db.CreateTable<IAccount> ();
db.CreateTable<Product> ();
db.CreateTable<Order> ();
db.CreateTable<OrderLine> ();
db.CreateTable<OrderHistory> ();

VerifyCreations(db);
}

[Test]
public void CreateAsPassedInTypes ()
{
var db = new TestDb();
var db = new TestDb {
Resolver = _contractResolver
};

db.CreateTable(typeof(IAccount));
db.CreateTable(typeof(Product));
db.CreateTable(typeof(Order));
db.CreateTable(typeof(OrderLine));
@@ -54,22 +94,37 @@ public void CreateAsPassedInTypes ()
VerifyCreations(db);
}

[Test]
public void CreateTwice ()
{
var db = new TestDb ();

db.CreateTable<Product> ();
db.CreateTable<OrderLine> ();
db.CreateTable<Order> ();
db.CreateTable<OrderLine> ();
db.CreateTable<OrderHistory> ();

VerifyCreations(db);
}
[Test]
public void CreateTwice ()
{
var db = new TestDb {
Resolver = _contractResolver
};

db.CreateTable<IAccount> ();
db.CreateTable<Product> ();
db.CreateTable<OrderLine> ();
db.CreateTable<Order> ();
db.CreateTable<OrderLine> ();
db.CreateTable<OrderHistory> ();

VerifyCreations(db);
}

private static void VerifyCreations(TestDb db)
{
var account = db.GetMapping<IAccount> ();
Assert.AreEqual(3, account.Columns.Length);

var a = new Account() {
Name = $"Account Created By Unit Test On {DateTime.Now.ToLongDateString()}",
CreatedOn = DateTime.Now
};

db.Insert (a);
var ao = db.Table<IAccount>().First();
Assert.AreEqual(ao.Name, a.Name);

var orderLine = db.GetMapping(typeof(OrderLine));
Assert.AreEqual(6, orderLine.Columns.Length);

@@ -82,72 +137,72 @@ private static void VerifyCreations(TestDb db)
Assert.AreEqual(lo.Id, l.Id);
}

class Issue115_MyObject
{
[PrimaryKey]
public string UniqueId { get; set; }
public byte OtherValue { get; set; }
}
class Issue115_MyObject
{
[PrimaryKey]
public string UniqueId { get; set; }
public byte OtherValue { get; set; }
}

[Test]
public void Issue115_MissingPrimaryKey ()
{
using (var conn = new TestDb ()) {

conn.CreateTable<Issue115_MyObject> ();
conn.InsertAll (from i in Enumerable.Range (0, 10) select new Issue115_MyObject {
UniqueId = i.ToString (),
OtherValue = (byte)(i * 10),
});

var query = conn.Table<Issue115_MyObject> ();
foreach (var itm in query) {
itm.OtherValue++;
Assert.AreEqual (1, conn.Update (itm, typeof(Issue115_MyObject)));
}
}
}
[Test]
public void Issue115_MissingPrimaryKey ()
{
using (var conn = new TestDb ()) {

conn.CreateTable<Issue115_MyObject> ();
conn.InsertAll (from i in Enumerable.Range (0, 10) select new Issue115_MyObject {
UniqueId = i.ToString (),
OtherValue = (byte)(i * 10),
});

var query = conn.Table<Issue115_MyObject> ();
foreach (var itm in query) {
itm.OtherValue++;
Assert.AreEqual (1, conn.Update (itm, typeof(Issue115_MyObject)));
}
}
}

[Table("WantsNoRowId", WithoutRowId = true)]
class WantsNoRowId
{
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
}
[Table("WantsNoRowId", WithoutRowId = true)]
class WantsNoRowId
{
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
}

[Table("sqlite_master")]
class SqliteMaster
{
[Column ("type")]
public string Type { get; set; }
[Table("sqlite_master")]
class SqliteMaster
{
[Column ("type")]
public string Type { get; set; }

[Column ("name")]
public string Name { get; set; }
[Column ("name")]
public string Name { get; set; }

[Column ("tbl_name")]
public string TableName { get; set; }
[Column ("tbl_name")]
public string TableName { get; set; }

[Column ("rootpage")]
public int RootPage { get; set; }
[Column ("rootpage")]
public int RootPage { get; set; }

[Column ("sql")]
public string Sql { get; set; }
}
[Column ("sql")]
public string Sql { get; set; }
}

[Test]
public void WithoutRowId ()
{
using(var conn = new TestDb ())
{
conn.CreateTable<OrderLine> ();
var info = conn.Table<SqliteMaster>().Where(m => m.TableName=="OrderLine").First ();
Assert.That (!info.Sql.Contains ("without rowid"));
conn.CreateTable<WantsNoRowId> ();
info = conn.Table<SqliteMaster>().Where(m => m.TableName=="WantsNoRowId").First ();
Assert.That (info.Sql.Contains ("without rowid"));
}
}
[Test]
public void WithoutRowId ()
{
using(var conn = new TestDb ())
{
conn.CreateTable<OrderLine> ();
var info = conn.Table<SqliteMaster>().Where(m => m.TableName=="OrderLine").First ();
Assert.That (!info.Sql.Contains ("without rowid"));
conn.CreateTable<WantsNoRowId> ();
info = conn.Table<SqliteMaster>().Where(m => m.TableName=="WantsNoRowId").First ();
Assert.That (info.Sql.Contains ("without rowid"));
}
}
}
}
2 changes: 1 addition & 1 deletion tests/LinqTest.cs
Original file line number Diff line number Diff line change
@@ -275,7 +275,7 @@ class Entity : IEntity
public string Value { get; set; }
}

static T GetEntity<T> (TestDb db, int id) where T : IEntity, new ()
static T GetEntity<T> (TestDb db, int id) where T : class, IEntity
{
return db.Table<T> ().FirstOrDefault (x => x.Id == id);
}
11 changes: 11 additions & 0 deletions tests/SQLite.Tests.csproj
Original file line number Diff line number Diff line change
@@ -30,13 +30,24 @@
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
134 changes: 77 additions & 57 deletions tests/TestDb.cs
Original file line number Diff line number Diff line change
@@ -4,77 +4,97 @@
#if NETFX_CORE
class DescriptionAttribute : Attribute
{
public DescriptionAttribute (string desc)
{
}
public DescriptionAttribute (string desc)
{
}
}
#endif

namespace SQLite.Tests
{
public class Product
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[Table("Account")]
public interface IAccount
{
int Id { get; set; }
string Name { get; set; }
DateTime CreatedOn { get; set; }
}

public uint TotalSales { get; set; }
}
public class Order
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public DateTime PlacedTime { get; set; }
}
public class OrderHistory {
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public int OrderId { get; set; }
public DateTime Time { get; set; }
public string Comment { get; set; }
}
public class OrderLine
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public class Account : IAccount
{
#region Implementation of IAccount

public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }

#endregion
}


public class Product
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }

public uint TotalSales { get; set; }
}
public class Order
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public DateTime PlacedTime { get; set; }
}
public class OrderHistory {
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public int OrderId { get; set; }
public DateTime Time { get; set; }
public string Comment { get; set; }
}
public class OrderLine
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
[Indexed("IX_OrderProduct", 1)]
public int OrderId { get; set; }
public int OrderId { get; set; }
[Indexed("IX_OrderProduct", 2)]
public int ProductId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public OrderLineStatus Status { get; set; }
}
public enum OrderLineStatus {
Placed = 1,
Shipped = 100
}
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public OrderLineStatus Status { get; set; }
}
public enum OrderLineStatus {
Placed = 1,
Shipped = 100
}

public class TestDb : SQLiteConnection
{
public TestDb (bool storeDateTimeAsTicks = true) : base (TestPath.GetTempFileName (), storeDateTimeAsTicks)
{
Trace = true;
}
public class TestDb : SQLiteConnection
{
public TestDb (bool storeDateTimeAsTicks = true) : base (TestPath.GetTempFileName (), storeDateTimeAsTicks)
{
Trace = true;
}

public TestDb (string path, bool storeDateTimeAsTicks = true) : base (path, storeDateTimeAsTicks)
{
Trace = true;
}
}
public TestDb (string path, bool storeDateTimeAsTicks = true) : base (path, storeDateTimeAsTicks)
{
Trace = true;
}
}

public class TestPath
{
public static string GetTempFileName ()
{
public class TestPath
{
public static string GetTempFileName ()
{
#if NETFX_CORE
var name = Guid.NewGuid () + ".sqlite";
return Path.Combine (Windows.Storage.ApplicationData.Current.LocalFolder.Path, name);
var name = Guid.NewGuid () + ".sqlite";
return Path.Combine (Windows.Storage.ApplicationData.Current.LocalFolder.Path, name);
#else
return Path.GetTempFileName ();
return Path.GetTempFileName ();
#endif
}
}
}
}
}

1 change: 1 addition & 0 deletions tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="4.6.2" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages>