ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
533 stars 109 forks source link

Bindings for Formatted String Printing Functions #153

Open MKuckert opened 7 years ago

MKuckert commented 7 years ago

I just found the sqlite3_*printf functions and was wondering if there's a reason those are missing from the bindings. They are pretty handy to escape special chars in strings for example (using sqlite3_mprintf("%q", str)).

Is there a way to use these printing functions using a custom binding or do I have to implement my own as a port of the original functions?

ericsink commented 7 years ago

I would like those functions too. I'll live this issue open for now, to remind me to explore possibilities.

But they are unusually difficult. They accept a varying number of args which are without a specified type. That doesn't map well onto the semantics of P/Invoke.

So yes, there is a reason they are missing from the bindings. But I agree that it would be nice to have them.

praeclarum commented 7 years ago

I've had a request/need for them too. In this issue: https://github.com/praeclarum/sqlite-net/issues/382

Also pragma commands don't seem to like to be parameterized so it would be nice to have this to safely set the key for SQLCipher.

praeclarum commented 7 years ago

If the binding is difficult, perhaps you can just expose the simplest overload that would let us execute sqlite3_mprintf("%u", unsafeString) - an overload that expects two strings. This is the trick Xamarin uses to bind to objc_msgSend - lots of overloads.

ericsink commented 7 years ago

Acknowledged. Good idea.

ericsink commented 7 years ago

I just committed the lamest possible implementation of this. In efb0a02.

Like I said, this is lame. I'll probably explore more angles.

Even with the overloads approach, the problems are:

  1. How do I know what the arg types should be?
  2. Do I have to worry about somebody passing %z?

I could resolve (1) by only supporting strings.

I could resolve (2) by ignoring the matter and just saying that if you pass %z and your app crashes, you got what you deserved. Or I could maybe scan the format string for %z and give an error.