Skip to content

Commit 75d3cfb

Browse files
committed
Keep both PK and composite PK as a part of TableMapping public API.
1 parent b090ab2 commit 75d3cfb

File tree

5 files changed

+146
-86
lines changed

5 files changed

+146
-86
lines changed

src/SQLite.Net/Orm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ internal static class Orm
3737
public const string ImplicitIndexSuffix = "Id";
3838

3939
internal static string SqlDecl(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer,
40-
IDictionary<Type, string> extraTypeMappings, int primaryKeyCount = 0)
40+
IDictionary<Type, string> extraTypeMappings, bool hasCompositePK = false)
4141
{
4242
var decl = "\"" + p.Name + "\" " + SqlType(p, storeDateTimeAsTicks, serializer, extraTypeMappings) + " ";
4343

44-
if (p.IsPK && primaryKeyCount == 1)
44+
if (p.IsPK && !hasCompositePK)
4545
{
4646
decl += "primary key ";
4747
}

src/SQLite.Net/SQLiteConnection.cs

Lines changed: 89 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,23 @@ public int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None)
376376
throw new Exception("Table has no (public) columns");
377377
}
378378

379-
var PKs = mapColumns.Where(c => c.IsPK).ToList();
380-
381-
var decls = mapColumns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings, PKs.Count));
382-
var decl = string.Join(",\n", decls.ToArray());
383-
query.Append(decl).Append(",\n");
384-
385-
386-
if (PKs.Count > 1)
379+
if (map.HasCompositePK)
387380
{
381+
var PKs = mapColumns.Where(c => c.IsPK).ToList();
382+
383+
var decls = mapColumns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings, map.HasCompositePK));
384+
var decl = string.Join(",\n", decls.ToArray());
385+
query.Append(decl).Append(",\n");
388386
query.Append("primary key (").Append(string.Join(",", PKs.Select(pk => pk.Name))).Append(")");
387+
query.Append(")");
389388
}
390389
else
391390
{
392-
query.Remove(query.Length - 2, 2);
391+
var decls = mapColumns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings));
392+
var decl = string.Join(",\n", decls.ToArray());
393+
query.Append(decl).Append(")");
393394
}
394395

395-
query.Append(")");
396-
397396
var count = Execute(query.ToString());
398397

399398
if (count == 0)
@@ -575,7 +574,7 @@ private void MigrateTable(TableMapping map)
575574
}
576575

577576
var addCol = "alter table \"" + map.TableName + "\" add column " +
578-
Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings, PKscount);
577+
Orm.SqlDecl(p, StoreDateTimeAsTicks, Serializer, ExtraTypeMappings, map.HasCompositePK);
579578
Execute(addCol);
580579
}
581580
}
@@ -832,10 +831,6 @@ public T Get<T>(object pk) where T : class
832831
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
833832
}
834833
var pks = map.PKs;
835-
if (pks == null || pks.Length == 0)
836-
{
837-
throw new NotSupportedException("Cannot get from " + map.TableName + ": it has no PK");
838-
}
839834
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
840835
{
841836
throw new NotSupportedException("Cannot get from " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
@@ -889,10 +884,6 @@ public T Find<T>(object pk) where T : class
889884
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
890885
}
891886
var pks = map.PKs;
892-
if (pks == null || pks.Length == 0)
893-
{
894-
throw new NotSupportedException("Cannot find in " + map.TableName + ": it has no PK");
895-
}
896887
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
897888
{
898889
throw new NotSupportedException("Cannot find in " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
@@ -951,10 +942,6 @@ public object Find(object pk, TableMapping map)
951942
throw new NotSupportedException(map.TableName + " table has a composite primary key. Make sure primary key is passed in as Dictionary<string, object>.");
952943
}
953944
var pks = map.PKs;
954-
if (pks == null || pks.Length == 0)
955-
{
956-
throw new NotSupportedException("Cannot find in " + map.TableName + ": it has no PK");
957-
}
958945
if (PKs.Keys.Intersect(pks.Select(p => p.Name)).Count() < pks.Length)
959946
{
960947
throw new NotSupportedException("Cannot find in " + map.TableName + ": PKs mismatch. Make sure PK names are valid.");
@@ -1522,13 +1509,19 @@ public int Insert(object obj, string extra, Type objType)
15221509
}
15231510

15241511
var map = GetMapping(objType);
1525-
15261512
TableMapping.Column pk = null;
15271513

1528-
if (map.PKs != null)
1514+
if (map.HasCompositePK)
15291515
{
15301516
pk = map.PKs.FirstOrDefault(p => p.IsAutoGuid);
15311517
}
1518+
else
1519+
{
1520+
if (map.PK != null && map.PK.IsAutoGuid)
1521+
{
1522+
pk = map.PK;
1523+
}
1524+
}
15321525

15331526
if (pk != null)
15341527
{
@@ -1637,27 +1630,57 @@ public int Update(object obj, Type objType)
16371630
}
16381631

16391632
var map = GetMapping(objType);
1633+
string q = null;
1634+
object[] ps = null;
16401635

1641-
var pks = map.PKs;
1642-
1643-
if (pks == null || pks.Length == 0)
1636+
if (map.HasCompositePK)
16441637
{
1645-
throw new NotSupportedException("Cannot update " + map.TableName + ": it has no PK");
1638+
var pks = map.PKs;
1639+
var cols = from p in map.Columns
1640+
where !pks.Any(pk => pk == p)
1641+
select p;
1642+
1643+
var pslist = (from c in cols
1644+
select c.GetValue(obj)).ToList();
1645+
1646+
pslist.AddRange(pks.Select(pk => pk.GetValue(obj)));
1647+
1648+
q = string.Format("update \"{0}\" set {1} where {2}", map.TableName,
1649+
string.Join(",", (from c in cols
1650+
select "\"" + c.Name + "\" = ? ").ToArray()), string.Join(" and ", pks.Select(pk => "\"" + pk.Name + "\" = ? ")));
1651+
1652+
ps = pslist.ToArray();
16461653
}
1654+
else
1655+
{
1656+
var pk = map.PK;
16471657

1648-
var cols = from p in map.Columns
1649-
where !pks.Any(pk => pk == p)
1650-
select p;
1651-
var ps = (from c in cols
1652-
select c.GetValue(obj)).ToList();
1658+
if (pk == null)
1659+
{
1660+
throw new NotSupportedException("Cannot update " + map.TableName + ": it has no PK");
1661+
}
1662+
1663+
var cols = from p in map.Columns
1664+
where p != pk
1665+
select p;
1666+
1667+
var vals = from c in cols
1668+
select c.GetValue(obj);
1669+
var pslist = new List<object>(vals)
1670+
{
1671+
pk.GetValue(obj)
1672+
};
1673+
1674+
q = string.Format("update \"{0}\" set {1} where {2} = ? ", map.TableName,
1675+
string.Join(",", (from c in cols
1676+
select "\"" + c.Name + "\" = ? ").ToArray()), pk.Name);
1677+
1678+
ps = pslist.ToArray();
1679+
}
16531680

1654-
ps.AddRange(pks.Select(pk=>pk.GetValue(obj)));
1655-
var q = string.Format("update \"{0}\" set {1} where {2}", map.TableName,
1656-
string.Join(",", (from c in cols
1657-
select "\"" + c.Name + "\" = ? ").ToArray()), string.Join(" and ", pks.Select(pk => "\"" + pk.Name + "\" = ? ")));
16581681
try
16591682
{
1660-
rowsAffected = Execute(q, ps.ToArray());
1683+
rowsAffected = Execute(q, ps);
16611684
}
16621685
catch (SQLiteException ex)
16631686
{
@@ -1719,14 +1742,27 @@ public int UpdateAll(IEnumerable objects, bool runInTransaction = true)
17191742
public int Delete(object objectToDelete)
17201743
{
17211744
var map = GetMapping(objectToDelete.GetType());
1722-
var pks = map.PKs;
1723-
if (pks == null || pks.Length == 0)
1745+
string q = null;
1746+
object[] ps = null;
1747+
1748+
if (map.HasCompositePK)
17241749
{
1725-
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
1726-
}
1727-
var q = string.Format("delete from \"{0}\" where {1}", map.TableName, string.Join(" and ", pks.Select(pk => "\"" + pk.Name + "\" = ? ")));
1728-
var ps = (from pk in pks
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
17291753
select pk.GetValue(objectToDelete)).ToArray();
1754+
}
1755+
else
1756+
{
1757+
var pk = map.PK;
1758+
if (pk == null)
1759+
{
1760+
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
1761+
}
1762+
q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
1763+
ps = new object[] { pk.GetValue(objectToDelete) };
1764+
}
1765+
17301766
return Execute(q, ps);
17311767
}
17321768

@@ -1746,14 +1782,10 @@ public int Delete(object objectToDelete)
17461782
public int Delete<T>(object primaryKey)
17471783
{
17481784
var map = GetMapping(typeof (T));
1749-
var pks = map.PKs;
1750-
if (pks == null || pks.Length == 0)
1751-
{
1752-
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
1753-
}
17541785

17551786
if (map.HasCompositePK)
17561787
{
1788+
var pks = map.PKs;
17571789
IDictionary<string, object> PKs = primaryKey as Dictionary<string, object>;
17581790
if (PKs == null)
17591791
{
@@ -1770,7 +1802,12 @@ public int Delete<T>(object primaryKey)
17701802
}
17711803
else
17721804
{
1773-
var q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pks.FirstOrDefault().Name);
1805+
var pk = map.PK;
1806+
if (pk == null)
1807+
{
1808+
throw new NotSupportedException("Cannot delete " + map.TableName + ": it has no PK");
1809+
}
1810+
var q = string.Format("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
17741811
return Execute(q, primaryKey);
17751812
}
17761813
}

src/SQLite.Net/TableMapping.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class TableMapping
3434
{
3535
private readonly Column _autoPk;
3636
private Column[] _insertColumns;
37+
private Column _pk;
3738

3839
[PublicAPI]
3940
public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags createFlags = CreateFlags.None)
@@ -72,6 +73,11 @@ public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags
7273
{
7374
_autoPk = c;
7475
}
76+
77+
if (c.IsPK)
78+
{
79+
_pk = c;
80+
}
7581
}
7682

7783
HasAutoIncPK = _autoPk != null;
@@ -103,6 +109,22 @@ public TableMapping(Type type, IEnumerable<PropertyInfo> properties, CreateFlags
103109
[PublicAPI]
104110
public Column[] Columns { get; private set; }
105111

112+
[PublicAPI]
113+
public Column PK
114+
{
115+
get
116+
{
117+
if (HasCompositePK)
118+
{
119+
throw new NotSupportedException("Table has a composite primary key. Use PKs property instead.");
120+
}
121+
else
122+
{
123+
return _pk;
124+
}
125+
}
126+
}
127+
106128
[PublicAPI]
107129
public Column[] PKs { get; private set; }
108130

0 commit comments

Comments
 (0)