Include Internal Row ID with DbCellValue (#308)

Update to include the Row ID with a DbCellValue. This will be super useful to us for the purposes of managing individual rows via slick grid.

Although this changes the API for edit/subset and query/subset, it is effectively backwards compatible since it's adding a parameter, not removing anything.

* New DbCellValue has an internal Row ID

* Adding unit tests
This commit is contained in:
Benjamin Russell
2017-04-17 14:31:59 -07:00
committed by GitHub
parent 88eb0f699b
commit 96d46b5c09
5 changed files with 143 additions and 98 deletions

View File

@@ -27,6 +27,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts
/// </summary> /// </summary>
internal object RawObject { get; set; } internal object RawObject { get; set; }
/// <summary>
/// The internal ID for the row. Should be used when directly referencing the row for edit
/// or other purposes.
/// </summary>
public long RowId { get; set; }
/// <summary> /// <summary>
/// Copies the values of this DbCellValue into another DbCellValue (or child object) /// Copies the values of this DbCellValue into another DbCellValue (or child object)
/// </summary> /// </summary>
@@ -38,6 +44,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts
other.DisplayValue = DisplayValue; other.DisplayValue = DisplayValue;
other.IsNull = IsNull; other.IsNull = IsNull;
other.RawObject = RawObject; other.RawObject = RawObject;
other.RowId = RowId;
} }
} }
} }

View File

@@ -14,6 +14,6 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// </summary> /// </summary>
public interface IFileStreamReader : IDisposable public interface IFileStreamReader : IDisposable
{ {
IList<DbCellValue> ReadRow(long offset, IEnumerable<DbColumnWrapper> columns); IList<DbCellValue> ReadRow(long offset, long rowId, IEnumerable<DbColumnWrapper> columns);
} }
} }

View File

@@ -30,13 +30,15 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
#region Member Variables #region Member Variables
private delegate FileStreamReadResult ReadMethod(long fileOffset, long rowId, DbColumnWrapper column);
private byte[] buffer; private byte[] buffer;
private readonly QueryExecutionSettings executionSettings; private readonly QueryExecutionSettings executionSettings;
private readonly Stream fileStream; private readonly Stream fileStream;
private readonly Dictionary<Type, Func<long, DbColumnWrapper, FileStreamReadResult>> readMethods; private readonly Dictionary<Type, ReadMethod> readMethods;
#endregion #endregion
@@ -63,37 +65,37 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
buffer = new byte[DefaultBufferSize]; buffer = new byte[DefaultBufferSize];
// Create the methods that will be used to read back // Create the methods that will be used to read back
readMethods = new Dictionary<Type, Func<long, DbColumnWrapper, FileStreamReadResult>> readMethods = new Dictionary<Type, ReadMethod>
{ {
{typeof(string), (o, col) => ReadString(o)}, {typeof(string), (o, id, col) => ReadString(o, id)},
{typeof(short), (o, col) => ReadInt16(o)}, {typeof(short), (o, id, col) => ReadInt16(o, id)},
{typeof(int), (o, col) => ReadInt32(o)}, {typeof(int), (o, id, col) => ReadInt32(o, id)},
{typeof(long), (o, col) => ReadInt64(o)}, {typeof(long), (o, id, col) => ReadInt64(o, id)},
{typeof(byte), (o, col) => ReadByte(o)}, {typeof(byte), (o, id, col) => ReadByte(o, id)},
{typeof(char), (o, col) => ReadChar(o)}, {typeof(char), (o, id, col) => ReadChar(o, id)},
{typeof(bool), (o, col) => ReadBoolean(o)}, {typeof(bool), (o, id, col) => ReadBoolean(o, id)},
{typeof(double), (o, col) => ReadDouble(o)}, {typeof(double), (o, id, col) => ReadDouble(o, id)},
{typeof(float), (o, col) => ReadSingle(o)}, {typeof(float), (o, id, col) => ReadSingle(o, id)},
{typeof(decimal), (o, col) => ReadDecimal(o)}, {typeof(decimal), (o, id, col) => ReadDecimal(o, id)},
{typeof(DateTime), ReadDateTime}, {typeof(DateTime), ReadDateTime},
{typeof(DateTimeOffset), (o, col) => ReadDateTimeOffset(o)}, {typeof(DateTimeOffset), (o, id, col) => ReadDateTimeOffset(o, id)},
{typeof(TimeSpan), (o, col) => ReadTimeSpan(o)}, {typeof(TimeSpan), (o, id, col) => ReadTimeSpan(o, id)},
{typeof(byte[]), (o, col) => ReadBytes(o)}, {typeof(byte[]), (o, id, col) => ReadBytes(o, id)},
{typeof(SqlString), (o, col) => ReadString(o)}, {typeof(SqlString), (o, id, col) => ReadString(o, id)},
{typeof(SqlInt16), (o, col) => ReadInt16(o)}, {typeof(SqlInt16), (o, id, col) => ReadInt16(o, id)},
{typeof(SqlInt32), (o, col) => ReadInt32(o)}, {typeof(SqlInt32), (o, id, col) => ReadInt32(o, id)},
{typeof(SqlInt64), (o, col) => ReadInt64(o)}, {typeof(SqlInt64), (o, id, col) => ReadInt64(o, id)},
{typeof(SqlByte), (o, col) => ReadByte(o)}, {typeof(SqlByte), (o, id, col) => ReadByte(o, id)},
{typeof(SqlBoolean), (o, col) => ReadBoolean(o)}, {typeof(SqlBoolean), (o, id, col) => ReadBoolean(o, id)},
{typeof(SqlDouble), (o, col) => ReadDouble(o)}, {typeof(SqlDouble), (o, id, col) => ReadDouble(o, id)},
{typeof(SqlSingle), (o, col) => ReadSingle(o)}, {typeof(SqlSingle), (o, id, col) => ReadSingle(o, id)},
{typeof(SqlDecimal), (o, col) => ReadSqlDecimal(o)}, {typeof(SqlDecimal), (o, id, col) => ReadSqlDecimal(o, id)},
{typeof(SqlDateTime), ReadDateTime}, {typeof(SqlDateTime), ReadDateTime},
{typeof(SqlBytes), (o, col) => ReadBytes(o)}, {typeof(SqlBytes), (o, id, col) => ReadBytes(o, id)},
{typeof(SqlBinary), (o, col) => ReadBytes(o)}, {typeof(SqlBinary), (o, id, col) => ReadBytes(o, id)},
{typeof(SqlGuid), (o, col) => ReadGuid(o)}, {typeof(SqlGuid), (o, id, col) => ReadGuid(o, id)},
{typeof(SqlMoney), (o, col) => ReadMoney(o)}, {typeof(SqlMoney), (o, id, col) => ReadMoney(o, id)},
}; };
} }
@@ -103,9 +105,10 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a row from the file, based on the columns provided /// Reads a row from the file, based on the columns provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file where the row starts</param> /// <param name="fileOffset">Offset into the file where the row starts</param>
/// <param name="rowId">Internal ID of the row to set for all cells in this row</param>
/// <param name="columns">The columns that were encoded</param> /// <param name="columns">The columns that were encoded</param>
/// <returns>The objects from the row, ready for output to the client</returns> /// <returns>The objects from the row, ready for output to the client</returns>
public IList<DbCellValue> ReadRow(long fileOffset, IEnumerable<DbColumnWrapper> columns) public IList<DbCellValue> ReadRow(long fileOffset, long rowId, IEnumerable<DbColumnWrapper> columns)
{ {
// Initialize for the loop // Initialize for the loop
long currentFileOffset = fileOffset; long currentFileOffset = fileOffset;
@@ -119,7 +122,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
if (column.IsSqlVariant) if (column.IsSqlVariant)
{ {
// For SQL Variant columns, the type is written first in string format // For SQL Variant columns, the type is written first in string format
FileStreamReadResult sqlVariantTypeResult = ReadString(currentFileOffset); FileStreamReadResult sqlVariantTypeResult = ReadString(currentFileOffset, rowId);
currentFileOffset += sqlVariantTypeResult.TotalLength; currentFileOffset += sqlVariantTypeResult.TotalLength;
string sqlVariantType = (string)sqlVariantTypeResult.Value.RawObject; string sqlVariantType = (string)sqlVariantTypeResult.Value.RawObject;
@@ -146,13 +149,13 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
} }
// Use the right read function for the type to read the data from the file // Use the right read function for the type to read the data from the file
Func<long, DbColumnWrapper, FileStreamReadResult> readFunc; ReadMethod readFunc;
if(!readMethods.TryGetValue(colType, out readFunc)) if(!readMethods.TryGetValue(colType, out readFunc))
{ {
// Treat everything else as a string // Treat everything else as a string
readFunc = readMethods[typeof(string)]; readFunc = readMethods[typeof(string)];
} }
FileStreamReadResult result = readFunc(currentFileOffset, column); FileStreamReadResult result = readFunc(currentFileOffset, rowId, column);
currentFileOffset += result.TotalLength; currentFileOffset += result.TotalLength;
results.Add(result.Value); results.Add(result.Value);
} }
@@ -183,6 +186,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// <paramref name="convertFunc"/>. /// <paramref name="convertFunc"/>.
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read from</param> /// <param name="offset">Offset into the file to read from</param>
/// <param name="rowId">Internal ID of the row to set on all cells in this row</param>
/// <param name="convertFunc">Function to use to convert the buffer to the target type</param> /// <param name="convertFunc">Function to use to convert the buffer to the target type</param>
/// <param name="isNullFunc"> /// <param name="isNullFunc">
/// If provided, this function will be used to determine if the value is null /// If provided, this function will be used to determine if the value is null
@@ -190,10 +194,13 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// <param name="toStringFunc">Optional function to use to convert the object to a string.</param> /// <param name="toStringFunc">Optional function to use to convert the object to a string.</param>
/// <typeparam name="T">The expected type of the cell. Used to keep the code honest</typeparam> /// <typeparam name="T">The expected type of the cell. Used to keep the code honest</typeparam>
/// <returns>The object, a display value, and the length of the value + its length</returns> /// <returns>The object, a display value, and the length of the value + its length</returns>
private FileStreamReadResult ReadCellHelper<T>(long offset, Func<int, T> convertFunc, Func<int, bool> isNullFunc = null, Func<T, string> toStringFunc = null) private FileStreamReadResult ReadCellHelper<T>(long offset, long rowId,
Func<int, T> convertFunc,
Func<int, bool> isNullFunc = null,
Func<T, string> toStringFunc = null)
{ {
LengthResult length = ReadLength(offset); LengthResult length = ReadLength(offset);
DbCellValue result = new DbCellValue(); DbCellValue result = new DbCellValue {RowId = rowId};
if (isNullFunc == null ? length.ValueLength == 0 : isNullFunc(length.TotalLength)) if (isNullFunc == null ? length.ValueLength == 0 : isNullFunc(length.TotalLength))
{ {
@@ -218,61 +225,67 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a short from the file at the offset provided /// Reads a short from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the short from</param> /// <param name="fileOffset">Offset into the file to read the short from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A short</returns> /// <returns>A short</returns>
internal FileStreamReadResult ReadInt16(long fileOffset) internal FileStreamReadResult ReadInt16(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToInt16(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToInt16(buffer, 0));
} }
/// <summary> /// <summary>
/// Reads a int from the file at the offset provided /// Reads a int from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the int from</param> /// <param name="fileOffset">Offset into the file to read the int from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>An int</returns> /// <returns>An int</returns>
internal FileStreamReadResult ReadInt32(long fileOffset) internal FileStreamReadResult ReadInt32(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToInt32(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToInt32(buffer, 0));
} }
/// <summary> /// <summary>
/// Reads a long from the file at the offset provided /// Reads a long from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the long from</param> /// <param name="fileOffset">Offset into the file to read the long from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A long</returns> /// <returns>A long</returns>
internal FileStreamReadResult ReadInt64(long fileOffset) internal FileStreamReadResult ReadInt64(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToInt64(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToInt64(buffer, 0));
} }
/// <summary> /// <summary>
/// Reads a byte from the file at the offset provided /// Reads a byte from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the byte from</param> /// <param name="fileOffset">Offset into the file to read the byte from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A byte</returns> /// <returns>A byte</returns>
internal FileStreamReadResult ReadByte(long fileOffset) internal FileStreamReadResult ReadByte(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => buffer[0]); return ReadCellHelper(fileOffset, rowId, length => buffer[0]);
} }
/// <summary> /// <summary>
/// Reads a char from the file at the offset provided /// Reads a char from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the char from</param> /// <param name="fileOffset">Offset into the file to read the char from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A char</returns> /// <returns>A char</returns>
internal FileStreamReadResult ReadChar(long fileOffset) internal FileStreamReadResult ReadChar(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToChar(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToChar(buffer, 0));
} }
/// <summary> /// <summary>
/// Reads a bool from the file at the offset provided /// Reads a bool from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the bool from</param> /// <param name="fileOffset">Offset into the file to read the bool from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A bool</returns> /// <returns>A bool</returns>
internal FileStreamReadResult ReadBoolean(long fileOffset) internal FileStreamReadResult ReadBoolean(long fileOffset, long rowId)
{ {
// Override the stringifier with numeric values if the user prefers that // Override the stringifier with numeric values if the user prefers that
return ReadCellHelper(fileOffset, length => buffer[0] == 0x1, return ReadCellHelper(fileOffset, rowId, length => buffer[0] == 0x1,
toStringFunc: val => executionSettings.DisplayBitAsNumber toStringFunc: val => executionSettings.DisplayBitAsNumber
? val ? "1" : "0" ? val ? "1" : "0"
: val.ToString()); : val.ToString());
@@ -282,20 +295,22 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a single from the file at the offset provided /// Reads a single from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the single from</param> /// <param name="fileOffset">Offset into the file to read the single from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A single</returns> /// <returns>A single</returns>
internal FileStreamReadResult ReadSingle(long fileOffset) internal FileStreamReadResult ReadSingle(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToSingle(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToSingle(buffer, 0));
} }
/// <summary> /// <summary>
/// Reads a double from the file at the offset provided /// Reads a double from the file at the offset provided
/// </summary> /// </summary>
/// <param name="fileOffset">Offset into the file to read the double from</param> /// <param name="fileOffset">Offset into the file to read the double from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A double</returns> /// <returns>A double</returns>
internal FileStreamReadResult ReadDouble(long fileOffset) internal FileStreamReadResult ReadDouble(long fileOffset, long rowId)
{ {
return ReadCellHelper(fileOffset, length => BitConverter.ToDouble(buffer, 0)); return ReadCellHelper(fileOffset, rowId, length => BitConverter.ToDouble(buffer, 0));
} }
/// <summary> /// <summary>
@@ -303,9 +318,9 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the SqlDecimal from</param> /// <param name="offset">Offset into the file to read the SqlDecimal from</param>
/// <returns>A SqlDecimal</returns> /// <returns>A SqlDecimal</returns>
internal FileStreamReadResult ReadSqlDecimal(long offset) internal FileStreamReadResult ReadSqlDecimal(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
int[] arrInt32 = new int[(length - 3) / 4]; int[] arrInt32 = new int[(length - 3) / 4];
Buffer.BlockCopy(buffer, 3, arrInt32, 0, length - 3); Buffer.BlockCopy(buffer, 3, arrInt32, 0, length - 3);
@@ -318,9 +333,9 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the decimal from</param> /// <param name="offset">Offset into the file to read the decimal from</param>
/// <returns>A decimal</returns> /// <returns>A decimal</returns>
internal FileStreamReadResult ReadDecimal(long offset) internal FileStreamReadResult ReadDecimal(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
int[] arrInt32 = new int[length / 4]; int[] arrInt32 = new int[length / 4];
Buffer.BlockCopy(buffer, 0, arrInt32, 0, length); Buffer.BlockCopy(buffer, 0, arrInt32, 0, length);
@@ -332,11 +347,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a DateTime from the file at the offset provided /// Reads a DateTime from the file at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the DateTime from</param> /// <param name="offset">Offset into the file to read the DateTime from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <param name="col">Column metadata, used for determining what precision to output</param> /// <param name="col">Column metadata, used for determining what precision to output</param>
/// <returns>A DateTime</returns> /// <returns>A DateTime</returns>
internal FileStreamReadResult ReadDateTime(long offset, DbColumnWrapper col) internal FileStreamReadResult ReadDateTime(long offset, long rowId, DbColumnWrapper col)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
long ticks = BitConverter.ToInt64(buffer, 0); long ticks = BitConverter.ToInt64(buffer, 0);
return new DateTime(ticks); return new DateTime(ticks);
@@ -379,12 +395,13 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a DateTimeOffset from the file at the offset provided /// Reads a DateTimeOffset from the file at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the DateTimeOffset from</param> /// <param name="offset">Offset into the file to read the DateTimeOffset from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A DateTimeOffset</returns> /// <returns>A DateTimeOffset</returns>
internal FileStreamReadResult ReadDateTimeOffset(long offset) internal FileStreamReadResult ReadDateTimeOffset(long offset, long rowId)
{ {
// DateTimeOffset is represented by DateTime.Ticks followed by TimeSpan.Ticks // DateTimeOffset is represented by DateTime.Ticks followed by TimeSpan.Ticks
// both as Int64 values // both as Int64 values
return ReadCellHelper(offset, length => { return ReadCellHelper(offset, rowId, length => {
long dtTicks = BitConverter.ToInt64(buffer, 0); long dtTicks = BitConverter.ToInt64(buffer, 0);
long dtOffset = BitConverter.ToInt64(buffer, 8); long dtOffset = BitConverter.ToInt64(buffer, 8);
return new DateTimeOffset(new DateTime(dtTicks), new TimeSpan(dtOffset)); return new DateTimeOffset(new DateTime(dtTicks), new TimeSpan(dtOffset));
@@ -395,10 +412,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a TimeSpan from the file at the offset provided /// Reads a TimeSpan from the file at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the TimeSpan from</param> /// <param name="offset">Offset into the file to read the TimeSpan from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A TimeSpan</returns> /// <returns>A TimeSpan</returns>
internal FileStreamReadResult ReadTimeSpan(long offset) internal FileStreamReadResult ReadTimeSpan(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
long ticks = BitConverter.ToInt64(buffer, 0); long ticks = BitConverter.ToInt64(buffer, 0);
return new TimeSpan(ticks); return new TimeSpan(ticks);
@@ -409,10 +427,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads a string from the file at the offset provided /// Reads a string from the file at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the string from</param> /// <param name="offset">Offset into the file to read the string from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A string</returns> /// <returns>A string</returns>
internal FileStreamReadResult ReadString(long offset) internal FileStreamReadResult ReadString(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
length > 0 length > 0
? Encoding.Unicode.GetString(buffer, 0, length) ? Encoding.Unicode.GetString(buffer, 0, length)
: string.Empty, totalLength => totalLength == 1); : string.Empty, totalLength => totalLength == 1);
@@ -422,10 +441,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads bytes from the file at the offset provided /// Reads bytes from the file at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the bytes from</param> /// <param name="offset">Offset into the file to read the bytes from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A byte array</returns> /// <returns>A byte array</returns>
internal FileStreamReadResult ReadBytes(long offset) internal FileStreamReadResult ReadBytes(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
byte[] output = new byte[length]; byte[] output = new byte[length];
Buffer.BlockCopy(buffer, 0, output, 0, length); Buffer.BlockCopy(buffer, 0, output, 0, length);
@@ -446,10 +466,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// Reads the bytes that make up a GUID at the offset provided /// Reads the bytes that make up a GUID at the offset provided
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the bytes from</param> /// <param name="offset">Offset into the file to read the bytes from</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A guid type object</returns> /// <returns>A guid type object</returns>
internal FileStreamReadResult ReadGuid(long offset) internal FileStreamReadResult ReadGuid(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
byte[] output = new byte[length]; byte[] output = new byte[length];
Buffer.BlockCopy(buffer, 0, output, 0, length); Buffer.BlockCopy(buffer, 0, output, 0, length);
@@ -462,10 +483,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
/// into a /// into a
/// </summary> /// </summary>
/// <param name="offset">Offset into the file to read the value</param> /// <param name="offset">Offset into the file to read the value</param>
/// <param name="rowId">Internal ID of the row that will be stored in the cell</param>
/// <returns>A sql money type object</returns> /// <returns>A sql money type object</returns>
internal FileStreamReadResult ReadMoney(long offset) internal FileStreamReadResult ReadMoney(long offset, long rowId)
{ {
return ReadCellHelper(offset, length => return ReadCellHelper(offset, rowId, length =>
{ {
int[] arrInt32 = new int[length / 4]; int[] arrInt32 = new int[length / 4];
Buffer.BlockCopy(buffer, 0, arrInt32, 0, length); Buffer.BlockCopy(buffer, 0, arrInt32, 0, length);

View File

@@ -214,7 +214,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
using (IFileStreamReader fileStreamReader = fileStreamFactory.GetReader(outputFileName)) using (IFileStreamReader fileStreamReader = fileStreamFactory.GetReader(outputFileName))
{ {
return fileStreamReader.ReadRow(fileOffsets[rowId], Columns); return fileStreamReader.ReadRow(fileOffsets[rowId], rowId, Columns);
} }
} }
@@ -255,13 +255,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
{ {
// Iterate over all the rows and process them into a list of string builders // Iterate over all the rows and process them into a list of string builders
// ReSharper disable once AccessToDisposedClosure The lambda is used immediately in string.Join call // ReSharper disable once AccessToDisposedClosure The lambda is used immediately in string.Join call
IEnumerable<string> rowValues = fileOffsets.Select(rowOffset => fileStreamReader.ReadRow(rowOffset, Columns)[0].DisplayValue); IEnumerable<string> rowValues = fileOffsets.Select(rowOffset => fileStreamReader.ReadRow(rowOffset, 0, Columns)[0].DisplayValue);
string singleString = string.Join(string.Empty, rowValues); string singleString = string.Join(string.Empty, rowValues);
DbCellValue cellValue = new DbCellValue DbCellValue cellValue = new DbCellValue
{ {
DisplayValue = singleString, DisplayValue = singleString,
IsNull = false, IsNull = false,
RawObject = singleString RawObject = singleString,
RowId = 0
}; };
rows = new[] { new[] { cellValue } }; rows = new[] { new[] { cellValue } };
} }
@@ -272,7 +273,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
// Iterate over the rows we need and process them into output // Iterate over the rows we need and process them into output
// ReSharper disable once AccessToDisposedClosure The lambda is used immediately in .ToArray call // ReSharper disable once AccessToDisposedClosure The lambda is used immediately in .ToArray call
rows = rowOffsets.Select(rowOffset => fileStreamReader.ReadRow(rowOffset, Columns).ToArray()).ToArray(); rows = rowOffsets.Select((offset, id) => fileStreamReader.ReadRow(offset, id, Columns).ToArray()).ToArray();
} }
} }
// Retrieve the subset of the results as per the request // Retrieve the subset of the results as per the request
@@ -313,7 +314,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
using (IFileStreamReader fileStreamReader = fileStreamFactory.GetReader(outputFileName)) using (IFileStreamReader fileStreamReader = fileStreamFactory.GetReader(outputFileName))
{ {
// Determine the format and get the first col/row of XML // Determine the format and get the first col/row of XML
content = fileStreamReader.ReadRow(0, Columns)[0].DisplayValue; content = fileStreamReader.ReadRow(0, 0, Columns)[0].DisplayValue;
if (specialAction.ExpectYukonXMLShowPlan) if (specialAction.ExpectYukonXMLShowPlan)
{ {
@@ -482,7 +483,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
// Iterate over the rows that are in the selected row set // Iterate over the rows that are in the selected row set
for (long i = rowStartIndex; i < rowEndIndex; ++i) for (long i = rowStartIndex; i < rowEndIndex; ++i)
{ {
var row = fileReader.ReadRow(fileOffsets[i], Columns); var row = fileReader.ReadRow(fileOffsets[i], i, Columns);
fileWriter.WriteRow(row, Columns); fileWriter.WriteRow(row, Columns);
} }
if (successHandler != null) if (successHandler != null)

View File

@@ -116,12 +116,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[SuppressMessage("ReSharper", "UnusedParameter.Local")] [SuppressMessage("ReSharper", "UnusedParameter.Local")]
private static string VerifyReadWrite<T>(int valueLength, T value, private static string VerifyReadWrite<T>(int valueLength, T value,
Func<ServiceBufferFileStreamWriter, T, int> writeFunc, Func<ServiceBufferFileStreamWriter, T, int> writeFunc,
Func<ServiceBufferFileStreamReader, FileStreamReadResult> readFunc, Func<ServiceBufferFileStreamReader, long, FileStreamReadResult> readFunc,
QueryExecutionSettings overrideSettings = null) QueryExecutionSettings overrideSettings = null)
{ {
// Setup: Create a mock file stream // Setup: Create a mock file stream
byte[] storage = new byte[8192]; byte[] storage = new byte[8192];
overrideSettings = overrideSettings ?? new QueryExecutionSettings(); overrideSettings = overrideSettings ?? new QueryExecutionSettings();
const long rowId = 100;
// If: // If:
// ... I write a type T to the writer // ... I write a type T to the writer
@@ -135,7 +136,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
FileStreamReadResult outValue; FileStreamReadResult outValue;
using (ServiceBufferFileStreamReader reader = new ServiceBufferFileStreamReader(new MemoryStream(storage), overrideSettings)) using (ServiceBufferFileStreamReader reader = new ServiceBufferFileStreamReader(new MemoryStream(storage), overrideSettings))
{ {
outValue = readFunc(reader); outValue = readFunc(reader, rowId);
} }
// Then: // Then:
@@ -143,6 +144,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
Assert.Equal(valueLength, outValue.TotalLength); Assert.Equal(valueLength, outValue.TotalLength);
Assert.NotNull(outValue.Value); Assert.NotNull(outValue.Value);
// ... The id we set should be stored in the returned db cell value
Assert.Equal(rowId, outValue.Value.RowId);
return outValue.Value.DisplayValue; return outValue.Value.DisplayValue;
} }
@@ -154,7 +158,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(short.MinValue)] // Negative two byte number [InlineData(short.MinValue)] // Negative two byte number
public void Int16(short value) public void Int16(short value)
{ {
VerifyReadWrite(sizeof(short) + 1, value, (writer, val) => writer.WriteInt16(val), reader => reader.ReadInt16(0)); VerifyReadWrite(sizeof(short) + 1, value, (writer, val) => writer.WriteInt16(val), (reader, rowId) => reader.ReadInt16(0, rowId));
} }
[Theory] [Theory]
@@ -167,7 +171,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(int.MinValue)] // Negative four byte number [InlineData(int.MinValue)] // Negative four byte number
public void Int32(int value) public void Int32(int value)
{ {
VerifyReadWrite(sizeof(int) + 1, value, (writer, val) => writer.WriteInt32(val), reader => reader.ReadInt32(0)); VerifyReadWrite(sizeof(int) + 1, value, (writer, val) => writer.WriteInt32(val), (reader, rowId) => reader.ReadInt32(0, rowId));
} }
[Theory] [Theory]
@@ -182,7 +186,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(long.MinValue)] // Negative eight byte number [InlineData(long.MinValue)] // Negative eight byte number
public void Int64(long value) public void Int64(long value)
{ {
VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteInt64(val), reader => reader.ReadInt64(0)); VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteInt64(val), (reader, rowId) => reader.ReadInt64(0, rowId));
} }
[Theory] [Theory]
@@ -190,7 +194,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(10)] [InlineData(10)]
public void Byte(byte value) public void Byte(byte value)
{ {
VerifyReadWrite(sizeof(byte) + 1, value, (writer, val) => writer.WriteByte(val), reader => reader.ReadByte(0)); VerifyReadWrite(sizeof(byte) + 1, value, (writer, val) => writer.WriteByte(val), (reader, rowId) => reader.ReadByte(0, rowId));
} }
[Theory] [Theory]
@@ -199,7 +203,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData((char)0x9152)] // Test something in the UTF-16 space [InlineData((char)0x9152)] // Test something in the UTF-16 space
public void Char(char value) public void Char(char value)
{ {
VerifyReadWrite(sizeof(char) + 1, value, (writer, val) => writer.WriteChar(val), reader => reader.ReadChar(0)); VerifyReadWrite(sizeof(char) + 1, value, (writer, val) => writer.WriteChar(val), (reader, rowId) => reader.ReadChar(0, rowId));
} }
[Theory] [Theory]
@@ -211,7 +215,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
{ {
string displayValue = VerifyReadWrite(sizeof(bool) + 1, value, string displayValue = VerifyReadWrite(sizeof(bool) + 1, value,
(writer, val) => writer.WriteBoolean(val), (writer, val) => writer.WriteBoolean(val),
reader => reader.ReadBoolean(0), (reader, rowId) => reader.ReadBoolean(0, rowId),
new QueryExecutionSettings {DisplayBitAsNumber = preferNumeric} new QueryExecutionSettings {DisplayBitAsNumber = preferNumeric}
); );
@@ -238,7 +242,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(float.NegativeInfinity)] [InlineData(float.NegativeInfinity)]
public void Single(float value) public void Single(float value)
{ {
VerifyReadWrite(sizeof(float) + 1, value, (writer, val) => writer.WriteSingle(val), reader => reader.ReadSingle(0)); VerifyReadWrite(sizeof(float) + 1, value, (writer, val) => writer.WriteSingle(val), (reader, rowId) => reader.ReadSingle(0, rowId));
} }
[Theory] [Theory]
@@ -255,7 +259,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
[InlineData(double.MaxValue)] [InlineData(double.MaxValue)]
public void Double(double value) public void Double(double value)
{ {
VerifyReadWrite(sizeof(double) + 1, value, (writer, val) => writer.WriteDouble(val), reader => reader.ReadDouble(0)); VerifyReadWrite(sizeof(double) + 1, value, (writer, val) => writer.WriteDouble(val), (reader, rowId) => reader.ReadDouble(0, rowId));
} }
[Fact] [Fact]
@@ -270,7 +274,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (SqlDecimal value in testValues) foreach (SqlDecimal value in testValues)
{ {
int valueLength = 4 + value.BinData.Length; int valueLength = 4 + value.BinData.Length;
VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteSqlDecimal(val), reader => reader.ReadSqlDecimal(0)); VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteSqlDecimal(val), (reader, rowId) => reader.ReadSqlDecimal(0, rowId));
} }
} }
@@ -287,7 +291,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (decimal value in testValues) foreach (decimal value in testValues)
{ {
int valueLength = decimal.GetBits(value).Length*4 + 1; int valueLength = decimal.GetBits(value).Length*4 + 1;
VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteDecimal(val), reader => reader.ReadDecimal(0)); VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteDecimal(val), (reader, rowId) => reader.ReadDecimal(0, rowId));
} }
} }
@@ -306,7 +310,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (DateTime value in testValues) foreach (DateTime value in testValues)
{ {
string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val), reader => reader.ReadDateTime(0, col)); string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val),
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value does not have a time string // Make sure the display value does not have a time string
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2}$")); Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2}$"));
@@ -328,7 +333,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (DateTime value in testValues) foreach (DateTime value in testValues)
{ {
string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val), reader => reader.ReadDateTime(0, col)); string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val),
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 3 milliseconds // Make sure the display value has a time string with 3 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{3}$")); Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{3}$"));
@@ -361,7 +367,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (DateTime value in testValues) foreach (DateTime value in testValues)
{ {
string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val), reader => reader.ReadDateTime(0, col)); string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val),
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with variable number of milliseconds // Make sure the display value has a time string with variable number of milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}")); Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}"));
@@ -387,7 +394,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (DateTime value in testValues) foreach (DateTime value in testValues)
{ {
string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val), reader => reader.ReadDateTime(0, col)); string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val),
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 0 milliseconds // Make sure the display value has a time string with 0 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}$")); Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}$"));
@@ -409,7 +417,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
foreach (DateTime value in testValues) foreach (DateTime value in testValues)
{ {
string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val), reader => reader.ReadDateTime(0, col)); string displayValue = VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteDateTime(val),
(reader, rowId) => reader.ReadDateTime(0, rowId, col));
// Make sure the display value has a time string with 7 milliseconds // Make sure the display value has a time string with 7 milliseconds
Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{7}$")); Assert.True(Regex.IsMatch(displayValue, @"^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}\.[\d]{7}$"));
@@ -428,7 +437,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
}; };
foreach (DateTimeOffset value in testValues) foreach (DateTimeOffset value in testValues)
{ {
VerifyReadWrite(sizeof(long)*2 + 1, value, (writer, val) => writer.WriteDateTimeOffset(val), reader => reader.ReadDateTimeOffset(0)); VerifyReadWrite(sizeof(long)*2 + 1, value, (writer, val) => writer.WriteDateTimeOffset(val),
(reader, rowId) => reader.ReadDateTimeOffset(0, rowId));
} }
} }
@@ -443,7 +453,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
}; };
foreach (TimeSpan value in testValues) foreach (TimeSpan value in testValues)
{ {
VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteTimeSpan(val), reader => reader.ReadTimeSpan(0)); VerifyReadWrite(sizeof(long) + 1, value, (writer, val) => writer.WriteTimeSpan(val),
(reader, rowId) => reader.ReadTimeSpan(0, rowId));
} }
} }
@@ -481,7 +492,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
} }
string value = sb.ToString(); string value = sb.ToString();
int lengthLength = length == 0 || length > 255 ? 5 : 1; int lengthLength = length == 0 || length > 255 ? 5 : 1;
VerifyReadWrite(sizeof(char)*length + lengthLength, value, (writer, val) => writer.WriteString(value), reader => reader.ReadString(0)); VerifyReadWrite(sizeof(char)*length + lengthLength, value, (writer, val) => writer.WriteString(value),
(reader, rowId) => reader.ReadString(0, rowId));
} }
[Fact] [Fact]
@@ -519,7 +531,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
byte[] value = sb.ToArray(); byte[] value = sb.ToArray();
int lengthLength = length == 0 || length > 255 ? 5 : 1; int lengthLength = length == 0 || length > 255 ? 5 : 1;
int valueLength = sizeof(byte)*length + lengthLength; int valueLength = sizeof(byte)*length + lengthLength;
VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteBytes(value), reader => reader.ReadBytes(0)); VerifyReadWrite(valueLength, value, (writer, val) => writer.WriteBytes(value),
(reader, rowId) => reader.ReadBytes(0, rowId));
} }
[Fact] [Fact]
@@ -534,7 +547,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
}; };
foreach (Guid guid in guids) foreach (Guid guid in guids)
{ {
VerifyReadWrite(guid.ToByteArray().Length + 1, new SqlGuid(guid), (writer, val) => writer.WriteGuid(guid), reader => reader.ReadGuid(0)); VerifyReadWrite(guid.ToByteArray().Length + 1, new SqlGuid(guid), (writer, val) => writer.WriteGuid(guid),
(reader, rowId) => reader.ReadGuid(0, rowId));
} }
} }
@@ -549,7 +563,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
}; };
foreach (SqlMoney money in monies) foreach (SqlMoney money in monies)
{ {
VerifyReadWrite(sizeof(decimal) + 1, money, (writer, val) => writer.WriteMoney(money), reader => reader.ReadMoney(0)); VerifyReadWrite(sizeof(decimal) + 1, money, (writer, val) => writer.WriteMoney(money),
(reader, rowId) => reader.ReadMoney(0, rowId));
} }
} }
} }