1616//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
1717
1818using System ;
19+ using System . Threading ;
1920using System . Threading . Tasks ;
2021using FirebirdSql . Data . Client . Managed ;
2122using FirebirdSql . Data . Common ;
@@ -26,37 +27,77 @@ namespace FirebirdSql.Data.Client
2627{
2728 internal static class ClientFactory
2829 {
29- public static async ValueTask < DatabaseBase > CreateDatabaseAsync ( ConnectionString options , AsyncWrappingCommonArgs async )
30+ public static DatabaseBase CreateDatabase ( ConnectionString options )
3031 {
3132 return options . ServerType switch
3233 {
33- FbServerType . Default => await CreateManagedDatabaseAsync ( options , async ) . ConfigureAwait( false) ,
34+ FbServerType . Default => CreateManagedDatabase ( options ) ,
35+ FbServerType . Embedded => new Native . FesDatabase ( options . ClientLibrary , Charset . GetCharset ( options . Charset ) ) ,
36+ _ => throw IncorrectServerTypeException ( ) ,
37+ } ;
38+ }
39+ public static async ValueTask < DatabaseBase > CreateDatabaseAsync ( ConnectionString options , CancellationToken cancellationToken = default )
40+ {
41+ return options . ServerType switch
42+ {
43+ FbServerType . Default => await CreateManagedDatabaseAsync ( options , cancellationToken ) . ConfigureAwait ( false ) ,
3444 FbServerType . Embedded => new Native . FesDatabase ( options . ClientLibrary , Charset . GetCharset ( options . Charset ) ) ,
3545 _ => throw IncorrectServerTypeException ( ) ,
3646 } ;
3747 }
3848
39- public static async ValueTask < ServiceManagerBase > CreateServiceManagerAsync ( ConnectionString options , AsyncWrappingCommonArgs async )
49+ public static ServiceManagerBase CreateServiceManager ( ConnectionString options )
50+ {
51+ return options . ServerType switch
52+ {
53+ FbServerType . Default => CreateManagedServiceManager ( options ) ,
54+ FbServerType . Embedded => new Native . FesServiceManager ( options . ClientLibrary , Charset . GetCharset ( options . Charset ) ) ,
55+ _ => throw IncorrectServerTypeException ( ) ,
56+ } ;
57+ }
58+ public static async ValueTask < ServiceManagerBase > CreateServiceManagerAsync ( ConnectionString options , CancellationToken cancellationToken = default )
4059 {
4160 return options . ServerType switch
4261 {
43- FbServerType . Default => await CreateManagedServiceManagerAsync ( options , async ) . ConfigureAwait ( false) ,
62+ FbServerType . Default => await CreateManagedServiceManagerAsync ( options , cancellationToken ) . ConfigureAwait ( false ) ,
4463 FbServerType . Embedded => new Native . FesServiceManager ( options . ClientLibrary , Charset . GetCharset ( options . Charset ) ) ,
4564 _ => throw IncorrectServerTypeException ( ) ,
4665 } ;
4766 }
4867
49- private static async ValueTask < DatabaseBase > CreateManagedDatabaseAsync ( ConnectionString options , AsyncWrappingCommonArgs async )
68+ private static DatabaseBase CreateManagedDatabase ( ConnectionString options )
5069 {
5170 var connection = new GdsConnection ( options . UserID , options . Password , options . DataSource , options . Port , options . ConnectionTimeout , options . PacketSize , Charset . GetCharset ( options . Charset ) , options . Compression , FbWireCryptToWireCryptOption ( options . WireCrypt ) ) ;
52- await connection . ConnectAsync ( async ) . ConfigureAwait ( false ) ;
71+ connection . Connect ( ) ;
5372 try
5473 {
55- await connection . IdentifyAsync ( options . Database , async ) . ConfigureAwait ( false ) ;
74+ connection . Identify ( options . Database ) ;
5675 }
5776 catch
5877 {
59- await connection . DisconnectAsync ( async ) . ConfigureAwait( false) ;
78+ connection . Disconnect ( ) ;
79+ throw ;
80+ }
81+ return connection . ProtocolVersion switch
82+ {
83+ IscCodes . PROTOCOL_VERSION13 => new Managed . Version13 . GdsDatabase ( connection ) ,
84+ IscCodes . PROTOCOL_VERSION12 => new Managed . Version12 . GdsDatabase ( connection ) ,
85+ IscCodes . PROTOCOL_VERSION11 => new Managed . Version11 . GdsDatabase ( connection ) ,
86+ IscCodes . PROTOCOL_VERSION10 => new Managed . Version10 . GdsDatabase ( connection ) ,
87+ _ => throw UnsupportedProtocolException ( ) ,
88+ } ;
89+ }
90+ private static async ValueTask < DatabaseBase > CreateManagedDatabaseAsync ( ConnectionString options , CancellationToken cancellationToken = default )
91+ {
92+ var connection = new GdsConnection ( options . UserID , options . Password , options . DataSource , options . Port , options . ConnectionTimeout , options . PacketSize , Charset . GetCharset ( options . Charset ) , options . Compression , FbWireCryptToWireCryptOption ( options . WireCrypt ) ) ;
93+ await connection . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
94+ try
95+ {
96+ await connection . IdentifyAsync ( options . Database , cancellationToken ) . ConfigureAwait ( false ) ;
97+ }
98+ catch
99+ {
100+ await connection . DisconnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
60101 throw ;
61102 }
62103 return connection . ProtocolVersion switch
@@ -69,17 +110,17 @@ private static async ValueTask<DatabaseBase> CreateManagedDatabaseAsync(Connecti
69110 } ;
70111 }
71112
72- private static async ValueTask < ServiceManagerBase > CreateManagedServiceManagerAsync ( ConnectionString options , AsyncWrappingCommonArgs async )
113+ private static ServiceManagerBase CreateManagedServiceManager ( ConnectionString options )
73114 {
74115 var connection = new GdsConnection ( options . UserID , options . Password , options . DataSource , options . Port , options . ConnectionTimeout , options . PacketSize , Charset . GetCharset ( options . Charset ) , options . Compression , FbWireCryptToWireCryptOption ( options . WireCrypt ) ) ;
75- await connection . ConnectAsync ( async ) . ConfigureAwait ( false ) ;
116+ connection . Connect ( ) ;
76117 try
77118 {
78- await connection . IdentifyAsync ( ! string . IsNullOrEmpty ( options . Database ) ? options . Database : string . Empty , async ) . ConfigureAwait ( false ) ;
119+ connection . Identify ( ! string . IsNullOrEmpty ( options . Database ) ? options . Database : string . Empty ) ;
79120 }
80121 catch
81122 {
82- await connection . DisconnectAsync ( async ) . ConfigureAwait ( false ) ;
123+ connection . Disconnect ( ) ;
83124 throw ;
84125 }
85126 return connection . ProtocolVersion switch
@@ -91,8 +132,30 @@ private static async ValueTask<ServiceManagerBase> CreateManagedServiceManagerAs
91132 _ => throw UnsupportedProtocolException ( ) ,
92133 } ;
93134 }
94-
95- private static NotSupportedException UnsupportedProtocolException( )
135+ private static async ValueTask < ServiceManagerBase > CreateManagedServiceManagerAsync ( ConnectionString options , CancellationToken cancellationToken = default )
136+ {
137+ var connection = new GdsConnection ( options . UserID , options . Password , options . DataSource , options . Port , options . ConnectionTimeout , options . PacketSize , Charset . GetCharset ( options . Charset ) , options . Compression , FbWireCryptToWireCryptOption ( options . WireCrypt ) ) ;
138+ await connection . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
139+ try
140+ {
141+ await connection . IdentifyAsync ( ! string . IsNullOrEmpty ( options . Database ) ? options . Database : string . Empty , cancellationToken ) . ConfigureAwait ( false ) ;
142+ }
143+ catch
144+ {
145+ await connection . DisconnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
146+ throw ;
147+ }
148+ return connection . ProtocolVersion switch
149+ {
150+ IscCodes . PROTOCOL_VERSION13 => new Managed . Version13 . GdsServiceManager ( connection ) ,
151+ IscCodes . PROTOCOL_VERSION12 => new Managed . Version12 . GdsServiceManager ( connection ) ,
152+ IscCodes . PROTOCOL_VERSION11 => new Managed . Version11 . GdsServiceManager ( connection ) ,
153+ IscCodes . PROTOCOL_VERSION10 => new Managed . Version10 . GdsServiceManager ( connection ) ,
154+ _ => throw UnsupportedProtocolException ( ) ,
155+ } ;
156+ }
157+
158+ private static Exception UnsupportedProtocolException ( )
96159 {
97160 return new NotSupportedException ( "Protocol not supported." ) ;
98161 }
0 commit comments