Skip to content

Commit 571497b

Browse files
committed
Rename "Stream" to "Source".
This prepares for other types of sources to be added.
1 parent ee921eb commit 571497b

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,26 @@ public async Task ReadResultSetHeaderAsync(IOBehavior ioBehavior)
7272
if (!Connection.AllowLoadLocalInfile)
7373
throw new NotSupportedException("To use LOAD DATA LOCAL INFILE, set AllowLoadLocalInfile=true in the connection string. See https://fl.vu/mysql-load-data");
7474
var localInfile = LocalInfilePayload.Create(payload.Span);
75-
if (!IsHostVerified(Connection)
76-
&& !localInfile.FileName.StartsWith(MySqlBulkLoader.StreamPrefix, StringComparison.Ordinal))
75+
var hasSourcePrefix = localInfile.FileName.StartsWith(MySqlBulkLoader.SourcePrefix, StringComparison.Ordinal);
76+
if (!IsHostVerified(Connection) && !hasSourcePrefix)
7777
throw new NotSupportedException("Use SourceStream or SslMode >= VerifyCA for LOAD DATA LOCAL INFILE. See https://fl.vu/mysql-load-data");
7878

79-
using var stream = localInfile.FileName.StartsWith(MySqlBulkLoader.StreamPrefix, StringComparison.Ordinal) ?
80-
MySqlBulkLoader.GetAndRemoveStream(localInfile.FileName) :
79+
var source = hasSourcePrefix ?
80+
MySqlBulkLoader.GetAndRemoveSource(localInfile.FileName) :
8181
File.OpenRead(localInfile.FileName);
82-
var readBuffer = new byte[65536];
83-
int byteCount;
84-
while ((byteCount = await stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) > 0)
82+
83+
if (source is Stream stream)
8584
{
86-
payload = new PayloadData(new ArraySegment<byte>(readBuffer, 0, byteCount));
87-
await Session.SendReplyAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
85+
using (stream)
86+
{
87+
var readBuffer = new byte[65536];
88+
int byteCount;
89+
while ((byteCount = await stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) > 0)
90+
{
91+
payload = new PayloadData(new ArraySegment<byte>(readBuffer, 0, byteCount));
92+
await Session.SendReplyAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
93+
}
94+
}
8895
}
8996
}
9097
catch (Exception ex)

src/MySqlConnector/MySql.Data.MySqlClient/MySqlBulkLoader.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed class MySqlBulkLoader
1616
private const char defaultEscapeCharacter = '\\';
1717

1818
private static readonly object s_lock = new object();
19-
private static readonly Dictionary<string, Stream> s_streams = new Dictionary<string, Stream>();
19+
private static readonly Dictionary<string, object> s_sources = new Dictionary<string, object>();
2020

2121
public string? CharacterSet { get; set; }
2222
public List<string> Columns { get; }
@@ -165,7 +165,7 @@ private async Task<int> LoadAsync(IOBehavior ioBehavior, CancellationToken cance
165165

166166
FileName = GenerateSourceStreamName();
167167
lock (s_lock)
168-
s_streams.Add(FileName, SourceStream);
168+
s_sources.Add(FileName, SourceStream);
169169
}
170170

171171
if (string.IsNullOrWhiteSpace(FileName) || string.IsNullOrWhiteSpace(TableName))
@@ -203,12 +203,7 @@ private async Task<int> LoadAsync(IOBehavior ioBehavior, CancellationToken cance
203203
finally
204204
{
205205
if (closeStream)
206-
{
207-
using (GetAndRemoveStream(FileName!))
208-
{
209-
// close the stream
210-
}
211-
}
206+
((IDisposable) GetAndRemoveSource(FileName!)).Dispose();
212207

213208
if (closeConnection)
214209
Connection.Close();
@@ -229,17 +224,17 @@ private Stream CreateFileStream(string fileName)
229224

230225
private static string GenerateSourceStreamName()
231226
{
232-
return StreamPrefix + Guid.NewGuid().ToString("N");
227+
return SourcePrefix + Guid.NewGuid().ToString("N");
233228
}
234229

235-
internal const string StreamPrefix = ":STREAM:";
230+
internal const string SourcePrefix = ":SOURCE:";
236231

237-
internal static Stream GetAndRemoveStream(string streamKey)
232+
internal static object GetAndRemoveSource(string sourceKey)
238233
{
239234
lock (s_lock)
240235
{
241-
var stream = s_streams[streamKey];
242-
s_streams.Remove(streamKey);
236+
var stream = s_sources[sourceKey];
237+
s_sources.Remove(sourceKey);
243238
return stream;
244239
}
245240
}

0 commit comments

Comments
 (0)