Repository navigation
Expand file tree
/
Copy pathMethodGen.cs
More file actions
152 lines (139 loc) · 6.33 KB
/
Copy pathMethodGen.cs
File metadata and controls
152 lines (139 loc) · 6.33 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Plugin;
using RubyCodegen;
using System.Collections.Generic;
using System.Linq;
namespace SqlcGenRuby.Drivers;
public class MethodGen(DbDriver dbDriver)
{
public MethodDeclaration OneDeclare(string funcName, string queryTextConstant, string argInterface,
string returnInterface, IList<Parameter> parameters, IList<Column> columns)
{
var newObjectExpression = new NewObject(returnInterface, GetColumnsInitExpressions(columns));
IEnumerable<IComposable> withResourceBody = new List<IComposable>();
var queryParams = GetQueryParams(argInterface, parameters);
withResourceBody = withResourceBody.AppendIfNotNull(queryParams);
withResourceBody = withResourceBody
.Concat(
[
dbDriver.PrepareStmt(funcName, queryTextConstant),
ExecuteAndAssign(funcName, queryParams),
new SimpleStatement(Variable.Row.AsVar(), new SimpleExpression($"{Variable.Result.AsVar()}.first")),
new SimpleExpression($"return nil if {Variable.Row.AsVar()}.nil?"),
new SimpleStatement($"{Variable.Entity.AsVar()}", newObjectExpression),
new SimpleExpression($"return {Variable.Entity.AsVar()}")
]
).ToList();
return new MethodDeclaration(
funcName,
argInterface,
GetMethodArgs(argInterface, parameters),
$"{returnInterface}?",
new List<IComposable>
{
new WithResource(Variable.Pool.AsProperty(), Variable.Client.AsVar(), withResourceBody.ToList())
});
}
private static string? GetMethodArgs(string argInterface, IList<Parameter> parameters)
{
return parameters.Count == 0 ? null : argInterface.SnakeCase();
}
public MethodDeclaration ManyDeclare(string funcName, string queryTextConstant, string argInterface,
string returnInterface, IList<Parameter> parameters, IList<Column> columns)
{
var listAppend = new ListAppend(Variable.Entities.AsVar(),
new NewObject(returnInterface, GetColumnsInitExpressions(columns)));
IEnumerable<IComposable> withResourceBody = new List<IComposable>();
var queryParams = GetQueryParams(argInterface, parameters);
withResourceBody = withResourceBody.AppendIfNotNull(queryParams);
withResourceBody = withResourceBody
.Concat(
[
dbDriver.PrepareStmt(funcName, queryTextConstant),
ExecuteAndAssign(funcName, queryParams),
new SimpleStatement(Variable.Entities.AsVar(), new SimpleExpression("[]")),
new ForeachLoop(
Variable.Result.AsVar(),
Variable.Row.AsVar(),
new List<IComposable> { listAppend }
),
new SimpleExpression($"return {Variable.Entities.AsVar()}")
]
);
return new MethodDeclaration(
funcName,
argInterface,
GetMethodArgs(argInterface, parameters),
$"Array[{returnInterface}]",
new List<IComposable>
{
new WithResource(
Variable.Pool.AsProperty(),
Variable.Client.AsVar(),
withResourceBody.ToList()
)
});
}
public MethodDeclaration ExecDeclare(string funcName, string queryTextConstant, string argInterface,
IList<Parameter> parameters)
{
IEnumerable<IComposable> withResourceBody = new List<IComposable>();
var queryParams = GetQueryParams(argInterface, parameters);
withResourceBody = withResourceBody.AppendIfNotNull(queryParams);
withResourceBody = withResourceBody
.Append(dbDriver.PrepareStmt(funcName, queryTextConstant))
.Append(dbDriver.ExecuteStmt(funcName, queryParams))
.ToList();
return new MethodDeclaration(funcName,
argInterface,
GetMethodArgs(argInterface, parameters),
null,
new List<IComposable>
{
new WithResource(Variable.Pool.AsProperty(), Variable.Client.AsVar(), withResourceBody.ToList()
)
});
}
public MethodDeclaration ExecLastIdDeclare(string funcName, string queryTextConstant, string argInterface,
IList<Parameter> parameters)
{
IEnumerable<IComposable> withResourceBody = new List<IComposable>();
var queryParams = GetQueryParams(argInterface, parameters);
withResourceBody = withResourceBody.AppendIfNotNull(queryParams);
withResourceBody = withResourceBody
.Concat(
[
dbDriver.PrepareStmt(funcName, queryTextConstant),
dbDriver.ExecuteStmt(funcName, queryParams),
new SimpleExpression($"return {Variable.Client.AsVar()}.last_id")
]
);
return new MethodDeclaration(
funcName,
argInterface,
GetMethodArgs(argInterface, parameters),
"Integer",
new List<IComposable>
{
new WithResource(Variable.Pool.AsProperty(), Variable.Client.AsVar(),
withResourceBody.ToList())
}
);
}
private static SimpleStatement? GetQueryParams(string argInterface, IList<Parameter> parameters)
{
var queryParams = parameters.Select(p => $"{argInterface.SnakeCase()}.{p.Column.Name}").ToList();
return queryParams.Count == 0
? null
: new SimpleStatement(Variable.QueryParams.AsVar(),
new SimpleExpression($"[{queryParams.JoinByCommaAndFormat()}]"));
}
private static IList<SimpleExpression> GetColumnsInitExpressions(IList<Column> columns)
{
return columns.Select(c => new SimpleExpression($"{Variable.Row.AsVar()}['{c.Name}']")).ToList();
}
private SimpleStatement ExecuteAndAssign(string funcName, SimpleStatement? queryParams)
{
return new SimpleStatement(Variable.Result.AsVar(),
dbDriver.ExecuteStmt(funcName, queryParams));
}
}