Skip to content
1 change: 1 addition & 0 deletions nuget/SQLite-net-base/SQLite-net-base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyName>SQLite-net</AssemblyName>
<Version>1.0.0</Version>
<AssemblyTitle>SQLite-net Official .NET Standard Base Library</AssemblyTitle>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<Description>Light weight library providing easy SQLite database storage</Description>
<Company>Krueger Systems, Inc.</Company>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions nuget/SQLite-net-std/SQLite-net-std.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AssemblyName>SQLite-net</AssemblyName>
<Version>1.0.0</Version>
<AssemblyTitle>SQLite-net Official .NET Standard Library</AssemblyTitle>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<Description>Light weight library providing easy SQLite database storage</Description>
<Company>Krueger Systems, Inc.</Company>
</PropertyGroup>
Expand Down
51 changes: 38 additions & 13 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public partial class SQLiteConnection : IDisposable
/// </summary>
public bool StoreDateTimeAsTicks { get; private set; }

/// <summary>
/// Whether to store Guid properties as strings (false) or blobs (true). The default is (false)
/// </summary>
public bool StoreGuidsAsBlobs { get; private set; }

#if USE_SQLITEPCL_RAW && !NO_SQLITEPCL_RAW_BATTERIES
static SQLiteConnection ()
{
Expand All @@ -228,8 +233,9 @@ static SQLiteConnection ()
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true)
: this (databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks)
/// <param name="storeGuidsAsBlobs"></param>
public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true, bool storeGuidsAsBlobs = false)
: this (databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, storeGuidsAsBlobs)
{
}

Expand All @@ -250,7 +256,8 @@ public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true)
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = true)
/// <param name="storeGuidsAsBlobs"></param>
public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = true, bool storeGuidsAsBlobs = false)
{
if (databasePath==null)
throw new ArgumentException ("Must be specified", nameof(databasePath));
Expand Down Expand Up @@ -282,6 +289,7 @@ public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool st
_open = true;

StoreDateTimeAsTicks = storeDateTimeAsTicks;
StoreGuidsAsBlobs = storeGuidsAsBlobs;

BusyTimeout = TimeSpan.FromSeconds (0.1);
if (openFlags.HasFlag (SQLiteOpenFlags.ReadWrite)) {
Expand Down Expand Up @@ -511,7 +519,7 @@ public CreateTableResult CreateTable (Type ty, CreateFlags createFlags = CreateF

// Build query.
var query = "create " + @virtual + "table if not exists \"" + map.TableName + "\" " + @using + "(\n";
var decls = map.Columns.Select (p => Orm.SqlDecl (p, StoreDateTimeAsTicks));
var decls = map.Columns.Select (p => Orm.SqlDecl (p, StoreDateTimeAsTicks, StoreGuidsAsBlobs));
var decl = string.Join (",\n", decls.ToArray ());
query += decl;
query += ")";
Expand Down Expand Up @@ -777,7 +785,7 @@ void MigrateTable (TableMapping map, List<ColumnInfo> existingCols)
}

foreach (var p in toBeAdded) {
var addCol = "alter table \"" + map.TableName + "\" add column " + Orm.SqlDecl (p, StoreDateTimeAsTicks);
var addCol = "alter table \"" + map.TableName + "\" add column " + Orm.SqlDecl (p, StoreDateTimeAsTicks, StoreGuidsAsBlobs);
Execute (addCol);
}
}
Expand Down Expand Up @@ -2021,6 +2029,7 @@ public class SQLiteConnectionString
public string ConnectionString { get; private set; }
public string DatabasePath { get; private set; }
public bool StoreDateTimeAsTicks { get; private set; }
public bool StoreGuidsAsBlobs { get; private set; }

#if NETFX_CORE
static readonly string MetroStyleDataPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Expand All @@ -2038,10 +2047,11 @@ public static bool IsInMemoryPath(string databasePath)

#endif

public SQLiteConnectionString (string databasePath, bool storeDateTimeAsTicks)
public SQLiteConnectionString (string databasePath, bool storeDateTimeAsTicks, bool storeGuidsAsBlobs)
{
ConnectionString = databasePath;
StoreDateTimeAsTicks = storeDateTimeAsTicks;
StoreGuidsAsBlobs = storeGuidsAsBlobs;

#if NETFX_CORE
DatabasePath = IsInMemoryPath(databasePath)
Expand Down Expand Up @@ -2432,9 +2442,9 @@ public static Type GetType (object obj)
return obj.GetType ();
}

public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks)
public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks, bool storeGuidsAsBlobs)
{
string decl = "\"" + p.Name + "\" " + SqlType (p, storeDateTimeAsTicks) + " ";
string decl = "\"" + p.Name + "\" " + SqlType (p, storeDateTimeAsTicks, storeGuidsAsBlobs) + " ";

if (p.IsPK) {
decl += "primary key ";
Expand All @@ -2452,7 +2462,7 @@ public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks)
return decl;
}

public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks)
public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks, bool storeGuidsAsBlobs)
{
var clrType = p.ColumnType;
if (clrType == typeof (Boolean) || clrType == typeof (Byte) || clrType == typeof (UInt16) || clrType == typeof (SByte) || clrType == typeof (Int16) || clrType == typeof (Int32) || clrType == typeof (UInt32) || clrType == typeof (Int64)) {
Expand Down Expand Up @@ -2488,6 +2498,9 @@ public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks)
return "blob";
}
else if (clrType == typeof (Guid)) {
if (storeGuidsAsBlobs)
return "blob";

return "varchar(36)";
}
else {
Expand Down Expand Up @@ -2758,15 +2771,17 @@ void BindAll (Sqlite3Statement stmt)
b.Index = nextIdx++;
}

BindParameter (stmt, b.Index, b.Value, _conn.StoreDateTimeAsTicks);
BindParameter (stmt, b.Index, b.Value, _conn.StoreDateTimeAsTicks, _conn.StoreGuidsAsBlobs);
}
}

internal static IntPtr NegativePointer = new IntPtr (-1);

const string DateTimeExactStoreFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff";

internal static void BindParameter (Sqlite3Statement stmt, int index, object value, bool storeDateTimeAsTicks)
internal static void BindParameter (Sqlite3Statement stmt, int index, object value,
bool storeDateTimeAsTicks,
bool connStoreGuidsAsBlobs)
{
if (value == null) {
SQLite3.BindNull (stmt, index);
Expand Down Expand Up @@ -2808,7 +2823,13 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
SQLite3.BindBlob (stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
}
else if (value is Guid) {
SQLite3.BindText (stmt, index, ((Guid)value).ToString (), 72, NegativePointer);
if (connStoreGuidsAsBlobs) {
var rawGuid = ((Guid)value).ToByteArray ();
SQLite3.BindBlob (stmt, index, rawGuid, rawGuid.Length, NegativePointer);
}
else {
SQLite3.BindText (stmt, index, ((Guid)value).ToString (), 72, NegativePointer);
}
}
else if (value is Uri) {
SQLite3.BindText (stmt, index, ((Uri)value).ToString (), -1, NegativePointer);
Expand Down Expand Up @@ -2920,6 +2941,10 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr
return SQLite3.ColumnByteArray (stmt, index);
}
else if (clrType == typeof (Guid)) {
if (_conn.StoreGuidsAsBlobs) {
var array = SQLite3.ColumnByteArray (stmt, index);
return new Guid (array);
}
var text = SQLite3.ColumnString (stmt, index);
return new Guid (text);
}
Expand Down Expand Up @@ -2982,7 +3007,7 @@ public int ExecuteNonQuery (object[] source)
//bind the values.
if (source != null) {
for (int i = 0; i < source.Length; i++) {
SQLiteCommand.BindParameter (Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
SQLiteCommand.BindParameter (Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks, Connection.StoreGuidsAsBlobs);
}
}
r = SQLite3.Step (Statement);
Expand Down
32 changes: 27 additions & 5 deletions src/SQLiteAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,27 @@ public partial class SQLiteAsyncConnection
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = true)
: this (databasePath, SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks)
: this (databasePath, storeDateTimeAsTicks, true)
{
}

/// <summary>
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database specified by databasePath.
/// </summary>
/// <param name="databasePath">
/// Specifies the path to the database file.
/// </param>
/// <param name="storeDateTimeAsTicks">
/// Specifies whether to store DateTime properties as ticks (true) or strings (false). You
/// absolutely do want to store them as Ticks in all new projects. The value of false is
/// only here for backwards compatibility. There is a *significant* speed advantage, with no
/// down sides, when setting storeDateTimeAsTicks = true.
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
/// <param name="storeGuidsAsBlobs"></param>
public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = true, bool storeGuidsAsBlobs = false)
: this (databasePath, SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, storeGuidsAsBlobs)
{
}

Expand All @@ -76,11 +96,12 @@ public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = t
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteAsyncConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = true)
/// <param name="storeGuidsAsBlobs"></param>
public SQLiteAsyncConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = true, bool storeGuidsAsBlobs = false)
{
_openFlags = openFlags;
isFullMutex = _openFlags.HasFlag (SQLiteOpenFlags.FullMutex);
_connectionString = new SQLiteConnectionString (databasePath, storeDateTimeAsTicks);
_connectionString = new SQLiteConnectionString (databasePath, storeDateTimeAsTicks, storeGuidsAsBlobs);
if(isFullMutex)
_fullMutexReadConnection = new SQLiteConnectionWithLock (_connectionString, openFlags) { SkipLock = true };
}
Expand Down Expand Up @@ -1377,7 +1398,8 @@ public void CloseConnection (SQLiteConnectionString connectionString, SQLiteOpen
}
}

entry.Close ();
if(entry != null)
entry.Close ();
}

/// <summary>
Expand Down Expand Up @@ -1411,7 +1433,7 @@ public class SQLiteConnectionWithLock : SQLiteConnection
/// <param name="connectionString">Connection string containing the DatabasePath.</param>
/// <param name="openFlags">Open flags.</param>
public SQLiteConnectionWithLock (SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags)
: base (connectionString.DatabasePath, openFlags, connectionString.StoreDateTimeAsTicks)
: base (connectionString.DatabasePath, openFlags, connectionString.StoreDateTimeAsTicks, connectionString.StoreGuidsAsBlobs)
{
}

Expand Down
7 changes: 3 additions & 4 deletions tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public void SetUp()
#else
_connectionString = Path.Combine (Path.GetTempPath (), DatabaseName);
_path = _connectionString;
GC.Collect ();
GC.WaitForPendingFinalizers ();
System.IO.File.Delete (_path);
#endif
}
Expand Down Expand Up @@ -797,8 +799,7 @@ public void TestAsyncTableElementAtAsync ()
// check...
Assert.AreEqual ("7", loaded.FirstName);
}



[Test]
public void TestAsyncGetWithExpression()
{
Expand Down Expand Up @@ -857,7 +858,5 @@ public void CloseAsync ()

conn.CloseAsync ().Wait ();
}


}
}
Loading