Skip to content

Commit 8d43ce7

Browse files
committed
#91: Use NameCreator.EscapeName if an indexname was specified manually.
1 parent 9e6bebe commit 8d43ce7

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

SQLite.CodeFirst.Test/IntegrationTests/SqlGenerationTest.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ public class SqlGenerationTest
2222
CREATE TABLE ""FooSelves"" ([FooSelfId] INTEGER PRIMARY KEY, [FooId] int NOT NULL, [Number] int NOT NULL, FOREIGN KEY (FooId) REFERENCES ""Foos""(FooId) ON DELETE CASCADE);
2323
CREATE TABLE ""FooSteps"" ([FooStepId] INTEGER PRIMARY KEY, [FooId] int NOT NULL, [Number] int NOT NULL, FOREIGN KEY (FooId) REFERENCES ""Foos""(FooId) ON DELETE CASCADE);
2424
CREATE INDEX ""IX_MyTable_Id"" ON ""MyTable"" (Id);
25-
CREATE INDEX IX_Team_TeamsName ON ""MyTable"" (Name);
26-
25+
CREATE INDEX ""IX_Team_TeamsName"" ON ""MyTable"" (Name);
2726
CREATE INDEX ""IX_TeamPlayer_Number"" ON ""TeamPlayer"" (Number);
28-
CREATE UNIQUE INDEX IX_TeamPlayer_NumberPerTeam ON ""TeamPlayer"" (Number, TeamId);
27+
CREATE UNIQUE INDEX ""IX_TeamPlayer_NumberPerTeam"" ON ""TeamPlayer"" (Number, TeamId);
2928
CREATE INDEX ""IX_TeamPlayer_Mentor_Id"" ON ""TeamPlayer"" (Mentor_Id);
30-
CREATE UNIQUE INDEX IX_Stadion_Main ON ""Stadions"" (Street, Name);
29+
CREATE UNIQUE INDEX ""IX_Stadion_Main"" ON ""Stadions"" (Street, Name);
3130
CREATE INDEX ""IX_Stadion_Team_Id"" ON ""Stadions"" (Team_Id);
3231
CREATE INDEX ""IX_Foo_FooSelf1Id"" ON ""Foos"" (FooSelf1Id);
3332
CREATE INDEX ""IX_Foo_FooSelf2Id"" ON ""Foos"" (FooSelf2Id);
@@ -52,11 +51,18 @@ public void SqliteSqlGeneratorTest()
5251
// ReSharper disable once UnusedVariable
5352
Player fo = context.Set<Player>().FirstOrDefault();
5453

55-
Assert.IsTrue(string.Equals(ReferenceSql.Trim(), generatedSql.Trim(), StringComparison.OrdinalIgnoreCase));
54+
Assert.IsTrue(string.Equals(RemoveLineEndings(ReferenceSql), RemoveLineEndings(generatedSql)));
5655
}
5756
}
5857
}
5958

59+
private static string RemoveLineEndings(string input)
60+
{
61+
string lineSeparator = ((char)0x2028).ToString();
62+
string paragraphSeparator = ((char)0x2029).ToString();
63+
return input.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSeparator, string.Empty).Replace(paragraphSeparator, string.Empty);
64+
}
65+
6066
private class DummyDbContext : DbContext
6167
{
6268
public DummyDbContext(DbConnection connection)

SQLite.CodeFirst/Internal/Builder/CreateIndexStatementBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public CreateIndexStatementCollection BuildStatement()
5959

6060
private string GetIndexName(IndexAttribute index, EdmProperty property)
6161
{
62-
return index.Name ?? IndexNameCreator.CreateIndexName(entitySet.ElementType.GetTableName(), property.Name);
62+
return index.Name == null ? IndexNameCreator.CreateName(entitySet.ElementType.GetTableName(), property.Name) : NameCreator.EscapeName(index.Name);
6363
}
6464
}
6565
}

SQLite.CodeFirst/Internal/Builder/NameCreators/IndexNameCreator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ internal static class IndexNameCreator
77
{
88
public const string IndexNamePrefix = "IX_";
99

10-
public static string CreateIndexName(string tableName, string propertyName)
10+
public static string CreateName(string tableName, string propertyName)
1111
{
1212
// If the tableName is escaped it means that this name contains special chars e.g. a dot (base.myTable)
1313
// Because the tablename is used to build the index name the index name must also be escaped.
1414
tableName = tableName.Trim(SpecialChars.EscapeCharOpen, SpecialChars.EscapeCharClose);
1515
return String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}_{3}{4}", SpecialChars.EscapeCharOpen, IndexNamePrefix, tableName, propertyName, SpecialChars.EscapeCharClose);
1616
}
1717
}
18-
}
18+
}

SQLite.CodeFirst/Internal/Convention/SqliteForeignKeyIndexConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public virtual void Apply(AssociationType item, DbModel model)
7070

7171
private static IndexAnnotation CreateIndexAnnotation(string tableName, string propertyName, int count)
7272
{
73-
var indexName = IndexNameCreator.CreateIndexName(tableName, propertyName);
73+
var indexName = IndexNameCreator.CreateName(tableName, propertyName);
7474

7575
// If there are two Indicies on the same property, the count is added.
7676
// In SQLite an Index name must be global unique.

0 commit comments

Comments
 (0)