'Trying to create user defined function `LIKE` using SQLite
I am (trying to) using the sqlite-net-pcl package version 1.8.116, to create a user defined function in C#.
I tried to start simple by creating a function to reverse a string, like REVERSE
When i try the query: SELECT 1,'abc','def',
I am expecting the output to be: 1:abc:fed
But the output is: 0:abc:System.Char[], So I must have done something wrong...
Can anyone give a tip? because I am pretty clueless right now... 😉
My code:
SQLiteConnection db = new SQLiteConnection("test.db");
// https://sqlite.org/c3ref/create_function.html
sqlite3 dbhandle = db.Handle;
string function = "myReverse";
int narg = 1;
int eTextRep = SQLitePCL.raw.SQLITE_UTF8 | SQLitePCL.raw.SQLITE_DETERMINISTIC;
int ok = SQLitePCL.raw.sqlite3_create_function(dbhandle, function, narg, eTextRep, myReverse);
//Console.WriteLine($"OK:{ok}");
SQLiteCommand cmd = db.CreateCommand("SELECT 1 as id, 'abc' as t, myReverse('def') as revt");
var result = cmd.ExecuteQuery<Record>();
Console.WriteLine($"{result[0].id}:{result[0].t}:{result[0].revt}");
and:
static void myReverse(sqlite3_context ctx, object user_data, sqlite3_value[] args)
{
SQLitePCL.utf8z utf8z = raw.sqlite3_value_text(args[0]);
string input = utf8z.utf8_to_string();
//Console.WriteLine($"INPUT:{input}"); // This shows INPUT:def
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
//raw.sqlite3_result_text(ctx, utf8z.FromString(charArray.ToString())); // see: comments...
raw.sqlite3_result_text(ctx, utf8z.FromString(new string(charArray)));
}
Record:
public class Record
{
public int id { get; set; }
public string t { get; set; }
public string revt { get; set; }
}
Above was my first try, finally I would like to have an implementation of the function LIKE, like is mentioned in paragraph 5 "The infix LIKE operator is implemented by calling the application-defined SQL functions"
Currently returning a string is working, but I am looking for something like raw.sqlite3_result_boolean, or raw.sqlite3_result_logical, because the return value for the function LIKE is a boolean.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
