Skip to content

Commit f5876dd

Browse files
committed
Merge branch 'master' of https://github.com/oysteinkrog/SQLite.Net-PCL into feature-composite-primary-keys
2 parents 75d3cfb + db0116b commit f5876dd

File tree

8 files changed

+252
-16
lines changed

8 files changed

+252
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ TestResults
1212
/packages
1313
/SQLite.Net.sln.ide
1414
/.vs
15+
/nuget/SQLite.Net*

src/SQLite.Net/Orm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ internal static IEnumerable<IndexedAttribute> GetIndices(MemberInfo p)
169169
[CanBeNull]
170170
internal static int? MaxStringLength(PropertyInfo p)
171171
{
172-
foreach (var attribute in p.CustomAttributes.Where(a => a.AttributeType == typeof (MaxLengthAttribute)))
172+
foreach (var attribute in p.GetCustomAttributes<MaxLengthAttribute>())
173173
{
174-
return (int) attribute.ConstructorArguments[0].Value;
174+
return attribute.Value;
175175
}
176176
return null;
177177
}

src/SQLite.Net/TableMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags
4141
{
4242
MappedType = type;
4343

44-
var tableAttr = type.GetTypeInfo().CustomAttributes.FirstOrDefault(data => data.AttributeType == typeof (TableAttribute));
44+
var tableAttr = type.GetTypeInfo().GetCustomAttributes<TableAttribute>().FirstOrDefault();
4545

46-
TableName = tableAttr != null ? (string) tableAttr.ConstructorArguments.FirstOrDefault().Value : MappedType.Name;
46+
TableName = tableAttr != null ? tableAttr.Name : MappedType.Name;
4747

4848
var props = properties;
4949

src/SQLite.Net/TableQuery.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,22 @@ public int Delete([NotNull] Expression<Func<T, bool>> predExpr)
142142
{
143143
throw new NotSupportedException("Must be a predicate");
144144
}
145+
if (_limit != null)
146+
{
147+
//SQLite provides a limit to deletions so this would be possible to implement in the future
148+
//You would need to take care that the correct order was being applied.
149+
throw new NotSupportedException("Cannot delete if a limit has been specified");
150+
}
151+
if (_offset != null)
152+
{
153+
throw new NotSupportedException("Cannot delete if an offset has been specified");
154+
}
145155
var lambda = (LambdaExpression) predExpr;
146156
var pred = lambda.Body;
157+
if (_where != null)
158+
{
159+
pred = Expression.AndAlso(pred, _where);
160+
}
147161
var args = new List<object>();
148162
var w = CompileExpr(pred, args);
149163
var cmdText = "delete from \"" + Table.TableName + "\"";
@@ -552,7 +566,7 @@ private string CompileNullBinaryExpression(BinaryExpression expression, CompileR
552566
expression.NodeType);
553567
}
554568

555-
private string GetSqlName(Expression expr)
569+
private string GetSqlName(BinaryExpression expr)
556570
{
557571
var n = expr.NodeType;
558572
if (n == ExpressionType.GreaterThan)
@@ -595,6 +609,20 @@ private string GetSqlName(Expression expr)
595609
{
596610
return "!=";
597611
}
612+
if (n == ExpressionType.Add)
613+
{
614+
if (expr.Left.Type == typeof(string))
615+
{
616+
return "||";
617+
}
618+
return "+";
619+
620+
}
621+
if (n == ExpressionType.Subtract)
622+
{
623+
return "-";
624+
}
625+
598626
throw new NotSupportedException("Cannot get SQL for: " + n);
599627
}
600628

tests/ArithmeticTest.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using SQLite.Net.Attributes;
6+
7+
#if __WIN32__
8+
using SQLitePlatformTest = SQLite.Net.Platform.Win32.SQLitePlatformWin32;
9+
#elif WINDOWS_PHONE
10+
using SQLitePlatformTest = SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8;
11+
#elif __WINRT__
12+
using SQLitePlatformTest = SQLite.Net.Platform.WinRT.SQLitePlatformWinRT;
13+
#elif __IOS__
14+
using SQLitePlatformTest = SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS;
15+
#elif __ANDROID__
16+
using SQLitePlatformTest = SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid;
17+
#else
18+
using SQLitePlatformTest = SQLite.Net.Platform.Generic.SQLitePlatformGeneric;
19+
#endif
20+
21+
22+
namespace SQLite.Net.Tests
23+
{
24+
[TestFixture]
25+
internal class ArithmeticTest
26+
{
27+
public abstract class TestObjBase<T>
28+
{
29+
[AutoIncrement, PrimaryKey]
30+
public int Id { get; set; }
31+
32+
public T Data { get; set; }
33+
34+
}
35+
36+
public class TestObjString : TestObjBase<string>
37+
{
38+
}
39+
40+
public class TestObjInt : TestObjBase<int>
41+
{
42+
}
43+
44+
public class TestDb : SQLiteConnection
45+
{
46+
public TestDb(String path)
47+
: base(new SQLitePlatformTest(), path)
48+
{
49+
CreateTable<TestObjString>();
50+
CreateTable<TestObjInt>();
51+
}
52+
}
53+
54+
[Test]
55+
public void CanHaveAddInWhereClause()
56+
{
57+
int n = 20;
58+
IEnumerable<TestObjInt> cq = from i in Enumerable.Range(1, n)
59+
select new TestObjInt()
60+
{
61+
Data = i,
62+
};
63+
64+
var db = new TestDb(TestPath.GetTempFileName());
65+
db.InsertAll(cq);
66+
67+
TableQuery<TestObjInt> results = db.Table<TestObjInt>().Where(o => o.Data + 10 >= n);
68+
Assert.AreEqual(results.Count(), 11);
69+
Assert.AreEqual(results.OrderBy(o => o.Data).FirstOrDefault().Data, 10);
70+
}
71+
72+
[Test]
73+
public void CanHaveSubtractInWhereClause()
74+
{
75+
int n = 20;
76+
IEnumerable<TestObjInt> cq = from i in Enumerable.Range(1, n)
77+
select new TestObjInt()
78+
{
79+
Data = i,
80+
};
81+
82+
var db = new TestDb(TestPath.GetTempFileName());
83+
db.InsertAll(cq);
84+
85+
TableQuery<TestObjInt> results = db.Table<TestObjInt>().Where(o => o.Data - 10 >= 0);
86+
Assert.AreEqual(results.Count(), 11);
87+
Assert.AreEqual(results.OrderBy(o => o.Data).FirstOrDefault().Data, 10);
88+
}
89+
90+
[Test]
91+
public void AddForStringsMeansConcatenate()
92+
{
93+
int n = 20;
94+
IEnumerable<TestObjString> cq = from i in Enumerable.Range(1, n)
95+
select new TestObjString()
96+
{
97+
Data = i.ToString(),
98+
};
99+
100+
var db = new TestDb(TestPath.GetTempFileName());
101+
db.InsertAll(cq);
102+
103+
TableQuery<TestObjString> results = db.Table<TestObjString>().Where(o => o.Data + "1" == "11");
104+
105+
Assert.AreEqual(1, results.Count());
106+
Assert.AreEqual("1", results.OrderBy(o => o.Data).FirstOrDefault().Data);
107+
}
108+
}
109+
}

tests/CreateTableTest.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using NUnit.Framework;
34
using SQLite.Net.Attributes;
@@ -9,7 +10,7 @@ public class CreateTableTest
910
{
1011
private static void VerifyCreations(TestDb db)
1112
{
12-
TableMapping orderLine = db.GetMapping(typeof (OrderLine));
13+
TableMapping orderLine = db.GetMapping(typeof(OrderLine));
1314
Assert.AreEqual(6, orderLine.Columns.Length);
1415

1516
var l = new OrderLine
@@ -34,10 +35,10 @@ public void CreateAsPassedInTypes()
3435
{
3536
var db = new TestDb();
3637

37-
db.CreateTable(typeof (Product));
38-
db.CreateTable(typeof (Order));
39-
db.CreateTable(typeof (OrderLine));
40-
db.CreateTable(typeof (OrderHistory));
38+
db.CreateTable(typeof(Product));
39+
db.CreateTable(typeof(Order));
40+
db.CreateTable(typeof(OrderLine));
41+
db.CreateTable(typeof(OrderHistory));
4142

4243
VerifyCreations(db);
4344
}
@@ -76,19 +77,63 @@ public void Issue115_MissingPrimaryKey()
7677
{
7778
conn.CreateTable<Issue115_MyObject>();
7879
conn.InsertAll(from i in Enumerable.Range(0, 10)
79-
select new Issue115_MyObject
80-
{
81-
UniqueId = i.ToString(),
82-
OtherValue = (byte) (i*10),
83-
});
80+
select new Issue115_MyObject
81+
{
82+
UniqueId = i.ToString(),
83+
OtherValue = (byte)(i * 10),
84+
});
8485

8586
TableQuery<Issue115_MyObject> query = conn.Table<Issue115_MyObject>();
8687
foreach (Issue115_MyObject itm in query)
8788
{
8889
itm.OtherValue++;
89-
Assert.AreEqual(1, conn.Update(itm, typeof (Issue115_MyObject)));
90+
Assert.AreEqual(1, conn.Update(itm, typeof(Issue115_MyObject)));
9091
}
9192
}
9293
}
94+
95+
public class ObjWithMaxLength
96+
{
97+
[MaxLength(20)]
98+
public string Name { get; set; }
99+
}
100+
101+
[Test]
102+
public void CheckMaxLengthAttributesRespected()
103+
{
104+
var db = new TestDb();
105+
106+
db.CreateTable<ObjWithMaxLength>();
107+
108+
string creationString = db.ExecuteScalar<string>("select min(sql) from sqlite_master");
109+
Assert.That(creationString, Is.StringContaining("varchar(20)"));
110+
}
111+
112+
113+
public class TweetStringAttribute : MaxLengthAttribute
114+
{
115+
public TweetStringAttribute() : base(140)
116+
{
117+
}
118+
}
119+
public class Tweet
120+
{
121+
[TweetString]
122+
public string Message { get; set; }
123+
124+
125+
public string Sender { get; set; }
126+
}
127+
128+
[Test]
129+
public void CheckMaxLengthAttributesSubtypesRespected()
130+
{
131+
var db = new TestDb();
132+
133+
db.CreateTable<Tweet>();
134+
135+
string creationString = db.ExecuteScalar<string>("select min(sql) from sqlite_master");
136+
Assert.That(creationString, Is.StringContaining("varchar(140)"));
137+
}
93138
}
94139
}

tests/DeleteTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ public void DeleteAllWithPredicateHalf()
102102
Assert.AreEqual(1, db.Table<TestTable>().Count());
103103
}
104104

105+
[Test]
106+
public void DeleteWithWhereAndPredicate()
107+
{
108+
var db = CreateDb();
109+
var testString = "TestData";
110+
var first = db.Insert(new TestTable
111+
{
112+
Datum = 3,
113+
Test = testString
114+
115+
});
116+
var second = db.Insert(new TestTable
117+
{
118+
Datum = 4,
119+
Test = testString
120+
});
121+
122+
//Should only delete first
123+
var r = db.Table<TestTable>().Where(t => t.Datum == 3).Delete(t => t.Test == testString);
124+
125+
Assert.AreEqual(1, r);
126+
Assert.AreEqual(Count + 1, db.Table<TestTable>().Count());
127+
}
128+
105129
[Test]
106130
public void DeleteEntityOne()
107131
{

tests/MappingTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,34 @@ public void Issue86()
6363
Assert.AreEqual(69, ordered[0].Bar);
6464
Assert.AreEqual(42, ordered[1].Bar);
6565
}
66+
67+
private class PluralisedTableAttribute : TableAttribute
68+
{
69+
public PluralisedTableAttribute(string name) : base(name + "s")
70+
{
71+
}
72+
}
73+
74+
[PluralisedTable("Cat")]
75+
public class Cat
76+
{
77+
public string Breed { get; set; }
78+
}
79+
80+
[Test]
81+
public void CanUseSubtypeOfTableAttribute()
82+
{
83+
var db = new TestDb();
84+
db.CreateTable<Cat>();
85+
86+
db.Insert(new Cat()
87+
{
88+
Breed = "Siamese"
89+
});
90+
91+
int numCats = db.ExecuteScalar<int>("select count(*) from Cats");
92+
93+
Assert.That(numCats,Is.EqualTo(1), "The resulting num cats should be 1.");
94+
}
6695
}
6796
}

0 commit comments

Comments
 (0)