Skip to content

Commit b7a5571

Browse files
committed
Cleanup and renaming PKs to CompositePK
1 parent f5876dd commit b7a5571

File tree

3 files changed

+52
-54
lines changed

3 files changed

+52
-54
lines changed

src/SQLite.Net/SQLiteConnection.cs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ public int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None)
378378

379379
if (map.HasCompositePK)
380380
{
381-
var PKs = mapColumns.Where(c => c.IsPK).ToList();
381+
var compositePK = mapColumns.Where(c => c.IsPK).ToList();
382382

383383
var decls = mapColumns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings, map.HasCompositePK));
384384
var decl = string.Join(",\n", decls.ToArray());
385385
query.Append(decl).Append(",\n");
386-
query.Append("primary key (").Append(string.Join(",", PKs.Select(pk => pk.Name))).Append(")");
386+
query.Append("primary key (").Append(string.Join(",", compositePK.Select(pk => pk.Name))).Append(")");
387387
query.Append(")");
388388
}
389389
else
@@ -547,8 +547,6 @@ private void MigrateTable(TableMapping map)
547547

548548
var toBeAdded = new List<TableMapping.Column>();
549549

550-
var PKscount = map.Columns.Where(c => c.IsPK).Count();
551-
552550
foreach (var p in map.Columns)
553551
{
554552
var found = false;
@@ -825,17 +823,17 @@ public T Get<T>(object pk) where T : class
825823
var map = GetMapping(typeof (T));
826824
if (map.HasCompositePK)
827825
{
828-
IDictionary<string, object> PKs = pk as Dictionary<string, object>;
829-
if (PKs == null)
826+
IDictionary<string, object> compositePK = pk as Dictionary<string, object>;
827+
if (compositePK == null)
830828
{
831829
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
832830
}
833-
var pks = map.PKs;
834-
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
831+
var cpk = map.CompositePK;
832+
if (compositePK.Keys.Intersect(cpk.Select(p => p.Name)).Count() < cpk.Length)
835833
{
836-
throw new NotSupportedException("Cannot get from " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
834+
throw new NotSupportedException("Cannot get from " + map.TableName + ": CompositePK mismatch. Make sure PK names are valid.");
837835
}
838-
return Query<T>(map.GetByPrimaryKeySql, PKs.Values.ToArray()).First();
836+
return Query<T>(map.GetByPrimaryKeySql, compositePK.Values.ToArray()).First();
839837
}
840838
else
841839
{
@@ -878,17 +876,17 @@ public T Find<T>(object pk) where T : class
878876
var map = GetMapping(typeof(T));
879877
if (map.HasCompositePK)
880878
{
881-
IDictionary<string, object> PKs = pk as Dictionary<string, object>;
882-
if (PKs == null)
879+
IDictionary<string, object> compositePK = pk as Dictionary<string, object>;
880+
if (compositePK == null)
883881
{
884882
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
885883
}
886-
var pks = map.PKs;
887-
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
884+
var cpk = map.CompositePK;
885+
if (compositePK.Keys.Intersect(cpk.Select(p => p.Name)).Count() < cpk.Length)
888886
{
889-
throw new NotSupportedException("Cannot find in " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
887+
throw new NotSupportedException("Cannot find in " + map.TableName + ": CompositePK mismatch. Make sure PK names are valid.");
890888
}
891-
return Query<T>(map.GetByPrimaryKeySql, PKs.Values.ToArray()).FirstOrDefault();
889+
return Query<T>(map.GetByPrimaryKeySql, compositePK.Values.ToArray()).FirstOrDefault();
892890
}
893891
else
894892
{
@@ -936,17 +934,17 @@ public object Find(object pk, TableMapping map)
936934
{
937935
if (map.HasCompositePK)
938936
{
939-
IDictionary<string, object> PKs = pk as Dictionary<string, object>;
940-
if (PKs == null)
937+
IDictionary<string, object> compositePK = pk as Dictionary<string, object>;
938+
if (compositePK == null)
941939
{
942940
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
943941
}
944-
var pks = map.PKs;
945-
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
942+
var cpk = map.CompositePK;
943+
if (compositePK.Keys.Intersect(cpk.Select(p => p.Name)).Count() < cpk.Length)
946944
{
947-
throw new NotSupportedException("Cannot find in " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
945+
throw new NotSupportedException("Cannot find in " + map.TableName + ": CompositePK mismatch. Make sure PK names are valid.");
948946
}
949-
return Query(map, map.GetByPrimaryKeySql, PKs.Values.ToArray()).FirstOrDefault();
947+
return Query(map, map.GetByPrimaryKeySql, compositePK.Values.ToArray()).FirstOrDefault();
950948
}
951949
else
952950
{
@@ -1513,7 +1511,7 @@ public int Insert(object obj, string extra, Type objType)
15131511

15141512
if (map.HasCompositePK)
15151513
{
1516-
pk = map.PKs.FirstOrDefault(p => p.IsAutoGuid);
1514+
pk = map.CompositePK.FirstOrDefault(p => p.IsAutoGuid);
15171515
}
15181516
else
15191517
{
@@ -1635,19 +1633,19 @@ public int Update(object obj, Type objType)
16351633

16361634
if (map.HasCompositePK)
16371635
{
1638-
var pks = map.PKs;
1636+
var compositePK = map.CompositePK;
16391637
var cols = from p in map.Columns
1640-
where !pks.Any(pk => pk == p)
1638+
where !compositePK.Any(pk => pk == p)
16411639
select p;
16421640

16431641
var pslist = (from c in cols
16441642
select c.GetValue(obj)).ToList();
16451643

1646-
pslist.AddRange(pks.Select(pk => pk.GetValue(obj)));
1644+
pslist.AddRange(compositePK.Select(pk => pk.GetValue(obj)));
16471645

16481646
q = string.Format("update \"{0}\" set {1} where {2}", map.TableName,
16491647
string.Join(",", (from c in cols
1650-
select "\"" + c.Name + "\" = ? ").ToArray()), string.Join(" and ", pks.Select(pk => "\"" + pk.Name + "\" = ? ")));
1648+
select "\"" + c.Name + "\" = ? ").ToArray()), string.Join(" and ", compositePK.Select(pk => "\"" + pk.Name + "\" = ? ")));
16511649

16521650
ps = pslist.ToArray();
16531651
}
@@ -1747,9 +1745,9 @@ public int Delete(object objectToDelete)
17471745

17481746
if (map.HasCompositePK)
17491747
{
1750-
var pks = map.PKs;
1751-
q = string.Format("delete from \"{0}\" where {1}", map.TableName, string.Join(" and ", pks.Select(pk => "\"" + pk.Name + "\" = ? ")));
1752-
ps = (from pk in pks
1748+
var compositePK = map.CompositePK;
1749+
q = string.Format("delete from \"{0}\" where {1}", map.TableName, string.Join(" and ", compositePK.Select(pk => "\"" + pk.Name + "\" = ? ")));
1750+
ps = (from pk in compositePK
17531751
select pk.GetValue(objectToDelete)).ToArray();
17541752
}
17551753
else
@@ -1785,18 +1783,18 @@ public int Delete<T>(object primaryKey)
17851783

17861784
if (map.HasCompositePK)
17871785
{
1788-
var pks = map.PKs;
1789-
IDictionary<string, object> PKs = primaryKey as Dictionary<string, object>;
1790-
if (PKs == null)
1786+
var cpk = map.CompositePK;
1787+
IDictionary<string, object> compositePK = primaryKey as Dictionary<string, object>;
1788+
if (compositePK == null)
17911789
{
17921790
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
17931791
}
1794-
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
1792+
if (compositePK.Keys.Intersect(cpk.Select(p => p.Name)).Count() < cpk.Length)
17951793
{
1796-
throw new NotSupportedException("Cannot delete " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
1794+
throw new NotSupportedException("Cannot delete " + map.TableName + ": CompositePK mismatch. Make sure PK names are valid.");
17971795
}
1798-
var q = string.Format("delete from \"{0}\" where {1}", map.TableName, string.Join(" and ", PKs.Keys.Select(pk => "\"" + pk + "\" = ? ")));
1799-
var ps = (from pk in PKs.Values
1796+
var q = string.Format("delete from \"{0}\" where {1}", map.TableName, string.Join(" and ", compositePK.Keys.Select(pk => "\"" + pk + "\" = ? ")));
1797+
var ps = (from pk in compositePK.Values
18001798
select pk).ToArray();
18011799
return Execute(q, ps);
18021800
}

src/SQLite.Net/TableMapping.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags
5858
}
5959
}
6060
Columns = cols.ToArray();
61-
PKs = Columns.Where(col => col.IsPK).ToArray();
61+
CompositePK = Columns.Where(col => col.IsPK).ToArray();
6262

63-
if (PKs.Length > 1)
63+
if (CompositePK.Length > 1)
6464
{
6565
HasCompositePK = true;
6666
}
@@ -83,14 +83,14 @@ public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags
8383
HasAutoIncPK = _autoPk != null;
8484
}
8585

86-
if (PKs.Length > 1)
86+
if (CompositePK.Length > 1)
8787
{
88-
string pksString = string.Join(" and ", PKs.Select(pk => "\"" + pk.Name + "\" = ? "));
89-
GetByPrimaryKeySql = string.Format("select * from \"{0}\" where {1}", TableName, pksString);
88+
string compositePKString = string.Join(" and ", CompositePK.Select(pk => "\"" + pk.Name + "\" = ? "));
89+
GetByPrimaryKeySql = string.Format("select * from \"{0}\" where {1}", TableName, compositePKString);
9090
}
91-
else if (PKs.Length == 1)
91+
else if (PK != null)
9292
{
93-
string pkString = PKs.FirstOrDefault().Name;
93+
string pkString = PK.Name;
9494
GetByPrimaryKeySql = string.Format("select * from \"{0}\" where \"{1}\" = ?", TableName, pkString);
9595
}
9696
else
@@ -116,7 +116,7 @@ public Column PK
116116
{
117117
if (HasCompositePK)
118118
{
119-
throw new NotSupportedException("Table has a composite primary key. Use PKs property instead.");
119+
throw new NotSupportedException("Table has a composite primary key. Use CompositePK property instead.");
120120
}
121121
else
122122
{
@@ -126,7 +126,7 @@ public Column PK
126126
}
127127

128128
[PublicAPI]
129-
public Column[] PKs { get; private set; }
129+
public Column[] CompositePK { get; private set; }
130130

131131
[PublicAPI]
132132
public string GetByPrimaryKeySql { get; private set; }

tests/DeleteTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ public void DeletePKNoneComposite()
164164
{
165165
var db = CreateDb();
166166

167-
var pks = new Dictionary<string, object>();
168-
pks.Add("Id", 348597);
169-
pks.Add("TestIndex", 348598);
167+
var compositePK = new Dictionary<string, object>();
168+
compositePK.Add("Id", 348597);
169+
compositePK.Add("TestIndex", 348598);
170170

171-
var r = db.Delete<TestTableCompositeKey>(pks);
171+
var r = db.Delete<TestTableCompositeKey>(compositePK);
172172

173173
Assert.AreEqual(0, r);
174174
Assert.AreEqual(Count, db.Table<TestTableCompositeKey>().Count());
@@ -179,11 +179,11 @@ public void DeletePKOneComposite()
179179
{
180180
var db = CreateDb();
181181

182-
var pks = new Dictionary<string, object>();
183-
pks.Add("Id", 1);
184-
pks.Add("TestIndex", 2);
182+
var compositePK = new Dictionary<string, object>();
183+
compositePK.Add("Id", 1);
184+
compositePK.Add("TestIndex", 2);
185185

186-
var r = db.Delete<TestTableCompositeKey>(pks);
186+
var r = db.Delete<TestTableCompositeKey>(compositePK);
187187

188188
Assert.AreEqual(1, r);
189189
Assert.AreEqual(Count - 1, db.Table<TestTableCompositeKey>().Count());

0 commit comments

Comments
 (0)