Skip to content

Commit

Permalink
SNOW-76790 fetch RowsAffected earlier for dataReader so that this pro…
Browse files Browse the repository at this point in the history
…perty can be available even if the connection/reader is closed (#136)
  • Loading branch information
ChTimTsubasa authored and ankit-bhatnagar167 committed Jun 3, 2019
1 parent aa9833a commit ae1c36c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
32 changes: 32 additions & 0 deletions Snowflake.Data.Tests/SFDbDataReaderIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ class SFDbDataReaderIT : SFBaseTest
{
static private readonly Random rand = new Random();

[Test]
public void testRecordsAffected()
{
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = connectionString;
conn.Open();

IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "create or replace table testRecordsAffected(cola number)";
int count = cmd.ExecuteNonQuery();
Assert.AreEqual(0, count);

string insertCommand = "insert into testRecordsAffected values (1),(1),(1)";
cmd.CommandText = insertCommand;
IDataReader reader = cmd.ExecuteReader();
Assert.AreEqual(3, reader.RecordsAffected);

// Reader's RecordsAffected should be available even if the reader is closed
reader.Close();
Assert.AreEqual(3, reader.RecordsAffected);

cmd.CommandText = "drop table if exists testRecordsAffected";
count = cmd.ExecuteNonQuery();
Assert.AreEqual(0, count);

// Reader's RecordsAffected should be available even if the connection is closed
conn.Close();
Assert.AreEqual(3, reader.RecordsAffected);
}
}

[Test]
public void testGetNumber()
{
Expand Down
2 changes: 0 additions & 2 deletions Snowflake.Data/Client/SnowflakeDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public override int ExecuteNonQuery()
{
logger.Debug($"ExecuteNonQuery, command: {CommandText}");
SFBaseResultSet resultSet = ExecuteInternal();
resultSet.Next();
return resultSet.CalculateUpdateCount();
}

Expand All @@ -168,7 +167,6 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
throw new TaskCanceledException();

var resultSet = await ExecuteInternalAsync(cancellationToken);
await resultSet.NextAsync();
return resultSet.CalculateUpdateCount();
}

Expand Down
4 changes: 3 additions & 1 deletion Snowflake.Data/Client/SnowflakeDbDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ internal SnowflakeDbDataReader(SnowflakeDbCommand command, SFBaseResultSet resul
this.resultSet = resultSet;
this.isClosed = false;
this.SchemaTable = PopulateSchemaTable(resultSet);
RecordsAffected = resultSet.CalculateUpdateCount();
}

public override object this[string name]
{
get
Expand Down Expand Up @@ -82,7 +84,7 @@ public override bool IsClosed
}
}

public override int RecordsAffected => resultSet.CalculateUpdateCount();
public override int RecordsAffected { get; }

public override DataTable GetSchemaTable()
{
Expand Down
2 changes: 2 additions & 0 deletions Snowflake.Data/Core/ResultSetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ internal static int CalculateUpdateCount(this SFBaseResultSet resultSet)
case SFStatementType.DELETE:
case SFStatementType.MERGE:
case SFStatementType.MULTI_INSERT:
resultSet.Next();
for (int i = 0; i < resultSet.columnCount; i++)
{
updateCount += resultSet.GetValue<long>(i);
}

break;
case SFStatementType.COPY:
resultSet.Next();
var index = resultSet.sfResultSetMetaData.getColumnIndexByName("rows_loaded");
if (index >= 0) updateCount = resultSet.GetValue<long>(index);
break;
Expand Down
2 changes: 0 additions & 2 deletions Snowflake.Data/Core/SFResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ protected override string getObjectInternal(int columnIndex)
throw new SnowflakeDbException(SFError.COLUMN_INDEX_OUT_OF_BOUND, columnIndex);
}

//Logger.DebugFmt("RowIndex: {0}, ColumnIndex:{1}, CurrentChunkIndex: {2}, CurrentChunkRowCount: {3}",
// _currentChunkRowIdx, columnIndex, _currentChunk.GetChunkIndex(), _currentChunk.GetRowCount());
return _currentChunk.ExtractCell(_currentChunkRowIdx, columnIndex);
}

Expand Down

0 comments on commit ae1c36c

Please sign in to comment.