Skip to content

Adding last query executed to table query. #18

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

Merged
merged 2 commits into from
May 17, 2024
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
10 changes: 10 additions & 0 deletions SQLite.Net.Tests/StringQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,15 @@ public void StartsWith()
List<Product> bs = db.Table<Product>().Where(x => x.Name.StartsWith("B")).ToList();
Assert.AreEqual(1, bs.Count);
}

[Test]
public void RegexTest()
{
var fs = db.Table<Product>().Where(x => x.Name.IsMatch("^F.*")).ToList();
Assert.AreEqual(2, fs.Count);

var bs = db.Table<Product>().Where(x => x.Name.IsMatch(".*o$")).ToList();
Assert.AreEqual(1, bs.Count);
}
}
}
18 changes: 18 additions & 0 deletions src/SQLite.Net/Interop/SQLiteApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.RegularExpressions;
using SQLitePCL;

namespace SQLite.Net2
Expand All @@ -14,6 +15,23 @@ public Result Open(string filename, out IDbHandle db, int flags, string zvfs)
return (Result)r;
}

public int BindRegexpFunction(IDbHandle db)
{
void RegexMatch(
sqlite3_context ctx,
object user_data,
sqlite3_value[] args)
{
var pattern = raw.sqlite3_value_text(args[0]).utf8_to_string();
var input = raw.sqlite3_value_text(args[1]).utf8_to_string();
var r = Regex.IsMatch(input, pattern);
raw.sqlite3_result_int(ctx, r ? 1 : 0);
}

var handle = (DbHandle)db;
return raw.sqlite3_create_function(handle.DbPtr, "REGEXP", 2, null, RegexMatch);
}

public ExtendedResult ExtendedErrCode(IDbHandle db)
{
var internalDbHandle = (DbHandle)db;
Expand Down
2 changes: 2 additions & 0 deletions src/SQLite.Net/SQLiteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public SQLiteConnection(string databasePath, SQLiteOpenFlags openFlags = SQLiteO
if (r != Result.OK)
throw new SQLiteException(r, $"Could not open database file: {DatabasePath} ({r})");

sqlite.BindRegexpFunction(handle);

Handle = handle ?? throw new NullReferenceException("Database handle is null");
_open = true;
databaseOpenFlags = openFlags;
Expand Down
23 changes: 23 additions & 0 deletions src/SQLite.Net/TableQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;

namespace SQLite.Net2
{
Expand All @@ -44,6 +45,7 @@ public class TableQuery<T> : BaseTableQuery, IEnumerable<T>
private int? _offset;
private List<Ordering> _orderBys;
private Expression _where;
private string? _lastQueryExecuted;

private TableQuery(SQLiteConnection conn, TableMapping table)
{
Expand All @@ -64,6 +66,7 @@ public TableQuery(SQLiteConnection conn)

public TableMapping Table { get; private set; }

public string? LastQueryExecuted => _lastQueryExecuted;

public IEnumerator<T> GetEnumerator()
{
Expand Down Expand Up @@ -161,6 +164,7 @@ public int Delete( Expression<Func<T, bool>> predExpr)
cmdText += " where " + w.CommandText;
var command = Connection.CreateCommand(cmdText, args.ToArray());

_lastQueryExecuted = cmdText;
var result = command.ExecuteNonQuery();
return result;
}
Expand Down Expand Up @@ -355,6 +359,8 @@ private SQLiteCommand GenerateCommand( string selectionList)
}
cmdText.Append(" offset ").Append(_offset.Value);
}

_lastQueryExecuted = cmdText.ToString();
return Connection.CreateCommand(cmdText.ToString(), args.ToArray());
}

Expand Down Expand Up @@ -536,6 +542,10 @@ private CompileResult CompileExpr( Expression expr, List<object> queryArgs)
{
sqlCall.AppendFormat("(replace({0}, {1}, {2}))", obj.CommandText, args[0].CommandText, args[1].CommandText);
}
else if (call.Method.Name == nameof(TableQueryExtensions.IsMatch) && args.Length == 2)
{
sqlCall.AppendFormat("{0} REGEXP {1}", args[0].CommandText, args[1].CommandText);
}
else
{
sqlCall.Append(call.Method.Name.ToLower()).Append("(").Append(String.Join(",", args.Select(a => a.CommandText).ToArray())).Append(")");
Expand Down Expand Up @@ -825,4 +835,17 @@ private class CompileResult
public object Value { get; set; }
}
}

public static class TableQueryExtensions
{
public static bool IsMatch(this ISerializable<string> a, string b)
{
return IsMatch(a.Serialize(), b);
}

public static bool IsMatch(this string a, string b)
{
return Regex.IsMatch(a, b);
}
}
}
Loading