-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryTemplates.cs
More file actions
119 lines (98 loc) · 3.88 KB
/
QueryTemplates.cs
File metadata and controls
119 lines (98 loc) · 3.88 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using DbSyncKit.Templates.Interface;
using Fluid;
using System;
namespace DbSyncKit.Templates.SQLite
{
/// <summary>
/// Implementation of <see cref="IQueryTemplates"/> for SQLite database, providing templates for various SQL queries.
/// </summary>
public class QueryTemplates : IQueryTemplates, IDisposable
{
#region Public Properties
/// <summary>
/// Gets the template for a SELECT query.
/// </summary>
public IFluidTemplate SelectTemplate => _selectQueryTemplate.Value;
/// <summary>
/// Gets the template for an INSERT query.
/// </summary>
public IFluidTemplate InsertTemplate => _insertQueryTemplate.Value;
/// <summary>
/// Gets the template for an UPDATE query.
/// </summary>
public IFluidTemplate UpdateTemplate => _updateQueryTemplate.Value;
/// <summary>
/// Gets the template for a DELETE query.
/// </summary>
public IFluidTemplate DeleteTemplate => _deleteQueryTemplate.Value;
/// <summary>
/// Gets the template for a COMMENT query.
/// </summary>
public IFluidTemplate CommentTemplate => _commentQueryTemplate.Value;
#endregion
#region Private Properties
private static readonly Lazy<IFluidTemplate> _selectQueryTemplate = new Lazy<IFluidTemplate>(CreateSelectQueryTemplate);
private static readonly Lazy<IFluidTemplate> _insertQueryTemplate = new Lazy<IFluidTemplate>(CreateInsertQueryTemplate);
private static readonly Lazy<IFluidTemplate> _updateQueryTemplate = new Lazy<IFluidTemplate>(CreateUpdateQueryTemplate);
private static readonly Lazy<IFluidTemplate> _deleteQueryTemplate = new Lazy<IFluidTemplate>(CreateDeleteQueryTemplate);
private static readonly Lazy<IFluidTemplate> _commentQueryTemplate = new Lazy<IFluidTemplate>(CreateCommentQueryTemplate);
private static readonly FluidParser parser = new FluidParser();
#endregion
#region Templates
/// <summary>
/// Creates a template for a SELECT query.
/// </summary>
private static IFluidTemplate CreateSelectQueryTemplate()
{
var str = @"SELECT {{ Columns | join: ', ' }} FROM {{ TableName }};";
return parser.Parse(str);
}
/// <summary>
/// Creates a template for an INSERT query.
/// </summary>
private static IFluidTemplate CreateInsertQueryTemplate()
{
var str = @"
INSERT INTO {{ TableName }} ({{ Columns | join: ', ' }}) VALUES ({{ Values | join: ', ' }});";
return parser.Parse(str);
}
/// <summary>
/// Creates a template for an UPDATE query.
/// </summary>
private static IFluidTemplate CreateUpdateQueryTemplate()
{
var str = @"
UPDATE {{ TableName }} SET {{ Set | join: ', ' }} WHERE {{ Where | join: ' AND ' }};";
return parser.Parse(str);
}
/// <summary>
/// Creates a template for a DELETE query.
/// </summary>
private static IFluidTemplate CreateDeleteQueryTemplate()
{
var str = @"
DELETE FROM {{ TableName }} WHERE {{ Where | join: ' AND ' }};";
return parser.Parse(str);
}
/// <summary>
/// Creates a template for a COMMENT query.
/// </summary>
private static IFluidTemplate CreateCommentQueryTemplate()
{
var str = @"{% unless isMultiLine %}-- {{ Comment }}{% else %}
/*
{{ Comment }}
*/
{% endunless %}";
return parser.Parse(str);
}
#endregion
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting resources.
/// </summary>
public void Dispose()
{
// No additional resources to release.
}
}
}