@@ -19,26 +19,38 @@ static void WriteSingleImplementation(
1919 in CommandFactoryState factories ,
2020 in RowReaderState readers ,
2121 string ? fixedSql ,
22- AdditionalCommandState ? additionalCommandState )
22+ AdditionalCommandState ? additionalCommandState ,
23+ bool viaCommandDefinition )
2324 {
2425 sb . Append ( "return " ) ;
2526 if ( flags . HasAll ( OperationFlags . Async | OperationFlags . Query | OperationFlags . Buffered ) )
2627 {
2728 sb . Append ( "global::Dapper.DapperAotExtensions.AsEnumerableAsync(" ) . Indent ( false ) . NewLine ( ) ;
2829 }
2930 // (DbConnection connection, DbTransaction? transaction, string sql, TArgs args, CommandType commandType, int timeout, CommandFactory<TArgs>? commandFactory)
30- sb . Append ( "global::Dapper.DapperAotExtensions.Command(cnn, " ) . Append ( Forward ( methodParameters , "transaction" ) ) . Append ( ", " ) ;
31+ sb . Append ( "global::Dapper.DapperAotExtensions.Command(cnn, " ) ;
32+
33+ if ( viaCommandDefinition ) sb . Append ( "command.Transaction, " ) ;
34+ else sb . Append ( Forward ( methodParameters , "transaction" ) ) . Append ( ", " ) ;
35+
3136 if ( fixedSql is not null )
3237 {
3338 sb . AppendVerbatimLiteral ( fixedSql ) . Append ( ", " ) ;
3439 }
3540 else
3641 {
37- sb . Append ( "sql, " ) ;
42+ if ( viaCommandDefinition ) sb . Append ( "command.CommandText, " ) ;
43+ else sb . Append ( "sql, " ) ;
3844 }
45+
3946 if ( commandTypeMode == 0 )
40- { // not hard-coded
41- if ( HasParam ( methodParameters , "command" ) )
47+ {
48+ if ( viaCommandDefinition )
49+ {
50+ sb . Append ( "command.CommandType ?? default" ) ;
51+ }
52+ // not hard-coded
53+ else if ( HasParam ( methodParameters , "command" ) )
4254 {
4355 sb . Append ( "command.GetValueOrDefault()" ) ;
4456 }
@@ -49,9 +61,14 @@ static void WriteSingleImplementation(
4961 }
5062 else
5163 {
52- sb . Append ( "global::System.Data.CommandType." ) . Append ( commandTypeMode . ToString ( ) ) ;
64+ if ( viaCommandDefinition ) sb . Append ( "command.CommandType ?? default" ) ;
65+ else sb . Append ( "global::System.Data.CommandType." ) . Append ( commandTypeMode . ToString ( ) ) ;
5366 }
54- sb . Append ( ", " ) . Append ( Forward ( methodParameters , "commandTimeout" ) ) . Append ( HasParam ( methodParameters , "commandTimeout" ) ? ".GetValueOrDefault()" : "" ) . Append ( ", " ) ;
67+ sb . Append ( ", " ) ;
68+
69+ if ( viaCommandDefinition ) sb . Append ( "command.CommandTimeout ?? default, " ) ;
70+ else sb . Append ( Forward ( methodParameters , "commandTimeout" ) ) . Append ( HasParam ( methodParameters , "commandTimeout" ) ? ".GetValueOrDefault()" : "" ) . Append ( ", " ) ;
71+
5572 if ( flags . HasAny ( OperationFlags . HasParameters ) )
5673 {
5774 var index = factories . GetIndex ( parameterType ! , map , cache , additionalCommandState , out var subIndex ) ;
@@ -79,7 +96,7 @@ static void WriteSingleImplementation(
7996 OperationFlags . Unbuffered => "Unbuffered" ,
8097 _ => ""
8198 } ) . Append ( isAsync ? "Async" : "" ) . Append ( "(" ) ;
82- WriteTypedArg ( sb , parameterType ) . Append ( ", " ) ;
99+ WriteTypedArg ( sb , parameterType , viaCommandDefinition ) . Append ( ", " ) ;
83100 if ( ! flags . HasAny ( OperationFlags . SingleRow ) )
84101 {
85102 switch ( flags & ( OperationFlags . Buffered | OperationFlags . Unbuffered ) )
@@ -107,7 +124,7 @@ static void WriteSingleImplementation(
107124 sb . Append ( "<" ) . Append ( resultType ) . Append ( ">" ) ;
108125 }
109126 sb . Append ( "(" ) ;
110- WriteTypedArg ( sb , parameterType ) ;
127+ WriteTypedArg ( sb , parameterType , viaCommandDefinition ) ;
111128 }
112129 else
113130 {
@@ -124,9 +141,11 @@ static void WriteSingleImplementation(
124141 sb . Append ( ", rowCountHint: ((" ) . Append ( parameterType ) . Append ( ")param!)." ) . Append ( additionalCommandState . RowCountHintMemberName ) ;
125142 }
126143 }
127- if ( isAsync && HasParam ( methodParameters , "cancellationToken" ) )
144+ if ( isAsync && ( HasParam ( methodParameters , "cancellationToken" ) || viaCommandDefinition ) )
128145 {
129- sb . Append ( ", cancellationToken: " ) . Append ( Forward ( methodParameters , "cancellationToken" ) ) ;
146+ sb . Append ( ", cancellationToken: " ) ;
147+ if ( viaCommandDefinition ) sb . Append ( "command.CancellationToken" ) ;
148+ else sb . Append ( Forward ( methodParameters , "cancellationToken" ) ) ;
130149 }
131150 if ( flags . HasAll ( OperationFlags . Async | OperationFlags . Query | OperationFlags . Buffered ) )
132151 {
@@ -153,10 +172,17 @@ static void WriteSingleImplementation(
153172 }
154173 sb . Append ( ";" ) . NewLine ( ) ;
155174
156- static CodeWriter WriteTypedArg ( CodeWriter sb , ITypeSymbol ? parameterType )
175+ static CodeWriter WriteTypedArg ( CodeWriter sb , ITypeSymbol ? parameterType , bool viaCommandDefinition )
157176 {
177+ if ( viaCommandDefinition )
178+ {
179+ sb . Append ( "command.Parameters" ) ;
180+ return sb ;
181+ }
182+
158183 if ( parameterType is null || parameterType . IsAnonymousType )
159184 {
185+
160186 sb . Append ( "param" ) ;
161187 }
162188 else
0 commit comments