@@ -681,6 +681,16 @@ func (tx *SQLiteTx) Rollback() error {
681681 return err
682682}
683683
684+ // errConnClosed is returned by methods called on a connection whose
685+ // Close has already run. SQLite dereferences the connection handle
686+ // without checking it, so calling into it with a released handle
687+ // crashes the process instead of reporting an error.
688+ //
689+ // The methods check c.db without taking c.mu, as they already pass it to
690+ // SQLite without it: Close holds c.mu while SQLite runs callbacks such as
691+ // the rollback hook, and these may call the methods.
692+ var errConnClosed = errors .New ("sqlite connection is already closed" )
693+
684694// RegisterCollation makes a Go function available as a collation.
685695//
686696// cmp receives two UTF-8 strings, a and b. The result should be 0 if
@@ -694,6 +704,9 @@ func (tx *SQLiteTx) Rollback() error {
694704// If cmp does not obey these constraints, sqlite3's behavior is
695705// undefined when the collation is used.
696706func (c * SQLiteConn ) RegisterCollation (name string , cmp func (string , string ) int ) error {
707+ if c .db == nil {
708+ return errConnClosed
709+ }
697710 handle := newHandle (c , cmp )
698711 cname := C .CString (name )
699712 defer C .free (unsafe .Pointer (cname ))
@@ -711,7 +724,12 @@ func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int
711724// If there is an existing commit hook for this connection, it will be
712725// removed. If callback is nil the existing hook (if any) will be removed
713726// without creating a new one.
727+ //
728+ // The call is a no-op once the connection has been closed.
714729func (c * SQLiteConn ) RegisterCommitHook (callback func () int ) {
730+ if c .db == nil {
731+ return
732+ }
715733 if callback == nil {
716734 C .sqlite3_commit_hook (c .db , nil , nil )
717735 } else {
@@ -724,7 +742,12 @@ func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
724742// If there is an existing rollback hook for this connection, it will be
725743// removed. If callback is nil the existing hook (if any) will be removed
726744// without creating a new one.
745+ //
746+ // The call is a no-op once the connection has been closed.
727747func (c * SQLiteConn ) RegisterRollbackHook (callback func ()) {
748+ if c .db == nil {
749+ return
750+ }
728751 if callback == nil {
729752 C .sqlite3_rollback_hook (c .db , nil , nil )
730753 } else {
@@ -741,7 +764,12 @@ func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
741764// If there is an existing update hook for this connection, it will be
742765// removed. If callback is nil the existing hook (if any) will be removed
743766// without creating a new one.
767+ //
768+ // The call is a no-op once the connection has been closed.
744769func (c * SQLiteConn ) RegisterUpdateHook (callback func (int , string , string , int64 )) {
770+ if c .db == nil {
771+ return
772+ }
745773 if callback == nil {
746774 C .sqlite3_update_hook (c .db , nil , nil )
747775 } else {
@@ -755,7 +783,12 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64
755783// SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), and 1 to 3 arguments,
756784// depending on operation. More details see:
757785// https://www.sqlite.org/c3ref/c_alter_table.html
786+ //
787+ // The call is a no-op once the connection has been closed.
758788func (c * SQLiteConn ) RegisterAuthorizer (callback func (int , string , string , string ) int ) {
789+ if c .db == nil {
790+ return
791+ }
759792 c .authorizerMu .Lock ()
760793 defer c .authorizerMu .Unlock ()
761794
@@ -790,6 +823,9 @@ func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, strin
790823//
791824// See _example/go_custom_funcs for a detailed example.
792825func (c * SQLiteConn ) RegisterFunc (name string , impl any , pure bool ) error {
826+ if c .db == nil {
827+ return errConnClosed
828+ }
793829 var fi functionInfo
794830 fi .f = reflect .ValueOf (impl )
795831 t := fi .f .Type ()
@@ -872,6 +908,9 @@ func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTe
872908//
873909// See _example/go_custom_funcs for a detailed example.
874910func (c * SQLiteConn ) RegisterAggregator (name string , impl any , pure bool ) error {
911+ if c .db == nil {
912+ return errConnClosed
913+ }
875914 var ai aggInfo
876915 ai .constructor = reflect .ValueOf (impl )
877916 t := ai .constructor .Type ()
@@ -980,9 +1019,13 @@ func (c *SQLiteConn) RegisterAggregator(name string, impl any, pure bool) error
9801019}
9811020
9821021// AutoCommit return which currently auto commit or not.
1022+ // It reports false once the connection has been closed.
9831023func (c * SQLiteConn ) AutoCommit () bool {
9841024 c .mu .Lock ()
9851025 defer c .mu .Unlock ()
1026+ if c .db == nil {
1027+ return false
1028+ }
9861029 return int (C .sqlite3_get_autocommit (c .db )) != 0
9871030}
9881031
@@ -2201,8 +2244,12 @@ const (
22012244// GetFilename returns the absolute path to the file containing
22022245// the requested schema. When passed an empty string, it will
22032246// instead use the database's default schema: "main".
2247+ // It returns an empty string once the connection has been closed.
22042248// See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
22052249func (c * SQLiteConn ) GetFilename (schemaName string ) string {
2250+ if c .db == nil {
2251+ return ""
2252+ }
22062253 if schemaName == "" {
22072254 schemaName = "main"
22082255 }
@@ -2212,15 +2259,23 @@ func (c *SQLiteConn) GetFilename(schemaName string) string {
22122259}
22132260
22142261// GetLimit returns the current value of a run-time limit.
2262+ // It returns -1 once the connection has been closed.
22152263// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
22162264func (c * SQLiteConn ) GetLimit (id int ) int {
2265+ if c .db == nil {
2266+ return - 1
2267+ }
22172268 return int (C ._sqlite3_limit (c .db , C .int (id ), C .int (- 1 )))
22182269}
22192270
22202271// SetLimit changes the value of a run-time limits.
22212272// Then this method returns the prior value of the limit.
2273+ // It returns -1 once the connection has been closed.
22222274// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
22232275func (c * SQLiteConn ) SetLimit (id int , newVal int ) int {
2276+ if c .db == nil {
2277+ return - 1
2278+ }
22242279 return int (C ._sqlite3_limit (c .db , C .int (id ), C .int (newVal )))
22252280}
22262281
@@ -2237,6 +2292,9 @@ func (c *SQLiteConn) SetLimit(id int, newVal int) int {
22372292//
22382293// See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
22392294func (c * SQLiteConn ) SetFileControlInt (dbName string , op int , arg int ) error {
2295+ if c .db == nil {
2296+ return errConnClosed
2297+ }
22402298 if dbName == "" {
22412299 dbName = "main"
22422300 }
@@ -2265,6 +2323,9 @@ func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
22652323//
22662324// See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
22672325func (c * SQLiteConn ) SetFileControlInt64 (dbName string , op int , arg int64 ) error {
2326+ if c .db == nil {
2327+ return errConnClosed
2328+ }
22682329 if dbName == "" {
22692330 dbName = "main"
22702331 }
0 commit comments