-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathOracleContainer.cs
More file actions
45 lines (39 loc) · 2 KB
/
OracleContainer.cs
File metadata and controls
45 lines (39 loc) · 2 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
namespace Testcontainers.Oracle;
/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class OracleContainer : DockerContainer, IDatabaseContainer
{
private readonly OracleConfiguration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="OracleContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
public OracleContainer(OracleConfiguration configuration)
: base(configuration)
{
_configuration = configuration;
}
/// <summary>
/// Gets the Oracle connection string.
/// </summary>
/// <returns>The Oracle connection string.</returns>
public string GetConnectionString()
{
const string dataSource = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SERVICE_NAME={2})));User Id={3};Password={4};";
return string.Format(dataSource, Hostname, GetMappedPublicPort(OracleBuilder.OraclePort), _configuration.Database, _configuration.Username, _configuration.Password);
}
/// <summary>
/// Executes the SQL script in the Oracle container.
/// </summary>
/// <param name="scriptContent">The content of the SQL script to execute.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the SQL script has been executed.</returns>
public async Task<ExecResult> ExecScriptAsync(string scriptContent, CancellationToken ct = default)
{
var scriptFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid().ToString("D"), Path.GetRandomFileName());
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, fileMode: Unix.FileMode644, ct: ct)
.ConfigureAwait(false);
return await ExecAsync(new[] { "/bin/sh", "-c", $"exit | sqlplus -LOGON -SILENT {_configuration.Username}/{_configuration.Password}@localhost:1521/{_configuration.Database} @{scriptFilePath}" }, ct)
.ConfigureAwait(false);
}
}