| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using CBAM.Abstractions; |
| | 19 | | using System; |
| | 20 | | using System.Collections.Generic; |
| | 21 | | using System.IO; |
| | 22 | | using System.Linq; |
| | 23 | | using System.Reflection; |
| | 24 | | using System.Text; |
| | 25 | | using System.Threading; |
| | 26 | | using System.Threading.Tasks; |
| | 27 | | using UtilPack; |
| | 28 | |
|
| | 29 | |
|
| | 30 | | namespace CBAM.SQL.PostgreSQL.Implementation |
| | 31 | | { |
| | 32 | | using TextSizeAdditionalInfo = ValueTuple<BackendSizeInfo[], ValueTuple<Int32[], Int32[], Int32>>; |
| | 33 | |
|
| | 34 | | internal sealed class PgSQLTypeFunctionalityForArrays : PgSQLTypeFunctionality |
| | 35 | | { |
| | 36 | | internal const Byte ARRAY_START = (Byte) '{'; |
| | 37 | | internal const Byte ARRAY_END = (Byte) '}'; |
| | 38 | | internal const Char ESCAPE = '\\'; |
| | 39 | | internal const Char QUOTE = '"'; |
| | 40 | | internal const Byte ARRAY_LOBO_START = (Byte) '['; |
| | 41 | | internal const Byte ARRAY_LOBO_END = (Byte) ']'; |
| | 42 | | internal const Byte ARRAY_LOBO_SPEC_SEPARATOR = (Byte) '='; |
| | 43 | | internal const Char DIM_SEPARATOR = ':'; |
| | 44 | | internal const Int32 NULL_CHAR_COUNT = 4; |
| | 45 | | internal const String NULL_STRING = "NULL"; |
| | 46 | |
|
| | 47 | | private readonly Array _emptyArray; |
| | 48 | | private readonly Type _arrayElementType; |
| | 49 | | private readonly Lazy<TypeFunctionalityInformation> _elementTypeInfo; |
| | 50 | |
|
| 534 | 51 | | public PgSQLTypeFunctionalityForArrays( |
| 534 | 52 | | TypeRegistry protocol, |
| 534 | 53 | | ref Type arrayElementType, |
| 534 | 54 | | Int32 elementTypeID |
| 534 | 55 | | ) |
| | 56 | | { |
| 544 | 57 | | this._elementTypeInfo = new Lazy<TypeFunctionalityInformation>( () => protocol.TryGetTypeInfo( elementTypeID ), |
| 534 | 58 | | if ( arrayElementType.GetTypeInfo().IsValueType && !arrayElementType.IsNullable() ) |
| | 59 | | { |
| | 60 | | // Allow nulls |
| 408 | 61 | | arrayElementType = typeof( Nullable<> ).MakeGenericType( arrayElementType ); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // TODO maybe make CLRType getter throw exception? Since the actual type may be X[], X[,], X[,,], etc...? |
| 534 | 65 | | this._arrayElementType = arrayElementType; |
| 534 | 66 | | this._emptyArray = Array.CreateInstance( arrayElementType, 0 ); |
| 534 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | public Boolean SupportsReadingBinaryFormat => this._elementTypeInfo.Value.Functionality.SupportsReadingBinaryForma |
| | 70 | |
|
| 45 | 71 | | public Boolean SupportsWritingBinaryFormat => this._elementTypeInfo.Value.Functionality.SupportsWritingBinaryForma |
| | 72 | |
|
| | 73 | | public Object ChangeTypeFrameworkToPgSQL( PgSQLTypeDatabaseData dbData, Object obj ) |
| | 74 | | { |
| | 75 | | // We will enter here for multidimensional arrays, from BindMessage |
| 11 | 76 | | var objType = obj.GetType(); |
| 12 | 77 | | return objType.IsArray |
| 12 | 78 | | && EqualsIgnoreNullability( objType.GetElementType(), this._arrayElementType ) ? // The WriteArrayText and W |
| 12 | 79 | | obj : |
| 12 | 80 | | throw new InvalidCastException( $"The object to cast must be single- or multidimensionsal array with elem |
| | 81 | | } |
| | 82 | |
|
| | 83 | | private static Boolean EqualsIgnoreNullability( Type x, Type y ) |
| | 84 | | { |
| 12 | 85 | | return Equals( x, y ) |
| 12 | 86 | | || Equals( GetActualIfNullable( x ), GetActualIfNullable( y ) ); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private static Type GetActualIfNullable( Type type ) |
| | 90 | | { |
| 0 | 91 | | return type.IsNullable( out var actual ) ? actual : type; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | public Object ChangeTypePgSQLToFramework( PgSQLTypeDatabaseData dbData, Object obj, Type typeTo ) |
| | 95 | | { |
| | 96 | | // TODO cast all elements of array...? |
| 0 | 97 | | throw new InvalidCastException(); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | public BackendSizeInfo GetBackendSize( DataFormat dataFormat, PgSQLTypeDatabaseData boundData, BackendABIHelper he |
| | 101 | | { |
| 32 | 102 | | switch ( dataFormat ) |
| | 103 | | { |
| | 104 | | case DataFormat.Text: |
| 8 | 105 | | return this.GetBackendTextSize( boundData, helper, value ); |
| | 106 | | case DataFormat.Binary: |
| 23 | 107 | | return this.GetBackendBinarySize( boundData, helper, value ); |
| | 108 | | default: |
| 0 | 109 | | throw new NotSupportedException( $"Data format {dataFormat} is not recognized." ); |
| | 110 | | } |
| | 111 | | } |
| | 112 | |
|
| | 113 | | private BackendSizeInfo GetBackendBinarySize( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Object val |
| | 114 | | { |
| 22 | 115 | | var array = (Array) value; |
| | 116 | | BackendSizeInfo retVal; |
| 23 | 117 | | var arrayLength = array.Length; |
| | 118 | | // The header size is three integers (rank, null map, element type id), and then two integers for each rank |
| 23 | 119 | | var size = sizeof( Int32 ) * 3; |
| | 120 | | BackendSizeInfo[] elementSizes; |
| 24 | 121 | | if ( arrayLength > 0 ) |
| | 122 | | { |
| 21 | 123 | | size += array.Rank * 2 * sizeof( Int32 ); |
| 21 | 124 | | elementSizes = new BackendSizeInfo[arrayLength]; |
| 18 | 125 | | var i = 0; |
| 18 | 126 | | var elementInfo = this._elementTypeInfo.Value; |
| 179 | 127 | | foreach ( var elem in array ) |
| | 128 | | { |
| 71 | 129 | | var sizeInfo = elementInfo.Functionality.GetBackendSizeCheckNull( DataFormat.Binary, elementInfo.Database |
| 70 | 130 | | elementSizes[i++] = sizeInfo; |
| 70 | 131 | | size += sizeof( Int32 ); |
| 71 | 132 | | if ( sizeInfo.ByteCount > 0 ) |
| | 133 | | { |
| 62 | 134 | | size += sizeInfo.ByteCount; |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |
| | 138 | | else |
| | 139 | | { |
| 3 | 140 | | elementSizes = null; |
| | 141 | | } |
| 20 | 142 | | retVal = new BackendSizeInfo( size, elementSizes ); |
| | 143 | |
|
| 22 | 144 | | return retVal; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | private BackendSizeInfo GetBackendTextSize( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Object value |
| | 148 | | { |
| 8 | 149 | | var array = (Array) value; |
| 8 | 150 | | var helperEncoding = helper.Encoding; |
| 8 | 151 | | var encoding = helperEncoding.Encoding; |
| 8 | 152 | | var asciiSize = helperEncoding.BytesPerASCIICharacter; |
| 8 | 153 | | var length = array.Length; |
| | 154 | | BackendSizeInfo retVal; |
| 8 | 155 | | if ( length <= 0 ) |
| | 156 | | { |
| 1 | 157 | | retVal = new BackendSizeInfo( 2 * asciiSize ); |
| 1 | 158 | | } |
| | 159 | | else |
| | 160 | | { |
| 7 | 161 | | var rank = array.Rank; |
| 7 | 162 | | var bracesCount = 0; // Amount of array start/end braces |
| 7 | 163 | | var innermostLength = array.GetLength( rank - 1 ); |
| 7 | 164 | | var delimitersCount = Math.Max( innermostLength - 1, 0 ); // Amount of delimiter characters |
| | 165 | | // Iterate from second-innermost dimension towards |
| 22 | 166 | | for ( var i = rank - 2; i >= 0; --i ) |
| | 167 | | { |
| 4 | 168 | | var curLen = array.GetLength( i ); |
| 4 | 169 | | bracesCount = bracesCount * curLen + 2 * curLen; |
| 4 | 170 | | delimitersCount = curLen - 1 + delimitersCount * curLen; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | // Remember outermost braces |
| 7 | 174 | | bracesCount += 2; |
| 7 | 175 | | var elementSizes = new BackendSizeInfo[length]; |
| 7 | 176 | | var elementInfo = this._elementTypeInfo.Value; |
| 7 | 177 | | var j = 0; |
| 62 | 178 | | foreach ( var elem in array ) |
| | 179 | | { |
| 24 | 180 | | elementSizes[j++] = elementInfo.Functionality.GetBackendSizeCheckNull( DataFormat.Text, elementInfo.Datab |
| | 181 | | } |
| | 182 | |
|
| | 183 | | // All the space taken by array structure information |
| 7 | 184 | | var sizeForArrayInfra = encoding.GetByteCount( boundData.ArrayDelimiter ) * delimitersCount + bracesCount * |
| 7 | 185 | | var lobos = array.GetLowerBounds(); |
| 7 | 186 | | var loboSpecByteCount = 0; |
| 7 | 187 | | Int32[] upbos = null; |
| 7 | 188 | | if ( lobos != null ) |
| | 189 | | { |
| | 190 | | // Bounds specification: "[lobo1:upbo1][lobo2:upbo2]...=" |
| 2 | 191 | | loboSpecByteCount = rank * 3 * asciiSize + asciiSize; // Amount of '[', ']', ':', and '=' |
| 2 | 192 | | upbos = new Int32[rank]; |
| 12 | 193 | | for ( var i = 0; i < rank; ++i ) |
| | 194 | | { |
| | 195 | | // Remember that (Pg)SQL array indexing starts from 1 by default |
| 4 | 196 | | upbos[i] = array.GetUpperBound( i ) + 1; |
| 4 | 197 | | ++lobos[i]; |
| | 198 | | // Increment spec count |
| 4 | 199 | | loboSpecByteCount += helperEncoding.GetTextualIntegerRepresentationSize( lobos[i] ) + helperEncoding.G |
| | 200 | | } |
| 2 | 201 | | sizeForArrayInfra += loboSpecByteCount; |
| | 202 | | } |
| | 203 | |
|
| 7 | 204 | | var nullSize = NULL_CHAR_COUNT * asciiSize; |
| 7 | 205 | | retVal = new BackendSizeInfo( |
| 31 | 206 | | sizeForArrayInfra + elementSizes.Aggregate( 0, ( cur, item ) => cur + ( item.ByteCount >= 0 ? item.ByteCo |
| 7 | 207 | | (elementSizes, (lobos, upbos, loboSpecByteCount)) |
| 7 | 208 | | ); |
| | 209 | | } |
| | 210 | |
|
| 8 | 211 | | return retVal; |
| | 212 | | } |
| | 213 | |
|
| | 214 | | public async ValueTask<Object> ReadBackendValueAsync( |
| | 215 | | DataFormat dataFormat, |
| | 216 | | PgSQLTypeDatabaseData boundData, |
| | 217 | | BackendABIHelper helper, |
| | 218 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | 219 | | ) |
| | 220 | | { |
| | 221 | | Array retVal; |
| 39 | 222 | | switch ( dataFormat ) |
| | 223 | | { |
| | 224 | | case DataFormat.Text: |
| 40 | 225 | | if ( stream.TotalByteCount > 2 * helper.Encoding.BytesPerASCIICharacter ) |
| | 226 | | { |
| 35 | 227 | | retVal = await this.ReadArrayText( boundData, helper, stream ); |
| 34 | 228 | | } |
| | 229 | | else |
| | 230 | | { |
| | 231 | | // Empty array |
| 5 | 232 | | retVal = this._emptyArray; |
| | 233 | | } |
| 5 | 234 | | break; |
| | 235 | | case DataFormat.Binary: |
| 0 | 236 | | retVal = await this.ReadArrayBinary( boundData, helper, stream ); |
| 0 | 237 | | break; |
| | 238 | | default: |
| 0 | 239 | | throw new NotSupportedException( $"Data format {dataFormat} is not recognized." ); |
| | 240 | | } |
| | 241 | |
|
| 40 | 242 | | return retVal; |
| 40 | 243 | | } |
| | 244 | |
|
| | 245 | | public async Task WriteBackendValueAsync( |
| | 246 | | DataFormat dataFormat, |
| | 247 | | PgSQLTypeDatabaseData boundData, |
| | 248 | | BackendABIHelper helper, |
| | 249 | | StreamWriterWithResizableBufferAndLimitedSize stream, |
| | 250 | | Object value, |
| | 251 | | BackendSizeInfo sizeInfo, |
| | 252 | | Boolean isArrayElement |
| | 253 | | ) |
| | 254 | | { |
| 29 | 255 | | switch ( dataFormat ) |
| | 256 | | { |
| | 257 | | case DataFormat.Text: |
| 8 | 258 | | await this.WriteArrayText( boundData, helper, stream, (Array) value, sizeInfo ); |
| 8 | 259 | | break; |
| | 260 | | case DataFormat.Binary: |
| 24 | 261 | | await this.WriteArrayBinary( boundData, helper, stream, (Array) value, sizeInfo ); |
| 22 | 262 | | break; |
| | 263 | | default: |
| 0 | 264 | | throw new NotSupportedException( $"Data format {dataFormat} is not recognized." ); |
| | 265 | | } |
| 29 | 266 | | } |
| | 267 | |
|
| | 268 | | private async Task<Array> ReadArrayText( |
| | 269 | | PgSQLTypeDatabaseData boundData, |
| | 270 | | BackendABIHelper helper, |
| | 271 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | 272 | | ) |
| | 273 | | { |
| 34 | 274 | | (var rank, var lobos, var lengths, var retVal) = await this.ReadArrayTextHeader( boundData, helper, stream ); |
| | 275 | |
|
| | 276 | | // Use exponentially expanding array instead of list. That way we can use Array.Copy right away when creating r |
| 35 | 277 | | var useTempArray = retVal == null; |
| 35 | 278 | | var totalCount = 0; |
| | 279 | | // We only need temporary array if we are creating array without dimension specification prefix |
| | 280 | | Int32[] retValIndices; |
| | 281 | | ResizableArray<Object> tempArray; |
| | 282 | | Int32 arrayDelimiterByteCount; |
| 35 | 283 | | if ( useTempArray ) |
| | 284 | | { |
| 25 | 285 | | tempArray = new ResizableArray<Object>( initialSize: 2, exponentialResize: true ); |
| 23 | 286 | | lengths = new Int32[rank]; |
| 25 | 287 | | retValIndices = null; |
| 24 | 288 | | arrayDelimiterByteCount = -1; |
| 25 | 289 | | } |
| | 290 | | else |
| | 291 | | { |
| 10 | 292 | | retValIndices = new Int32[rank]; |
| 10 | 293 | | Array.Copy( lobos, retValIndices, rank ); |
| 10 | 294 | | tempArray = null; |
| 10 | 295 | | arrayDelimiterByteCount = helper.Encoding.Encoding.GetByteCount( boundData.ArrayDelimiter ); |
| | 296 | | } |
| | 297 | |
|
| | 298 | | // We start with innermost array |
| 34 | 299 | | var innermostArrayIndex = rank - 1; |
| 34 | 300 | | var lowestEncounteredArrayEnd = innermostArrayIndex; |
| 34 | 301 | | var asciiSize = helper.Encoding.BytesPerASCIICharacter; |
| 35 | 302 | | Boolean hasMore = true; |
| 155 | 303 | | while ( hasMore ) |
| | 304 | | { |
| 120 | 305 | | (var value, var ending) = await this.ReadArrayElementText( boundData, helper, stream ); |
| | 306 | |
|
| 119 | 307 | | if ( useTempArray ) |
| | 308 | | { |
| 78 | 309 | | tempArray.CurrentMaxCapacity = totalCount + 1; |
| 80 | 310 | | tempArray.Array[totalCount++] = value; |
| 80 | 311 | | if ( lowestEncounteredArrayEnd == innermostArrayIndex ) |
| | 312 | | { |
| 30 | 313 | | ++lengths[innermostArrayIndex]; |
| | 314 | | } |
| | 315 | |
|
| | 316 | | // If array end encountered, we must find start of next element, if possible |
| 80 | 317 | | if ( ending == ElementEndingWay.ArrayEnd ) |
| | 318 | | { |
| 50 | 319 | | (lowestEncounteredArrayEnd, lengths, hasMore) = await this.ReadArrayTextDimensionEnd( |
| 50 | 320 | | boundData, |
| 50 | 321 | | helper, |
| 50 | 322 | | stream, |
| 50 | 323 | | lowestEncounteredArrayEnd, |
| 50 | 324 | | lengths, |
| 50 | 325 | | rank |
| 50 | 326 | | ); |
| | 327 | | } |
| 48 | 328 | | } |
| | 329 | | else |
| | 330 | | { |
| 40 | 331 | | retVal.SetValue( value, retValIndices ); |
| 40 | 332 | | var dimsEnded = MoveNextMultiDimensionalIndex( lengths, retValIndices, lobos ); |
| | 333 | | // At this point, the ReadArrayElementText method has already read either complete array delimiter, or on |
| | 334 | | // So we only need to skip thru bytes if we have read array end character (dimsEnded > 0) |
| 40 | 335 | | if ( dimsEnded > 0 ) |
| | 336 | | { |
| 15 | 337 | | hasMore = dimsEnded < rank; |
| 15 | 338 | | if ( hasMore ) |
| | 339 | | { |
| | 340 | | // Skip thru array end, array delimiter, and array start characters |
| 5 | 341 | | await stream.ReadMoreOrThrow( ( dimsEnded * 2 - 1 ) * asciiSize + arrayDelimiterByteCount ); |
| | 342 | | } |
| | 343 | | } |
| | 344 | | } |
| | 345 | |
|
| 119 | 346 | | stream.EraseReadBytesFromBuffer(); |
| | 347 | | } |
| | 348 | |
|
| | 349 | | // Now, construct the actual array to return, if needed |
| | 350 | | // If array to be returned is null at this stage, this means that no lower bound specifications were given. |
| 34 | 351 | | if ( retVal == null ) |
| | 352 | | { |
| 25 | 353 | | if ( rank == 1 ) |
| | 354 | | { |
| | 355 | | // Create normal one-dimensional array (we always need to create it, since we must return X[] instead of |
| 20 | 356 | | retVal = Array.CreateInstance( this._arrayElementType, totalCount ); |
| | 357 | | // Populate it |
| 19 | 358 | | Array.Copy( tempArray.Array, retVal, totalCount ); |
| 20 | 359 | | } |
| | 360 | | else |
| | 361 | | { |
| | 362 | | // Create multi-dimensional array |
| 5 | 363 | | retVal = Array.CreateInstance( this._arrayElementType, lengths ); |
| | 364 | | // Populate it |
| 5 | 365 | | var curIndices = new Int32[lengths.Length]; |
| 5 | 366 | | var idx = 0; |
| 5 | 367 | | var actualArray = tempArray.Array; |
| | 368 | | do |
| | 369 | | { |
| 60 | 370 | | var elem = actualArray[idx++]; |
| 60 | 371 | | if ( elem != null ) |
| | 372 | | { |
| 60 | 373 | | retVal.SetValue( elem, curIndices ); |
| | 374 | | } |
| 60 | 375 | | MoveNextMultiDimensionalIndex( lengths, curIndices ); |
| 60 | 376 | | } while ( idx < totalCount ); |
| | 377 | | } |
| | 378 | | } |
| | 379 | |
|
| 34 | 380 | | return retVal; |
| | 381 | |
|
| 35 | 382 | | } |
| | 383 | |
|
| | 384 | | private async ValueTask<(Int32 Rank, Int32[] Lobos, Int32[] Lengths, Array CreatedArray)> ReadArrayTextHeader( |
| | 385 | | PgSQLTypeDatabaseData boundData, |
| | 386 | | BackendABIHelper helper, |
| | 387 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | 388 | | ) |
| | 389 | | { |
| 34 | 390 | | stream.EraseReadBytesFromBuffer(); |
| | 391 | |
|
| | 392 | | Char curChar; |
| | 393 | | // Read the optional "[lobo1:upbo1][lobo2:upbo2]...=" dimension specification into buffer. |
| 34 | 394 | | var rank = 0; |
| 35 | 395 | | var charReader = helper.CharacterReader; |
| | 396 | | do |
| | 397 | | { |
| 155 | 398 | | curChar = await charReader.ReadNextAsync( stream ); |
| 153 | 399 | | if ( curChar == DIM_SEPARATOR ) |
| | 400 | | { |
| 20 | 401 | | ++rank; |
| | 402 | | } |
| 154 | 403 | | } while ( curChar != ARRAY_START ); |
| | 404 | |
|
| 34 | 405 | | Int32[] lobos = null; |
| 34 | 406 | | Int32[] lengths = null; |
| 33 | 407 | | Array retVal = null; |
| 34 | 408 | | if ( rank > 0 ) |
| | 409 | | { |
| | 410 | | // We encountered the explicit dimension specification. Backend should issue this only when there are 'speci |
| | 411 | | // As a bonus, we will know the array dimensions before array elements start, and we can create the array to |
| | 412 | | // instead of reading elements into temporary array |
| 10 | 413 | | lobos = new Int32[rank]; |
| 10 | 414 | | lengths = new Int32[rank]; |
| 10 | 415 | | var encoding = helper.Encoding; |
| 10 | 416 | | var asciiSize = encoding.BytesPerASCIICharacter; |
| 10 | 417 | | var byteArray = stream.Buffer; |
| 10 | 418 | | var idx = asciiSize; // Skip first '[' |
| 60 | 419 | | for ( var i = 0; i < rank; ++i ) |
| | 420 | | { |
| | 421 | | // In (Pg)SQL, lower bounds normally start at 1. So 1 translates to 0 in CLR, 0 to -1, etc. |
| 20 | 422 | | lobos[i] = encoding.ParseInt32Textual( byteArray, ref idx ) - 1; |
| 20 | 423 | | idx += asciiSize; // Skip ':' |
| 20 | 424 | | lengths[i] = encoding.ParseInt32Textual( byteArray, ref idx ) - lobos[i]; |
| 20 | 425 | | idx += asciiSize * 2; // Skip ']' and next '[' or '=' |
| | 426 | | } |
| 10 | 427 | | retVal = Array.CreateInstance( this._arrayElementType, lengths, lobos ); |
| | 428 | | } |
| | 429 | |
|
| | 430 | | // Read amount of starting '{' characters. That will be the array rank (unless we already learned about the ran |
| 33 | 431 | | rank = 0; |
| | 432 | | Int32 prevIdx; |
| | 433 | | do |
| | 434 | | { |
| 54 | 435 | | ++rank; |
| 53 | 436 | | prevIdx = stream.ReadBytesCount; |
| 53 | 437 | | curChar = await charReader.ReadNextAsync( stream ); |
| 54 | 438 | | } while ( curChar == ARRAY_START ); |
| | 439 | |
|
| 35 | 440 | | if ( retVal != null && rank != retVal.Rank ) |
| | 441 | | { |
| 0 | 442 | | throw new PgSQLException( "Backend array lower-bound specification had different rank than actual array spec |
| | 443 | | } |
| | 444 | |
|
| | 445 | | // Back one character (the one we read, that wasn't array start character) |
| 35 | 446 | | stream.UnreadBytes( stream.ReadBytesCount - prevIdx ); |
| | 447 | |
|
| | 448 | | // Remember to get rid of array start characters currently in buffer (ReadArrayElementText expects clean buffer |
| 35 | 449 | | stream.EraseReadBytesFromBuffer(); |
| | 450 | |
|
| 35 | 451 | | return (rank, lobos, lengths, retVal); |
| 35 | 452 | | } |
| | 453 | |
|
| | 454 | | private async ValueTask<(Int32 LowestEncounteredArrayEnd, Int32[] Lengths, Boolean HasMore)> ReadArrayTextDimensio |
| | 455 | | PgSQLTypeDatabaseData boundData, |
| | 456 | | BackendABIHelper helper, |
| | 457 | | StreamReaderWithResizableBufferAndLimitedSize stream, |
| | 458 | | Int32 lowestEncounteredArrayEnd, |
| | 459 | | Int32[] lengths, |
| | 460 | | Int32 rank |
| | 461 | | ) |
| | 462 | | { |
| 50 | 463 | | stream.EraseReadBytesFromBuffer(); |
| | 464 | |
|
| 49 | 465 | | var wasArrayEnd = true; |
| 50 | 466 | | var innermostArrayIndex = rank - 1; |
| 50 | 467 | | var curArrayIndex = innermostArrayIndex; |
| | 468 | | Char curChar; |
| 50 | 469 | | var charReader = helper.CharacterReader; |
| | 470 | |
|
| 90 | 471 | | while ( wasArrayEnd && --curArrayIndex >= 0 ) |
| | 472 | | { |
| | 473 | | // End current array block |
| 40 | 474 | | lowestEncounteredArrayEnd = Math.Min( lowestEncounteredArrayEnd, curArrayIndex ); |
| 40 | 475 | | if ( curArrayIndex <= lowestEncounteredArrayEnd ) |
| | 476 | | { |
| 25 | 477 | | lengths[curArrayIndex]++; |
| | 478 | | } |
| | 479 | |
|
| | 480 | | // Read next character |
| 40 | 481 | | curChar = await charReader.ReadNextAsync( stream ); |
| 40 | 482 | | wasArrayEnd = curChar == ARRAY_END; |
| | 483 | | } |
| | 484 | |
|
| 50 | 485 | | var hasMore = curArrayIndex >= 0; |
| 50 | 486 | | if ( hasMore ) |
| | 487 | | { |
| | 488 | | // More arrays follow |
| | 489 | | // Read until we are at innermost array level again |
| 55 | 490 | | while ( curArrayIndex < innermostArrayIndex ) |
| | 491 | | { |
| 30 | 492 | | curChar = await charReader.ReadNextAsync( stream ); |
| 30 | 493 | | if ( curChar == ARRAY_START ) |
| | 494 | | { |
| 30 | 495 | | ++curArrayIndex; |
| | 496 | | } |
| | 497 | | } |
| | 498 | | } |
| | 499 | |
|
| 50 | 500 | | stream.EraseReadBytesFromBuffer(); |
| | 501 | |
|
| 50 | 502 | | return (lowestEncounteredArrayEnd, lengths, hasMore); |
| 49 | 503 | | } |
| | 504 | |
|
| | 505 | | private static Int32 MoveNextMultiDimensionalIndex( |
| | 506 | | Int32[] lengths, |
| | 507 | | Int32[] indices, |
| | 508 | | Int32[] loBos = null |
| | 509 | | ) |
| | 510 | | { |
| 116 | 511 | | var i = indices.Length - 1; |
| 116 | 512 | | if ( loBos == null ) |
| | 513 | | { |
| 180 | 514 | | for ( ; i >= 0 && ++indices[i] == lengths[i]; --i ) |
| | 515 | | { |
| 52 | 516 | | indices[i] = 0; |
| | 517 | | } |
| 71 | 518 | | } |
| | 519 | | else |
| | 520 | | { |
| 90 | 521 | | for ( ; i >= 0 && ++indices[i] == lengths[i] + loBos[i]; --i ) |
| | 522 | | { |
| 25 | 523 | | indices[i] = loBos[i]; |
| | 524 | | } |
| | 525 | | } |
| | 526 | |
|
| 116 | 527 | | return lengths.Length - i - 1; |
| | 528 | | } |
| | 529 | |
|
| | 530 | | // We never encounter empty arrays when calling this, since inner empty arrays are not possible, and whole empty a |
| | 531 | | private async ValueTask<(Object Value, ElementEndingWay Ending)> ReadArrayElementText( |
| | 532 | | PgSQLTypeDatabaseData boundData, |
| | 533 | | BackendABIHelper helper, |
| | 534 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | 535 | | ) |
| | 536 | | { |
| | 537 | | // Scan for element end |
| 118 | 538 | | stream.EraseReadBytesFromBuffer(); |
| | 539 | |
|
| 120 | 540 | | var insideQuote = false; |
| 120 | 541 | | var delimLength = boundData.ArrayDelimiter.Length; |
| 119 | 542 | | var delim0 = boundData.ArrayDelimiter[0]; |
| 118 | 543 | | var delim1 = delimLength > 1 ? boundData.ArrayDelimiter[1] : '\0'; |
| | 544 | | Int32 prevIdx; |
| | 545 | | Char curChar; |
| 119 | 546 | | Char curChar2 = '\0'; |
| 119 | 547 | | Boolean wasArrayDelimiter = false; |
| 119 | 548 | | var prevWasEscape = false; |
| 119 | 549 | | var asciiSize = helper.Encoding.BytesPerASCIICharacter; |
| 119 | 550 | | var charReader = helper.CharacterReader; |
| | 551 | | Boolean continueReading; |
| | 552 | | do |
| | 553 | | { |
| 369 | 554 | | prevIdx = stream.ReadBytesCount; |
| 369 | 555 | | var charNullable = await charReader.TryReadNextAsync( stream ); |
| 369 | 556 | | continueReading = charNullable.HasValue; |
| 369 | 557 | | curChar = charNullable.GetValueOrDefault(); |
| 369 | 558 | | if ( continueReading ) |
| | 559 | | { |
| 370 | 560 | | if ( prevWasEscape ) |
| | 561 | | { |
| 5 | 562 | | prevWasEscape = false; |
| 5 | 563 | | } |
| | 564 | | else |
| | 565 | | { |
| 365 | 566 | | if ( delimLength > 1 && Char.IsHighSurrogate( curChar ) ) |
| | 567 | | { |
| 0 | 568 | | curChar2 = await charReader.TryReadNextAsync( stream ) ?? '\0'; |
| | 569 | | } |
| | 570 | |
|
| 363 | 571 | | switch ( curChar ) |
| | 572 | | { |
| | 573 | | case ESCAPE: |
| | 574 | | case QUOTE: |
| 24 | 575 | | if ( curChar == QUOTE ) |
| | 576 | | { |
| 19 | 577 | | insideQuote = !insideQuote; |
| 19 | 578 | | } |
| | 579 | | else |
| | 580 | | { |
| 5 | 581 | | prevWasEscape = true; |
| | 582 | | } |
| | 583 | | // "Shift" the array |
| 24 | 584 | | stream.EraseReadBufferSegment( prevIdx, asciiSize ); |
| 25 | 585 | | break; |
| | 586 | | default: |
| 338 | 587 | | wasArrayDelimiter = !insideQuote |
| 338 | 588 | | && ( |
| 338 | 589 | | ( delimLength == 1 && curChar == delim0 ) |
| 338 | 590 | | || ( delimLength > 1 && curChar == delim0 && curChar2 == delim1 ) |
| 338 | 591 | | ); |
| 340 | 592 | | continueReading = !wasArrayDelimiter && curChar != ARRAY_END; |
| | 593 | | break; |
| | 594 | | } |
| | 595 | | } |
| | 596 | | } |
| 370 | 597 | | } while ( continueReading ); |
| | 598 | |
|
| 120 | 599 | | var elementAndSeparatorSize = stream.ReadBytesCount; |
| 120 | 600 | | var ending = wasArrayDelimiter ? ElementEndingWay.ArrayDelimiter : ( curChar == ARRAY_END ? ElementEndingWay.Ar |
| | 601 | | Object arrayElement; |
| 120 | 602 | | if ( ending == ElementEndingWay.Abnormal || IsNullArrayElement( helper.Encoding, stream.Buffer, prevIdx ) ) |
| | 603 | | { |
| 10 | 604 | | arrayElement = null; |
| 10 | 605 | | } |
| | 606 | | else |
| | 607 | | { |
| 110 | 608 | | stream.UnreadBytes(); |
| 110 | 609 | | var elementTypeInfo = this._elementTypeInfo.Value; |
| 110 | 610 | | using ( var elementStream = stream.CreateWithLimitedSizeAndSharedBuffer( prevIdx ) ) |
| | 611 | | { |
| 110 | 612 | | arrayElement = await elementTypeInfo.Functionality.ReadBackendValueAsync( |
| 110 | 613 | | DataFormat.Text, |
| 110 | 614 | | elementTypeInfo.DatabaseData, |
| 110 | 615 | | helper, |
| 110 | 616 | | elementStream |
| 110 | 617 | | ); |
| | 618 | |
|
| 108 | 619 | | } |
| | 620 | |
|
| | 621 | | // Re-read separator |
| 110 | 622 | | await stream.ReadMoreOrThrow( elementAndSeparatorSize - prevIdx ); |
| | 623 | | } |
| | 624 | |
|
| | 625 | | // Erase all previous read data (element + separator) |
| 120 | 626 | | stream.EraseReadBytesFromBuffer(); |
| 120 | 627 | | return ( |
| 120 | 628 | | arrayElement, |
| 120 | 629 | | ending |
| 120 | 630 | | ); |
| 120 | 631 | | } |
| | 632 | |
|
| | 633 | | private const Int32 CHUNK_SIZE = 1024; |
| | 634 | |
|
| | 635 | |
|
| | 636 | |
|
| | 637 | |
|
| | 638 | | private enum ElementEndingWay |
| | 639 | | { |
| | 640 | | ArrayDelimiter, |
| | 641 | | ArrayEnd, |
| | 642 | | Abnormal |
| | 643 | | } |
| | 644 | |
|
| | 645 | | private static Boolean IsNullArrayElement( |
| | 646 | | IEncodingInfo encoding, |
| | 647 | | Byte[] array, |
| | 648 | | Int32 elementByteCount |
| | 649 | | ) |
| | 650 | | { |
| 118 | 651 | | Int32 idx = 0; |
| | 652 | | Byte lastASCIIByte; |
| 118 | 653 | | return elementByteCount == encoding.BytesPerASCIICharacter * NULL_CHAR_COUNT |
| 118 | 654 | | // Poor man's case insensitive matching |
| 118 | 655 | | && ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'N' || lastASCIIByte == 'n' ) |
| 118 | 656 | | && ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'U' || lastASCIIByte == 'u' ) |
| 118 | 657 | | && ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'L' || lastASCIIByte == 'l' ) |
| 118 | 658 | | && ( ( lastASCIIByte = encoding.ReadASCIIByte( array, ref idx ) ) == 'L' || lastASCIIByte == 'l' ) |
| 118 | 659 | | ; |
| | 660 | | } |
| | 661 | |
|
| | 662 | | private async Task<Array> ReadArrayBinary( |
| | 663 | | PgSQLTypeDatabaseData boundData, |
| | 664 | | BackendABIHelper helper, |
| | 665 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | 666 | | ) |
| | 667 | | { |
| 0 | 668 | | await stream.ReadOrThrow( sizeof( Int32 ) ); |
| 0 | 669 | | var idx = 0; |
| 0 | 670 | | var rank = stream.Buffer.ReadPgInt32( ref idx ); |
| | 671 | | Array retVal; |
| 0 | 672 | | if ( rank < 0 ) |
| | 673 | | { |
| 0 | 674 | | throw new PgSQLException( "Array rank must be zero or more." ); |
| | 675 | | } |
| 0 | 676 | | else if ( rank == 0 ) |
| | 677 | | { |
| | 678 | | // Empty array |
| 0 | 679 | | retVal = this._emptyArray; |
| 0 | 680 | | } |
| | 681 | | else |
| | 682 | | { |
| | 683 | | // Read the rest of the header (null-map (Int32), element type id (Int32), and rank infos (2 integers per ra |
| 0 | 684 | | await stream.ReadMoreOrThrow( ( 2 + 2 * rank ) * sizeof( Int32 ) ); |
| | 685 | | // Skip null-map and element type id. |
| 0 | 686 | | idx = sizeof( Int32 ) * 3; |
| | 687 | |
|
| | 688 | | // Read lengths and lower bounds for dimensions |
| 0 | 689 | | var lengths = new Int32[rank]; |
| 0 | 690 | | Int32[] loBos = null; |
| 0 | 691 | | for ( var i = 0; i < rank; ++i ) |
| | 692 | | { |
| 0 | 693 | | var curNumber = stream.Buffer.ReadPgInt32( ref idx ); |
| 0 | 694 | | lengths[i] = curNumber; |
| 0 | 695 | | curNumber = stream.Buffer.ReadPgInt32( ref idx ); |
| 0 | 696 | | if ( curNumber != 1 ) // In SQL, default min lo bo is 1. In C#, the default is 0. |
| | 697 | | { |
| 0 | 698 | | if ( loBos == null ) |
| | 699 | | { |
| 0 | 700 | | loBos = new Int32[rank]; |
| | 701 | | } |
| 0 | 702 | | loBos[i] = curNumber - 1; |
| | 703 | | } |
| | 704 | | } |
| | 705 | |
|
| | 706 | | // Create & populate array instance |
| 0 | 707 | | stream.EraseReadBytesFromBuffer(); |
| 0 | 708 | | var elemInfo = this._elementTypeInfo.Value; |
| 0 | 709 | | if ( rank == 1 && loBos == null ) |
| | 710 | | { |
| | 711 | | // Can just use normal array |
| 0 | 712 | | var len = lengths[0]; |
| 0 | 713 | | retVal = Array.CreateInstance( this._arrayElementType, len ); |
| 0 | 714 | | for ( var i = 0; i < len; ++i ) |
| | 715 | | { |
| 0 | 716 | | var curInfo = await elemInfo.Functionality.ReadBackendValueCheckNull( DataFormat.Binary, elemInfo.Data |
| 0 | 717 | | retVal.SetValue( curInfo.Value, i ); |
| | 718 | | } |
| 0 | 719 | | } |
| | 720 | | else |
| | 721 | | { |
| | 722 | | // Have to create multi-dimensional array |
| 0 | 723 | | retVal = loBos == null ? Array.CreateInstance( this._arrayElementType, lengths ) : Array.CreateInstance( |
| 0 | 724 | | var indices = new Int32[rank]; |
| 0 | 725 | | if ( loBos != null ) |
| | 726 | | { |
| 0 | 727 | | Array.Copy( loBos, indices, rank ); |
| | 728 | | } |
| | 729 | | do |
| | 730 | | { |
| 0 | 731 | | var curInfo = await elemInfo.Functionality.ReadBackendValueCheckNull( DataFormat.Binary, elemInfo.Data |
| 0 | 732 | | retVal.SetValue( curInfo.Value, indices ); |
| 0 | 733 | | } while ( MoveNextMultiDimensionalIndex( lengths, indices, loBos ) < rank ); |
| 0 | 734 | | } |
| 0 | 735 | | } |
| | 736 | |
|
| 0 | 737 | | return retVal; |
| 0 | 738 | | } |
| | 739 | |
|
| | 740 | | private async Task WriteArrayText( |
| | 741 | | PgSQLTypeDatabaseData boundData, |
| | 742 | | BackendABIHelper helper, |
| | 743 | | StreamWriterWithResizableBufferAndLimitedSize stream, |
| | 744 | | Array value, |
| | 745 | | BackendSizeInfo sizeInfo |
| | 746 | | ) |
| | 747 | | { |
| 8 | 748 | | var encoding = helper.Encoding; |
| 8 | 749 | | var length = value.Length; |
| 8 | 750 | | if ( length <= 0 ) |
| | 751 | | { |
| | 752 | | // Just write '{}' |
| 2 | 753 | | stream.AppendToBytes( 2 * encoding.BytesPerASCIICharacter, ( bArray, idx, count ) => encoding.WriteASCIIByte |
| 1 | 754 | | } |
| | 755 | | else |
| | 756 | | { |
| 7 | 757 | | var additionalSizeInfo = (TextSizeAdditionalInfo) sizeInfo.CustomInformation; |
| 7 | 758 | | var loboInfo = additionalSizeInfo.Item2; |
| 7 | 759 | | var rank = value.Rank; |
| 7 | 760 | | var lobos = loboInfo.Item1; |
| 7 | 761 | | var asciiSize = encoding.BytesPerASCIICharacter; |
| 7 | 762 | | if ( lobos != null ) |
| | 763 | | { |
| | 764 | | // We must write the lower bound specification: '[lobo1:upbo1][lobo2:upbo2]...=' |
| 2 | 765 | | var upbos = loboInfo.Item2; |
| 2 | 766 | | stream.AppendToBytes( loboInfo.Item3, ( array, idx, count ) => |
| 2 | 767 | | { |
| 14 | 768 | | for ( var i = 0; i < rank; ++i ) |
| 2 | 769 | | { |
| 2 | 770 | | // Both lobos and upbos have correct values from GetBackendBinarySize method |
| 6 | 771 | | encoding |
| 6 | 772 | | .WriteASCIIByte( array, ref idx, ARRAY_LOBO_START ) |
| 6 | 773 | | .WriteIntegerTextual( array, ref idx, lobos[i] ) |
| 6 | 774 | | .WriteASCIIByte( array, ref idx, (Byte) DIM_SEPARATOR ) |
| 6 | 775 | | .WriteIntegerTextual( array, ref idx, upbos[i] ) |
| 6 | 776 | | .WriteASCIIByte( array, ref idx, ARRAY_LOBO_END ); |
| 2 | 777 | | } |
| 2 | 778 | |
|
| 2 | 779 | | // Write final '=' |
| 4 | 780 | | encoding.WriteASCIIByte( array, ref idx, ARRAY_LOBO_SPEC_SEPARATOR ); |
| 4 | 781 | | } ); |
| | 782 | |
|
| | 783 | | } |
| | 784 | |
|
| 7 | 785 | | Int32[] indices = null; |
| 7 | 786 | | Int32[] lengths = null; |
| 7 | 787 | | if ( rank > 1 ) |
| | 788 | | { |
| | 789 | | // We have to use indices and lengths in order to properly emit '}' and '{' in between array elements |
| | 790 | | // N.B.! One-ranked array with lobo-specification still doesn't need to use indices and lengths, as it wo |
| | 791 | | // We also don't need to initialize indices with lower bounds, as we only need to know when dimensions en |
| 2 | 792 | | indices = new Int32[rank]; |
| 2 | 793 | | lengths = value.GetLengths(); |
| | 794 | | } |
| | 795 | |
|
| | 796 | | // Write '{'s |
| 7 | 797 | | var rankASCIISize = rank * asciiSize; |
| 7 | 798 | | stream.AppendToBytes( rankASCIISize, ( bArray, idx, count ) => |
| 7 | 799 | | { |
| 43 | 800 | | for ( var i = 0; i < rank; ++i ) |
| 7 | 801 | | { |
| 18 | 802 | | encoding.WriteASCIIByte( bArray, ref idx, ARRAY_START ); |
| 7 | 803 | | } |
| 14 | 804 | | } ); |
| | 805 | |
|
| | 806 | | // Send prefix, then send elements |
| 7 | 807 | | await stream.FlushAsync(); |
| | 808 | |
|
| 7 | 809 | | var elementSizeInfos = additionalSizeInfo.Item1; |
| 7 | 810 | | var eIdx = 0; |
| 7 | 811 | | var elementTypeInfo = this._elementTypeInfo.Value; |
| 7 | 812 | | var delimByteCount = encoding.Encoding.GetByteCount( boundData.ArrayDelimiter ); |
| 7 | 813 | | var curIdx = 0; |
| 62 | 814 | | foreach ( var element in value ) |
| | 815 | | { |
| 24 | 816 | | var elementSizeInfo = elementSizeInfos[eIdx++]; |
| 24 | 817 | | if ( element == null ) |
| | 818 | | { |
| | 819 | | // Write 'NULL' |
| 4 | 820 | | stream.AppendToBytes( asciiSize * NULL_CHAR_COUNT, ( bArray, idx, count ) => encoding.Encoding.GetByte |
| 2 | 821 | | await stream.FlushAsync(); |
| 2 | 822 | | } |
| | 823 | | else |
| | 824 | | { |
| 22 | 825 | | using ( var elementStream = await stream.CreateWithLimitedSizeAndSharedBuffer( elementSizeInfo.ByteCou |
| | 826 | | { |
| 22 | 827 | | await elementTypeInfo.Functionality.WriteBackendValueAsync( |
| 22 | 828 | | DataFormat.Text, |
| 22 | 829 | | elementTypeInfo.DatabaseData, |
| 22 | 830 | | helper, |
| 22 | 831 | | elementStream, |
| 22 | 832 | | element, |
| 22 | 833 | | elementSizeInfo, |
| 22 | 834 | | true |
| 22 | 835 | | ); |
| 22 | 836 | | await elementStream.FlushAsync(); |
| 22 | 837 | | } |
| | 838 | | } |
| | 839 | |
|
| 24 | 840 | | if ( curIdx++ < length - 1 ) |
| | 841 | | { |
| 17 | 842 | | if ( indices == null ) |
| | 843 | | { |
| 2 | 844 | | stream.AppendToBytes( delimByteCount, ( bArray, idx, count ) => encoding.Encoding.GetBytes( boundDa |
| 1 | 845 | | } |
| | 846 | | else |
| | 847 | | { |
| | 848 | | // We might need to write '}'s |
| 16 | 849 | | var amountOfDimensionsEnded = MoveNextMultiDimensionalIndex( lengths, indices ); |
| | 850 | | // Have to write '}'s, followed by array separator, followed by equally many '{'s. |
| 16 | 851 | | stream.AppendToBytes( amountOfDimensionsEnded * 2 * asciiSize + delimByteCount, ( bArray, idx, coun |
| 16 | 852 | | { |
| 62 | 853 | | for ( var i = 0; i < amountOfDimensionsEnded; ++i ) |
| 16 | 854 | | { |
| 23 | 855 | | encoding.WriteASCIIByte( bArray, ref idx, ARRAY_END ); |
| 16 | 856 | | } |
| 32 | 857 | | idx += encoding.Encoding.GetBytes( boundData.ArrayDelimiter, 0, boundData.ArrayDelimiter.Length, |
| 62 | 858 | | for ( var i = 0; i < amountOfDimensionsEnded; ++i ) |
| 16 | 859 | | { |
| 23 | 860 | | encoding.WriteASCIIByte( bArray, ref idx, ARRAY_START ); |
| 16 | 861 | | } |
| 32 | 862 | | } ); |
| | 863 | | } |
| | 864 | |
|
| 17 | 865 | | await stream.FlushAsync(); |
| | 866 | | } |
| 24 | 867 | | } |
| | 868 | |
|
| | 869 | | // Write final '}'s |
| 7 | 870 | | stream.AppendToBytes( rankASCIISize, ( bArray, idx, count ) => |
| 7 | 871 | | { |
| 43 | 872 | | for ( var i = 0; i < rank; ++i ) |
| 7 | 873 | | { |
| 18 | 874 | | encoding.WriteASCIIByte( bArray, ref idx, ARRAY_END ); |
| 7 | 875 | | } |
| 14 | 876 | | } ); |
| 7 | 877 | | } |
| 8 | 878 | | await stream.FlushAsync(); |
| 8 | 879 | | } |
| | 880 | |
|
| | 881 | | private async Task WriteArrayBinary( |
| | 882 | | PgSQLTypeDatabaseData boundData, |
| | 883 | | BackendABIHelper helper, |
| | 884 | | StreamWriterWithResizableBufferAndLimitedSize stream, |
| | 885 | | Array value, |
| | 886 | | BackendSizeInfo sizeInfo |
| | 887 | | ) |
| | 888 | | { |
| | 889 | | // Write header (3 integers + 2 integers per rank) |
| 20 | 890 | | var elemInfo = this._elementTypeInfo.Value; |
| 24 | 891 | | var arrayLength = value.Length; |
| 24 | 892 | | var rank = arrayLength <= 0 ? 0 : value.Rank; |
| 24 | 893 | | stream.AppendToBytes( ( 3 + 2 * rank ) * sizeof( Int32 ), ( bArray, idx, count ) => |
| 24 | 894 | | { |
| 44 | 895 | | bArray |
| 44 | 896 | | .WritePgInt32( ref idx, rank ) |
| 44 | 897 | | .WritePgInt32( ref idx, 1 ) // null map, always zero in our case |
| 44 | 898 | | .WritePgInt32( ref idx, elemInfo.DatabaseData.TypeID ); |
| 136 | 899 | | for ( var i = 0; i < rank; ++i ) |
| 24 | 900 | | { |
| 56 | 901 | | bArray |
| 56 | 902 | | .WritePgInt32( ref idx, value.GetLength( i ) ) |
| 56 | 903 | | .WritePgInt32( ref idx, value.GetLowerBound( i ) + 1 ); // SQL lower bounds for arrays are 1 by defa |
| 24 | 904 | | } |
| 48 | 905 | | } ); |
| | 906 | |
|
| | 907 | | // Send header |
| 24 | 908 | | await stream.FlushAsync(); |
| | 909 | |
|
| 23 | 910 | | if ( arrayLength > 0 ) |
| | 911 | | { |
| | 912 | | // Send elements |
| 21 | 913 | | var additionalSizeInfo = (BackendSizeInfo[]) sizeInfo.CustomInformation; |
| 18 | 914 | | var j = 0; |
| 175 | 915 | | foreach ( var element in value ) |
| | 916 | | { |
| 70 | 917 | | var elementSizeInfo = additionalSizeInfo[j++]; |
| 69 | 918 | | using ( var elementStream = await stream.CreateWithLimitedSizeAndSharedBuffer( |
| 69 | 919 | | Math.Max( 0, elementSizeInfo.ByteCount ) + sizeof( Int32 ) |
| 69 | 920 | | ) ) |
| | 921 | | { |
| 68 | 922 | | await elemInfo.Functionality.WriteBackendValueCheckNull( |
| 68 | 923 | | DataFormat.Binary, |
| 68 | 924 | | elemInfo.DatabaseData, |
| 68 | 925 | | helper, |
| 68 | 926 | | elementStream, |
| 68 | 927 | | element, |
| 68 | 928 | | elementSizeInfo, |
| 68 | 929 | | true |
| 68 | 930 | | ); |
| 72 | 931 | | } |
| 71 | 932 | | } |
| 20 | 933 | | } |
| 23 | 934 | | } |
| | 935 | |
|
| | 936 | | } |
| | 937 | | } |