-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathClientFactory.cs
More file actions
221 lines (201 loc) · 9.27 KB
/
ClientFactory.cs
File metadata and controls
221 lines (201 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System;
using System.Threading;
using System.Threading.Tasks;
using FirebirdSql.Data.Client.Managed;
using FirebirdSql.Data.Common;
using FirebirdSql.Data.FirebirdClient;
using WireCryptOption = FirebirdSql.Data.Client.Managed.Version13.WireCryptOption;
namespace FirebirdSql.Data.Client;
internal static class ClientFactory
{
public static DatabaseBase CreateDatabase(ConnectionString options)
{
return options.ServerType switch
{
FbServerType.Default => CreateManagedDatabase(options),
FbServerType.Embedded => CreateNativeDatabase(options),
_ => throw IncorrectServerTypeException(),
};
}
public static ValueTask<DatabaseBase> CreateDatabaseAsync(ConnectionString options, CancellationToken cancellationToken = default)
{
return options.ServerType switch
{
FbServerType.Default => CreateManagedDatabaseAsync(options, cancellationToken),
FbServerType.Embedded => CreateNativeDatabaseAsync(options),
_ => throw IncorrectServerTypeException(),
};
}
public static ServiceManagerBase CreateServiceManager(ConnectionString options)
{
return options.ServerType switch
{
FbServerType.Default => CreateManagedServiceManager(options),
FbServerType.Embedded => CreateNativeServiceManager(options),
_ => throw IncorrectServerTypeException(),
};
}
public static ValueTask<ServiceManagerBase> CreateServiceManagerAsync(ConnectionString options, CancellationToken cancellationToken = default)
{
return options.ServerType switch
{
FbServerType.Default => CreateManagedServiceManagerAsync(options, cancellationToken),
FbServerType.Embedded => CreateNativeServiceManagerAsync(options),
_ => throw IncorrectServerTypeException(),
};
}
private static DatabaseBase CreateManagedDatabase(ConnectionString options)
{
var charset = GetCharset(options);
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, options.BlobSegmentSize, charset, options.Dialect, options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt), options.CryptKey);
connection.Connect();
try
{
connection.Identify(options.Database);
}
catch
{
connection.Disconnect();
throw;
}
return connection.ProtocolVersion switch
{
IscCodes.PROTOCOL_VERSION16 => new Managed.Version16.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION15 => new Managed.Version15.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsDatabase(connection),
_ => throw UnsupportedProtocolException(),
};
}
private static async ValueTask<DatabaseBase> CreateManagedDatabaseAsync(ConnectionString options, CancellationToken cancellationToken = default)
{
var charset = GetCharset(options);
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, options.BlobSegmentSize, charset, options.Dialect, options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt), options.CryptKey);
await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);
try
{
await connection.IdentifyAsync(options.Database, cancellationToken).ConfigureAwait(false);
}
catch
{
await connection.DisconnectAsync(cancellationToken).ConfigureAwait(false);
throw;
}
return connection.ProtocolVersion switch
{
IscCodes.PROTOCOL_VERSION16 => new Managed.Version16.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION15 => new Managed.Version15.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsDatabase(connection),
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsDatabase(connection),
_ => throw UnsupportedProtocolException(),
};
}
private static DatabaseBase CreateNativeDatabase(ConnectionString options)
{
var charset = GetCharset(options);
return new Native.FesDatabase(options.ClientLibrary, charset, options.PacketSize, options.BlobSegmentSize, options.Dialect);
}
private static ValueTask<DatabaseBase> CreateNativeDatabaseAsync(ConnectionString options)
{
var charset = GetCharset(options);
return ValueTask.FromResult<DatabaseBase>(new Native.FesDatabase(options.ClientLibrary, charset, options.PacketSize, options.BlobSegmentSize, options.Dialect));
}
private static ServiceManagerBase CreateManagedServiceManager(ConnectionString options)
{
var charset = GetCharset(options);
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, options.BlobSegmentSize, charset, options.Dialect, options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt), options.CryptKey);
connection.Connect();
try
{
connection.Identify(!string.IsNullOrEmpty(options.Database) ? options.Database : string.Empty);
}
catch
{
connection.Disconnect();
throw;
}
return connection.ProtocolVersion switch
{
IscCodes.PROTOCOL_VERSION16 => new Managed.Version16.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION15 => new Managed.Version15.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsServiceManager(connection),
_ => throw UnsupportedProtocolException(),
};
}
private static async ValueTask<ServiceManagerBase> CreateManagedServiceManagerAsync(ConnectionString options, CancellationToken cancellationToken = default)
{
var charset = GetCharset(options);
var connection = new GdsConnection(options.UserID, options.Password, options.DataSource, options.Port, options.ConnectionTimeout, options.PacketSize, options.BlobSegmentSize, charset, options.Dialect, options.Compression, FbWireCryptToWireCryptOption(options.WireCrypt), options.CryptKey);
await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);
try
{
await connection.IdentifyAsync(!string.IsNullOrEmpty(options.Database) ? options.Database : string.Empty, cancellationToken).ConfigureAwait(false);
}
catch
{
await connection.DisconnectAsync(cancellationToken).ConfigureAwait(false);
throw;
}
return connection.ProtocolVersion switch
{
IscCodes.PROTOCOL_VERSION16 => new Managed.Version16.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION15 => new Managed.Version15.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION13 => new Managed.Version13.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION12 => new Managed.Version12.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION11 => new Managed.Version11.GdsServiceManager(connection),
IscCodes.PROTOCOL_VERSION10 => new Managed.Version10.GdsServiceManager(connection),
_ => throw UnsupportedProtocolException(),
};
}
private static ServiceManagerBase CreateNativeServiceManager(ConnectionString options)
{
var charset = GetCharset(options);
return new Native.FesServiceManager(options.ClientLibrary, charset);
}
private static ValueTask<ServiceManagerBase> CreateNativeServiceManagerAsync(ConnectionString options)
{
var charset = GetCharset(options);
return ValueTask.FromResult<ServiceManagerBase>(new Native.FesServiceManager(options.ClientLibrary, charset));
}
private static Exception UnsupportedProtocolException() => new NotSupportedException("Protocol not supported.");
private static Exception IncorrectServerTypeException() => new NotSupportedException("Specified server type is not correct.");
private static Exception InvalidCharsetException() => new ArgumentException("Invalid character set specified.");
private static Charset GetCharset(ConnectionString options)
{
if (!Charset.TryGetByName(options.Charset, out var charset))
throw InvalidCharsetException();
return charset;
}
private static WireCryptOption FbWireCryptToWireCryptOption(FbWireCrypt wireCrypt)
{
return wireCrypt switch
{
FbWireCrypt.Disabled => WireCryptOption.Disabled,
FbWireCrypt.Enabled => WireCryptOption.Enabled,
FbWireCrypt.Required => WireCryptOption.Required,
_ => throw new ArgumentOutOfRangeException(nameof(wireCrypt), $"{nameof(wireCrypt)}={wireCrypt}"),
};
}
}