mattn / go-sqlite3

sqlite3 driver for go using database/sql
http://mattn.github.io/go-sqlite3
MIT License
7.9k stars 1.1k forks source link

Expose sqlite3_db_status #701

Open demaggus83 opened 5 years ago

demaggus83 commented 5 years ago

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?

rittneje commented 5 years ago

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.

demaggus83 commented 5 years ago

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

rittneje commented 4 years ago

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.

carwyn commented 4 years ago

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?

rittneje commented 4 years ago

@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.

otoolep commented 3 months ago

Following up https://github.com/mattn/go-sqlite3/issues/701#issuecomment-643683940

I came across this issue when looking into support for sqlite3_db_config. Because sqlite3_db_config is varadic it's tricky - Go doesn't support calling varadic functions directly. That said, I only needed a specific config option, so coded just that. Others may find what I did useful, in case they want to modify their own forks.

See https://github.com/rqlite/go-sqlite3/pull/30