better-sqlite3 icon indicating copy to clipboard operation
better-sqlite3 copied to clipboard

Deleting user-defined functions

Open Fabricio-191 opened this issue 3 years ago • 4 comments

How can i delete user-defined functions ?

db.function('add2', (a, b) => a + b);

for example, here i defined the function add2, if i wanted to delete it in postgresql i would do

DROP ROUTINE IF EXISTS add2

but this is not supported by sqlite3/better-sqlite3.

So I was wondering if there is a sqlite statement for that or a pragma, and if not when closing and reopening the database the functions are deleted, therefore they are not saved in the database, so, I was wondering if you could do something at the C++ level.

Fabricio-191 avatar Feb 28 '22 13:02 Fabricio-191

What's your use case? I don't understand what you're trying to achieve.

You can do this to achieve basically the same:

// "remove "the add2 function
db.function('add2', () => throw new Error('not implemented'));

Or as you said open a new connection. Or have two connections, one with your custom functions and one without.

Prinzhorn avatar Feb 28 '22 15:02 Prinzhorn

What's your use case? I don't understand what you're trying to achieve.

I am going to have dynamically generated functions, but when I stop using a certain function I would like to delete it so that it does not occupy memory.

db.function('add2', () => throw new Error('not implemented'));

this isn't what i want

Fabricio-191 avatar Feb 28 '22 16:02 Fabricio-191

Sounds like a valid feature request, the SQLite docs say:

To delete an existing SQL function or aggregate, pass NULL pointers for all three function callbacks.

but currently better-sqlite3 always expects a function. I think the current API could be extended in a backwards compatible way to allow db.function('add2', null) to explicitly remove a function.

Prinzhorn avatar Mar 01 '22 09:03 Prinzhorn

sounds great, I'll be waiting for it

Fabricio-191 avatar Mar 01 '22 12:03 Fabricio-191