Summary

Class:CBAM.SQL.PostgreSQL.JSON.CBAMExtensions
Assembly:CBAM.SQL.PostgreSQL.JSON
File(s):/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL.JSON/Support.cs
Covered lines:11
Uncovered lines:2
Coverable lines:13
Total lines:96
Line coverage:84.6%
Branch coverage:100%

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
EnableJSONSupport(...)101%0%
DisableJSONSupport(...)100%0%
Pool_AfterConnectionCreationEvent(...)101%0%
AddJSONSupportAsync()201%1%
CreateJSONSupport(...)101%0%
CreateJSONBSupport(...)101%0%

File(s)

/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL.JSON/Support.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 CBAM.Abstractions;
 19using CBAM.SQL;
 20using CBAM.SQL.PostgreSQL;
 21using CBAM.SQL.PostgreSQL.JSON;
 22using Newtonsoft.Json.Linq;
 23using ResourcePooling.Async.Abstractions;
 24using System;
 25using System.Collections.Generic;
 26using System.Text;
 27using System.Threading.Tasks;
 28
 29namespace CBAM.SQL.PostgreSQL.JSON
 30{
 31   /// <summary>
 32   /// This class contains extension methods for types defined in other assemblies.
 33   /// </summary>
 34   public static class CBAMExtensions
 35   {
 36      /// <summary>
 37      /// This method will add support for <c>json</c> and <c>jsonb</c> PostgreSQL types for all connections instantiate
 38      /// </summary>
 39      /// <param name="pool">This <see cref="AsyncResourcePoolObservable{TResource}"/>.</param>
 40      /// <exception cref="NullReferenceException">If this <see cref="AsyncResourcePoolObservable{TResource}"/> is <c>nu
 41      /// <remarks>
 42      /// The "support" here means that the value returned by <see cref="UtilPack.TabularData.AsyncDataColumn.TryGetValu
 43      /// </remarks>
 44      public static void EnableJSONSupport( this AsyncResourcePoolObservable<PgSQLConnection> pool )
 45      {
 346         pool.AfterResourceCreationEvent += Pool_AfterConnectionCreationEvent;
 347      }
 48
 49      /// <summary>
 50      /// This method will remove support for <c>json</c> and <c>jsonb</c> PostgreSQL types for all connections instanti
 51      /// </summary>
 52      /// <param name="pool">This <see cref="AsyncResourcePoolObservable{TResource}"/>.</param>
 53      /// <exception cref="NullReferenceException">If this <see cref="AsyncResourcePoolObservable{TResource}"/> is <c>nu
 54      /// <remarks>
 55      /// The "support" here means that the value returned by <see cref="UtilPack.TabularData.AsyncDataColumn.TryGetValu
 56      /// If this <paramref name="pool"/> has cached connections, the support will not be removed from them.
 57      /// </remarks>
 58      public static void DisableJSONSupport( this AsyncResourcePoolObservable<PgSQLConnection> pool )
 59      {
 060         pool.AfterResourceCreationEvent -= Pool_AfterConnectionCreationEvent;
 061      }
 62
 63      private static void Pool_AfterConnectionCreationEvent( AfterAsyncResourceCreationEventArgs<PgSQLConnection> e )
 64      {
 365         e.AddAwaitable( e.Resource.AddJSONSupportAsync() );
 366      }
 67
 68      /// <summary>
 69      /// This method will add support for <c>json</c> and <c>jsonb</c> PostgreSQL types for this specific <see cref="Pg
 70      /// </summary>
 71      /// <param name="connection">This <see cref="PgSQLConnection"/>.</param>
 72      /// <returns>A task which on completion has added support for <c>json</c> and <c>jsonb</c> PostgreSQL types for th
 73      /// <remarks>
 74      /// The "support" here means that the value returned by <see cref="UtilPack.TabularData.AsyncDataColumn.TryGetValu
 75      /// </remarks>
 76      public static async Task AddJSONSupportAsync( this PgSQLConnection connection )
 77      {
 78         // TODO detect if we already added support...
 379         await connection.TypeRegistry.AddTypeFunctionalitiesAsync(
 380            ("json", typeof( JToken ), CreateJSONSupport),
 381            ("jsonb", typeof( JToken ), CreateJSONBSupport)
 382            );
 83
 384      }
 85
 86      private static TypeFunctionalityCreationResult CreateJSONSupport( PgSQLTypeDatabaseData param )
 87      {
 388         return new TypeFunctionalityCreationResult( DefaultPgSQLJSONTypeFunctionality.Instance, false );
 89      }
 90
 91      private static TypeFunctionalityCreationResult CreateJSONBSupport( PgSQLTypeDatabaseData param )
 92      {
 393         return new TypeFunctionalityCreationResult( DefaultPgSQLJSONTypeFunctionality.Instance, true );
 94      }
 95   }
 96}