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

Expose sqlite3_db_status

Open demaggus83 opened this issue 6 years ago • 6 comments

For expvar I need the status of sqlite. sqlite delivers stats per sqlite3_db_status

I would try to implement it myself, but I have no experience in C and calling C from Go

Is there an workaround?

demaggus83 avatar Mar 13 '19 07:03 demaggus83

Do you want sqlite3_db_status or sqlite3_status? The former is problematic, not because it's hard to implement, but because you wouldn't have a straightforward way of calling it. It would be implemented as a method on *sqlite3.SQLiteConn, and there's no way to get to that through the database/sql API, unless you pull some shenanigans with the ConnectHook or something.

rittneje avatar Mar 14 '19 02:03 rittneje

Both would be really great. I would be fine with a ConnectHook. I am already using the ConnectHook for SetTrace in DEV environment.

demaggus83 avatar Mar 14 '19 05:03 demaggus83

As of Go 1.13, you can actually access the *sqlite3.SQLiteConn via Conn.Raw. So both sqlite3_db_status and sqlite3_status could be exposed without too much difficulty. However, do be advised that a sql.DB from the standard library represents a pool of connections, so unless you've configured it specially, you could have multiple *sqlite3.SQLiteConn instances behind the scenes, and there is no mechanism at present for looping over all of them.

rittneje avatar Jan 25 '20 23:01 rittneje

I have a similar requirement to get at sqlite3_db_config() to lock down the use of SQLite3 when processing untrusted database files as per point 9 in section 1.2 here: https://www.sqlite.org/security.html

  1. If the application does not use triggers or views, consider disabling the unused capabilities with: sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_TRIGGER,0,0); sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_VIEW,0,0);

I'm guessing the "multiple *sqlite3.SQLiteConn instances" would be an issue here too?

carwyn avatar Jun 13 '20 22:06 carwyn

@carwyn For that you would want to use the ConnectHook. https://github.com/mattn/go-sqlite3/blob/baaf8a978416040e7f2d00ac36e345098d0588d8/sqlite3.go#L294-L297 That would allow you to perform the requisite operation any time a new connection gets opened. That being said, sqlite3_db_config is not currently exposed, and so a new method on *SQLiteConn would have to be added.

rittneje avatar Jun 13 '20 22:06 rittneje