@@ -65,30 +65,56 @@ public MySqlCommand(string commandText, MySqlConnection connection, MySqlTransac
65
65
66
66
public new MySqlDataReader ExecuteReader ( CommandBehavior commandBehavior ) => ( MySqlDataReader ) base . ExecuteReader ( commandBehavior ) ;
67
67
68
- public override void Prepare ( ) => PrepareAsync ( IOBehavior . Synchronous , default ) . GetAwaiter ( ) . GetResult ( ) ;
68
+ public override void Prepare ( )
69
+ {
70
+ if ( ! NeedsPrepare ( out var exception ) )
71
+ {
72
+ if ( exception != null )
73
+ throw exception ;
74
+ return ;
75
+ }
76
+
77
+ DoPrepareAsync ( IOBehavior . Synchronous , default ) . GetAwaiter ( ) . GetResult ( ) ;
78
+ }
79
+
69
80
public Task PrepareAsync ( ) => PrepareAsync ( AsyncIOBehavior , default ) ;
70
81
public Task PrepareAsync ( CancellationToken cancellationToken ) => PrepareAsync ( AsyncIOBehavior , cancellationToken ) ;
71
-
72
- private async Task PrepareAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
82
+
83
+ private Task PrepareAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
84
+ {
85
+ if ( ! NeedsPrepare ( out var exception ) )
86
+ return exception != null ? Utility . TaskFromException ( exception ) : Utility . CompletedTask ;
87
+
88
+ return DoPrepareAsync ( ioBehavior , cancellationToken ) ;
89
+ }
90
+
91
+ private bool NeedsPrepare ( out Exception exception )
73
92
{
93
+ exception = null ;
74
94
if ( Connection == null )
75
- throw new InvalidOperationException ( "Connection property must be non-null." ) ;
76
- if ( Connection . State != ConnectionState . Open )
77
- throw new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( Connection . State ) ) ;
78
- if ( string . IsNullOrWhiteSpace ( CommandText ) )
79
- throw new InvalidOperationException ( "CommandText must be specified" ) ;
80
- if ( m_connection ? . HasActiveReader ?? false )
81
- throw new InvalidOperationException ( "Cannot call Prepare when there is an open DataReader for this command; it must be closed first." ) ;
82
- if ( Connection . IgnorePrepare )
83
- return ;
95
+ exception = new InvalidOperationException ( "Connection property must be non-null." ) ;
96
+ else if ( Connection . State != ConnectionState . Open )
97
+ exception = new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( Connection . State ) ) ;
98
+ else if ( string . IsNullOrWhiteSpace ( CommandText ) )
99
+ exception = new InvalidOperationException ( "CommandText must be specified" ) ;
100
+ else if ( Connection ? . HasActiveReader ?? false )
101
+ exception = new InvalidOperationException ( "Cannot call Prepare when there is an open DataReader for this command; it must be closed first." ) ;
102
+
103
+ if ( exception != null || Connection . IgnorePrepare )
104
+ return false ;
84
105
85
106
if ( CommandType != CommandType . Text )
86
- throw new NotSupportedException ( "Only CommandType.Text is currently supported by MySqlCommand.Prepare" ) ;
107
+ {
108
+ exception = new NotSupportedException ( "Only CommandType.Text is currently supported by MySqlCommand.Prepare" ) ;
109
+ return false ;
110
+ }
87
111
88
112
// don't prepare the same SQL twice
89
- if ( m_connection . Session . TryGetPreparedStatement ( CommandText ) != null )
90
- return ;
113
+ return Connection . Session . TryGetPreparedStatement ( CommandText ) == null ;
114
+ }
91
115
116
+ private async Task DoPrepareAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
117
+ {
92
118
var statementPreparer = new StatementPreparer ( CommandText , Parameters , CreateStatementPreparerOptions ( ) ) ;
93
119
var parsedStatements = statementPreparer . SplitStatements ( ) ;
94
120
@@ -146,7 +172,7 @@ private async Task PrepareAsync(IOBehavior ioBehavior, CancellationToken cancell
146
172
preparedStatements . Add ( new PreparedStatement ( response . StatementId , statement , columns , parameters ) ) ;
147
173
}
148
174
149
- m_connection . Session . AddPreparedStatement ( CommandText , new PreparedStatements ( preparedStatements , parsedStatements ) ) ;
175
+ Connection . Session . AddPreparedStatement ( CommandText , new PreparedStatements ( preparedStatements , parsedStatements ) ) ;
150
176
}
151
177
152
178
public override string CommandText
0 commit comments