| | | 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 Newtonsoft.Json.Linq; |
| | | 19 | | using System; |
| | | 20 | | using System.Collections.Generic; |
| | | 21 | | using System.IO; |
| | | 22 | | using System.Linq; |
| | | 23 | | using System.Text; |
| | | 24 | | using System.Threading; |
| | | 25 | | using System.Threading.Tasks; |
| | | 26 | | using UtilPack; |
| | | 27 | | |
| | | 28 | | namespace CBAM.SQL.PostgreSQL.JSON |
| | | 29 | | { |
| | | 30 | | internal class DefaultPgSQLJSONTypeFunctionality : PgSQLTypeFunctionality |
| | | 31 | | { |
| | 1 | 32 | | public static readonly PgSQLTypeFunctionality Instance = new DefaultPgSQLJSONTypeFunctionality(); |
| | | 33 | | |
| | | 34 | | |
| | 0 | 35 | | public Boolean SupportsReadingBinaryFormat => false; |
| | | 36 | | |
| | 12 | 37 | | public Boolean SupportsWritingBinaryFormat => false; |
| | | 38 | | |
| | | 39 | | public Object ChangeTypeFrameworkToPgSQL( PgSQLTypeDatabaseData dbData, Object obj ) |
| | | 40 | | { |
| | | 41 | | // JToken is abstract class, so we will enter here always |
| | 12 | 42 | | return obj is JToken ? obj : throw new InvalidCastException( $"The object must be descendant of {typeof( JToken |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public Object ChangeTypePgSQLToFramework( PgSQLTypeDatabaseData dbData, Object obj, Type typeTo ) |
| | | 46 | | { |
| | 0 | 47 | | throw new NotSupportedException(); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public BackendSizeInfo GetBackendSize( DataFormat dataFormat, PgSQLTypeDatabaseData boundData, BackendABIHelper he |
| | | 51 | | { |
| | 12 | 52 | | switch ( dataFormat ) |
| | | 53 | | { |
| | | 54 | | case DataFormat.Text: |
| | 12 | 55 | | return new BackendSizeInfo( helper.Encoding.CalculateJTokenTextSize( (JToken) value ) ); |
| | | 56 | | case DataFormat.Binary: |
| | 0 | 57 | | throw new NotSupportedException(); |
| | | 58 | | default: |
| | 0 | 59 | | throw new NotSupportedException( $"Data format {dataFormat} is not recognized." ); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public async ValueTask<Object> ReadBackendValueAsync( |
| | | 64 | | DataFormat dataFormat, |
| | | 65 | | PgSQLTypeDatabaseData boundData, |
| | | 66 | | BackendABIHelper helper, |
| | | 67 | | StreamReaderWithResizableBufferAndLimitedSize stream |
| | | 68 | | ) |
| | | 69 | | { |
| | | 70 | | // No truly async support for deserializing JTokens in Newtonsoft, at least yet, so let's do it ourselves |
| | 34 | 71 | | switch ( dataFormat ) |
| | | 72 | | { |
| | | 73 | | case DataFormat.Text: |
| | | 74 | | |
| | 34 | 75 | | var boundReader = ReaderFactory.NewNullableMemorizingValueReader( |
| | 34 | 76 | | helper.CharacterReader, |
| | 34 | 77 | | stream |
| | 34 | 78 | | ); |
| | | 79 | | // Allow underlying stream buffer to become roughly max 1024 bytes length |
| | 34 | 80 | | using ( boundReader.ClearStreamWhenStreamBufferTooBig( stream, 1024 ) ) |
| | | 81 | | { |
| | 34 | 82 | | return await UtilPack.JSON.JTokenStreamReader.Instance.TryReadNextAsync( boundReader ); |
| | | 83 | | } |
| | | 84 | | case DataFormat.Binary: |
| | 0 | 85 | | throw new InvalidOperationException( "This data format is not supported" ); |
| | | 86 | | default: |
| | 0 | 87 | | throw new NotSupportedException( $"Unrecognized data format: ${dataFormat}." ); |
| | | 88 | | } |
| | 34 | 89 | | } |
| | | 90 | | |
| | | 91 | | public async Task WriteBackendValueAsync( |
| | | 92 | | DataFormat dataFormat, |
| | | 93 | | PgSQLTypeDatabaseData boundData, |
| | | 94 | | BackendABIHelper helper, |
| | | 95 | | StreamWriterWithResizableBufferAndLimitedSize stream, |
| | | 96 | | Object value, |
| | | 97 | | BackendSizeInfo additionalInfoFromSize, |
| | | 98 | | Boolean isArrayElement |
| | | 99 | | ) |
| | | 100 | | { |
| | | 101 | | // No truly async support for serializing JTokens in Newtonsoft, at least yet, so let's do it ourselves |
| | 12 | 102 | | switch ( dataFormat ) |
| | | 103 | | { |
| | | 104 | | case DataFormat.Text: |
| | 12 | 105 | | var bytesWritten = await helper.CharacterWriter.CreateJTokenWriter( stream ).TryWriteAsync( (JToken) valu |
| | 12 | 106 | | break; |
| | | 107 | | case DataFormat.Binary: |
| | 0 | 108 | | throw new InvalidOperationException( "This data format is not supported" ); |
| | | 109 | | default: |
| | 0 | 110 | | throw new NotSupportedException( $"Unrecognized data format: ${dataFormat}." ); |
| | | 111 | | } |
| | 12 | 112 | | } |
| | | 113 | | |
| | | 114 | | |
| | | 115 | | |
| | | 116 | | } |
| | | 117 | | } |