Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9b69e30

Browse files
committed
Add support for composite index of fields with spaces
1 parent e5a247a commit 9b69e30

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,10 +1155,17 @@ public virtual List<string> ToCreateIndexStatements(Type tableType)
11551155
if (sb.Length > 0)
11561156
sb.Append(", ");
11571157

1158-
var parts = fieldName.SplitOnFirst(' ');
1159-
sb.Append(GetQuotedColumnName(parts[0]))
1160-
.Append(' ')
1161-
.Append(parts.Length > 1 ? parts[1] : "ASC");
1158+
var parts = fieldName.SplitOnLast(' ');
1159+
if (parts.Length == 2 && (parts[1].ToLower().StartsWith("desc") || parts[1].ToLower().StartsWith("asc")))
1160+
{
1161+
sb.Append(GetQuotedColumnName(parts[0]))
1162+
.Append(' ')
1163+
.Append(parts[1]);
1164+
}
1165+
else
1166+
{
1167+
sb.Append(GetQuotedColumnName(fieldName));
1168+
}
11621169
}
11631170

11641171
sqlIndexes.Add(

tests/ServiceStack.OrmLite.Tests/OrmLiteCreateTableWithIndexesTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ public void Can_create_ModelWithCompositeIndexFieldsDesc_table()
7878
}
7979
}
8080

81+
[Test]
82+
public void Can_create_ModelWithCompositeIndexOnFieldSpacesDesc_table()
83+
{
84+
using (var db = OpenDbConnection())
85+
{
86+
db.CreateTable<ModelWithCompositeIndexOnFieldSpacesDesc>(true);
87+
db.GetLastSql().Print();
88+
89+
var sql = OrmLiteConfig.DialectProvider.ToCreateIndexStatements(typeof(ModelWithCompositeIndexOnFieldSpacesDesc)).Join();
90+
91+
var compositeName = "idx_modelwithcompositeindexonfieldspacesdesc_field_field";
92+
93+
if (Dialect == Dialect.Oracle || Dialect == Dialect.Firebird)
94+
compositeName = OrmLiteConfig.DialectProvider.NamingStrategy.ApplyNameRestrictions(compositeName).ToLower();
95+
96+
Assert.That(sql, Is.StringContaining(compositeName));
97+
}
98+
}
99+
81100
[Test]
82101
public void Can_create_ModelWithNamedCompositeIndex_table()
83102
{

tests/ServiceStack.OrmLite.Tests/Shared/ModelWithCompositeIndexFields.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public class ModelWithCompositeIndexFieldsDesc
3737

3838
public string Composite2 { get; set; }
3939
}
40+
41+
[CompositeIndex("Field WithSpace1", "Field WithSpace2 DESC")]
42+
public class ModelWithCompositeIndexOnFieldSpacesDesc
43+
{
44+
public string Id { get; set; }
45+
46+
[Alias("Field WithSpace1")]
47+
public string FieldWithSpace1 { get; set; }
48+
49+
[Alias("Field WithSpace2")]
50+
public string FieldWithSpace2 { get; set; }
51+
}
4052
}

0 commit comments

Comments
 (0)