1
1
using Plugin ;
2
2
using RubyCodegen ;
3
+ using System ;
3
4
using System . Collections . Generic ;
4
5
using System . Linq ;
5
6
6
7
namespace SqlcGenRuby . Drivers ;
7
8
8
9
public class MethodGen ( DbDriver dbDriver )
9
10
{
11
+ private static string ? GetMethodArgs ( string argInterface , IList < Parameter > parameters )
12
+ {
13
+ return parameters . Count == 0 ? null : argInterface . SnakeCase ( ) ;
14
+ }
15
+
10
16
public MethodDeclaration OneDeclare ( string funcName , string queryTextConstant , string argInterface ,
11
- string returnInterface , IList < Parameter > parameters , IList < Column > columns )
17
+ string returnInterface , IList < Parameter > parameters , IList < Column > columns , bool poolingEnabled = true ,
18
+ RowDataType rowDataType = RowDataType . Hash )
12
19
{
13
- var newObjectExpression = new NewObject ( returnInterface , GetColumnsInitExpressions ( columns ) ) ;
20
+ var newObjectExpression = new NewObject ( returnInterface , GetColumnsInitExpressions ( columns , rowDataType ) ) ;
14
21
IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
15
22
var queryParams = GetQueryParams ( argInterface , parameters ) ;
16
23
withResourceBody = withResourceBody . AppendIfNotNull ( queryParams ) ;
@@ -26,27 +33,34 @@ public MethodDeclaration OneDeclare(string funcName, string queryTextConstant, s
26
33
]
27
34
) . ToList ( ) ;
28
35
29
- return new MethodDeclaration (
30
- funcName ,
31
- argInterface ,
32
- GetMethodArgs ( argInterface , parameters ) ,
33
- $ "{ returnInterface } ?",
34
- new List < IComposable >
35
- {
36
- new WithResource ( Variable . Pool . AsProperty ( ) , Variable . Client . AsVar ( ) , withResourceBody . ToList ( ) )
37
- } ) ;
36
+ var methodArgs = GetMethodArgs ( argInterface , parameters ) ;
37
+ var methodBody = OptionallyAddPoolUsage ( poolingEnabled , withResourceBody ) ;
38
+ return new MethodDeclaration ( funcName , argInterface , methodArgs , $ "{ returnInterface } ?", methodBody ) ;
38
39
}
39
40
40
- private static string ? GetMethodArgs ( string argInterface , IList < Parameter > parameters )
41
+ private static IEnumerable < IComposable > OptionallyAddPoolUsage ( bool poolingEnabled , IEnumerable < IComposable > body )
41
42
{
42
- return parameters . Count == 0 ? null : argInterface . SnakeCase ( ) ;
43
+ return poolingEnabled
44
+ ?
45
+ [
46
+ new WithResource (
47
+ Variable . Db . AsProperty ( ) ,
48
+ Variable . Client . AsVar ( ) ,
49
+ body . ToList ( ) )
50
+ ]
51
+ : new List < IComposable >
52
+ {
53
+ new SimpleExpression ( $ "{ Variable . Client . AsVar ( ) } = { Variable . Db . AsProperty ( ) } ")
54
+ }
55
+ . Concat ( body ) ;
43
56
}
44
57
45
58
public MethodDeclaration ManyDeclare ( string funcName , string queryTextConstant , string argInterface ,
46
- string returnInterface , IList < Parameter > parameters , IList < Column > columns )
59
+ string returnInterface , IList < Parameter > parameters , IList < Column > columns , bool poolingEnabled = true ,
60
+ RowDataType rowDataType = RowDataType . Hash )
47
61
{
48
62
var listAppend = new ListAppend ( Variable . Entities . AsVar ( ) ,
49
- new NewObject ( returnInterface , GetColumnsInitExpressions ( columns ) ) ) ;
63
+ new NewObject ( returnInterface , GetColumnsInitExpressions ( columns , rowDataType ) ) ) ;
50
64
IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
51
65
var queryParams = GetQueryParams ( argInterface , parameters ) ;
52
66
withResourceBody = withResourceBody . AppendIfNotNull ( queryParams ) ;
@@ -65,23 +79,13 @@ public MethodDeclaration ManyDeclare(string funcName, string queryTextConstant,
65
79
]
66
80
) ;
67
81
68
- return new MethodDeclaration (
69
- funcName ,
70
- argInterface ,
71
- GetMethodArgs ( argInterface , parameters ) ,
72
- $ "Array[{ returnInterface } ]",
73
- new List < IComposable >
74
- {
75
- new WithResource (
76
- Variable . Pool . AsProperty ( ) ,
77
- Variable . Client . AsVar ( ) ,
78
- withResourceBody . ToList ( )
79
- )
80
- } ) ;
82
+ var methodArgs = GetMethodArgs ( argInterface , parameters ) ;
83
+ var methodBody = OptionallyAddPoolUsage ( poolingEnabled , withResourceBody ) ;
84
+ return new MethodDeclaration ( funcName , argInterface , methodArgs , null , methodBody ) ;
81
85
}
82
86
83
87
public MethodDeclaration ExecDeclare ( string funcName , string queryTextConstant , string argInterface ,
84
- IList < Parameter > parameters )
88
+ IList < Parameter > parameters , bool poolingEnabled = true )
85
89
{
86
90
IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
87
91
var queryParams = GetQueryParams ( argInterface , parameters ) ;
@@ -91,15 +95,9 @@ public MethodDeclaration ExecDeclare(string funcName, string queryTextConstant,
91
95
. Append ( dbDriver . ExecuteStmt ( funcName , queryParams ) )
92
96
. ToList ( ) ;
93
97
94
- return new MethodDeclaration ( funcName ,
95
- argInterface ,
96
- GetMethodArgs ( argInterface , parameters ) ,
97
- null ,
98
- new List < IComposable >
99
- {
100
- new WithResource ( Variable . Pool . AsProperty ( ) , Variable . Client . AsVar ( ) , withResourceBody . ToList ( )
101
- )
102
- } ) ;
98
+ var methodArgs = GetMethodArgs ( argInterface , parameters ) ;
99
+ var methodBody = OptionallyAddPoolUsage ( poolingEnabled , withResourceBody ) ;
100
+ return new MethodDeclaration ( funcName , argInterface , methodArgs , null , methodBody ) ;
103
101
}
104
102
105
103
public MethodDeclaration ExecLastIdDeclare ( string funcName , string queryTextConstant , string argInterface ,
@@ -124,7 +122,7 @@ public MethodDeclaration ExecLastIdDeclare(string funcName, string queryTextCons
124
122
"Integer" ,
125
123
new List < IComposable >
126
124
{
127
- new WithResource ( Variable . Pool . AsProperty ( ) , Variable . Client . AsVar ( ) ,
125
+ new WithResource ( Variable . Db . AsProperty ( ) , Variable . Client . AsVar ( ) ,
128
126
withResourceBody . ToList ( ) )
129
127
}
130
128
) ;
@@ -139,9 +137,11 @@ public MethodDeclaration ExecLastIdDeclare(string funcName, string queryTextCons
139
137
new SimpleExpression ( $ "[{ queryParams . JoinByCommaAndFormat ( ) } ]") ) ;
140
138
}
141
139
142
- private static IList < SimpleExpression > GetColumnsInitExpressions ( IList < Column > columns )
140
+ private static IList < SimpleExpression > GetColumnsInitExpressions ( IList < Column > columns , RowDataType rowDataType )
143
141
{
144
- return columns . Select ( c => new SimpleExpression ( $ "{ Variable . Row . AsVar ( ) } ['{ c . Name } ']") ) . ToList ( ) ;
142
+ return rowDataType == RowDataType . Hash
143
+ ? columns . Select ( c => new SimpleExpression ( $ "{ Variable . Row . AsVar ( ) } ['{ c . Name } ']") ) . ToList ( )
144
+ : columns . Select ( ( _ , i ) => new SimpleExpression ( $ "{ Variable . Row . AsVar ( ) } [{ i } ]") ) . ToList ( ) ;
145
145
}
146
146
147
147
private SimpleStatement ExecuteAndAssign ( string funcName , SimpleStatement ? queryParams )
0 commit comments