Skip to content
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

Adding a simple ready only query cache #233

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1,452 changes: 782 additions & 670 deletions SQLite.Net.sln

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions nuget/SQLite.Net.Async.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>SQLite.Net.Async-PCL</id>
<version>3.0.5</version>
<version>3.1.1</version>
<title>SQLite.Net.Async PCL</title>
<authors>Øystein Krog,Frank Krueger,Tim Heuer</authors>
<owners>Øystein Krog</owners>
Expand All @@ -17,7 +17,7 @@
<releaseNotes>https://github.com/oysteinkrog/SQLite.Net-PCL/commits</releaseNotes>
<tags>sqlite pcl sql database ios android windows metro winrt xamarin monotouch monodroid win32 windowsphone wp wp8 wp8.1</tags>
<dependencies>
<dependency id="SQLite.Net.Core-PCL" version="3.0.5"/>
<dependency id="SQLite.Net.Core-PCL" version="3.1.1"/>
</dependencies>
</metadata>
<files>
Expand Down
2 changes: 1 addition & 1 deletion nuget/SQLite.Net.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>SQLite.Net.Core-PCL</id>
<version>3.0.5</version>
<version>3.1.1</version>
<title>SQLite.Net PCL</title>
<authors>Øystein Krog,Frank Krueger,Tim Heuer</authors>
<owners>Øystein Krog</owners>
Expand Down
6 changes: 3 additions & 3 deletions nuget/SQLite.Net.nuspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>SQLite.Net-PCL</id>
<version>3.0.5</version>
<version>3.1.1</version>
<title>SQLite.Net PCL</title>
<authors>Øystein Krog,Frank Krueger,Tim Heuer</authors>
<owners>Øystein Krog</owners>
Expand All @@ -20,7 +20,7 @@
<dependency id="sqlite-net-wp8" version="3.8.5"/>
</group>
<group>
<dependency id="SQLite.Net.Core-PCL" version="3.0.5"/>
<dependency id="SQLite.Net.Core-PCL" version="3.1.1"/>
</group>
</dependencies>
</metadata>
Expand Down
6 changes: 3 additions & 3 deletions nuget/upload.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
..\.nuget\nuget push output\SQLite.Net.Core-PCL.3.0.5.nupkg
..\.nuget\nuget push output\SQLite.Net-PCL.3.0.5.nupkg
..\.nuget\nuget push output\SQLite.Net.Async-PCL.3.0.5.nupkg
..\.nuget\nuget push output\SQLite.Net.Core-PCL.3.1.1.nupkg
..\.nuget\nuget push output\SQLite.Net-PCL.3.1.1.nupkg
..\.nuget\nuget push output\SQLite.Net.Async-PCL.3.1.1.nupkg
4 changes: 2 additions & 2 deletions src/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("3.0.5.0")]
[assembly: AssemblyFileVersion("3.0.5.0")]
[assembly: AssemblyVersion("3.1.1.0")]
[assembly: AssemblyFileVersion("3.1.1.0")]
1 change: 1 addition & 0 deletions src/SQLite.Net.Async/SQLite.Net.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<DefineConstants>TRACE;DEBUG;JETBRAINS_ANNOTATIONS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion src/SQLite.Net.Async/SQLiteAsyncConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SQLiteAsyncConnection(
}

[PublicAPI]
protected SQLiteConnectionWithLock GetConnection()
public SQLiteConnectionWithLock GetConnection()
{
return _sqliteConnectionFunc();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/SQLite.Net/PreparedSqlLiteInsertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public void Dispose()
public int ExecuteNonQuery(object[] source)
{
Connection.TraceListener.WriteLine("Executing: {0}", CommandText);
if (Connection.ReadOnlyCaching)
{
var commandString = CommandText.ToLower();
ReadOnlyQueryCache.CheckIfCacheNeedsClearing(Connection, commandString);
}

if (!Initialized)
{
Expand Down
17 changes: 17 additions & 0 deletions src/SQLite.Net/ReadOnlyQueryCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;

namespace SQLite.Net
{
public class ReadOnlyQueryCache : Dictionary<string, string>
{
public static void CheckIfCacheNeedsClearing(SQLiteConnection connection, string commandString)
{
if (commandString.StartsWith("insert") || commandString.StartsWith("update") || commandString.StartsWith("delete") ||
commandString.StartsWith("create") || commandString.StartsWith("alter") || commandString.StartsWith("drop"))
{
connection.ReadOnlyCache.Clear();
}
}
}
}
2 changes: 2 additions & 0 deletions src/SQLite.Net/SQLite.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<DefineConstants>TRACE;DEBUG;JETBRAINS_ANNOTATIONS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -99,6 +100,7 @@
<Compile Include="Attributes\IndexedAttribute.cs" />
<Compile Include="Attributes\MaxLengthAttribute.cs" />
<Compile Include="Attributes\PrimaryKeyAttribute.cs" />
<Compile Include="ReadOnlyQueryCache.cs" />
<Compile Include="SQLiteException.cs" />
<Compile Include="SQLiteCommand.cs" />
<Compile Include="SQLiteConnection.cs" />
Expand Down
77 changes: 70 additions & 7 deletions src/SQLite.Net/SQLiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using JetBrains.Annotations;
using SQLite.Net.Interop;

Expand All @@ -38,6 +44,7 @@ public class SQLiteCommand

private readonly SQLiteConnection _conn;
private readonly ISQLitePlatform _sqlitePlatform;
private const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";

internal SQLiteCommand(ISQLitePlatform platformImplementation, SQLiteConnection conn)
{
Expand All @@ -54,6 +61,11 @@ internal SQLiteCommand(ISQLitePlatform platformImplementation, SQLiteConnection
public int ExecuteNonQuery()
{
_conn.TraceListener.WriteLine("Executing: {0}", this);
if (_conn.ReadOnlyCaching)
{
var commandString = this.ToString().ToLower();
ReadOnlyQueryCache.CheckIfCacheNeedsClearing(_conn, commandString);
}

var stmt = Prepare();
var r = _sqlitePlatform.SQLiteApi.Step(stmt);
Expand Down Expand Up @@ -87,12 +99,60 @@ public IEnumerable<T> ExecuteDeferredQuery<T>()
[PublicAPI]
public List<T> ExecuteQuery<T>()
{
//check cache first
if (_conn.ReadOnlyCaching)
{
var commandString = this.ToString().ToLower();
if (!commandString.StartsWith("pragma"))
{
if (_conn.ReadOnlyCache.ContainsKey(commandString))
{
var ser = new XmlSerializer(typeof(T[]));
var result = ((T[])ser.Deserialize(new StringReader(_conn.ReadOnlyCache[commandString])));
return result.ToList();
}
else
{
var result = ExecuteDeferredQuery<T>(_conn.GetMapping(typeof (T))).ToArray();
var ser = new XmlSerializer(typeof(T[]));
var sb = new StringBuilder();
ser.Serialize(new StringWriter(sb), result);
_conn.ReadOnlyCache.Add(commandString, sb.ToString());
return result.ToList();
}
}
}

return ExecuteDeferredQuery<T>(_conn.GetMapping(typeof (T))).ToList();
}

[PublicAPI]
public List<T> ExecuteQuery<T>(TableMapping map)
{
//check cache first
if (_conn.ReadOnlyCaching)
{
var commandString = this.ToString().ToLower();
if (!commandString.StartsWith("pragma"))
{
if (_conn.ReadOnlyCache.ContainsKey(commandString))
{
var ser = new XmlSerializer(typeof(T[]));
var result = ((T[])ser.Deserialize(new StringReader(_conn.ReadOnlyCache[commandString])));
return result.ToList();
}
else
{
var result = ExecuteDeferredQuery<T>(map).ToArray();
var ser = new XmlSerializer(typeof(T[]));
var sb = new StringBuilder();
ser.Serialize(new StringWriter(sb), result);
_conn.ReadOnlyCache.Add(commandString, sb.ToString());
return result.ToList();
}
}
}

return ExecuteDeferredQuery<T>(map).ToList();
}

Expand Down Expand Up @@ -343,11 +403,13 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in
{
if (storeDateTimeAsTicks)
{
isqLite3Api.BindInt64(stmt, index, ((DateTime) value).ToUniversalTime().Ticks);
long ticks = ((DateTime) value).ToUniversalTime().Ticks;
isqLite3Api.BindInt64(stmt, index, ticks);
}
else
{
isqLite3Api.BindText16(stmt, index, ((DateTime) value).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"), -1, NegativePointer);
string val = ((DateTime) value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture);
isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer);
}
}
else if (value is DateTimeOffset)
Expand All @@ -358,12 +420,13 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in
{
if (storeDateTimeAsTicks)
{
isqLite3Api.BindInt64(stmt, index, ((ISerializable<DateTime>) value).Serialize().ToUniversalTime().Ticks);
long ticks = ((ISerializable<DateTime>) value).Serialize().ToUniversalTime().Ticks;
isqLite3Api.BindInt64(stmt, index, ticks);
}
else
{
isqLite3Api.BindText16(stmt, index,
((ISerializable<DateTime>) value).Serialize().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"), -1, NegativePointer);
string val = ((ISerializable<DateTime>) value).Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture);
isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer);
}
}
else if (value.GetType().GetTypeInfo().IsEnum)
Expand Down Expand Up @@ -468,7 +531,7 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType)
{
return new DateTime(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index), DateTimeKind.Utc);
}
return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index));
return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture);
}
if (clrType == typeof (DateTimeOffset))
{
Expand All @@ -483,7 +546,7 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType)
}
else
{
value = DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index));
value = DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture);
}
return Activator.CreateInstance(clrType, value);
}
Expand Down
Loading