Sqlite-net: Support multiple primary keys in schema

Created on 23 Apr 2014  路  12Comments  路  Source: praeclarum/sqlite-net

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 ";
            }
Feature

Most helpful comment

Any news on this? Would be great to have multicolumn primary keys!

All 12 comments

Please create a pullrequest for this! this is awesome.

Done. Pull request is #281.

thank you :)

Has this not been merged in yet?

Any news on this? Would be great to have multicolumn primary keys!

Any updates on this? @praeclarum

Wow, this is old.

A note: I am 100% sure that there are issues with the PR as it was submitted. It should be very carefully tested before proceeding.

Without this my work becomes more complicated 馃槃
Can this feature make the next release?

Upvote

Upvote

Upvote

Upvote

Was this page helpful?
0 / 5 - 0 ratings