-
Notifications
You must be signed in to change notification settings - Fork 330
Add streaming support for netcore and netfx #2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
apoorvdeshmukh
merged 4 commits into
dotnet:feat-jsonsupport
from
apoorvdeshmukh:dev/streaming-support
Sep 3, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
99bf4ef
Add streaming support for netcore and netfx
apoorvdeshmukh dab5aee
Add multibyte character for testing and use UTF8 encoding in netcore …
apoorvdeshmukh aa956b5
Add async testcase for streaming support
apoorvdeshmukh c56da7b
Remove unnecessary UTF8 setting for Console
apoorvdeshmukh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonStreamTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Data; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
| { | ||
| public class JsonRecord | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } | ||
| } | ||
|
|
||
| public class JsonStreamTest | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
| private static readonly string _jsonFile = "randomRecords.json"; | ||
| private static readonly string _outputFile = "serverRecords.json"; | ||
|
|
||
| public JsonStreamTest(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| private void GenerateJsonFile(int noOfRecords, string filename) | ||
| { | ||
| DeleteFile(filename); | ||
| var random = new Random(); | ||
| var records = new List<JsonRecord>(); | ||
| int recordCount = noOfRecords; | ||
|
|
||
| for (int i = 0; i < recordCount; i++) | ||
| { | ||
| records.Add(new JsonRecord | ||
| { | ||
| Id = i + 1, | ||
| Name = "𩸽json" + random.Next(1, noOfRecords), | ||
| }); | ||
| } | ||
|
|
||
| string json = JsonConvert.SerializeObject(records, Formatting.Indented); | ||
| File.WriteAllText(filename, json); | ||
| Assert.True(File.Exists(filename)); | ||
| _output.WriteLine("Generated JSON file "+filename); | ||
| } | ||
|
|
||
| private void CompareJsonFiles() | ||
| { | ||
| using (var stream1 = File.OpenText(_jsonFile)) | ||
| using (var stream2 = File.OpenText(_outputFile)) | ||
| using (var reader1 = new JsonTextReader(stream1)) | ||
| using (var reader2 = new JsonTextReader(stream2)) | ||
| { | ||
| var jToken1 = JToken.ReadFrom(reader1); | ||
| var jToken2 = JToken.ReadFrom(reader2); | ||
| Assert.True(JToken.DeepEquals(jToken1, jToken2)); | ||
| } | ||
| } | ||
|
|
||
| private void PrintJsonDataToFile(SqlConnection connection) | ||
| { | ||
| DeleteFile(_outputFile); | ||
| using (SqlCommand command = new SqlCommand("SELECT [data] FROM [jsonTab]", connection)) | ||
| { | ||
| using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess)) | ||
| { | ||
| using (StreamWriter sw = new StreamWriter(_outputFile)) | ||
| { | ||
| while (reader.Read()) | ||
| { | ||
| char[] buffer = new char[4096]; | ||
| int charsRead = 0; | ||
|
|
||
| using (TextReader data = reader.GetTextReader(0)) | ||
| { | ||
| do | ||
| { | ||
| charsRead = data.Read(buffer, 0, buffer.Length); | ||
| sw.Write(buffer, 0, charsRead); | ||
|
|
||
| } while (charsRead > 0); | ||
| } | ||
| _output.WriteLine("Output written to " + _outputFile); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task PrintJsonDataToFileAsync(SqlConnection connection) | ||
| { | ||
| DeleteFile(_outputFile); | ||
| using (SqlCommand command = new SqlCommand("SELECT [data] FROM [jsonTab]", connection)) | ||
| { | ||
| using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess)) | ||
| { | ||
| using (StreamWriter sw = new StreamWriter(_outputFile)) | ||
| { | ||
| while (await reader.ReadAsync()) | ||
| { | ||
| char[] buffer = new char[4096]; | ||
| int charsRead = 0; | ||
|
|
||
| using (TextReader data = reader.GetTextReader(0)) | ||
| { | ||
| do | ||
| { | ||
| charsRead = await data.ReadAsync(buffer, 0, buffer.Length); | ||
| await sw.WriteAsync(buffer, 0, charsRead); | ||
|
|
||
| } while (charsRead > 0); | ||
| } | ||
| _output.WriteLine("Output written to file " + _outputFile); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void StreamJsonFileToServer(SqlConnection connection) | ||
| { | ||
| using (SqlCommand cmd = new SqlCommand("INSERT INTO [jsonTab] (data) VALUES (@jsondata)", connection)) | ||
| { | ||
| using (StreamReader jsonFile = File.OpenText(_jsonFile)) | ||
| { | ||
| cmd.Parameters.Add("@jsondata", Microsoft.Data.SqlDbTypeExtensions.Json, -1).Value = jsonFile; | ||
| cmd.ExecuteNonQuery(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task StreamJsonFileToServerAsync(SqlConnection connection) | ||
| { | ||
| using (SqlCommand cmd = new SqlCommand("INSERT INTO [jsonTab] (data) VALUES (@jsondata)", connection)) | ||
| { | ||
| using (StreamReader jsonFile = File.OpenText(_jsonFile)) | ||
| { | ||
| cmd.Parameters.Add("@jsondata", Microsoft.Data.SqlDbTypeExtensions.Json, -1).Value = jsonFile; | ||
| await cmd.ExecuteNonQueryAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void DeleteFile(string filename) | ||
| { | ||
| if (File.Exists(filename)) | ||
| { | ||
| File.Delete(filename); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))] | ||
| public void TestJsonStreaming() | ||
| { | ||
| GenerateJsonFile(10000, _jsonFile); | ||
|
|
||
| using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) | ||
| { | ||
| connection.Open(); | ||
| DataTestUtility.CreateTable(connection, "jsonTab", "(data json)"); | ||
| StreamJsonFileToServer(connection); | ||
| PrintJsonDataToFile(connection); | ||
| CompareJsonFiles(); | ||
| DeleteFile(_jsonFile); | ||
| DeleteFile(_outputFile); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))] | ||
| public async Task TestJsonStreamingAsync() | ||
| { | ||
| GenerateJsonFile(10000, _jsonFile); | ||
|
|
||
| using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString)) | ||
| { | ||
| await connection.OpenAsync(); | ||
| DataTestUtility.CreateTable(connection, "jsonTab", "(data json)"); | ||
| await StreamJsonFileToServerAsync(connection); | ||
| await PrintJsonDataToFileAsync(connection); | ||
| CompareJsonFiles(); | ||
| DeleteFile(_jsonFile); | ||
| DeleteFile(_outputFile); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.