praeclarum / praeclarum/sqlite-net

Support multiple primary keys in schema

Open
#280 13 comments 0 reactions 1 assignee View on GitHub

@praeclarum is already working on this.

Since Aug 13, 2017.

Feature
Dominant language
C#
Stars
4.5k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

Here's a patch for that:

@@ -362,14 +362,21 @@ namespace SQLite
            if (!_tables.TryGetValue (ty.FullName, out map)) {
                map = GetMapping (ty, createFlags);
                _tables.Add (ty.FullName, map);
            }
            var query = "create table if not exists \"" + map.TableName + "\"(\n";
-           
-           var decls = map.Columns.Select (p => Orm.SqlDecl (p, StoreDateTimeAsTicks));
+
+      IEnumerable<TableMapping.Column> pks = from c in map.Columns where c.IsPK select c;
+      int numPKs = pks.Count();
+
+      var decls = map.Columns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, numPKs == 1));
            var decl = string.Join (",\n", decls.ToArray ());
            query += decl;
+
+      if (numPKs > 1)
+        query += String.Format(",\nprimary key ({0})\n", string.Join(", ", pks.Select(c => "\"" + c.Name + "\"")));
+
            query += ")";

            var count = Execute (query);

            if (count == 0) { //Possible bug: This always seems to return 0?
@@ -1838,15 +1845,15 @@ namespace SQLite
    {
         public const int DefaultMaxStringLength = 140;
         public const string ImplicitPkName = "Id";
         public const string ImplicitIndexSuffix = "Id";

-       public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks)
+       public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks, bool setPK = true)
        {
            string decl = "\"" + p.Name + "\" " + SqlType (p, storeDateTimeAsTicks) + " ";

-           if (p.IsPK) {
+           if (setPK && p.IsPK) {
                decl += "primary key ";
            }
            if (p.IsAutoInc) {
                decl += "autoincrement ";
            }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.