| | 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 CBAM.SQL; |
| | 20 | | using CBAM.SQL.PostgreSQL; |
| | 21 | | using CBAM.SQL.PostgreSQL.JSON; |
| | 22 | | using Newtonsoft.Json.Linq; |
| | 23 | | using ResourcePooling.Async.Abstractions; |
| | 24 | | using System; |
| | 25 | | using System.Collections.Generic; |
| | 26 | | using System.Text; |
| | 27 | | using System.Threading.Tasks; |
| | 28 | |
|
| | 29 | | namespace 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 | | { |
| 3 | 46 | | pool.AfterResourceCreationEvent += Pool_AfterConnectionCreationEvent; |
| 3 | 47 | | } |
| | 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 | | { |
| 0 | 60 | | pool.AfterResourceCreationEvent -= Pool_AfterConnectionCreationEvent; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private static void Pool_AfterConnectionCreationEvent( AfterAsyncResourceCreationEventArgs<PgSQLConnection> e ) |
| | 64 | | { |
| 3 | 65 | | e.AddAwaitable( e.Resource.AddJSONSupportAsync() ); |
| 3 | 66 | | } |
| | 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... |
| 3 | 79 | | await connection.TypeRegistry.AddTypeFunctionalitiesAsync( |
| 3 | 80 | | ("json", typeof( JToken ), CreateJSONSupport), |
| 3 | 81 | | ("jsonb", typeof( JToken ), CreateJSONBSupport) |
| 3 | 82 | | ); |
| | 83 | |
|
| 3 | 84 | | } |
| | 85 | |
|
| | 86 | | private static TypeFunctionalityCreationResult CreateJSONSupport( PgSQLTypeDatabaseData param ) |
| | 87 | | { |
| 3 | 88 | | return new TypeFunctionalityCreationResult( DefaultPgSQLJSONTypeFunctionality.Instance, false ); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | private static TypeFunctionalityCreationResult CreateJSONBSupport( PgSQLTypeDatabaseData param ) |
| | 92 | | { |
| 3 | 93 | | return new TypeFunctionalityCreationResult( DefaultPgSQLJSONTypeFunctionality.Instance, true ); |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |