Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,27 @@ protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpressio
{
switch (sqlUnaryExpression.OperatorType)
{
case ExpressionType.Convert:
if (sqlUnaryExpression.Operand.Type == typeof(char)
&& sqlUnaryExpression.Type.IsInteger())
{
Sql.Append("unicode(");
Visit(sqlUnaryExpression.Operand);
Sql.Append(")");

return sqlUnaryExpression;
}
else if (sqlUnaryExpression.Operand.Type.IsInteger()
&& sqlUnaryExpression.Type == typeof(char))
{
Sql.Append("char(");
Visit(sqlUnaryExpression.Operand);
Sql.Append(")");

return sqlUnaryExpression;
}
goto default;

case ExpressionType.Not when sqlUnaryExpression.Type == typeof(bool):
switch (sqlUnaryExpression.Operand)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Sqlite/EFCore.Sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Microsoft.Data.Sqlite.SqliteTransaction</Description>
</ItemGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.6" />
</ItemGroup>

<ItemGroup>
Expand Down
106 changes: 70 additions & 36 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;
using static SQLitePCL.raw;
Expand Down Expand Up @@ -52,38 +54,50 @@ static SqliteConnection()
?.GetRuntimeMethod("Init", Type.EmptyTypes)
?.Invoke(null, null);

var appDataType = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET");

var storageFolderType = Type.GetType("Windows.Storage.StorageFolder, Windows, ContentType=WindowsRuntime")
?? Type.GetType("Windows.Storage.StorageFolder, Microsoft.Windows.SDK.NET");

object? currentAppData = null;
try
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
currentAppData = appDataType?.GetRuntimeProperty("Current")?.GetValue(null);
}
catch (TargetInvocationException)
{
// Ignore "The process has no package identity."
}
Type? appDataType = null;
Type? storageFolderType = null;
try
{
appDataType = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET");

if (currentAppData != null)
{
var localFolder = appDataType?.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData);
var localFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(localFolder);
if (localFolderPath != null)
storageFolderType = Type.GetType("Windows.Storage.StorageFolder, Windows, ContentType=WindowsRuntime")
?? Type.GetType("Windows.Storage.StorageFolder, Microsoft.Windows.SDK.NET");
}
catch (FileLoadException)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_DATA_DIRECTORY_TYPE, localFolderPath);
Debug.Assert(rc == SQLITE_OK);
// Ignore "Could not load assembly."
}

var tempFolder = appDataType?.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData);
var tempFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(tempFolder);
if (tempFolderPath != null)
object? currentAppData = null;
try
{
currentAppData = appDataType?.GetRuntimeProperty("Current")?.GetValue(null);
}
catch (TargetInvocationException)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_TEMP_DIRECTORY_TYPE, tempFolderPath);
Debug.Assert(rc == SQLITE_OK);
// Ignore "The process has no package identity."
}

if (currentAppData != null)
{
var localFolder = appDataType?.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData);
var localFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(localFolder);
if (localFolderPath != null)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_DATA_DIRECTORY_TYPE, localFolderPath);
Debug.Assert(rc == SQLITE_OK);
}

var tempFolder = appDataType?.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData);
var tempFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(tempFolder);
if (tempFolderPath != null)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_TEMP_DIRECTORY_TYPE, tempFolderPath);
Debug.Assert(rc == SQLITE_OK);
}
}
}
}
Expand Down Expand Up @@ -821,22 +835,24 @@ private void CreateAggregateCore<TAccumulate, TResult>(
delegate_function_aggregate_step? func_step = null;
if (func != null)
{
func_step = (ctx, user_data, args) =>
func_step = static (ctx, user_data, args) =>
{
var context = (AggregateContext<TAccumulate>)user_data;
var definition = (AggregateDefinition<TAccumulate, TResult>)user_data;
ctx.state ??= new AggregateContext<TAccumulate>(definition.Seed);

var context = (AggregateContext<TAccumulate>)ctx.state;
if (context.Exception != null)
{
return;
}

// TODO: Avoid allocation when niladic
var reader = new SqliteParameterReader(name, args);
var reader = new SqliteParameterReader(definition.Name, args);

try
{
// TODO: Avoid closure by passing func via user_data
// NB: No need to set ctx.state since we just mutate the instance
context.Accumulate = func(context.Accumulate, reader);
context.Accumulate = definition.Func!(context.Accumulate, reader);
}
catch (Exception ex)
{
Expand All @@ -848,16 +864,18 @@ private void CreateAggregateCore<TAccumulate, TResult>(
delegate_function_aggregate_final? func_final = null;
if (resultSelector != null)
{
func_final = (ctx, user_data) =>
func_final = static (ctx, user_data) =>
{
var context = (AggregateContext<TAccumulate>)user_data;
var definition = (AggregateDefinition<TAccumulate, TResult>)user_data;
ctx.state ??= new AggregateContext<TAccumulate>(definition.Seed);

var context = (AggregateContext<TAccumulate>)ctx.state;

if (context.Exception == null)
{
try
{
// TODO: Avoid closure by passing resultSelector via user_data
var result = resultSelector(context.Accumulate);
var result = definition.ResultSelector!(context.Accumulate);

new SqliteResultBinder(ctx, result).Bind();
}
Expand All @@ -881,7 +899,7 @@ private void CreateAggregateCore<TAccumulate, TResult>(
}

var flags = isDeterministic ? SQLITE_DETERMINISTIC : 0;
var state = new AggregateContext<TAccumulate>(seed);
var state = new AggregateDefinition<TAccumulate, TResult>(name, seed, func, resultSelector);

if (State == ConnectionState.Open)
{
Expand Down Expand Up @@ -915,6 +933,22 @@ private void CreateAggregateCore<TAccumulate, TResult>(
return values;
}

private sealed class AggregateDefinition<TAccumulate, TResult>
{
public AggregateDefinition(string name, TAccumulate seed, Func<TAccumulate, SqliteValueReader, TAccumulate>? func, Func<TAccumulate, TResult>? resultSelector)
{
Name = name;
Seed = seed;
Func = func;
ResultSelector = resultSelector;
}

public string Name { get; }
public TAccumulate Seed { get; }
public Func<TAccumulate, SqliteValueReader, TAccumulate>? Func { get; }
public Func<TAccumulate, TResult>? ResultSelector { get; }
}

private sealed class AggregateContext<T>
{
public AggregateContext(T seed)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Data.Sqlite/Microsoft.Data.Sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Microsoft.Data.Sqlite.SqliteTransaction</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Design.Tests/EFCore.Design.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(MicrosoftExtensionsDependencyModelVersion)" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/EFCore.NativeAotTests/EFCore.NativeAotTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationJsonVersion)" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6-pre20230809203314" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" />
</ItemGroup>

</Project>
Loading