Skip to content

Commit 813964d

Browse files
committed
sqlite: Generalize sqlite driver selection for use in sql_query/sql_table
Fixes #724, #819.
1 parent d7551cf commit 813964d

File tree

6 files changed

+53
-26
lines changed

6 files changed

+53
-26
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
//go:build !nosqlite3 && cgo
2-
// +build !nosqlite3,cgo
3-
41
/*
52
Maddy Mail Server - Composable all-in-one email server.
6-
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
3+
Copyright © 2019-2026 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
74
85
This program is free software: you can redistribute it and/or modify
96
it under the terms of the GNU General Public License as published by
@@ -19,6 +16,8 @@ You should have received a copy of the GNU General Public License
1916
along with this program. If not, see <https://www.gnu.org/licenses/>.
2017
*/
2118

22-
package table
19+
package sqliteprovider
2320

24-
import _ "github.com/mattn/go-sqlite3"
21+
func IsSqliteDriver(name string) bool {
22+
return name == "sqlite" || name == "sqlite3"
23+
}

internal/storage/imapsql/modernc_sqlite3.go renamed to internal/sqlite/modernc_sqlite3.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//go:build !nosqlite3 && !cgo
2-
// +build !nosqlite3,!cgo
32

43
/*
54
Maddy Mail Server - Composable all-in-one email server.
6-
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
5+
Copyright © 2019-2026 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
76
87
This program is free software: you can redistribute it and/or modify
98
it under the terms of the GNU General Public License as published by
@@ -19,8 +18,18 @@ You should have received a copy of the GNU General Public License
1918
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019
*/
2120

22-
package imapsql
21+
package sqliteprovider
2322

2423
import _ "modernc.org/sqlite"
2524

26-
const sqliteImpl = "modernc"
25+
const (
26+
IsAvailable = true
27+
IsTranspiled = true
28+
)
29+
30+
func MapDriverName(n string) string {
31+
if n == "sqlite3" {
32+
return "sqlite"
33+
}
34+
return n
35+
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//go:build nosqlite3
2-
// +build nosqlite3
32

43
/*
54
Maddy Mail Server - Composable all-in-one email server.
6-
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
5+
Copyright © 2019-2026 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
76
87
This program is free software: you can redistribute it and/or modify
98
it under the terms of the GNU General Public License as published by
@@ -19,6 +18,13 @@ You should have received a copy of the GNU General Public License
1918
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019
*/
2120

22-
package imapsql
21+
package sqliteprovider
2322

24-
const sqliteImpl = "missing"
23+
const (
24+
IsAvailable = false
25+
IsTranspiled = false
26+
)
27+
28+
func MapDriverName(n string) string {
29+
return n
30+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//go:build !nosqlite3 && cgo
2-
// +build !nosqlite3,cgo
32

43
/*
54
Maddy Mail Server - Composable all-in-one email server.
6-
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
5+
Copyright © 2019-2026 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
76
87
This program is free software: you can redistribute it and/or modify
98
it under the terms of the GNU General Public License as published by
@@ -19,8 +18,18 @@ You should have received a copy of the GNU General Public License
1918
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019
*/
2120

22-
package imapsql
21+
package sqliteprovider
2322

2423
import _ "github.com/mattn/go-sqlite3"
2524

26-
const sqliteImpl = "cgo"
25+
const (
26+
IsAvailable = true
27+
IsTranspiled = false
28+
)
29+
30+
func MapDriverName(n string) string {
31+
if n == "sqlite" {
32+
return "sqlite3"
33+
}
34+
return n
35+
}

internal/storage/imapsql/imapsql.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/foxcpp/maddy/framework/log"
4949
"github.com/foxcpp/maddy/framework/module"
5050
"github.com/foxcpp/maddy/internal/authz"
51+
sqliteprovider "github.com/foxcpp/maddy/internal/sqlite"
5152
"github.com/foxcpp/maddy/internal/updatepipe"
5253
"github.com/foxcpp/maddy/internal/updatepipe/pubsub"
5354

@@ -172,16 +173,16 @@ func (store *Storage) Configure(inlineArgs []string, cfg *config.Map) error {
172173
return errors.New("imapsql: driver is required")
173174
}
174175

175-
if driver == "sqlite3" {
176-
if sqliteImpl == "modernc" {
177-
store.Log.Println("using transpiled SQLite (modernc.org/sqlite), this is experimental")
178-
driver = "sqlite"
179-
} else if sqliteImpl == "cgo" {
176+
if sqliteprovider.IsSqliteDriver(driver) {
177+
if sqliteprovider.IsTranspiled {
178+
store.Log.Println("using transpiled SQLite (modernc.org/sqlite)")
179+
} else if sqliteprovider.IsAvailable {
180180
store.Log.Debugln("using cgo SQLite")
181-
} else if sqliteImpl == "missing" {
181+
} else {
182182
return errors.New("imapsql: SQLite is not supported, recompile without no_sqlite3 tag set")
183183
}
184184
}
185+
driver = sqliteprovider.MapDriverName(driver)
185186

186187
deliveryNormFunc, ok := authz.NormalizeFuncs[deliveryNormalize]
187188
if !ok {
@@ -301,7 +302,7 @@ func (store *Storage) EnableUpdatePipe(mode updatepipe.BackendMode) error {
301302
}
302303

303304
switch store.driver {
304-
case "sqlite3":
305+
case "sqlite3", "sqlite":
305306
dbId := sha1.Sum([]byte(strings.Join(store.dsn, " ")))
306307
sockPath := filepath.Join(
307308
config.RuntimeDirectory,

internal/table/sql_query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ package table
2121
import (
2222
"context"
2323
"database/sql"
24+
"errors"
2425
"fmt"
2526
"strings"
2627

2728
"github.com/foxcpp/maddy/framework/config"
2829
"github.com/foxcpp/maddy/framework/module"
30+
sqliteprovider "github.com/foxcpp/maddy/internal/sqlite"
2931
_ "github.com/lib/pq"
3032
)
3133

@@ -89,6 +91,7 @@ func (s *SQL) Configure(inlineArgs []string, cfg *config.Map) error {
8991
if driver == "postgres" && s.namedArgs {
9092
return config.NodeErr(cfg.Block, "PostgreSQL driver does not support named_args")
9193
}
94+
driver = sqliteprovider.MapDriverName(driver)
9295

9396
db, err := sql.Open(driver, strings.Join(dsnParts, " "))
9497
if err != nil {
@@ -156,7 +159,7 @@ func (s *SQL) Lookup(ctx context.Context, val string) (string, bool, error) {
156159
row = s.lookup.QueryRowContext(ctx, val)
157160
}
158161
if err := row.Scan(&repl); err != nil {
159-
if err == sql.ErrNoRows {
162+
if errors.Is(err, sql.ErrNoRows) {
160163
return "", false, nil
161164
}
162165
return "", false, fmt.Errorf("%s: lookup %s: %w", s.modName, val, err)

0 commit comments

Comments
 (0)