@@ -9,11 +9,13 @@ namespace MySqlConnector.Core
9
9
{
10
10
internal sealed class StatementPreparer
11
11
{
12
+ public StatementPreparerOptions Options { get ; }
13
+
12
14
public StatementPreparer ( string commandText , MySqlParameterCollection ? parameters , StatementPreparerOptions options )
13
15
{
14
16
m_commandText = commandText ;
15
17
m_parameters = parameters ;
16
- m_options = options ;
18
+ Options = options ;
17
19
}
18
20
19
21
public ParsedStatements SplitStatements ( )
@@ -42,7 +44,7 @@ public bool ParseAndBindParameters(ByteBufferWriter writer)
42
44
private int GetParameterIndex ( string name )
43
45
{
44
46
var index = m_parameters ? . NormalizedIndexOf ( name ) ?? - 1 ;
45
- if ( index == - 1 && ( m_options & StatementPreparerOptions . AllowUserVariables ) == 0 )
47
+ if ( index == - 1 && ( Options & StatementPreparerOptions . AllowUserVariables ) == 0 )
46
48
throw new MySqlException ( "Parameter '{0}' must be defined. To use this as a variable, set 'Allow User Variables=true' in the connection string." . FormatInvariant ( name ) ) ;
47
49
return index ;
48
50
}
@@ -52,24 +54,24 @@ private MySqlParameter GetInputParameter(int index)
52
54
if ( index >= ( m_parameters ? . Count ?? 0 ) )
53
55
throw new MySqlException ( "Parameter index {0} is invalid when only {1} parameter{2} defined." . FormatInvariant ( index , m_parameters ? . Count ?? 0 , m_parameters ? . Count == 1 ? " is" : "s are" ) ) ;
54
56
var parameter = m_parameters ! [ index ] ;
55
- if ( parameter . Direction != ParameterDirection . Input && ( m_options & StatementPreparerOptions . AllowOutputParameters ) == 0 )
57
+ if ( parameter . Direction != ParameterDirection . Input && ( Options & StatementPreparerOptions . AllowOutputParameters ) == 0 )
56
58
throw new MySqlException ( "Only ParameterDirection.Input is supported when CommandType is Text (parameter name: {0})" . FormatInvariant ( parameter . ParameterName ) ) ;
57
59
return parameter ;
58
60
}
59
61
60
62
private sealed class ParameterSqlParser : SqlParser
61
63
{
62
64
public ParameterSqlParser ( StatementPreparer preparer , ByteBufferWriter writer )
65
+ : base ( preparer )
63
66
{
64
- m_preparer = preparer ;
65
67
m_writer = writer ;
66
68
}
67
69
68
70
public bool IsComplete { get ; private set ; }
69
71
70
72
protected override void OnNamedParameter ( int index , int length )
71
73
{
72
- var parameterIndex = m_preparer . GetParameterIndex ( m_preparer . m_commandText . Substring ( index , length ) ) ;
74
+ var parameterIndex = Preparer . GetParameterIndex ( Preparer . m_commandText . Substring ( index , length ) ) ;
73
75
if ( parameterIndex != - 1 )
74
76
DoAppendParameter ( parameterIndex , index , length ) ;
75
77
}
@@ -82,23 +84,22 @@ protected override void OnPositionalParameter(int index)
82
84
83
85
private void DoAppendParameter ( int parameterIndex , int textIndex , int textLength )
84
86
{
85
- m_writer . Write ( m_preparer . m_commandText , m_lastIndex , textIndex - m_lastIndex ) ;
86
- var parameter = m_preparer . GetInputParameter ( parameterIndex ) ;
87
- parameter . AppendSqlString ( m_writer , m_preparer . m_options ) ;
87
+ m_writer . Write ( Preparer . m_commandText , m_lastIndex , textIndex - m_lastIndex ) ;
88
+ var parameter = Preparer . GetInputParameter ( parameterIndex ) ;
89
+ parameter . AppendSqlString ( m_writer , Preparer . Options ) ;
88
90
m_lastIndex = textIndex + textLength ;
89
91
}
90
92
91
93
protected override void OnParsed ( FinalParseStates states )
92
94
{
93
- m_writer . Write ( m_preparer . m_commandText , m_lastIndex , m_preparer . m_commandText . Length - m_lastIndex ) ;
95
+ m_writer . Write ( Preparer . m_commandText , m_lastIndex , Preparer . m_commandText . Length - m_lastIndex ) ;
94
96
if ( ( states & FinalParseStates . NeedsNewline ) == FinalParseStates . NeedsNewline )
95
97
m_writer . Write ( ( byte ) '\n ' ) ;
96
98
if ( ( states & FinalParseStates . NeedsSemicolon ) == FinalParseStates . NeedsSemicolon )
97
99
m_writer . Write ( ( byte ) ';' ) ;
98
100
IsComplete = ( states & FinalParseStates . Complete ) == FinalParseStates . Complete ;
99
101
}
100
102
101
- readonly StatementPreparer m_preparer ;
102
103
readonly ByteBufferWriter m_writer ;
103
104
int m_currentParameterIndex ;
104
105
int m_lastIndex ;
@@ -107,8 +108,8 @@ protected override void OnParsed(FinalParseStates states)
107
108
private sealed class PreparedCommandSqlParser : SqlParser
108
109
{
109
110
public PreparedCommandSqlParser ( StatementPreparer preparer , List < ParsedStatement > statements , List < int > statementStartEndIndexes , ByteBufferWriter writer )
111
+ : base ( preparer )
110
112
{
111
- m_preparer = preparer ;
112
113
m_statements = statements ;
113
114
m_statementStartEndIndexes = statementStartEndIndexes ;
114
115
m_writer = writer ;
@@ -124,7 +125,7 @@ protected override void OnStatementBegin(int index)
124
125
125
126
protected override void OnNamedParameter ( int index , int length )
126
127
{
127
- var parameterName = m_preparer . m_commandText . Substring ( index , length ) ;
128
+ var parameterName = Preparer . m_commandText . Substring ( index , length ) ;
128
129
DoAppendParameter ( parameterName , - 1 , index , length ) ;
129
130
}
130
131
@@ -137,7 +138,7 @@ protected override void OnPositionalParameter(int index)
137
138
private void DoAppendParameter ( string ? parameterName , int parameterIndex , int textIndex , int textLength )
138
139
{
139
140
// write all SQL up to the parameter
140
- m_writer . Write ( m_preparer . m_commandText , m_lastIndex , textIndex - m_lastIndex ) ;
141
+ m_writer . Write ( Preparer . m_commandText , m_lastIndex , textIndex - m_lastIndex ) ;
141
142
m_lastIndex = textIndex + textLength ;
142
143
143
144
// replace the parameter with a ? placeholder
@@ -150,12 +151,11 @@ private void DoAppendParameter(string? parameterName, int parameterIndex, int te
150
151
151
152
protected override void OnStatementEnd ( int index )
152
153
{
153
- m_writer . Write ( m_preparer . m_commandText , m_lastIndex , index - m_lastIndex ) ;
154
+ m_writer . Write ( Preparer . m_commandText , m_lastIndex , index - m_lastIndex ) ;
154
155
m_lastIndex = index ;
155
156
m_statementStartEndIndexes . Add ( m_writer . Position ) ;
156
157
}
157
158
158
- readonly StatementPreparer m_preparer ;
159
159
readonly List < ParsedStatement > m_statements ;
160
160
readonly List < int > m_statementStartEndIndexes ;
161
161
readonly ByteBufferWriter m_writer ;
@@ -166,6 +166,5 @@ protected override void OnStatementEnd(int index)
166
166
167
167
readonly string m_commandText ;
168
168
readonly MySqlParameterCollection ? m_parameters ;
169
- readonly StatementPreparerOptions m_options ;
170
169
}
171
170
}
0 commit comments