11using Plugin ;
22using RubyCodegen ;
3+ using System ;
34using System . Collections . Generic ;
45using System . Linq ;
56
67namespace SqlcGenRuby . Drivers ;
78
89public class MethodGen ( DbDriver dbDriver )
910{
11+ private static string ? GetMethodArgs ( string argInterface , IList < Parameter > parameters )
12+ {
13+ return parameters . Count == 0 ? null : argInterface . SnakeCase ( ) ;
14+ }
15+
1016 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 )
1219 {
13- var newObjectExpression = new NewObject ( returnInterface , GetColumnsInitExpressions ( columns ) ) ;
20+ var newObjectExpression = new NewObject ( returnInterface , GetColumnsInitExpressions ( columns , rowDataType ) ) ;
1421 IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
1522 var queryParams = GetQueryParams ( argInterface , parameters ) ;
1623 withResourceBody = withResourceBody . AppendIfNotNull ( queryParams ) ;
@@ -26,27 +33,34 @@ public MethodDeclaration OneDeclare(string funcName, string queryTextConstant, s
2633 ]
2734 ) . ToList ( ) ;
2835
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 ) ;
3839 }
3940
40- private static string ? GetMethodArgs ( string argInterface , IList < Parameter > parameters )
41+ private static IEnumerable < IComposable > OptionallyAddPoolUsage ( bool poolingEnabled , IEnumerable < IComposable > body )
4142 {
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 ) ;
4356 }
4457
4558 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 )
4761 {
4862 var listAppend = new ListAppend ( Variable . Entities . AsVar ( ) ,
49- new NewObject ( returnInterface , GetColumnsInitExpressions ( columns ) ) ) ;
63+ new NewObject ( returnInterface , GetColumnsInitExpressions ( columns , rowDataType ) ) ) ;
5064 IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
5165 var queryParams = GetQueryParams ( argInterface , parameters ) ;
5266 withResourceBody = withResourceBody . AppendIfNotNull ( queryParams ) ;
@@ -65,23 +79,13 @@ public MethodDeclaration ManyDeclare(string funcName, string queryTextConstant,
6579 ]
6680 ) ;
6781
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 ) ;
8185 }
8286
8387 public MethodDeclaration ExecDeclare ( string funcName , string queryTextConstant , string argInterface ,
84- IList < Parameter > parameters )
88+ IList < Parameter > parameters , bool poolingEnabled = true )
8589 {
8690 IEnumerable < IComposable > withResourceBody = new List < IComposable > ( ) ;
8791 var queryParams = GetQueryParams ( argInterface , parameters ) ;
@@ -91,15 +95,9 @@ public MethodDeclaration ExecDeclare(string funcName, string queryTextConstant,
9195 . Append ( dbDriver . ExecuteStmt ( funcName , queryParams ) )
9296 . ToList ( ) ;
9397
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 ) ;
103101 }
104102
105103 public MethodDeclaration ExecLastIdDeclare ( string funcName , string queryTextConstant , string argInterface ,
@@ -124,7 +122,7 @@ public MethodDeclaration ExecLastIdDeclare(string funcName, string queryTextCons
124122 "Integer" ,
125123 new List < IComposable >
126124 {
127- new WithResource ( Variable . Pool . AsProperty ( ) , Variable . Client . AsVar ( ) ,
125+ new WithResource ( Variable . Db . AsProperty ( ) , Variable . Client . AsVar ( ) ,
128126 withResourceBody . ToList ( ) )
129127 }
130128 ) ;
@@ -139,9 +137,11 @@ public MethodDeclaration ExecLastIdDeclare(string funcName, string queryTextCons
139137 new SimpleExpression ( $ "[{ queryParams . JoinByCommaAndFormat ( ) } ]") ) ;
140138 }
141139
142- private static IList < SimpleExpression > GetColumnsInitExpressions ( IList < Column > columns )
140+ private static IList < SimpleExpression > GetColumnsInitExpressions ( IList < Column > columns , RowDataType rowDataType )
143141 {
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 ( ) ;
145145 }
146146
147147 private SimpleStatement ExecuteAndAssign ( string funcName , SimpleStatement ? queryParams )
0 commit comments