| | 1 | | using CBAM.SQL.Implementation; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Reflection; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using UtilPack; |
| | 9 | | using TStaticTypeCacheValue = System.Collections.Generic.IDictionary<System.String, CBAM.SQL.PostgreSQL.PgSQLTypeDatabas |
| | 10 | | using TypeInfo = System.ValueTuple<CBAM.SQL.PostgreSQL.PgSQLTypeFunctionality, CBAM.SQL.PostgreSQL.PgSQLTypeDatabaseData |
| | 11 | | using TypeInfoWithCLRType = System.ValueTuple<System.Type, CBAM.SQL.PostgreSQL.PgSQLTypeFunctionality, CBAM.SQL.PostgreS |
| | 12 | |
|
| | 13 | | namespace CBAM.SQL.PostgreSQL.Implementation |
| | 14 | | { |
| | 15 | | using SQLConnectionFunctionality = Func<String, IAsyncEnumerable<SQLStatementExecutionResult>>; // Abstractions.Conne |
| | 16 | |
|
| | 17 | | internal class TypeRegistryImpl : TypeRegistry |
| | 18 | | { |
| | 19 | | private const Char ARRAY_PREFIX = '_'; |
| | 20 | |
|
| | 21 | | private readonly IDictionary<Int32, TypeFunctionalityInformation> _typeInfos; |
| | 22 | | private readonly IDictionary<Type, TypeFunctionalityInformation> _typeInfosByCLRType; |
| | 23 | |
|
| | 24 | | private readonly SQLConnectionVendorFunctionality _vendorFunctionality; |
| | 25 | | private readonly SQLConnectionFunctionality _connectionFunctionality; |
| | 26 | |
|
| 24 | 27 | | public TypeRegistryImpl( |
| 24 | 28 | | SQLConnectionVendorFunctionality vendorFunctionality, |
| 24 | 29 | | SQLConnectionFunctionality connectionFunctionality |
| 24 | 30 | | ) |
| | 31 | | { |
| 24 | 32 | | this._vendorFunctionality = ArgumentValidator.ValidateNotNull( nameof( vendorFunctionality ), vendorFunctionali |
| 24 | 33 | | this._connectionFunctionality = ArgumentValidator.ValidateNotNull( nameof( connectionFunctionality ), connectio |
| | 34 | |
|
| 24 | 35 | | this._typeInfos = new Dictionary<Int32, TypeFunctionalityInformation>(); |
| 24 | 36 | | this._typeInfosByCLRType = new Dictionary<Type, TypeFunctionalityInformation>(); |
| 24 | 37 | | } |
| | 38 | |
|
| | 39 | | public async ValueTask<Int32> AddTypeFunctionalitiesAsync( params (String DBTypeName, Type CLRType, Func<PgSQLType |
| | 40 | | { |
| 3 | 41 | | var retVal = 0; |
| 3 | 42 | | if ( functionalities != null ) |
| | 43 | | { |
| 15 | 44 | | var dic = functionalities.ToDictionary_Overwrite( tuple => tuple.DBTypeName, tuple => tuple ); |
| 3 | 45 | | retVal = dic.Count; |
| 3 | 46 | | this.AssignTypeData( |
| 3 | 47 | | await this.ReadTypeDataFromServer( dic.Keys ), |
| 15 | 48 | | typeName => dic[typeName].CLRType, |
| 9 | 49 | | tuple => dic[tuple.DBTypeName].FunctionalityCreator( tuple.BoundData ) |
| 3 | 50 | | ); |
| 3 | 51 | | } |
| | 52 | |
|
| 3 | 53 | | return retVal; |
| 3 | 54 | | } |
| | 55 | |
|
| | 56 | | public TypeFunctionalityInformation TryGetTypeInfo( Int32 typeID ) |
| | 57 | | { |
| 444 | 58 | | this._typeInfos.TryGetValue( typeID, out var retVal ); |
| 446 | 59 | | return retVal; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public TypeFunctionalityInformation TryGetTypeInfo( Type clrType ) |
| | 63 | | { |
| | 64 | | TypeFunctionalityInformation retVal; |
| 49 | 65 | | if ( clrType != null ) |
| | 66 | | { |
| | 67 | | KeyValuePair<Type, TypeFunctionalityInformation> kvp; |
| 53 | 68 | | if ( !this._typeInfosByCLRType.TryGetValue( clrType, out retVal ) ) |
| | 69 | | { |
| 1 | 70 | | if ( ( kvp = this.TryFindByParent( clrType ) ).Value != null ) |
| | 71 | | { |
| 1 | 72 | | retVal = kvp.Value; |
| 1 | 73 | | } |
| | 74 | | else |
| | 75 | | { |
| 0 | 76 | | retVal = default; |
| | 77 | | } |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 0 | 82 | | retVal = default; |
| | 83 | | } |
| 54 | 84 | | return retVal; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | private KeyValuePair<Type, TypeFunctionalityInformation> TryFindByParent( Type clrType ) |
| | 88 | | { |
| 1 | 89 | | var child = clrType |
| 1 | 90 | | #if !NET40 && !NET45 |
| 1 | 91 | | .GetTypeInfo() |
| 1 | 92 | | #endif |
| 1 | 93 | | ; |
| 36 | 94 | | return this._typeInfosByCLRType.FirstOrDefault( kvp => kvp.Key |
| 36 | 95 | | #if !NET40 && !NET45 |
| 36 | 96 | | .GetTypeInfo() |
| 36 | 97 | | #endif |
| 36 | 98 | | .IsAssignableFrom( child ) |
| 1 | 99 | | ); |
| | 100 | | } |
| | 101 | |
|
| | 102 | | public async ValueTask<TStaticTypeCacheValue> ReadTypeDataFromServer( |
| | 103 | | IEnumerable<String> typeNames |
| | 104 | | ) |
| | 105 | | { |
| 4 | 106 | | return await this._connectionFunctionality( |
| 4 | 107 | | "SELECT typname, oid, typdelim, typelem\n" + |
| 4 | 108 | | "FROM pg_type\n" + |
| 4 | 109 | | "WHERE typname IN (" + String.Join( ", ", typeNames.Select( typename => |
| 4 | 110 | | { |
| 33 | 111 | | typename = this._vendorFunctionality.EscapeLiteral( typename ); |
| 33 | 112 | | return "'" + typename + "', '" + ARRAY_PREFIX + typename + "'"; |
| 4 | 113 | | } ) ) + ")\n" |
| 4 | 114 | | ).IncludeDataRowsOnly() |
| 61 | 115 | | .Select( async row => new PgSQLTypeDatabaseData( |
| 61 | 116 | | // We need to get all values as strings, since we might not have type mapping yet (we might be building i |
| 61 | 117 | | await row.GetValueAsync<String>( 0 ), |
| 61 | 118 | | Int32.Parse( await row.GetValueAsync<String>( 1 ) ), |
| 61 | 119 | | await row.GetValueAsync<String>( 2 ), |
| 61 | 120 | | Int32.Parse( await row.GetValueAsync<String>( 3 ) |
| 61 | 121 | | ) ) ) |
| 118 | 122 | | .ToDictionaryAsync( type => type.TypeName, type => type ); |
| 4 | 123 | | } |
| | 124 | |
|
| | 125 | | public void AssignTypeData( |
| | 126 | | TStaticTypeCacheValue typeData, |
| | 127 | | Func<String, Type> clrTypeExtractor, |
| | 128 | | Func<(String DBTypeName, PgSQLTypeDatabaseData BoundData), TypeFunctionalityCreationResult> funcExtractor |
| | 129 | | ) |
| | 130 | | { |
| 2238 | 131 | | foreach ( var kvp in typeData ) |
| | 132 | | { |
| 1092 | 133 | | var typeName = kvp.Key; |
| 1092 | 134 | | var boundData = kvp.Value; |
| | 135 | | PgSQLTypeFunctionality thisFunc; |
| | 136 | | Boolean isDefaultForThisCLRType; |
| | 137 | | Type clrType; |
| 1092 | 138 | | if ( typeName[0] == ARRAY_PREFIX ) |
| | 139 | | { |
| 534 | 140 | | clrType = clrTypeExtractor( typeName.Substring( 1 ) ); |
| 534 | 141 | | thisFunc = new PgSQLTypeFunctionalityForArrays( this, ref clrType, kvp.Value.ElementTypeID ); |
| 534 | 142 | | clrType = clrType.MakeArrayType(); |
| 534 | 143 | | isDefaultForThisCLRType = true; |
| 534 | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 558 | 147 | | clrType = clrTypeExtractor( typeName ); |
| 558 | 148 | | var result = funcExtractor( (typeName, boundData) ); |
| 558 | 149 | | (thisFunc, isDefaultForThisCLRType) = (result.TypeFunctionality, result.IsDefaultForCLRType); |
| | 150 | | } |
| 1092 | 151 | | if ( thisFunc != null ) |
| | 152 | | { |
| 1092 | 153 | | var typeInfo = new TypeFunctionalityInformation( clrType, thisFunc, boundData ); |
| 1092 | 154 | | this._typeInfos[boundData.TypeID] = typeInfo; |
| 1092 | 155 | | if ( isDefaultForThisCLRType || !this._typeInfosByCLRType.ContainsKey( clrType ) ) |
| | 156 | | { |
| 1020 | 157 | | this._typeInfosByCLRType[clrType] = typeInfo; |
| | 158 | | } |
| | 159 | | } |
| | 160 | | } |
| 27 | 161 | | } |
| | 162 | | } |
| | 163 | | } |