c7b846589e
Change-Id: If1caa9cde746817e62cae32a152eeec81959129c
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package sqliteutil
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strconv"
|
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/private/dbutil/dbschema"
|
|
)
|
|
|
|
// LoadSchemaFromSQL inserts script into connstr and loads schema.
|
|
func LoadSchemaFromSQL(ctx context.Context, script string) (_ *dbschema.Schema, err error) {
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
defer func() { err = errs.Combine(err, db.Close()) }()
|
|
|
|
_, err = db.Exec(script)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
return QuerySchema(ctx, db)
|
|
}
|
|
|
|
// LoadSnapshotFromSQL inserts script into connstr and loads schema.
|
|
func LoadSnapshotFromSQL(ctx context.Context, script string) (_ *dbschema.Snapshot, err error) {
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
defer func() { err = errs.Combine(err, db.Close()) }()
|
|
|
|
_, err = db.Exec(script)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
snapshot, err := QuerySnapshot(ctx, db)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
snapshot.Script = script
|
|
return snapshot, nil
|
|
}
|
|
|
|
// QuerySnapshot loads snapshot from database
|
|
func QuerySnapshot(ctx context.Context, db dbschema.Queryer) (*dbschema.Snapshot, error) {
|
|
schema, err := QuerySchema(ctx, db)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
data, err := QueryData(ctx, db, schema)
|
|
if err != nil {
|
|
return nil, errs.Wrap(err)
|
|
}
|
|
|
|
return &dbschema.Snapshot{
|
|
Version: -1,
|
|
Schema: schema,
|
|
Data: data,
|
|
}, err
|
|
}
|
|
|
|
// QueryData loads all data from tables
|
|
func QueryData(ctx context.Context, db dbschema.Queryer, schema *dbschema.Schema) (*dbschema.Data, error) {
|
|
return dbschema.QueryData(db, schema, func(columnName string) string {
|
|
quoted := strconv.Quote(columnName)
|
|
return `quote(` + quoted + `) as ` + quoted
|
|
})
|
|
}
|
|
|
|
// IsConstraintError checks if given error is about constraint violation
|
|
func IsConstraintError(err error) bool {
|
|
return errs.IsFunc(err, func(err error) bool {
|
|
if e, ok := err.(sqlite3.Error); ok {
|
|
if e.Code == sqlite3.ErrConstraint {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
})
|
|
}
|