Repository navigation
Bitstring, "char" type, parameter fixes. #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,32 +423,78 @@ void PrepareInternal() | |
|
|
||
| _preparedCommandText = GetCommandText(true); | ||
| NpgsqlRowDescription returnRowDesc = null; | ||
| ParameterDescriptionResponse parameterDescription = null; | ||
|
|
||
|
|
||
| var numInputParameters = 0; | ||
| for (var i = 0; i < _parameters.Count; i++) | ||
| { | ||
| if (_parameters[i].IsInputDirection) | ||
| { | ||
| numInputParameters++; | ||
| } | ||
| } | ||
|
|
||
| var parameterTypeOIDs = new int[numInputParameters]; | ||
|
|
||
| for (int i = 0, j = 0; i < _parameters.Count; i++) | ||
| { | ||
| if (_parameters[i].IsInputDirection) | ||
| { | ||
| parameterTypeOIDs[j++] = _parameters[i].TypeInfo.NpgsqlDbType == NpgsqlDbType.Unknown ? 0 : _connector.OidToNameMapping[_parameters[i].TypeInfo.Name].OID; | ||
| } | ||
| } | ||
|
|
||
| // Write Parse, Describe, and Sync messages to the wire. | ||
| _connector.SendParse(_planName, _preparedCommandText, new int[] { }); | ||
| _connector.SendParse(_planName, _preparedCommandText, parameterTypeOIDs); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using the parameters collection to populate the parameterTypeOids in a 1-1 mapping, you can get a problem when the same parameter is used more than one time in the query string. For example, in the following query: or, you also could invert the order of the parameters:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I thought about this problem, my idea was to populate this parameterTypeOIDs collection from the procedure which handled the parameters substitution because there you would know which parameters were being used in the query and its order.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of appearance of the parameters in the query is not important. It is the order in the parameters collection that matters.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent! I didn't notice that the parameter substitution logic already took care of the parameters mapping in the query string regardless of their position and/or repetition when preparing the command. Parameters in the prepared query are numbered in the same order as they appear in the parameters list! Sweet!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more comment (maybe I'm confused). What happens if this is a function call and some parameters are OUT parameters? If we're only sending OIDs for input parameters can't the order get confused?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Therefore the IsInputDirection check. In the parameter substitution logic, the number each input parameter get is the index of input parameter in the parameter collection. So it will be correct. This was not handled correctly before. |
||
| _connector.SendDescribeStatement(_planName); | ||
| _connector.SendSync(); | ||
|
|
||
| // Tell to mediator what command is being sent. | ||
| _connector.Mediator.SetSqlSent(_preparedCommandText, NpgsqlMediator.SQLSentType.Parse); | ||
|
|
||
|
|
||
| // Look for ParameterDescriptionResponse, NpgsqlRowDescription in the responses, discarding everything else. | ||
| while (true) | ||
| { | ||
| var msg = _connector.ReadMessage(); | ||
| if (msg is ReadyForQueryMsg) { | ||
| break; | ||
| } | ||
| var asParameterDescription = msg as ParameterDescriptionResponse; | ||
| if (asParameterDescription != null) | ||
| { | ||
| parameterDescription = asParameterDescription; | ||
| continue; | ||
| } | ||
| var asRowDesc = msg as NpgsqlRowDescription; | ||
| if (asRowDesc != null) { | ||
| if (asRowDesc != null) | ||
| { | ||
| returnRowDesc = asRowDesc; | ||
| continue; | ||
| } | ||
| var asDisposable = msg as IDisposable; | ||
| if (asDisposable != null) { | ||
| if (asDisposable != null) | ||
| { | ||
| asDisposable.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| if (parameterDescription != null) | ||
| { | ||
| if (parameterDescription.TypeOIDs.Length != numInputParameters) | ||
| { | ||
| throw new InvalidOperationException("Wrong number of parameters. Got " + numInputParameters + " but expected " + parameterDescription.TypeOIDs.Length + "."); | ||
| } | ||
|
|
||
| for (int i = 0, j = 0; j < numInputParameters; i++) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse array from above rather than reiterating?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you mean? |
||
| { | ||
| if (_parameters[i].IsInputDirection) | ||
| { | ||
| _parameters[i].SetTypeOID(parameterDescription.TypeOIDs[j++], _connector.NativeToBackendTypeConverterOptions); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (returnRowDesc != null) | ||
| { | ||
| _resultFormatCodes = new short[returnRowDesc.NumFields]; | ||
|
|
@@ -532,7 +578,7 @@ bool CheckFunctionNeedsColumnDefinitionList() | |
| var seenDef = false; | ||
| foreach (NpgsqlParameter p in Parameters) | ||
| { | ||
| if ((p.Direction == ParameterDirection.Input) || (p.Direction == ParameterDirection.InputOutput)) | ||
| if (p.IsInputDirection) | ||
| { | ||
| parameterTypes.Append(Connection.Connector.OidToNameMapping[p.TypeInfo.Name].OID.ToString() + " "); | ||
| } | ||
|
|
@@ -717,10 +763,7 @@ void AppendParameterPlaceHolders(Stream dest) | |
| { | ||
| var parameter = _parameters[i]; | ||
|
|
||
| if ( | ||
| (parameter.Direction == ParameterDirection.Input) || | ||
| (parameter.Direction == ParameterDirection.InputOutput) | ||
| ) | ||
| if (parameter.IsInputDirection) | ||
| { | ||
| if (first) | ||
| { | ||
|
|
@@ -738,22 +781,15 @@ void AppendParameterPlaceHolders(Stream dest) | |
|
|
||
| void AppendParameterPlaceHolder(Stream dest, NpgsqlParameter parameter, int paramNumber) | ||
| { | ||
| var parameterSize = ""; | ||
|
|
||
| dest.WriteByte((byte)ASCIIBytes.ParenLeft); | ||
|
|
||
| if (parameter.TypeInfo.UseSize && (parameter.Size > 0)) | ||
| if (parameter.UseCast && parameter.Size > 0) | ||
| { | ||
| parameterSize = string.Format("({0})", parameter.Size); | ||
| } | ||
|
|
||
| if (parameter.UseCast) | ||
| { | ||
| dest.WriteString("${0}::{1}{2}", paramNumber, parameter.TypeInfo.CastName, parameterSize); | ||
| dest.WriteString("${0}::{1}", paramNumber, parameter.TypeInfo.GetCastName(parameter.Size)); | ||
| } | ||
| else | ||
| { | ||
| dest.WriteString("${0}{1}", paramNumber, parameterSize); | ||
| dest.WriteString("$" + paramNumber); | ||
| } | ||
|
|
||
| dest.WriteByte((byte)ASCIIBytes.ParenRight); | ||
|
|
@@ -767,10 +803,7 @@ void AppendParameterValues(Stream dest) | |
| { | ||
| var parameter = _parameters[i]; | ||
|
|
||
| if ( | ||
| (parameter.Direction == ParameterDirection.Input) || | ||
| (parameter.Direction == ParameterDirection.InputOutput) | ||
| ) | ||
| if (parameter.IsInputDirection) | ||
| { | ||
| if (first) | ||
| { | ||
|
|
@@ -801,12 +834,7 @@ void AppendParameterValue(Stream dest, NpgsqlParameter parameter) | |
|
|
||
| if (parameter.UseCast) | ||
| { | ||
| dest.WriteString("::{0}", parameter.TypeInfo.CastName); | ||
|
|
||
| if (parameter.TypeInfo.UseSize && (parameter.Size > 0)) | ||
| { | ||
| dest.WriteString("({0})", parameter.Size); | ||
| } | ||
| dest.WriteString("::{0}", parameter.TypeInfo.GetCastName(parameter.Size)); | ||
| } | ||
|
|
||
| dest.WriteByte((byte)ASCIIBytes.ParenRight); | ||
|
|
@@ -869,9 +897,12 @@ bool AppendCommandReplacingParameterValues(Stream dest, string src, bool prepare | |
| { | ||
| paramOrdinalMap = new Dictionary<NpgsqlParameter, int>(); | ||
|
|
||
| for (int i = 0; i < _parameters.Count; i++) | ||
| for (int i = 0, cnt = 0; i < _parameters.Count; i++) | ||
| { | ||
| paramOrdinalMap[_parameters[i]] = i + 1; | ||
| if (_parameters[i].IsInputDirection) | ||
| { | ||
| paramOrdinalMap[_parameters[i]] = ++cnt; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -946,7 +977,7 @@ bool AppendCommandReplacingParameterValues(Stream dest, string src, bool prepare | |
|
|
||
| if (_parameters.TryGetValue(paramName, out parameter)) | ||
| { | ||
| if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput) | ||
| if (parameter.IsInputDirection) | ||
| { | ||
| if (prepare) | ||
| { | ||
|
|
@@ -1260,19 +1291,14 @@ byte[] GetExecuteCommandText() | |
|
|
||
| byte[] serialization; | ||
|
|
||
| serialization = p.TypeInfo.ConvertToBackend(p.Value, false, Connector.NativeToBackendTypeConverterOptions); | ||
| serialization = p.TypeInfo.ConvertToBackend(p.NpgsqlValue, false, Connector.NativeToBackendTypeConverterOptions); | ||
|
|
||
| result.WriteBytes(serialization); | ||
| result.WriteByte((byte)ASCIIBytes.ParenRight); | ||
|
|
||
| if (p.UseCast) | ||
| { | ||
| result.WriteString(string.Format("::{0}", p.TypeInfo.CastName)); | ||
|
|
||
| if (p.TypeInfo.UseSize && (p.Size > 0)) | ||
| { | ||
| result.WriteString("({0})", p.Size); | ||
| } | ||
| result.WriteString(string.Format("::{0}", p.TypeInfo.GetCastName(p.Size))); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should probably be Linqified, something along the lines of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to, but if we strive for zero allocation that is not an option.
That example would make at least 7 allocations.
3 enumerator objects.
2 lambda function objects that contain captured variables.
ToArray doesn't know in advance how many elements to put in the array, so
it creates a buffer which size is doubled and copied on overflow and
finally copies all elements to a new destination array.
We also get function call overhead.
Den 21 okt 2014 10:41 skrev "Shay Rojansky" [email protected]: