C++ SQLite Yet Another Api (YAPI) Part 3 Easy UDF’s

After reading the second installment about what I announced as “simple” C++ API , you have been thinking it is a bit overwhelming and certainly not that simple. And then comes: C++ SQLite Yet Another Api (YAPI) Part 3 Easy UDF’s.

Easy UDF’s

You are still here and you know UDF stand’s for “User Defined Function”.  And, I assume, you have some SQLite UDF experience. After which you certainly are interested in how they can be made simple or even simpler.

With two fundamental application design techniques: Encapsulation and Decoupling. Let’s start immediately with the real life use case and with the client code showing the usage.

We have a demo database, with one table named: words, and one column in it named: word. And it happens to contain the (almost) whole English dictionary.

the requirement is to extract all the palindromes found within English dictionary. And also to show the length of each of them. So, the SQL for that is:

Very simple. With two user defined functions (UDF’s) required: strlen and palindrome. Using my dbj++sql , we start to code the solution:

Yes, as the comment says: sqlite3 api is hidden and safely tucked away in it’s own namespace. Now the fun starts. This is the code for the first UDF required.

udf_argument is the class that makes easy “getting” the arguments sent to the UDF. Transformed to the type your C++ code requires. While udf_retval is the class that makes easy “returning”  some value out of your UDF’s

No complicated sqlite 3 C API , no usage rules to remember and no more endless copy paste of code fragments required. Second UDF is a bit more involved, because it is not simple,strlen and there is a clever palindrome algorithm to be used too.

And that is it. You have already finished two easy UDF’s. All in standard C++17.

Now the usual callback that will process each row of the result.

We use the standard [[maybe_unused]] attribute to pacify the modern compilers, throwing warning all the time, about unused stuff.

And now the anti-climax. Simple test function that does it all, with the easy UDF’s used.

All very simple and functional. The two interesting lines are

We use the dbj::db::register_dbj_udf template function to register the dbj udf’s aka the “easy udf’s”.

Notice how we do not have to explain anything about udf arguments or return value types. SQLite passes the right type and value of the argument expected, and you return the type expected and the value computed. All are defined by the SQL statement using your udf.

I am sure this is so simple now, you do not need any additional hand-holding whatsoever.

There you have it: SQLite 3 User Defined Functions, the easy way.

This API is not properly documented. Yet. If you require (much) more explanations, code samples and a such please do not hesitate to ask. Either in comments below or sending us the email.

So little C++ so much good!

Enjoy the standard C++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.