-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathForeignKeyStatement.cs
More file actions
38 lines (34 loc) · 1.41 KB
/
Copy pathForeignKeyStatement.cs
File metadata and controls
38 lines (34 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using SQLite.CodeFirst.Builder.NameCreators;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SQLite.CodeFirst.Statement
{
internal class ForeignKeyStatement : IStatement
{
private const string Template = "FOREIGN KEY ({foreign-key}) REFERENCES {referenced-table}({referenced-id})";
private const string CascadeDeleteStatement = "ON DELETE CASCADE";
private const string CascadeUpdateStatement = "ON UPDATE CASCADE";
public IEnumerable<string> ForeignKey { get; set; }
public string ForeignTable { get; set; }
public IEnumerable<string> ForeignPrimaryKey { get; set; }
public bool CascadeDelete { get; set; }
public bool CascadeUpdate { get; set; }
public string CreateStatement()
{
var sb = new StringBuilder(Template);
sb.Replace("{foreign-key}", string.Join(", ", ForeignKey.Select(c => ColumnNameCreator.EscapeName(c))));
sb.Replace("{referenced-table}", ForeignTable);
sb.Replace("{referenced-id}", string.Join(", ", ForeignPrimaryKey.Select(c => ColumnNameCreator.EscapeName(c))));
if (CascadeDelete)
{
sb.Append(" " + CascadeDeleteStatement);
}
if (CascadeUpdate)
{
sb.Append(" " + CascadeUpdateStatement);
}
return sb.ToString();
}
}
}