Skip to content

Commit

Permalink
add sqlite driver name option
Browse files Browse the repository at this point in the history
  • Loading branch information
nidorx committed Oct 12, 2024
1 parent f63ca7f commit d2b1b13
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
8 changes: 4 additions & 4 deletions demo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ RUN go version

RUN apk --no-cache add gcc g++

ARG GO_TAGS
ARG PROJECT_VERSION

WORKDIR /go/src/demo/
COPY . ./
COPY go.mod.txt ./go.mod
Expand All @@ -22,7 +19,10 @@ COPY driver-modernc.go.txt ./driver-modernc.go

RUN set -Eeux && go mod tidy && go mod download && go mod verify

RUN GOOS=linux CGO_ENABLED=1 go build -tags $GO_TAGS -trimpath -ldflags="-w -s -X 'main.Version=${PROJECT_VERSION}'" -o app main.go
ARG GO_TAGS
ARG PROJECT_VERSION

RUN GOOS=linux CGO_ENABLED=1 go build -tags $GO_TAGS -trimpath -ldflags="-w -s -X 'main.Version=${PROJECT_VERSION}'" -o app .

# Stage 2
FROM alpine:3.20.3
Expand Down
3 changes: 2 additions & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
dev = true
dev = false

log sqlog.Log

Expand All @@ -33,6 +33,7 @@ func init() {
MaxOpenedDB: 2,
MaxRunningTasks: 5,
CloseIdleSec: 10,
Driver: "sqlite3",
})
if err != nil {
panic(err)
Expand Down
8 changes: 7 additions & 1 deletion sqlite/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Config struct {
Prefix string // Prefix for the database name (default "sqlog")
SQLiteOptions map[string]string // SQLite connection string options (https://github.com/mattn/go-sqlite3#connection-string)

Driver string // SQLite driver name (default sqlite3)

// Allows defining a custom expression processor.
ExprBuilder func(expression string) (*Expr, error)

Expand Down Expand Up @@ -104,6 +106,10 @@ func New(config *Config) (*storage, error) {
config.Prefix = strings.ToLower(strings.Join(strings.Fields(config.Prefix), "_"))
}

if config.Driver == "" {
config.Driver = "sqlite3"
}

if config.MaxFilesizeMB <= 0 {
config.MaxFilesizeMB = 20 // ~20MB
}
Expand Down Expand Up @@ -142,7 +148,7 @@ func New(config *Config) (*storage, error) {
}

if len(dbs) == 0 {
dbs = append(dbs, newDb(config.Dir, config.Prefix, time.Now(), config.MaxChunkAgeSec))
dbs = append(dbs, newDb(config.Driver, config.Dir, config.Prefix, time.Now(), config.MaxChunkAgeSec))
}

// Initialize the active database (live)
Expand Down
3 changes: 2 additions & 1 deletion sqlite/storage_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type storageDb struct {
db *sql.DB // SQLite connection object
taskCount int32 // Number of scheduled tasks
taskMap sync.Map // Map of scheduled tasks
driver string // SQLite driver name
}

// schedule schedules a query execution on this instance
Expand Down Expand Up @@ -190,7 +191,7 @@ func (s *storageDb) connect(options map[string]string) error {
}
}

db, err := sql.Open("sqlite3", connString)
db, err := sql.Open(s.driver, connString)
if err != nil {
atomic.StoreInt32(&s.status, db_closed)
return err
Expand Down
2 changes: 1 addition & 1 deletion sqlite/storage_routine_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *storage) doRoutineSizeCheck() {
// archiving dbs
if s.liveDbs[len(s.liveDbs)-1].size > int64(s.config.MaxFilesizeMB)*1000000 {
nextStart := time.Now().Add(time.Duration(s.config.IntervalSizeCheckSec * 2 * int32(time.Second)))
ndb := newDb(s.config.Dir, s.config.Prefix, nextStart, s.config.MaxChunkAgeSec)
ndb := newDb(s.config.Driver, s.config.Dir, s.config.Prefix, nextStart, s.config.MaxChunkAgeSec)
ndb.live = true
if err := ndb.connect(s.config.SQLiteOptions); err != nil {
slog.Warn(
Expand Down
3 changes: 2 additions & 1 deletion sqlite/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"
)

func newDb(dir, prefix string, start time.Time, maxChunkAgeSec int64) *storageDb {
func newDb(driver, dir, prefix string, start time.Time, maxChunkAgeSec int64) *storageDb {

epochStart := start.Unix()
name := fmt.Sprintf("%s_%d.db", prefix, epochStart)
Expand All @@ -28,6 +28,7 @@ func newDb(dir, prefix string, start time.Time, maxChunkAgeSec int64) *storageDb
newEpochStart: epochStart,
maxChunkAgeSec: maxChunkAgeSec,
epochEnd: 0,
driver: driver,
}
}

Expand Down

0 comments on commit d2b1b13

Please sign in to comment.