Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable load extension #1311

Closed
playaround88 opened this issue Jan 2, 2025 · 2 comments
Closed

How to enable load extension #1311

playaround88 opened this issue Jan 2, 2025 · 2 comments

Comments

@playaround88
Copy link

playaround88 commented Jan 2, 2025

Hi, I use gorm sqlite(underlay use go-sqlite3) as my project's sql driver , and when I try to load an extension like below

db, err = gorm.Open(sqlite.Open(conf.Dsn), &gorm.Config{})
if err != nil {
	fmt.Println(err)
	panic(err)
}
db.Exec("SELECT load_extension('" + path.Join(execDir, "libsqliteipv4.so") + "')")

I got an error.

2025/01/02 11:01:08 /**/app/dao/db.go:58 not authorized
[0.026ms] [rows:0] SELECT load_extension('/**/libsqliteipv4.so')

but when I connect db file use shell cmd, use .dbconfig can see load_extension is default on, and exec SELECT load_extension('./libsqliteipv4.so') is OK!

sqlite>.dbconfig
      ...
     load_extension on

I have check that go-sqlite default is enabled load extension, but why I got this exception? and how can I fix, pls help

@rittneje
Copy link
Collaborator

rittneje commented Jan 2, 2025

I have check that go-sqlite default is enabled load extension

This is not quite true - extension loading itself is disabled by default. However, if you load an extension via SQLiteConn.LoadExtension or via SQLiteDriver.Extensions, then extension loading will be enabled temporarily.

These two are the only supported mechanisms. Consequently, attempting to use load_extension in your SELECT query is not supported.

I do not personally use gorm, but I suspect you'll need to do something like:

func init() {
    sql.Register("sqlite3_with_extension", &sqlite3.SQLiteDriver{
         Extensions: []string{ "libsqliteipv4.so" },
    })
}

// ...

db, err := gorm.Open(sqlite.New(&sqlite.Dialector{DriverName: "sqlite3_with_extension", DSN: conf.Dsn}), &gorm.Config{})

@playaround88
Copy link
Author

@rittneje I tried, it works! THX very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants