Summary

Class:CBAM.SQL.PostgreSQL.JSON.DefaultPgSQLJSONTypeFunctionality
Assembly:CBAM.SQL.PostgreSQL.JSON
File(s):/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL.JSON/Functionality.cs
Covered lines:17
Uncovered lines:8
Coverable lines:25
Total lines:117
Line coverage:68%
Branch coverage:63.6%

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.cctor()101%0%
ChangeTypeFrameworkToPgSQL(...)201%0.5%
ChangeTypePgSQLToFramework(...)100%0%
GetBackendSize(...)400.5%0.25%
ReadBackendValueAsync()300.8%1%
WriteBackendValueAsync()200.667%1%

File(s)

/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL.JSON/Functionality.cs

#LineLine coverage
 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 */
 18using Newtonsoft.Json.Linq;
 19using System;
 20using System.Collections.Generic;
 21using System.IO;
 22using System.Linq;
 23using System.Text;
 24using System.Threading;
 25using System.Threading.Tasks;
 26using UtilPack;
 27
 28namespace CBAM.SQL.PostgreSQL.JSON
 29{
 30   internal class DefaultPgSQLJSONTypeFunctionality : PgSQLTypeFunctionality
 31   {
 132      public static readonly PgSQLTypeFunctionality Instance = new DefaultPgSQLJSONTypeFunctionality();
 33
 34
 035      public Boolean SupportsReadingBinaryFormat => false;
 36
 1237      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
 1242         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      {
 047         throw new NotSupportedException();
 48      }
 49
 50      public BackendSizeInfo GetBackendSize( DataFormat dataFormat, PgSQLTypeDatabaseData boundData, BackendABIHelper he
 51      {
 1252         switch ( dataFormat )
 53         {
 54            case DataFormat.Text:
 1255               return new BackendSizeInfo( helper.Encoding.CalculateJTokenTextSize( (JToken) value ) );
 56            case DataFormat.Binary:
 057               throw new NotSupportedException();
 58            default:
 059               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
 3471         switch ( dataFormat )
 72         {
 73            case DataFormat.Text:
 74
 3475               var boundReader = ReaderFactory.NewNullableMemorizingValueReader(
 3476                     helper.CharacterReader,
 3477                     stream
 3478                     );
 79               // Allow underlying stream buffer to become roughly max 1024 bytes length
 3480               using ( boundReader.ClearStreamWhenStreamBufferTooBig( stream, 1024 ) )
 81               {
 3482                  return await UtilPack.JSON.JTokenStreamReader.Instance.TryReadNextAsync( boundReader );
 83               }
 84            case DataFormat.Binary:
 085               throw new InvalidOperationException( "This data format is not supported" );
 86            default:
 087               throw new NotSupportedException( $"Unrecognized data format: ${dataFormat}." );
 88         }
 3489      }
 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
 12102         switch ( dataFormat )
 103         {
 104            case DataFormat.Text:
 12105               var bytesWritten = await helper.CharacterWriter.CreateJTokenWriter( stream ).TryWriteAsync( (JToken) valu
 12106               break;
 107            case DataFormat.Binary:
 0108               throw new InvalidOperationException( "This data format is not supported" );
 109            default:
 0110               throw new NotSupportedException( $"Unrecognized data format: ${dataFormat}." );
 111         }
 12112      }
 113
 114
 115
 116   }
 117}