| | 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.SQL.Implementation; |
| | 19 | | using System; |
| | 20 | | using System.Collections.Generic; |
| | 21 | | using System.Reflection; |
| | 22 | | using System.IO; |
| | 23 | | using System.Linq; |
| | 24 | | using System.Text; |
| | 25 | | using System.Threading; |
| | 26 | | using System.Threading.Tasks; |
| | 27 | | using UtilPack; |
| | 28 | | using TStaticTypeCacheValue = System.Collections.Generic.IDictionary<System.String, CBAM.SQL.PostgreSQL.PgSQLTypeDatabas |
| | 29 | | using TStaticTypeCache = System.Collections.Generic.IDictionary<System.Version, System.Collections.Generic.IDictionary<S |
| | 30 | | using TextBackendSizeInfo = UtilPack.EitherOr<System.ValueTuple<System.Int32, System.Object>, System.String>; |
| | 31 | | using TypeFunctionalityInfo = System.ValueTuple<System.ValueTuple<System.Type, CBAM.SQL.PostgreSQL.PgSQLTypeFunctionalit |
| | 32 | | using CBAM.SQL.PostgreSQL; |
| | 33 | |
|
| | 34 | | namespace CBAM.SQL.PostgreSQL.Implementation |
| | 35 | | { |
| | 36 | | using TSyncTextualSizeInfo = ValueTuple<Int32, String>; |
| | 37 | |
|
| | 38 | | internal sealed partial class PostgreSQLProtocol |
| | 39 | | { |
| | 40 | | private static readonly TStaticTypeCache _StaticTypeCache; |
| | 41 | | private static readonly IDictionary<String, TypeFunctionalityInfo> _DefaultTypes; |
| | 42 | |
|
| | 43 | | static PostgreSQLProtocol() |
| | 44 | | { |
| | 45 | | var typeInfo = PostgreSQLProtocol.CreateDefaultTypesInfos() |
| | 46 | | .ToDictionary( tInfo => tInfo.Item1, tInfo => tInfo ); |
| | 47 | |
|
| | 48 | | _DefaultTypes = new Dictionary<String, TypeFunctionalityInfo>() |
| | 49 | | { |
| | 50 | | { "text", (typeInfo[typeof(String)], true) }, |
| | 51 | | { "void", (typeInfo[typeof(String)], false) }, |
| | 52 | | { "char", (typeInfo[typeof(String)], false) }, |
| | 53 | | { "varchar", (typeInfo[typeof(String)], false) }, |
| | 54 | | { "bool", (typeInfo[typeof(Boolean)], true) }, |
| | 55 | | { "int2", (typeInfo[typeof(Int16)], true) }, |
| | 56 | | { "int4", (typeInfo[typeof(Int32)], true) }, |
| | 57 | | { "int8", (typeInfo[typeof(Int64)], true) }, |
| | 58 | | { "oid", (typeInfo[typeof(Int32)], false) }, |
| | 59 | | { "float4", (typeInfo[typeof(Single)], true) }, |
| | 60 | | { "float8", (typeInfo[typeof(Double)], true) }, |
| | 61 | | { "numeric", (typeInfo[typeof(Decimal)], true) }, |
| | 62 | | //{ "inet", typeof(PgSQLInternetAddress) }, |
| | 63 | | //{ "macaddr", typeof(PgSQLMacAddress) }, |
| | 64 | | { "money", (typeInfo[typeof(Decimal)], false) }, |
| | 65 | | { "uuid", (typeInfo[typeof(Guid)], true) }, |
| | 66 | | { "xml", (typeInfo[typeof(System.Xml.Linq.XElement)], true) }, |
| | 67 | | { "interval", (typeInfo[typeof(PgSQLInterval)], true) }, |
| | 68 | | { "date", (typeInfo[typeof(PgSQLDate)], true) }, |
| | 69 | | { "time", (typeInfo[typeof(PgSQLTime)], true) }, |
| | 70 | | { "timetz", (typeInfo[typeof(PgSQLTimeTZ)], true) }, |
| | 71 | | { "timestamp", (typeInfo[typeof(PgSQLTimestamp)], true) }, |
| | 72 | | { "timestamptz", (typeInfo[typeof(PgSQLTimestampTZ)], true) }, |
| | 73 | | { "abstime", (typeInfo[typeof(PgSQLTimestampTZ)], false) }, |
| | 74 | | { "bytea", (typeInfo[typeof(Byte[])], true) } |
| | 75 | |
|
| | 76 | | }; |
| | 77 | | _StaticTypeCache = new Dictionary<Version, TStaticTypeCacheValue>(); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | private async Task ReadTypesFromServer( Boolean force, CancellationToken token ) |
| | 81 | | { |
| | 82 | | var serverVersion = this._serverVersion; |
| | 83 | | var typeCache = _StaticTypeCache; |
| | 84 | | if ( force || !typeCache.TryGetValue( serverVersion, out TStaticTypeCacheValue types ) ) |
| | 85 | | { |
| | 86 | | this.CurrentCancellationToken = token; |
| | 87 | | try |
| | 88 | | { |
| | 89 | | types = await this.TypeRegistry.ReadTypeDataFromServer( _DefaultTypes.Keys ); |
| | 90 | | } |
| | 91 | | finally |
| | 92 | | { |
| | 93 | | this.ResetCancellationToken(); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | lock ( typeCache ) |
| | 97 | | { |
| | 98 | | typeCache[serverVersion] = types; |
| | 99 | | } |
| | 100 | | } |
| | 101 | |
|
| | 102 | | this.TypeRegistry.AssignTypeData( |
| | 103 | | types, |
| | 104 | | tName => _DefaultTypes[tName].Item1.Item1, |
| | 105 | | tuple => |
| | 106 | | { |
| | 107 | | var valTuple = _DefaultTypes[tuple.DBTypeName]; |
| | 108 | | return new TypeFunctionalityCreationResult( valTuple.Item1.Item2, valTuple.Item2 ); |
| | 109 | | } ); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private static IEnumerable<(Type, PgSQLTypeFunctionality)> CreateDefaultTypesInfos() |
| | 113 | | { |
| | 114 | | // Assumes that string is non-empty |
| | 115 | | Boolean ArrayElementStringNeedsQuotesSingleDelimiter( PgSQLTypeDatabaseData boundData, String value ) |
| | 116 | | { |
| | 117 | | Boolean needsQuotes = false; |
| | 118 | | var delimChar = boundData.ArrayDelimiter[0]; |
| | 119 | | for ( var i = 0; i < value.Length && !needsQuotes; ++i ) |
| | 120 | | { |
| | 121 | | var c = value[i]; |
| | 122 | | switch ( c ) |
| | 123 | | { |
| | 124 | | case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 125 | | case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 126 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 127 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 128 | | needsQuotes = true; |
| | 129 | | break; |
| | 130 | | default: |
| | 131 | | if ( c == delimChar || Char.IsWhiteSpace( c ) ) |
| | 132 | | { |
| | 133 | | needsQuotes = true; |
| | 134 | | } |
| | 135 | | break; |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| | 139 | | return needsQuotes; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | Boolean ArrayElementStringNeedsQuotesMultiDelimiter( PgSQLTypeDatabaseData boundData, String value ) |
| | 143 | | { |
| | 144 | | var needsQuotes = value.IndexOf( boundData.ArrayDelimiter ) >= 0; |
| | 145 | | if ( !needsQuotes ) |
| | 146 | | { |
| | 147 | | for ( var i = 0; i < value.Length && !needsQuotes; ++i ) |
| | 148 | | { |
| | 149 | | var c = value[i]; |
| | 150 | | switch ( c ) |
| | 151 | | { |
| | 152 | | case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 153 | | case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 154 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 155 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 156 | | needsQuotes = true; |
| | 157 | | break; |
| | 158 | | default: |
| | 159 | | if ( Char.IsWhiteSpace( c ) ) |
| | 160 | | { |
| | 161 | | needsQuotes = true; |
| | 162 | | } |
| | 163 | | break; |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |
| | 167 | | return needsQuotes; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | Int32 CalculateArrayElementStringSizeSingleDelimiter( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, |
| | 171 | | { |
| | 172 | | var asciiByteSize = encoding.BytesPerASCIICharacter; |
| | 173 | | var retVal = 2 * asciiByteSize + encoding.Encoding.GetByteCount( value ); |
| | 174 | | var delimChar = boundData.ArrayDelimiter[0]; |
| | 175 | | for ( var i = 0; i < value.Length; ++i ) |
| | 176 | | { |
| | 177 | | var c = value[i]; |
| | 178 | | switch ( c ) |
| | 179 | | { |
| | 180 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 181 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 182 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 183 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 184 | | retVal += asciiByteSize; |
| | 185 | | break; |
| | 186 | | //default: |
| | 187 | | // if ( delimChar == c || Char.IsWhiteSpace( c ) ) |
| | 188 | | // { |
| | 189 | | // retVal += asciiByteSize; |
| | 190 | | // } |
| | 191 | | // break; |
| | 192 | | } |
| | 193 | | } |
| | 194 | | return retVal; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | Int32 CalculateArrayElementStringSizeMultiDelimiter( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, S |
| | 198 | | { |
| | 199 | | var asciiByteSize = encoding.BytesPerASCIICharacter; |
| | 200 | | var retVal = 2 * asciiByteSize + encoding.Encoding.GetByteCount( value ) + asciiByteSize * value.CountOccurr |
| | 201 | | for ( var i = 0; i < value.Length; ++i ) |
| | 202 | | { |
| | 203 | | var c = value[i]; |
| | 204 | | switch ( c ) |
| | 205 | | { |
| | 206 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 207 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 208 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 209 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 210 | | retVal += asciiByteSize; |
| | 211 | | break; |
| | 212 | | //default: |
| | 213 | | // if ( Char.IsWhiteSpace( c ) ) |
| | 214 | | // { |
| | 215 | | // retVal += asciiByteSize; |
| | 216 | | // } |
| | 217 | | // break; |
| | 218 | | } |
| | 219 | | } |
| | 220 | | return retVal; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | void WriteArrayElementStringSingleDelimiter( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Byte[] a |
| | 224 | | { |
| | 225 | | var prevIdx = 0; |
| | 226 | | var delimChar = boundData.ArrayDelimiter[0]; |
| | 227 | | var encoding = helper.Encoding.Encoding; |
| | 228 | | for ( var i = 0; i < value.Length; ++i ) |
| | 229 | | { |
| | 230 | | var c = value[i]; |
| | 231 | | switch ( c ) |
| | 232 | | { |
| | 233 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 234 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 235 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 236 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 237 | | offset += encoding.GetBytes( value, prevIdx, i - prevIdx, array, offset ); |
| | 238 | | helper.Encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.ESCAPE ); |
| | 239 | | prevIdx = i; |
| | 240 | | break; |
| | 241 | | default: |
| | 242 | | //if ( delimChar == c || Char.IsWhiteSpace( c ) ) |
| | 243 | | //{ |
| | 244 | | // arrayIdx += encoding.GetBytes( value, prevIdx, i - prevIdx, array, arrayIdx ); |
| | 245 | | // helper.Encoding.WriteASCIIByte( array, ref arrayIdx, (Byte) PgSQLTypeFunctionalityForArrays.ES |
| | 246 | | // prevIdx = i; |
| | 247 | | //} |
| | 248 | | break; |
| | 249 | | } |
| | 250 | | } |
| | 251 | |
|
| | 252 | | // Last chunk |
| | 253 | | offset += encoding.GetBytes( value, prevIdx, value.Length - prevIdx, array, offset ); |
| | 254 | | } |
| | 255 | |
|
| | 256 | | void WriteArrayElementStringMultiDelimiter( PgSQLTypeDatabaseData boundData, BackendABIHelper helper, Byte[] ar |
| | 257 | | { |
| | 258 | | var prevIdx = 0; |
| | 259 | | var encoding = helper.Encoding.Encoding; |
| | 260 | | var delimCharStart = boundData.ArrayDelimiter[0]; |
| | 261 | | var delimCharEnd = boundData.ArrayDelimiter[1]; |
| | 262 | | for ( var i = 0; i < value.Length; ++i ) |
| | 263 | | { |
| | 264 | | var c = value[i]; |
| | 265 | | switch ( c ) |
| | 266 | | { |
| | 267 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_START: |
| | 268 | | //case (Char) PgSQLTypeFunctionalityForArrays.ARRAY_END: |
| | 269 | | case PgSQLTypeFunctionalityForArrays.ESCAPE: |
| | 270 | | case PgSQLTypeFunctionalityForArrays.QUOTE: |
| | 271 | | offset += encoding.GetBytes( value, prevIdx, i - prevIdx, array, offset ); |
| | 272 | | helper.Encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.ESCAPE ); |
| | 273 | | prevIdx = i; |
| | 274 | | break; |
| | 275 | | //default: |
| | 276 | | // var isDelimStart = c == delimCharStart && i < value.Length - 1 && value[i + 1] == delimCharEnd |
| | 277 | | // if ( isDelimStart || Char.IsWhiteSpace( c ) ) |
| | 278 | | // { |
| | 279 | | // arrayIdx += encoding.GetBytes( value, prevIdx, i - prevIdx, array, arrayIdx ); |
| | 280 | | // helper.Encoding.WriteASCIIByte( array, ref arrayIdx, (Byte) PgSQLTypeFunctionalityForArrays |
| | 281 | | // prevIdx = i; |
| | 282 | | // if ( isDelimStart ) |
| | 283 | | // { |
| | 284 | | // ++i; |
| | 285 | | // } |
| | 286 | | // } |
| | 287 | | // break; |
| | 288 | | } |
| | 289 | | } |
| | 290 | |
|
| | 291 | | // Last chunk |
| | 292 | | offset += encoding.GetBytes( value, prevIdx, value.Length - prevIdx, array, offset ); |
| | 293 | | } |
| | 294 | |
|
| | 295 | |
|
| | 296 | | // String |
| | 297 | | yield return CreateSingleBodyInfoWithType( |
| | 298 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 299 | | { |
| | 300 | | return args.GetStringWithPool( array, offset, count ); |
| | 301 | | }, |
| | 302 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 303 | | { |
| | 304 | | return args.GetStringWithPool( array, offset, count ); |
| | 305 | | }, |
| | 306 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, String value, Boolean isArrayElement ) => |
| | 307 | | { |
| | 308 | | EitherOr<Int32, String> retVal; |
| | 309 | | if ( isArrayElement ) |
| | 310 | | { |
| | 311 | | // From https://www.postgresql.org/docs/current/static/arrays.html : |
| | 312 | | // The array output routine will put double quotes around element values if they are empty strings, co |
| | 313 | | // Assumes that value is non-empty |
| | 314 | | var isNullString = String.Equals( value, "null", StringComparison.OrdinalIgnoreCase ); |
| | 315 | | var isSingleDelimiter = boundData.ArrayDelimiter.Length == 1; |
| | 316 | | if ( |
| | 317 | | value.Length == 0 |
| | 318 | | || isNullString |
| | 319 | | || ( isSingleDelimiter ? ArrayElementStringNeedsQuotesSingleDelimiter( boundData, value ) : ArrayEl |
| | 320 | | ) |
| | 321 | | { |
| | 322 | | retVal = isNullString ? |
| | 323 | | 6 * encoding.BytesPerASCIICharacter : |
| | 324 | | ( isSingleDelimiter ? |
| | 325 | | CalculateArrayElementStringSizeSingleDelimiter( boundData, encoding, value ) : |
| | 326 | | CalculateArrayElementStringSizeMultiDelimiter( boundData, encoding, value ) |
| | 327 | | ); |
| | 328 | | } |
| | 329 | | else |
| | 330 | | { |
| | 331 | | // We can serialize without quotes |
| | 332 | | retVal = value; |
| | 333 | | } |
| | 334 | |
|
| | 335 | | } |
| | 336 | | else |
| | 337 | | { |
| | 338 | | retVal = value; |
| | 339 | | } |
| | 340 | |
|
| | 341 | | return retVal; |
| | 342 | | }, |
| | 343 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, String value, Boolean isArrayElement ) => |
| | 344 | | { |
| | 345 | | return encoding.Encoding.GetByteCount( value ); |
| | 346 | | }, |
| | 347 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, String value, TSyncTex |
| | 348 | | { |
| | 349 | | if ( sizeInfo.Item2 == null ) |
| | 350 | | { |
| | 351 | | // Because of how text format size is calculated, and because we are handling string type, we will com |
| | 352 | |
|
| | 353 | | // Write starting quote |
| | 354 | | args.Encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.QUOTE ); |
| | 355 | |
|
| | 356 | | // Write content, and escape any double quotes and backslashes |
| | 357 | | if ( boundData.ArrayDelimiter.Length == 1 ) |
| | 358 | | { |
| | 359 | | WriteArrayElementStringSingleDelimiter( boundData, args, array, value, ref offset ); |
| | 360 | | } |
| | 361 | | else |
| | 362 | | { |
| | 363 | | WriteArrayElementStringMultiDelimiter( boundData, args, array, value, ref offset ); |
| | 364 | | } |
| | 365 | |
|
| | 366 | | // Write ending quote |
| | 367 | | args.Encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.QUOTE ); |
| | 368 | | } |
| | 369 | | else |
| | 370 | | { |
| | 371 | | offset += args.Encoding.Encoding.GetBytes( value, 0, value.Length, array, offset ); |
| | 372 | | } |
| | 373 | | System.Diagnostics.Debug.Assert( offset == sizeInfo.Item1 ); |
| | 374 | | }, |
| | 375 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, String value, Int32 si |
| | 376 | | { |
| | 377 | | args.Encoding.Encoding.GetBytes( value, 0, value.Length, array, offset ); |
| | 378 | | }, |
| | 379 | | null, |
| | 380 | | null |
| | 381 | | ); |
| | 382 | |
|
| | 383 | | // Boolean |
| | 384 | | yield return CreateSingleBodyInfoWithType( |
| | 385 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 386 | | { |
| | 387 | | Byte b; |
| | 388 | | return count > 0 |
| | 389 | | && ( |
| | 390 | | ( b = args.Encoding.ReadASCIIByte( array, ref offset ) ) == 'T' |
| | 391 | | || b == 't' |
| | 392 | | ); |
| | 393 | | }, |
| | 394 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 395 | | { |
| | 396 | | return count > 0 && array[offset] != 0; |
| | 397 | | }, |
| | 398 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Boolean value, Boolean isArrayElement ) => |
| | 399 | | { |
| | 400 | | return encoding.BytesPerASCIICharacter; |
| | 401 | | }, |
| | 402 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Boolean value, Boolean isArrayElement ) => |
| | 403 | | { |
| | 404 | | return 1; |
| | 405 | | }, |
| | 406 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Boolean value, TSyncTe |
| | 407 | | { |
| | 408 | | args.Encoding.WriteASCIIByte( array, ref offset, (Byte) ( value ? 't' : 'f' ) ); |
| | 409 | | }, |
| | 410 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Boolean value, Int32 s |
| | 411 | | { |
| | 412 | | array[offset] = (Byte) ( value ? 1 : 0 ); |
| | 413 | | }, |
| | 414 | | null, |
| | 415 | | null |
| | 416 | | ); |
| | 417 | |
|
| | 418 | | // Int16 |
| | 419 | | yield return CreateSingleBodyInfoWithType( |
| | 420 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 421 | | { |
| | 422 | | return (Int16) args.Encoding.ParseInt32Textual( array, ref offset, (count / args.Encoding.BytesPerASCIICh |
| | 423 | | }, |
| | 424 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 425 | | { |
| | 426 | | return array.ReadInt16BEFromBytes( ref offset ); |
| | 427 | | }, |
| | 428 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int16 value, Boolean isArrayElement ) => |
| | 429 | | { |
| | 430 | | return encoding.GetTextualIntegerRepresentationSize( value ); |
| | 431 | | }, |
| | 432 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int16 value, Boolean isArrayElement ) => |
| | 433 | | { |
| | 434 | | return sizeof( Int16 ); |
| | 435 | | }, |
| | 436 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int16 value, TSyncText |
| | 437 | | { |
| | 438 | | args.Encoding.WriteIntegerTextual( array, ref offset, value ); |
| | 439 | | }, |
| | 440 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int16 value, Int32 siz |
| | 441 | | { |
| | 442 | | array.WriteInt16BEToBytes( ref offset, value ); |
| | 443 | | }, |
| | 444 | | null, |
| | 445 | | null |
| | 446 | | ); |
| | 447 | |
|
| | 448 | | // Int32 |
| | 449 | | yield return CreateSingleBodyInfoWithType( |
| | 450 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 451 | | { |
| | 452 | | return args.Encoding.ParseInt32Textual( array, ref offset, (count / args.Encoding.BytesPerASCIICharacter, |
| | 453 | | }, |
| | 454 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 455 | | { |
| | 456 | | return array.ReadInt32BEFromBytes( ref offset ); |
| | 457 | | }, |
| | 458 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int32 value, Boolean isArrayElement ) => |
| | 459 | | { |
| | 460 | | return encoding.GetTextualIntegerRepresentationSize( value ); |
| | 461 | | }, |
| | 462 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int32 value, Boolean isArrayElement ) => |
| | 463 | | { |
| | 464 | | return sizeof( Int32 ); |
| | 465 | | }, |
| | 466 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 value, TSyncText |
| | 467 | | { |
| | 468 | | args.Encoding.WriteIntegerTextual( array, ref offset, value ); |
| | 469 | | }, |
| | 470 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 value, Int32 siz |
| | 471 | | { |
| | 472 | | array.WriteInt32BEToBytes( ref offset, value ); |
| | 473 | | }, |
| | 474 | | null, |
| | 475 | | null |
| | 476 | | ); |
| | 477 | |
|
| | 478 | | // Int64 |
| | 479 | | yield return CreateSingleBodyInfoWithType( |
| | 480 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 481 | | { |
| | 482 | | return args.Encoding.ParseInt64Textual( array, ref offset, (count / args.Encoding.BytesPerASCIICharacter, |
| | 483 | | }, |
| | 484 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 485 | | { |
| | 486 | | return array.ReadInt64BEFromBytes( ref offset ); |
| | 487 | | }, |
| | 488 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int64 value, Boolean isArrayElement ) => |
| | 489 | | { |
| | 490 | | return encoding.GetTextualIntegerRepresentationSize( value ); |
| | 491 | | }, |
| | 492 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Int64 value, Boolean isArrayElement ) => |
| | 493 | | { |
| | 494 | | return sizeof( Int64 ); |
| | 495 | | }, |
| | 496 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int64 value, TSyncText |
| | 497 | | { |
| | 498 | | args.Encoding.WriteIntegerTextual( array, ref offset, value ); |
| | 499 | | }, |
| | 500 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int64 value, Int32 siz |
| | 501 | | { |
| | 502 | | array.WriteInt64BEToBytes( ref offset, value ); |
| | 503 | | }, |
| | 504 | | null, |
| | 505 | | null |
| | 506 | | ); |
| | 507 | |
|
| | 508 | | // Unfortunately, parsing real numbers (single, double, decimal) directly from byte array containing the string |
| | 509 | | // so right now we have to allocate a new string and parse it. |
| | 510 | | // It's a bit of a performance killer, so maybe should be fixed one day. |
| | 511 | | // Single |
| | 512 | | yield return CreateSingleBodyInfoWithType( |
| | 513 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 514 | | { |
| | 515 | | return Single.Parse( args.GetStringWithPool( array, offset, count ), CommonPgSQLTypeFunctionalityInfo.Num |
| | 516 | | }, |
| | 517 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 518 | | { |
| | 519 | | return array.ReadSingleBEFromBytes( ref offset ); |
| | 520 | | }, |
| | 521 | | null, |
| | 522 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Single value, Boolean isArrayElement ) => |
| | 523 | | { |
| | 524 | | return sizeof( Single ); |
| | 525 | | }, |
| | 526 | | null, |
| | 527 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Single value, Int32 si |
| | 528 | | { |
| | 529 | | array.WriteSingleBEToBytes( ref offset, value ); |
| | 530 | | }, |
| | 531 | | null, |
| | 532 | | null |
| | 533 | | ); |
| | 534 | |
|
| | 535 | | // Double |
| | 536 | | yield return CreateSingleBodyInfoWithType( |
| | 537 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 538 | | { |
| | 539 | | return Double.Parse( args.GetStringWithPool( array, offset, count ), CommonPgSQLTypeFunctionalityInfo.Num |
| | 540 | | }, |
| | 541 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 542 | | { |
| | 543 | | return array.ReadDoubleBEFromBytes( ref offset ); |
| | 544 | | }, |
| | 545 | | null, |
| | 546 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Double value, Boolean isArrayElement ) => |
| | 547 | | { |
| | 548 | | return sizeof( Double ); |
| | 549 | | }, |
| | 550 | | null, |
| | 551 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Double value, Int32 si |
| | 552 | | { |
| | 553 | | array.WriteDoubleBEToBytes( ref offset, value ); |
| | 554 | | }, |
| | 555 | | null, |
| | 556 | | null |
| | 557 | | ); |
| | 558 | |
|
| | 559 | | // Decimal |
| | 560 | | yield return CreateSingleBodyInfoWithType( |
| | 561 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 562 | | { |
| | 563 | | return Decimal.Parse( args.GetStringWithPool( array, offset, count ), CommonPgSQLTypeFunctionalityInfo.Nu |
| | 564 | | }, |
| | 565 | | null, |
| | 566 | | null, |
| | 567 | | null, |
| | 568 | | null, |
| | 569 | | null, |
| | 570 | | null, |
| | 571 | | null |
| | 572 | | ); |
| | 573 | |
|
| | 574 | | // Guid |
| | 575 | | yield return CreateSingleBodyInfoWithType( |
| | 576 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 577 | | { |
| | 578 | | return Guid.Parse( args.GetStringWithPool( array, offset, count ) ); |
| | 579 | | }, |
| | 580 | | null, |
| | 581 | | null, |
| | 582 | | null, |
| | 583 | | null, |
| | 584 | | null, |
| | 585 | | null, |
| | 586 | | null |
| | 587 | | ); |
| | 588 | |
|
| | 589 | | // Xml |
| | 590 | | yield return CreateSingleBodyInfoWithType( |
| | 591 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 592 | | { |
| | 593 | | // If there would be "LoadAsync" in XEelement, we could use new PgSQLTypeUnboundInfo constructor directly |
| | 594 | | using ( var mStream = new System.IO.MemoryStream( array, offset, count, false ) ) |
| | 595 | | { |
| | 596 | | return System.Xml.Linq.XElement.Load( mStream ); |
| | 597 | | } |
| | 598 | | }, |
| | 599 | | null, |
| | 600 | | null, |
| | 601 | | null, |
| | 602 | | null, |
| | 603 | | null, |
| | 604 | | null, |
| | 605 | | null |
| | 606 | | ); |
| | 607 | |
|
| | 608 | | // PgSQLInterval |
| | 609 | | yield return CreateSingleBodyInfoWithType( |
| | 610 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 611 | | { |
| | 612 | | // TODO interval parsing without string allocation (PgSQLInterval.Load(Stream stream)) |
| | 613 | | // Or PgSQLInterval.ParseBinaryText |
| | 614 | | return PgSQLInterval.Parse( args.GetStringWithPool( array, offset, count ) ); |
| | 615 | | }, |
| | 616 | | null, |
| | 617 | | // TODO interval string writing without string allocation |
| | 618 | | null, |
| | 619 | | null, |
| | 620 | | null, |
| | 621 | | null, |
| | 622 | | ( PgSQLTypeDatabaseData dbData, PgSQLInterval pgSQLObject, Type targetType ) => |
| | 623 | | { |
| | 624 | | return (TimeSpan) pgSQLObject; |
| | 625 | | }, |
| | 626 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 627 | | { |
| | 628 | | return (TimeSpan) systemObject; |
| | 629 | | } |
| | 630 | | ); |
| | 631 | |
|
| | 632 | | // PgSQLDate |
| | 633 | | yield return CreateSingleBodyInfoWithType( |
| | 634 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 635 | | { |
| | 636 | | return PgSQLDate.ParseBinaryText( args.Encoding, array, ref offset, count ); |
| | 637 | | }, |
| | 638 | | null, |
| | 639 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, PgSQLDate value, Boolean isArrayElement ) => |
| | 640 | | { |
| | 641 | | var retVal = value.GetTextByteCount( encoding ); |
| | 642 | | if ( isArrayElement && value.NeedsQuotingInArrayElement() ) |
| | 643 | | { |
| | 644 | | // The " BC" substring contains whitespace, so we must put quotes around this |
| | 645 | | retVal += 2 * encoding.BytesPerASCIICharacter; |
| | 646 | | } |
| | 647 | | return retVal; |
| | 648 | | }, |
| | 649 | | null, |
| | 650 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, PgSQLDate value, TSync |
| | 651 | | { |
| | 652 | | value.WriteBytesAndPossiblyQuote( args.Encoding, array, ref offset, isArrayElement && value.NeedsQuotingI |
| | 653 | | }, |
| | 654 | | null, |
| | 655 | | ( PgSQLTypeDatabaseData dbData, PgSQLDate pgSQLObject, Type targetType ) => |
| | 656 | | { |
| | 657 | | return (DateTime) pgSQLObject; |
| | 658 | | }, |
| | 659 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 660 | | { |
| | 661 | | return (PgSQLDate) (DateTime) systemObject; |
| | 662 | | } |
| | 663 | | ); |
| | 664 | |
|
| | 665 | | // PgSQLTime |
| | 666 | | yield return CreateSingleBodyInfoWithType( |
| | 667 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 668 | | { |
| | 669 | | return PgSQLTime.ParseBinaryText( args.Encoding, array, ref offset, count ); |
| | 670 | | }, |
| | 671 | | null, |
| | 672 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, PgSQLTime value, Boolean isArrayElement ) => |
| | 673 | | { |
| | 674 | | return value.GetTextByteCount( encoding ); |
| | 675 | | }, |
| | 676 | | null, |
| | 677 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, PgSQLTime value, TSync |
| | 678 | | { |
| | 679 | | value.WriteTextBytes( args.Encoding, array, ref offset ); |
| | 680 | | }, |
| | 681 | | null, |
| | 682 | | ( PgSQLTypeDatabaseData dbData, PgSQLTime pgSQLObject, Type targetType ) => |
| | 683 | | { |
| | 684 | | if ( typeof( DateTime ).Equals( targetType ) ) |
| | 685 | | { |
| | 686 | | return (DateTime) pgSQLObject; |
| | 687 | | } |
| | 688 | | else if ( typeof( TimeSpan ).Equals( targetType ) ) |
| | 689 | | { |
| | 690 | | return (TimeSpan) pgSQLObject; |
| | 691 | | } |
| | 692 | | else if ( typeof( PgSQLInterval ).Equals( targetType ) ) |
| | 693 | | { |
| | 694 | | return (PgSQLInterval) pgSQLObject; |
| | 695 | | } |
| | 696 | | else |
| | 697 | | { |
| | 698 | | throw new InvalidCastException( "Can't cast time " + pgSQLObject + " to " + targetType + "." ); |
| | 699 | | } |
| | 700 | | }, |
| | 701 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 702 | | { |
| | 703 | | var tt = systemObject.GetType(); |
| | 704 | | if ( typeof( DateTime ).Equals( tt ) ) |
| | 705 | | { |
| | 706 | | return (PgSQLTime) (DateTime) systemObject; |
| | 707 | | } |
| | 708 | | else if ( typeof( TimeSpan ).Equals( tt ) ) |
| | 709 | | { |
| | 710 | | return (PgSQLTime) (TimeSpan) systemObject; |
| | 711 | | } |
| | 712 | | else if ( typeof( PgSQLInterval ).Equals( tt ) ) |
| | 713 | | { |
| | 714 | | return (PgSQLTime) (PgSQLInterval) systemObject; |
| | 715 | | } |
| | 716 | | else |
| | 717 | | { |
| | 718 | | throw new InvalidCastException( "Can't cast object " + systemObject + " to time." ); |
| | 719 | | } |
| | 720 | | } |
| | 721 | | ); |
| | 722 | |
|
| | 723 | | // PgSQLTimeTZ |
| | 724 | | yield return CreateSingleBodyInfoWithType( |
| | 725 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 726 | | { |
| | 727 | | return PgSQLTimeTZ.ParseBinaryText( args.Encoding, array, ref offset, count ); |
| | 728 | | }, |
| | 729 | | null, |
| | 730 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, PgSQLTimeTZ value, Boolean isArrayElement ) => |
| | 731 | | { |
| | 732 | | return value.GetTextByteCount( encoding ); |
| | 733 | | }, |
| | 734 | | null, |
| | 735 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, PgSQLTimeTZ value, TSy |
| | 736 | | { |
| | 737 | | value.WriteTextBytes( args.Encoding, array, ref offset ); |
| | 738 | | }, |
| | 739 | | null, |
| | 740 | | ( PgSQLTypeDatabaseData dbData, PgSQLTimeTZ pgSQLObject, Type targetType ) => |
| | 741 | | { |
| | 742 | | if ( typeof( DateTime ).Equals( targetType ) ) |
| | 743 | | { |
| | 744 | | return (DateTime) pgSQLObject; |
| | 745 | | } |
| | 746 | | else if ( typeof( TimeSpan ).Equals( targetType ) ) |
| | 747 | | { |
| | 748 | | return (TimeSpan) pgSQLObject; |
| | 749 | | } |
| | 750 | | else |
| | 751 | | { |
| | 752 | | throw new InvalidCastException( "Can't cast time " + pgSQLObject + " to " + targetType + "." ); |
| | 753 | | } |
| | 754 | | }, |
| | 755 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 756 | | { |
| | 757 | | var tt = systemObject.GetType(); |
| | 758 | | if ( typeof( DateTime ).Equals( tt ) ) |
| | 759 | | { |
| | 760 | | return (PgSQLTimeTZ) (DateTime) systemObject; |
| | 761 | | } |
| | 762 | | else if ( typeof( TimeSpan ).Equals( tt ) ) |
| | 763 | | { |
| | 764 | | return (PgSQLTimeTZ) (TimeSpan) systemObject; |
| | 765 | | } |
| | 766 | | else |
| | 767 | | { |
| | 768 | | throw new InvalidCastException( "Can't cast object " + systemObject + " to time with time zone." ); |
| | 769 | | } |
| | 770 | | } |
| | 771 | | ); |
| | 772 | |
|
| | 773 | | // PgSQLTimestamp |
| | 774 | | yield return CreateSingleBodyInfoWithType( |
| | 775 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 776 | | { |
| | 777 | | return PgSQLTimestamp.ParseBinaryText( args.Encoding, array, ref offset, count ); |
| | 778 | | }, |
| | 779 | | null, |
| | 780 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, PgSQLTimestamp value, Boolean isArrayElement ) => |
| | 781 | | { |
| | 782 | | var retVal = value.GetTextByteCount( encoding ); |
| | 783 | | if ( isArrayElement && value.NeedsQuotingInArrayElement() ) |
| | 784 | | { |
| | 785 | | // There will be a space -> the value must be quoted |
| | 786 | | retVal += 2 * encoding.BytesPerASCIICharacter; |
| | 787 | | } |
| | 788 | | return retVal; |
| | 789 | | }, |
| | 790 | | null, |
| | 791 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, PgSQLTimestamp value, |
| | 792 | | { |
| | 793 | | value.WriteBytesAndPossiblyQuote( args.Encoding, array, ref offset, isArrayElement && value.NeedsQuotingI |
| | 794 | | }, |
| | 795 | | null, |
| | 796 | | ( PgSQLTypeDatabaseData dbData, PgSQLTimestamp pgSQLObject, Type targetType ) => |
| | 797 | | { |
| | 798 | | if ( typeof( DateTime ).Equals( targetType ) ) |
| | 799 | | { |
| | 800 | | return (DateTime) pgSQLObject; |
| | 801 | | } |
| | 802 | | else |
| | 803 | | { |
| | 804 | | throw new InvalidCastException( "Can't cast time " + pgSQLObject + " to " + targetType + "." ); |
| | 805 | | } |
| | 806 | | }, |
| | 807 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 808 | | { |
| | 809 | | var tt = systemObject.GetType(); |
| | 810 | | if ( typeof( DateTime ).Equals( tt ) ) |
| | 811 | | { |
| | 812 | | return (PgSQLTimestamp) (DateTime) systemObject; |
| | 813 | | } |
| | 814 | | else |
| | 815 | | { |
| | 816 | | throw new InvalidCastException( "Can't cast object " + systemObject + " to timestamp." ); |
| | 817 | | } |
| | 818 | | } |
| | 819 | | ); |
| | 820 | |
|
| | 821 | | // PgSQLTimestampTZ |
| | 822 | | yield return CreateSingleBodyInfoWithType( |
| | 823 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 824 | | { |
| | 825 | | return PgSQLTimestampTZ.ParseBinaryText( args.Encoding, array, ref offset, count ); |
| | 826 | | }, |
| | 827 | | null, |
| | 828 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, PgSQLTimestampTZ value, Boolean isArrayElement ) |
| | 829 | | { |
| | 830 | | var retVal = value.GetTextByteCount( encoding ); |
| | 831 | | if ( isArrayElement && value.NeedsQuotingInArrayElement() ) |
| | 832 | | { |
| | 833 | | retVal += 2 * encoding.BytesPerASCIICharacter; |
| | 834 | | } |
| | 835 | | return retVal; |
| | 836 | | }, |
| | 837 | | null, |
| | 838 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, PgSQLTimestampTZ value |
| | 839 | | { |
| | 840 | | value.WriteBytesAndPossiblyQuote( args.Encoding, array, ref offset, isArrayElement && value.NeedsQuotingI |
| | 841 | | }, |
| | 842 | | null, |
| | 843 | | ( PgSQLTypeDatabaseData dbData, PgSQLTimestampTZ pgSQLObject, Type targetType ) => |
| | 844 | | { |
| | 845 | | if ( typeof( DateTime ).Equals( targetType ) ) |
| | 846 | | { |
| | 847 | | return (DateTime) pgSQLObject; |
| | 848 | | } |
| | 849 | | else if ( typeof( DateTimeOffset ).Equals( targetType ) ) |
| | 850 | | { |
| | 851 | | return (DateTimeOffset) pgSQLObject; |
| | 852 | | } |
| | 853 | | else |
| | 854 | | { |
| | 855 | | throw new InvalidCastException( "Can't cast time " + pgSQLObject + " to " + targetType + "." ); |
| | 856 | | } |
| | 857 | | }, |
| | 858 | | ( PgSQLTypeDatabaseData dbData, Object systemObject ) => |
| | 859 | | { |
| | 860 | | var tt = systemObject.GetType(); |
| | 861 | | if ( typeof( DateTime ).Equals( tt ) ) |
| | 862 | | { |
| | 863 | | return (PgSQLTimestampTZ) (DateTime) systemObject; |
| | 864 | | } |
| | 865 | | else if ( typeof( DateTimeOffset ).Equals( tt ) ) |
| | 866 | | { |
| | 867 | | return (PgSQLTimestampTZ) (DateTimeOffset) systemObject; |
| | 868 | | } |
| | 869 | | else |
| | 870 | | { |
| | 871 | | throw new InvalidCastException( "Can't cast object " + systemObject + " to timestamp with time zone." |
| | 872 | | } |
| | 873 | | } |
| | 874 | | ); |
| | 875 | |
|
| | 876 | | // Byte[] |
| | 877 | | yield return CreateSingleBodyInfoWithType( |
| | 878 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 879 | | { |
| | 880 | | // Byte[] textual format is "\x<hex decimal pairs>" |
| | 881 | | var encoding = args.Encoding; |
| | 882 | | Byte[] retVal; |
| | 883 | | if ( count > 2 |
| | 884 | | && encoding.ReadASCIIByte( array, ref offset ) == '\\' |
| | 885 | | && encoding.ReadASCIIByte( array, ref offset ) == 'x' |
| | 886 | | ) |
| | 887 | | { |
| | 888 | | var len = ( count - offset ) / encoding.BytesPerASCIICharacter / 2; |
| | 889 | | retVal = new Byte[len]; |
| | 890 | | for ( var i = 0; i < len; ++i ) |
| | 891 | | { |
| | 892 | | retVal[i] = encoding.ReadHexDecimal( array, ref offset ); |
| | 893 | | } |
| | 894 | | } |
| | 895 | | else |
| | 896 | | { |
| | 897 | | throw new PgSQLException( "Bytea strings must start with \"\\x\"." ); |
| | 898 | | } |
| | 899 | |
|
| | 900 | | return retVal; |
| | 901 | | }, |
| | 902 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Int32 count ) => |
| | 903 | | { |
| | 904 | | // Binary format is just raw byte array |
| | 905 | | var retVal = new Byte[count]; |
| | 906 | | Array.Copy( array, offset, retVal, 0, count ); |
| | 907 | | return retVal; |
| | 908 | | }, |
| | 909 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Byte[] value, Boolean isArrayElement ) => |
| | 910 | | { |
| | 911 | | // Text size is 2 ASCII bytes + 2 ASCII bytes per each actual byte |
| | 912 | | var retVal = ( 2 + 2 * value.Length ) * encoding.BytesPerASCIICharacter; |
| | 913 | | if ( isArrayElement ) |
| | 914 | | { |
| | 915 | | // Always need quotation since there is a '\\' character |
| | 916 | | retVal += 2 * encoding.BytesPerASCIICharacter; |
| | 917 | | } |
| | 918 | | return retVal; |
| | 919 | | }, |
| | 920 | | ( PgSQLTypeDatabaseData boundData, IEncodingInfo encoding, Byte[] value, Boolean isArrayElement ) => |
| | 921 | | { |
| | 922 | | // Binary size is same as array size |
| | 923 | | return value.Length; |
| | 924 | | }, |
| | 925 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Byte[] value, TSyncTex |
| | 926 | | { |
| | 927 | | // Write all text to array |
| | 928 | | var encoding = args.Encoding; |
| | 929 | | if ( isArrayElement ) |
| | 930 | | { |
| | 931 | | encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.QUOTE ); |
| | 932 | | } |
| | 933 | |
|
| | 934 | | encoding |
| | 935 | | .WriteASCIIByte( array, ref offset, (Byte) '\\' ) |
| | 936 | | .WriteASCIIByte( array, ref offset, (Byte) 'x' ); |
| | 937 | | foreach ( var b in value ) |
| | 938 | | { |
| | 939 | | encoding.WriteHexDecimal( array, ref offset, b ); |
| | 940 | | } |
| | 941 | | if ( isArrayElement ) |
| | 942 | | { |
| | 943 | | encoding.WriteASCIIByte( array, ref offset, (Byte) PgSQLTypeFunctionalityForArrays.QUOTE ); |
| | 944 | | } |
| | 945 | | }, |
| | 946 | | ( PgSQLTypeDatabaseData boundData, BackendABIHelper args, Byte[] array, Int32 offset, Byte[] value, Int32 ad |
| | 947 | | { |
| | 948 | | // Just copy array |
| | 949 | | Array.Copy( value, 0, array, offset, value.Length ); |
| | 950 | | }, |
| | 951 | | null, |
| | 952 | | null |
| | 953 | | ); |
| | 954 | | } |
| | 955 | |
|
| | 956 | | private static (Type Type, DefaultPgSQLTypeFunctionality<TValue> Functionality) CreateSingleBodyInfoWithType<TValu |
| | 957 | | ReadFromBackendSync<TValue> text2CLR, |
| | 958 | | ReadFromBackendSync<TValue> binary2CLR, |
| | 959 | | CalculateBackendSize<TValue, EitherOr<Int32, String>> textSize, |
| | 960 | | CalculateBackendSize<TValue, Int32> clr2BinarySize, |
| | 961 | | WriteToBackendSync<TValue, TSyncTextualSizeInfo> clr2Text, |
| | 962 | | WriteToBackendSync<TValue, Int32> clr2Binary, |
| | 963 | | ChangePgSQLToSystem<TValue> pgSQL2System, |
| | 964 | | ChangeSystemToPgSQL<TValue> system2PgSQL |
| | 965 | | ) |
| | 966 | | { |
| | 967 | | return (typeof( TValue ), DefaultPgSQLTypeFunctionality<TValue>.CreateSingleBodyUnboundInfo( text2CLR, binary2C |
| | 968 | | } |
| | 969 | | } |
| | 970 | |
|
| | 971 | | } |
| | 972 | |
|
| | 973 | | public static partial class E_CBAM |
| | 974 | | { |
| | 975 | |
|
| | 976 | | internal static Boolean NeedsQuotingInArrayElement( this PgSQLDate date ) |
| | 977 | | { |
| | 978 | | // Values with year less than 0 will be prefixed with " BC" suffix, thus requiring quoting |
| 0 | 979 | | return date.Year < 0; |
| | 980 | | } |
| | 981 | |
|
| | 982 | | internal static Boolean NeedsQuotingInArrayElement( this PgSQLTimestamp timestamp ) |
| | 983 | | { |
| | 984 | | // Anything other than "Infinity" and "-Infinity" will need quoting since there will be a space |
| 0 | 985 | | return timestamp.IsFinite; |
| | 986 | | } |
| | 987 | |
|
| | 988 | | internal static Boolean NeedsQuotingInArrayElement( this PgSQLTimestampTZ timestamp ) |
| | 989 | | { |
| | 990 | | // Anything other than "Infinity" and "-Infinity" will need quoting since there will be a space |
| 0 | 991 | | return timestamp.IsFinite; |
| | 992 | | } |
| | 993 | |
|
| | 994 | | internal delegate void WriteTextBytes<T>( T value, IEncodingInfo encoding, Byte[] array, ref Int32 idx ); |
| | 995 | |
|
| | 996 | | internal static void WriteBytesAndPossiblyQuote<T>( |
| | 997 | | this T value, |
| | 998 | | IEncodingInfo encoding, |
| | 999 | | Byte[] array, |
| | 1000 | | ref Int32 index, |
| | 1001 | | Boolean needsQuoting, |
| | 1002 | | WriteTextBytes<T> writeTextBytes |
| | 1003 | | ) |
| | 1004 | | { |
| 0 | 1005 | | if ( needsQuoting ) |
| | 1006 | | { |
| 0 | 1007 | | encoding.WriteASCIIByte( array, ref index, (Byte) '"' ); |
| | 1008 | | } |
| 0 | 1009 | | writeTextBytes( value, encoding, array, ref index ); |
| 0 | 1010 | | if ( needsQuoting ) |
| | 1011 | | { |
| 0 | 1012 | | encoding.WriteASCIIByte( array, ref index, (Byte) '"' ); |
| | 1013 | | } |
| 0 | 1014 | | } |
| | 1015 | | } |