Skip to content

Commit 343e5d9

Browse files
committed
Added CreateIndexStatement
1 parent 9aabb8c commit 343e5d9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

SQLite.CodeFirst/Builder/CreateDatabaseStatementBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Data.Entity.Core.Metadata.Edm;
3+
using System.Data.Entity.Infrastructure.Annotations;
34
using System.Linq;
45
using SQLite.CodeFirst.Statement;
56

@@ -27,6 +28,20 @@ private IEnumerable<CreateTableStatement> GetCreateTableStatements()
2728
ICollection<AssociationType> associationTypes =
2829
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entityType.Name).ToList();
2930

31+
var indexAnnotationGroups = edmModel.EntityTypes.GroupBy(e => e.Name, e => e.MetadataProperties.OfType<IndexAnnotation>());
32+
foreach (var indexAnnotationGroup in indexAnnotationGroups)
33+
{
34+
foreach (var indexAnnotations in indexAnnotationGroup)
35+
{
36+
foreach (var indexAnnotation in indexAnnotations)
37+
{
38+
foreach (var indexAttribute in indexAnnotation.Indexes)
39+
{
40+
}
41+
}
42+
}
43+
}
44+
3045
var tableStatementBuilder = new CreateTableStatementBuilder(entityType, associationTypes);
3146
yield return tableStatementBuilder.BuildStatement();
3247
}

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="Builder\CreateDatabaseStatementBuilder.cs" />
5555
<Compile Include="Builder\PrimaryKeyStatementBuilder.cs" />
5656
<Compile Include="Statement\ColumnConstraint\MaxLengthConstraint.cs" />
57+
<Compile Include="Statement\CreateIndexStatement.cs" />
5758
<Compile Include="Statement\PrimaryKeyStatement.cs" />
5859
<Compile Include="Properties\AssemblyInfo.cs" />
5960
<Compile Include="SqliteConnectionStringParser.cs" />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SQLite.CodeFirst.Statement
6+
{
7+
internal class CreateIndexStatement : IStatement
8+
{
9+
private const string Template = "CREATE {unique} INDEX {index-name} ON {table-name} ({column-def});";
10+
11+
private const string ColumnNameSeperator = ", ";
12+
13+
public string Name { get; set; }
14+
public string Table { get; set; }
15+
public IEnumerable<string> ColumnNames { get; set; }
16+
public bool IsUnique { get; set; }
17+
18+
public string CreateStatement()
19+
{
20+
var stringBuilder = new StringBuilder(Template);
21+
22+
stringBuilder.Replace("{unique}", IsUnique ? "UNIQUE" : string.Empty);
23+
stringBuilder.Replace("{index-name}", Name);
24+
stringBuilder.Replace("{table-name}", Table);
25+
26+
string columnNames = String.Join(ColumnNameSeperator, ColumnNames);
27+
stringBuilder.Replace("{column-def}", columnNames);
28+
29+
return stringBuilder.ToString();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)