Skip to content

Commit

Permalink
Added Uri, UriBuilder and StringBuilder support
Browse files Browse the repository at this point in the history
  • Loading branch information
xperiandri committed Aug 14, 2017
1 parent c7837ce commit 49a42b1
Showing 1 changed file with 53 additions and 31 deletions.
84 changes: 53 additions & 31 deletions src/SQLite.cs
@@ -1,16 +1,16 @@
//
// Copyright (c) 2009-2017 Krueger Systems, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -33,6 +33,7 @@
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;

#if USE_CSHARP_SQLITE
Expand Down Expand Up @@ -384,12 +385,12 @@ static byte[] GetNullTerminatedUtf8 (string s)
/// </summary>
/// <param name="type">
/// The type whose mapping to the database is returned.
/// </param>
/// </param>
/// <param name="createFlags">
/// Optional flags allowing implicit PK and indexes based on naming conventions
/// </param>
/// </param>
/// <returns>
/// The mapping represents the schema of the columns of the database and contains
/// The mapping represents the schema of the columns of the database and contains
/// methods to set and get properties of objects.
/// </returns>
public TableMapping GetMapping (Type type, CreateFlags createFlags = CreateFlags.None)
Expand All @@ -416,9 +417,9 @@ public TableMapping GetMapping (Type type, CreateFlags createFlags = CreateFlags
/// </summary>
/// <param name="createFlags">
/// Optional flags allowing implicit PK and indexes based on naming conventions
/// </param>
/// </param>
/// <returns>
/// The mapping represents the schema of the columns of the database and contains
/// The mapping represents the schema of the columns of the database and contains
/// methods to set and get properties of objects.
/// </returns>
public TableMapping GetMapping<T> (CreateFlags createFlags = CreateFlags.None)
Expand Down Expand Up @@ -481,7 +482,7 @@ public CreateTableResult CreateTable<T> (CreateFlags createFlags = CreateFlags.N
/// later access this schema by calling GetMapping.
/// </summary>
/// <param name="ty">Type to reflect to a database table.</param>
/// <param name="createFlags">Optional flags allowing implicit PK and indexes based on naming conventions.</param>
/// <param name="createFlags">Optional flags allowing implicit PK and indexes based on naming conventions.</param>
/// <returns>
/// Whether the table was created or migrated.
/// </returns>
Expand Down Expand Up @@ -1047,7 +1048,7 @@ public object Get (object pk, TableMapping map)

/// <summary>
/// Attempts to retrieve the first object that matches the predicate from the table
/// associated with the specified type.
/// associated with the specified type.
/// </summary>
/// <param name="predicate">
/// A predicate for which object to find.
Expand Down Expand Up @@ -1101,7 +1102,7 @@ public object Find (object pk, TableMapping map)

/// <summary>
/// Attempts to retrieve the first object that matches the predicate from the table
/// associated with the specified type.
/// associated with the specified type.
/// </summary>
/// <param name="predicate">
/// A predicate for which object to find.
Expand All @@ -1117,7 +1118,7 @@ public T Find<T> (Expression<Func<T, bool>> predicate) where T : new()

/// <summary>
/// Attempts to retrieve the first object that matches the query from the table
/// associated with the specified type.
/// associated with the specified type.
/// </summary>
/// <param name="query">
/// The fully escaped SQL.
Expand All @@ -1136,7 +1137,7 @@ public T FindWithQuery<T> (string query, params object[] args) where T : new()

/// <summary>
/// Attempts to retrieve the first object that matches the query from the table
/// associated with the specified type.
/// associated with the specified type.
/// </summary>
/// <param name="map">
/// The TableMapping used to identify the table.
Expand Down Expand Up @@ -1169,9 +1170,9 @@ public object FindWithQuery (TableMapping map, string query, params object[] arg
/// <example cref="System.InvalidOperationException">Throws if a transaction has already begun.</example>
public void BeginTransaction ()
{
// The BEGIN command only works if the transaction stack is empty,
// or in other words if there are no pending transactions.
// If the transaction stack is not empty when the BEGIN command is invoked,
// The BEGIN command only works if the transaction stack is empty,
// or in other words if there are no pending transactions.
// If the transaction stack is not empty when the BEGIN command is invoked,
// then the command fails with an error.
// Rather than crash with an error, we will just ignore calls to BeginTransaction
// that would result in an error.
Expand All @@ -1182,7 +1183,7 @@ public void BeginTransaction ()
catch (Exception ex) {
var sqlExp = ex as SQLiteException;
if (sqlExp != null) {
// It is recommended that applications respond to the errors listed below
// It is recommended that applications respond to the errors listed below
// by explicitly issuing a ROLLBACK command.
// TODO: This rollback failsafe should be localized to all throw sites.
switch (sqlExp.Result) {
Expand All @@ -1196,7 +1197,7 @@ public void BeginTransaction ()
}
}
else {
// Call decrement and not VolatileWrite in case we've already
// Call decrement and not VolatileWrite in case we've already
// created a transaction point in SaveTransactionPoint since the catch.
Interlocked.Decrement (ref _transactionDepth);
}
Expand All @@ -1213,7 +1214,7 @@ public void BeginTransaction ()
/// <summary>
/// Creates a savepoint in the database at the current point in the transaction timeline.
/// Begins a new transaction if one is not in progress.
///
///
/// Call <see cref="RollbackTo(string)"/> to undo transactions since the returned savepoint.
/// Call <see cref="Release"/> to commit transactions after the savepoint returned here.
/// Call <see cref="Commit"/> to end the transaction, committing all changes.
Expand All @@ -1230,7 +1231,7 @@ public string SaveTransactionPoint ()
catch (Exception ex) {
var sqlExp = ex as SQLiteException;
if (sqlExp != null) {
// It is recommended that applications respond to the errors listed below
// It is recommended that applications respond to the errors listed below
// by explicitly issuing a ROLLBACK command.
// TODO: This rollback failsafe should be localized to all throw sites.
switch (sqlExp.Result) {
Expand Down Expand Up @@ -1277,8 +1278,8 @@ public void RollbackTo (string savepoint)
/// <param name="noThrow">true to avoid throwing exceptions, false otherwise</param>
void RollbackTo (string savepoint, bool noThrow)
{
// Rolling back without a TO clause rolls backs all transactions
// and leaves the transaction stack empty.
// Rolling back without a TO clause rolls backs all transactions
// and leaves the transaction stack empty.
try {
if (String.IsNullOrEmpty (savepoint)) {
if (Interlocked.Exchange (ref _transactionDepth, 0) > 0) {
Expand All @@ -1298,10 +1299,10 @@ void RollbackTo (string savepoint, bool noThrow)
}

/// <summary>
/// Releases a savepoint returned from <see cref="SaveTransactionPoint"/>. Releasing a savepoint
/// Releases a savepoint returned from <see cref="SaveTransactionPoint"/>. Releasing a savepoint
/// makes changes since that savepoint permanent if the savepoint began the transaction,
/// or otherwise the changes are permanent pending a call to <see cref="Commit"/>.
///
///
/// The RELEASE command is like a COMMIT for a SAVEPOINT.
/// </summary>
/// <param name="savepoint">The name of the savepoint to release. The string should be the result of a call to <see cref="SaveTransactionPoint"/></param>
Expand Down Expand Up @@ -2029,7 +2030,7 @@ public class TableAttribute : Attribute

/// <summary>
/// Flag whether to create the table without rowid (see https://sqlite.org/withoutrowid.html)
///
///
/// The default is <c>false</c> so that sqlite adds an implicit <c>rowid</c> to every table created.
/// </summary>
public bool WithoutRowId { get; set; }
Expand Down Expand Up @@ -2429,7 +2430,7 @@ public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks)
else if (clrType == typeof (Single) || clrType == typeof (Double) || clrType == typeof (Decimal)) {
return "float";
}
else if (clrType == typeof (String)) {
else if (clrType == typeof (String) || clrType == typeof (StringBuilder) || clrType == typeof (Uri) || clrType == typeof (UriBuilder)) {
int? len = p.MaxStringLength;

if (len.HasValue)
Expand Down Expand Up @@ -2778,6 +2779,15 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
else if (value is Guid) {
SQLite3.BindText (stmt, index, ((Guid)value).ToString (), 72, NegativePointer);
}
else if (value is Uri) {
SQLite3.BindText (stmt, index, ((Uri)value).ToString (), -1, NegativePointer);
}
else if (value is StringBuilder) {
SQLite3.BindText (stmt, index, ((StringBuilder)value).ToString (), -1, NegativePointer);
}
else if (value is UriBuilder) {
SQLite3.BindText (stmt, index, ((UriBuilder)value).ToString (), -1, NegativePointer);
}
else {
// Now we could possibly get an enum, retrieve cached info
var valueType = value.GetType ();
Expand Down Expand Up @@ -2882,6 +2892,18 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr
var text = SQLite3.ColumnString (stmt, index);
return new Guid (text);
}
else if (clrType == typeof(Uri)) {
var text = SQLite3.ColumnString(stmt, index);
return new Uri(text);
}
else if (clrType == typeof (StringBuilder)) {
var text = SQLite3.ColumnString (stmt, index);
return new StringBuilder (text);
}
else if (clrType == typeof(UriBuilder)) {
var text = SQLite3.ColumnString(stmt, index);
return new UriBuilder(text);
}
else {
throw new NotSupportedException ("Don't know how to read " + clrType);
}
Expand Down Expand Up @@ -3749,7 +3771,7 @@ public enum ConfigOption : int

[DllImport(LibraryPath, EntryPoint = "sqlite3_open_v2", CallingConvention=CallingConvention.Cdecl)]
public static extern Result Open ([MarshalAs(UnmanagedType.LPStr)] string filename, out IntPtr db, int flags, IntPtr zvfs);

[DllImport(LibraryPath, EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
public static extern Result Open(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);

Expand All @@ -3761,16 +3783,16 @@ public enum ConfigOption : int

[DllImport(LibraryPath, EntryPoint = "sqlite3_close", CallingConvention=CallingConvention.Cdecl)]
public static extern Result Close (IntPtr db);

[DllImport(LibraryPath, EntryPoint = "sqlite3_close_v2", CallingConvention = CallingConvention.Cdecl)]
public static extern Result Close2(IntPtr db);

[DllImport(LibraryPath, EntryPoint = "sqlite3_initialize", CallingConvention=CallingConvention.Cdecl)]
public static extern Result Initialize();

[DllImport(LibraryPath, EntryPoint = "sqlite3_shutdown", CallingConvention=CallingConvention.Cdecl)]
public static extern Result Shutdown();

[DllImport(LibraryPath, EntryPoint = "sqlite3_config", CallingConvention=CallingConvention.Cdecl)]
public static extern Result Config (ConfigOption option);

Expand Down

0 comments on commit 49a42b1

Please sign in to comment.