mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-21 11:29:51 -05:00
removed dsqle.database dependency on dEnv
This commit is contained in:
@@ -110,7 +110,7 @@ func Sql(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEn
|
||||
|
||||
// run a single command and exit
|
||||
if query, ok := apr.GetValue(queryFlag); ok {
|
||||
se, err := newSqlEngine(dEnv, dsqle.NewDatabase("dolt", root, dEnv))
|
||||
se, err := newSqlEngine(dEnv, dsqle.NewDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState))
|
||||
if err != nil {
|
||||
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
@@ -128,7 +128,7 @@ func Sql(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEn
|
||||
var se *sqlEngine
|
||||
// Windows has a bug where STDIN can't be statted in some cases, see https://github.com/golang/go/issues/33570
|
||||
if (err != nil && osutil.IsWindows) || (fi.Mode()&os.ModeCharDevice) == 0 {
|
||||
se, err = newSqlEngine(dEnv, dsqle.NewBatchedDatabase("dolt", root, dEnv))
|
||||
se, err = newSqlEngine(dEnv, dsqle.NewBatchedDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState))
|
||||
if err != nil {
|
||||
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
@@ -139,7 +139,7 @@ func Sql(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEn
|
||||
} else if err != nil {
|
||||
HandleVErrAndExitCode(errhand.BuildDError("Couldn't stat STDIN. This is a bug.").Build(), usage)
|
||||
} else {
|
||||
se, err = newSqlEngine(dEnv, dsqle.NewDatabase("dolt", root, dEnv))
|
||||
se, err = newSqlEngine(dEnv, dsqle.NewDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState))
|
||||
if err != nil {
|
||||
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func Serve(ctx context.Context, serverConfig *ServerConfig, rootValue *doltdb.Ro
|
||||
|
||||
userAuth := auth.NewAudit(auth.NewNativeSingle(serverConfig.User, serverConfig.Password, permissions), auth.NewAuditLog(logrus.StandardLogger()))
|
||||
sqlEngine := sqle.NewDefault()
|
||||
sqlEngine.AddDatabase(dsqle.NewDatabase("dolt", rootValue, nil))
|
||||
sqlEngine.AddDatabase(dsqle.NewDatabase("dolt", rootValue, nil, nil))
|
||||
|
||||
hostPort := net.JoinHostPort(serverConfig.Host, strconv.Itoa(serverConfig.Port))
|
||||
timeout := time.Second * time.Duration(serverConfig.Timeout)
|
||||
|
||||
+2
-8
@@ -285,10 +285,7 @@ func (dEnv *DoltEnv) initDBAndStateWithTime(ctx context.Context, nbf *types.Noms
|
||||
}
|
||||
|
||||
func (dEnv *DoltEnv) WorkingRoot(ctx context.Context) (*doltdb.RootValue, error) {
|
||||
hashStr := dEnv.RepoState.Working
|
||||
h := hash.Parse(hashStr)
|
||||
|
||||
return dEnv.DoltDB.ReadRootValue(ctx, h)
|
||||
return dEnv.DoltDB.ReadRootValue(ctx, dEnv.RepoState.WorkingHash())
|
||||
}
|
||||
|
||||
func (dEnv *DoltEnv) UpdateWorkingRoot(ctx context.Context, newRoot *doltdb.RootValue) error {
|
||||
@@ -320,10 +317,7 @@ func (dEnv *DoltEnv) HeadRoot(ctx context.Context) (*doltdb.RootValue, error) {
|
||||
}
|
||||
|
||||
func (dEnv *DoltEnv) StagedRoot(ctx context.Context) (*doltdb.RootValue, error) {
|
||||
hashStr := dEnv.RepoState.Staged
|
||||
h := hash.Parse(hashStr)
|
||||
|
||||
return dEnv.DoltDB.ReadRootValue(ctx, h)
|
||||
return dEnv.DoltDB.ReadRootValue(ctx, dEnv.RepoState.StagedHash())
|
||||
}
|
||||
|
||||
func (dEnv *DoltEnv) UpdateStagedRoot(ctx context.Context, newRoot *doltdb.RootValue) (hash.Hash, error) {
|
||||
|
||||
+8
@@ -134,3 +134,11 @@ func (rs *RepoState) AddRemote(r Remote) {
|
||||
|
||||
rs.Remotes[r.Name] = r
|
||||
}
|
||||
|
||||
func (rs *RepoState) WorkingHash() hash.Hash {
|
||||
return hash.Parse(rs.Working)
|
||||
}
|
||||
|
||||
func (rs *RepoState) StagedHash() hash.Hash {
|
||||
return hash.Parse(rs.Staged)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import (
|
||||
// Runs the query given and returns the result. The schema result of the query's execution is currently ignored, and
|
||||
// the targetSchema given is used to prepare all rows.
|
||||
func executeSelect(ctx context.Context, dEnv *env.DoltEnv, targetSch schema.Schema, root *doltdb.RootValue, query string) ([]row.Row, schema.Schema, error) {
|
||||
db := NewDatabase("dolt", root, dEnv)
|
||||
db := NewDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine := sqle.NewDefault()
|
||||
engine.AddDatabase(db)
|
||||
engine.Catalog.RegisterIndexDriver(&DoltIndexDriver{db})
|
||||
@@ -69,7 +69,7 @@ func executeSelect(ctx context.Context, dEnv *env.DoltEnv, targetSch schema.Sche
|
||||
|
||||
// Runs the query given and returns the error (if any).
|
||||
func executeModify(ctx context.Context, root *doltdb.RootValue, query string) (*doltdb.RootValue, error) {
|
||||
db := NewDatabase("dolt", root, nil)
|
||||
db := NewDatabase("dolt", root, nil, nil)
|
||||
engine := sqle.NewDefault()
|
||||
engine.AddDatabase(db)
|
||||
engine.Init()
|
||||
|
||||
@@ -40,17 +40,19 @@ const (
|
||||
type Database struct {
|
||||
name string
|
||||
root *doltdb.RootValue
|
||||
dEnv *env.DoltEnv
|
||||
ddb *doltdb.DoltDB
|
||||
rs *env.RepoState
|
||||
batchMode batchMode
|
||||
tables map[string]*DoltTable
|
||||
}
|
||||
|
||||
// NewDatabase returns a new dolt database to use in queries.
|
||||
func NewDatabase(name string, root *doltdb.RootValue, dEnv *env.DoltEnv) *Database {
|
||||
func NewDatabase(name string, root *doltdb.RootValue, ddb *doltdb.DoltDB, rs *env.RepoState) *Database {
|
||||
return &Database{
|
||||
name: name,
|
||||
root: root,
|
||||
dEnv: dEnv,
|
||||
ddb: ddb,
|
||||
rs: rs,
|
||||
batchMode: single,
|
||||
tables: make(map[string]*DoltTable),
|
||||
}
|
||||
@@ -58,11 +60,12 @@ func NewDatabase(name string, root *doltdb.RootValue, dEnv *env.DoltEnv) *Databa
|
||||
|
||||
// NewBatchedDatabase returns a new dolt database executing in batch insert mode. Integrators must call Flush() to
|
||||
// commit any outstanding edits.
|
||||
func NewBatchedDatabase(name string, root *doltdb.RootValue, dEnv *env.DoltEnv) *Database {
|
||||
func NewBatchedDatabase(name string, root *doltdb.RootValue, ddb *doltdb.DoltDB, rs *env.RepoState) *Database {
|
||||
return &Database{
|
||||
name: name,
|
||||
root: root,
|
||||
dEnv: dEnv,
|
||||
ddb: ddb,
|
||||
rs: rs,
|
||||
batchMode: batched,
|
||||
tables: make(map[string]*DoltTable),
|
||||
}
|
||||
@@ -77,7 +80,7 @@ func (db *Database) GetTableInsensitive(ctx context.Context, tblName string) (sq
|
||||
lwrName := strings.ToLower(tblName)
|
||||
if strings.HasPrefix(lwrName, DoltDiffTablePrefix) {
|
||||
tblName = tblName[len(DoltDiffTablePrefix):]
|
||||
dt, err := NewDiffTable(tblName, db.dEnv)
|
||||
dt, err := NewDiffTable(tblName, db.ddb, db.rs)
|
||||
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
@@ -87,7 +90,7 @@ func (db *Database) GetTableInsensitive(ctx context.Context, tblName string) (sq
|
||||
}
|
||||
|
||||
if lwrName == LogTableName {
|
||||
return NewLogTable(db.dEnv), true, nil
|
||||
return NewLogTable(db.ddb, db.rs), true, nil
|
||||
}
|
||||
|
||||
tableNames, err := db.root.GetTableNames(ctx)
|
||||
|
||||
@@ -39,7 +39,8 @@ var _ sql.FilteredTable = (*DiffTable)(nil)
|
||||
|
||||
type DiffTable struct {
|
||||
name string
|
||||
dEnv *env.DoltEnv
|
||||
ddb *doltdb.DoltDB
|
||||
rs *env.RepoState
|
||||
ss rowconv.SuperSchema
|
||||
joiner *rowconv.Joiner
|
||||
fromRoot *doltdb.RootValue
|
||||
@@ -49,10 +50,10 @@ type DiffTable struct {
|
||||
filters []sql.Expression
|
||||
}
|
||||
|
||||
func NewDiffTable(name string, dEnv *env.DoltEnv) (*DiffTable, error) {
|
||||
func NewDiffTable(name string, ddb *doltdb.DoltDB, rs *env.RepoState) (*DiffTable, error) {
|
||||
ctx := context.TODO()
|
||||
ssg := rowconv.NewSuperSchemaGen()
|
||||
err := ssg.AddHistoryOfTable(ctx, name, dEnv.DoltDB)
|
||||
err := ssg.AddHistoryOfTable(ctx, name, ddb)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -77,19 +78,19 @@ func NewDiffTable(name string, dEnv *env.DoltEnv) (*DiffTable, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
root1, err := dEnv.WorkingRoot(ctx)
|
||||
root1, err := ddb.ReadRootValue(ctx, rs.WorkingHash())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
root2, err := dEnv.StagedRoot(ctx)
|
||||
root2, err := ddb.ReadRootValue(ctx, rs.StagedHash())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DiffTable{name, dEnv, ss, j, root2, root1, "current", "HEAD", nil}, nil
|
||||
return &DiffTable{name, ddb,rs, ss, j, root2, root1, "current", "HEAD", nil}, nil
|
||||
}
|
||||
|
||||
func (dt *DiffTable) Name() string {
|
||||
@@ -151,13 +152,13 @@ func tableData(ctx *sql.Context, root *doltdb.RootValue, tblName string, ddb *do
|
||||
}
|
||||
|
||||
func (dt *DiffTable) PartitionRows(ctx *sql.Context, part sql.Partition) (sql.RowIter, error) {
|
||||
fromData, fromSch, err := tableData(ctx, dt.fromRoot, dt.name, dt.dEnv.DoltDB)
|
||||
fromData, fromSch, err := tableData(ctx, dt.fromRoot, dt.name, dt.ddb)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
toData, toSch, err := tableData(ctx, dt.toRoot, dt.name, dt.dEnv.DoltDB)
|
||||
toData, toSch, err := tableData(ctx, dt.toRoot, dt.name, dt.ddb)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -300,13 +301,13 @@ func (dt *DiffTable) WithFilters(filters []sql.Expression) sql.Table {
|
||||
|
||||
value = strings.Trim(value, " \t\n\r\"")
|
||||
|
||||
cs, err := doltdb.NewCommitSpec(value, dt.dEnv.RepoState.Head.Ref.String())
|
||||
cs, err := doltdb.NewCommitSpec(value, dt.rs.Head.Ref.String())
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cm, err := dt.dEnv.DoltDB.Resolve(ctx, cs)
|
||||
cm, err := dt.ddb.Resolve(ctx, cs)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -33,12 +33,13 @@ var _ sql.Table = (*LogTable)(nil)
|
||||
|
||||
// LogTable is a sql.Table implementation that implements a system table which shows the dolt commit log
|
||||
type LogTable struct {
|
||||
dEnv *env.DoltEnv
|
||||
ddb *doltdb.DoltDB
|
||||
rs *env.RepoState
|
||||
}
|
||||
|
||||
// NewLogTable creates a LogTable
|
||||
func NewLogTable(dEnv *env.DoltEnv) *LogTable {
|
||||
return &LogTable{dEnv: dEnv}
|
||||
func NewLogTable(ddb *doltdb.DoltDB, rs *env.RepoState) *LogTable {
|
||||
return &LogTable{ddb: ddb, rs: rs}
|
||||
}
|
||||
|
||||
// Name is a sql.Table interface function which returns the name of the table which is defined by the constant
|
||||
@@ -71,7 +72,7 @@ func (dt *LogTable) Partitions(*sql.Context) (sql.PartitionIter, error) {
|
||||
|
||||
// PartitionRows is a sql.Table interface function that gets a row iterator for a partition
|
||||
func (dt *LogTable) PartitionRows(sqlCtx *sql.Context, part sql.Partition) (sql.RowIter, error) {
|
||||
return NewLogItr(sqlCtx, dt.dEnv)
|
||||
return NewLogItr(sqlCtx, dt.ddb, dt.rs)
|
||||
}
|
||||
|
||||
// LogItr is a sql.RowItr implementation which iterates over each commit as if it's a row in the table.
|
||||
@@ -81,10 +82,8 @@ type LogItr struct {
|
||||
}
|
||||
|
||||
// NewLogItr creates a LogItr from the current environment.
|
||||
func NewLogItr(sqlCtx *sql.Context, dEnv *env.DoltEnv) (*LogItr, error) {
|
||||
ddb := dEnv.DoltDB
|
||||
|
||||
cs, err := doltdb.NewCommitSpec("HEAD", dEnv.RepoState.Head.Ref.GetPath())
|
||||
func NewLogItr(sqlCtx *sql.Context, ddb *doltdb.DoltDB, rs *env.RepoState) (*LogItr, error) {
|
||||
cs, err := doltdb.NewCommitSpec("HEAD", rs.Head.Ref.GetPath())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -264,7 +264,7 @@ func resetEnv(root *doltdb.RootValue) *doltdb.RootValue {
|
||||
}
|
||||
|
||||
func sqlNewEngine(root *doltdb.RootValue) *sqle.Engine {
|
||||
db := dsql.NewDatabase("dolt", root, nil)
|
||||
db := dsql.NewDatabase("dolt", root, nil, nil)
|
||||
engine := sqle.NewDefault()
|
||||
engine.AddDatabase(db)
|
||||
return engine
|
||||
|
||||
@@ -66,7 +66,7 @@ func TestSqlBatchInserts(t *testing.T) {
|
||||
root, _ := dEnv.WorkingRoot(ctx)
|
||||
|
||||
engine := sqle.NewDefault()
|
||||
db := NewBatchedDatabase("dolt", root, dEnv)
|
||||
db := NewBatchedDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine.AddDatabase(db)
|
||||
|
||||
for _, stmt := range insertStatements {
|
||||
@@ -153,7 +153,7 @@ func TestSqlBatchInsertIgnoreReplace(t *testing.T) {
|
||||
root, _ := dEnv.WorkingRoot(ctx)
|
||||
|
||||
engine := sqle.NewDefault()
|
||||
db := NewBatchedDatabase("dolt", root, dEnv)
|
||||
db := NewBatchedDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine.AddDatabase(db)
|
||||
|
||||
for _, stmt := range insertStatements {
|
||||
@@ -191,7 +191,7 @@ func TestSqlBatchInsertErrors(t *testing.T) {
|
||||
root, _ := dEnv.WorkingRoot(ctx)
|
||||
|
||||
engine := sqle.NewDefault()
|
||||
db := NewBatchedDatabase("dolt", root, dEnv)
|
||||
db := NewBatchedDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine.AddDatabase(db)
|
||||
|
||||
_, rowIter, err := engine.Query(sql.NewEmptyContext(), `insert into people (id, first, last, is_married, age, rating, uuid, num_episodes) values
|
||||
|
||||
@@ -157,7 +157,7 @@ func TestTableEditor(t *testing.T) {
|
||||
|
||||
ctx := sql.NewEmptyContext()
|
||||
root, _ := dEnv.WorkingRoot(context.Background())
|
||||
db := NewDatabase("dolt", root, dEnv)
|
||||
db := NewDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
peopleTable, _, err := db.GetTableInsensitive(ctx, "people")
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import (
|
||||
// root, or an error. Statements in the input string are split by `;\n`
|
||||
func ExecuteSql(dEnv *env.DoltEnv, root *doltdb.RootValue, statements string) (*doltdb.RootValue, error) {
|
||||
engine := sqle.NewDefault()
|
||||
db := NewBatchedDatabase("dolt", root, dEnv)
|
||||
db := NewBatchedDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine.AddDatabase(db)
|
||||
|
||||
for _, query := range strings.Split(statements, ";\n") {
|
||||
@@ -113,7 +113,7 @@ func sqlDDL(db *Database, engine *sqle.Engine, dEnv *env.DoltEnv, query string)
|
||||
// Executes the select statement given and returns the resulting rows, or an error if one is encountered.
|
||||
// This uses the index functionality, which is not ready for prime time. Use with caution.
|
||||
func ExecuteSelect(root *doltdb.RootValue, query string) ([]sql.Row, error) {
|
||||
db := NewDatabase("dolt", root, nil)
|
||||
db := NewDatabase("dolt", root, nil, nil)
|
||||
engine := sqle.NewDefault()
|
||||
engine.AddDatabase(db)
|
||||
engine.Catalog.RegisterIndexDriver(NewDoltIndexDriver(db))
|
||||
|
||||
@@ -124,7 +124,7 @@ func roundTrip(t *testing.T, originalValue string, sqlType sql.Type, conn *dbr.C
|
||||
|
||||
// runQuery runs the given query and returns a new root value
|
||||
func runQuery(root *doltdb.RootValue, dEnv *env.DoltEnv, query string) (*doltdb.RootValue, error) {
|
||||
db := sqle.NewDatabase("dolt", root, dEnv)
|
||||
db := sqle.NewDatabase("dolt", root, dEnv.DoltDB, dEnv.RepoState)
|
||||
engine := sqlServer.NewDefault()
|
||||
engine.AddDatabase(db)
|
||||
_ = engine.Init()
|
||||
|
||||
Reference in New Issue
Block a user