Skip to content

Commit

Permalink
Merge pull request #4021 from dolthub/fulghum/dolt_clone
Browse files Browse the repository at this point in the history
Refactoring Dolt's use of `ExternalStoredProcedureDatabase` to `ExternalStoredProcedureProvider`
  • Loading branch information
fulghum authored Aug 11, 2022
2 parents b274ff5 + 81ce76c commit 2a14823
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
4 changes: 2 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20220809164912-4bb30c793f1b
github.com/dolthub/vitess v0.0.0-20220810172629-f4bdb4a721c0
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.13.0
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568
Expand Down Expand Up @@ -58,7 +58,7 @@ require (
)

require (
github.com/dolthub/go-mysql-server v0.12.1-0.20220809165815-50f64185343e
github.com/dolthub/go-mysql-server v0.12.1-0.20220810223941-32d5ccc8ebd4
github.com/google/flatbuffers v2.0.6+incompatible
github.com/gosuri/uilive v0.0.4
github.com/kch42/buzhash v0.0.0-20160816060738-9bdec3dec7c6
Expand Down
8 changes: 4 additions & 4 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-mysql-server v0.12.1-0.20220809165815-50f64185343e h1:R3KwWZAz3F2+GFFnlwWVA0AwfmG89snvk2C1HhQfk50=
github.com/dolthub/go-mysql-server v0.12.1-0.20220809165815-50f64185343e/go.mod h1:w3cXKwms994cgMN7+rJr4kRzdzpPYkz6Vjl7CAnlpYE=
github.com/dolthub/go-mysql-server v0.12.1-0.20220810223941-32d5ccc8ebd4 h1:lKQTRSTmv8q5w17iVij6ndf/kNRBSdnySwQpgSxe/rU=
github.com/dolthub/go-mysql-server v0.12.1-0.20220810223941-32d5ccc8ebd4/go.mod h1:1yIaqEcKyoKqR/XeOUtaCUqqo6FPdMMqqQKiJLWNTl0=
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371 h1:oyPHJlzumKta1vnOQqUnfdz+pk3EmnHS3Nd0cCT0I2g=
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371/go.mod h1:dhGBqcCEfK5kuFmeO5+WOx3hqc1k3M29c1oS/R7N4ms=
github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474 h1:xTrR+l5l+1Lfq0NvhiEsctylXinUMFhhsqaEcl414p8=
Expand All @@ -183,8 +183,8 @@ github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66 h1:WRPDbpJWEnPxP
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66/go.mod h1:N5ZIbMGuDUpTpOFQ7HcsN6WSIpTGQjHP+Mz27AfmAgk=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
github.com/dolthub/vitess v0.0.0-20220809164912-4bb30c793f1b h1:KDimfMuvlB0be3XhBBBF5QWIJGzazleccRlHyf/dXX8=
github.com/dolthub/vitess v0.0.0-20220809164912-4bb30c793f1b/go.mod h1:oVFIBdqMFEkt4Xz2fzFJBNtzKhDEjwdCF0dzde39iKs=
github.com/dolthub/vitess v0.0.0-20220810172629-f4bdb4a721c0 h1:u8yIimnhg5CHQZlBoJxfV+yezvNEmtgXUsmZvvlJ6uo=
github.com/dolthub/vitess v0.0.0-20220810172629-f4bdb4a721c0/go.mod h1:oVFIBdqMFEkt4Xz2fzFJBNtzKhDEjwdCF0dzde39iKs=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
7 changes: 0 additions & 7 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/env/actions/commitwalk"
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dprocedures"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dtables"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate"
Expand Down Expand Up @@ -161,7 +160,6 @@ var _ sql.TemporaryTableCreator = Database{}
var _ sql.TableRenamer = Database{}
var _ sql.TriggerDatabase = Database{}
var _ sql.StoredProcedureDatabase = Database{}
var _ sql.ExternalStoredProcedureDatabase = Database{}
var _ sql.TransactionDatabase = Database{}
var _ globalstate.StateProvider = Database{}

Expand Down Expand Up @@ -1094,11 +1092,6 @@ func (db Database) DropStoredProcedure(ctx *sql.Context, name string) error {
return DoltProceduresDropProcedure(ctx, db, name)
}

// GetExternalStoredProcedures implements sql.ExternalStoredProcedureDatabase.
func (db Database) GetExternalStoredProcedures(ctx *sql.Context) ([]sql.ExternalStoredProcedureDetails, error) {
return dprocedures.DoltProcedures, nil
}

func (db Database) addFragToSchemasTable(ctx *sql.Context, fragType, name, definition string, created time.Time, existingErr error) (err error) {
tbl, err := GetOrCreateDoltSchemasTable(ctx, db)
if err != nil {
Expand Down
50 changes: 35 additions & 15 deletions go/libraries/doltcore/sqle/database_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/dolt/go/libraries/doltcore/remotestorage"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dfunctions"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dprocedures"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/libraries/utils/earl"
Expand All @@ -43,10 +44,11 @@ const (

type DoltDatabaseProvider struct {
// dbLocations maps a database name to its file system root
dbLocations map[string]filesys.Filesys
databases map[string]sql.Database
functions map[string]sql.Function
mu *sync.RWMutex
dbLocations map[string]filesys.Filesys
databases map[string]sql.Database
functions map[string]sql.Function
externalProcedures sql.ExternalStoredProcedureRegistry
mu *sync.RWMutex

defaultBranch string
fs filesys.Filesys
Expand All @@ -58,6 +60,8 @@ type DoltDatabaseProvider struct {
var _ sql.DatabaseProvider = (*DoltDatabaseProvider)(nil)
var _ sql.FunctionProvider = (*DoltDatabaseProvider)(nil)
var _ sql.MutableDatabaseProvider = (*DoltDatabaseProvider)(nil)
var _ sql.ExternalStoredProcedureProvider = (*DoltDatabaseProvider)(nil)
var _ sql.TableFunctionProvider = (*DoltDatabaseProvider)(nil)
var _ dsess.DoltDatabaseProvider = (*DoltDatabaseProvider)(nil)

// NewDoltDatabaseProvider returns a new provider, initialized without any databases, along with any
Expand Down Expand Up @@ -97,14 +101,20 @@ func NewDoltDatabaseProviderWithDatabases(defaultBranch string, fs filesys.Files
funcs[strings.ToLower(fn.FunctionName())] = fn
}

externalProcedures := sql.NewExternalStoredProcedureRegistry()
for _, esp := range dprocedures.DoltProcedures {
externalProcedures.Register(esp)
}

return DoltDatabaseProvider{
dbLocations: dbLocations,
databases: dbs,
functions: funcs,
mu: &sync.RWMutex{},
fs: fs,
defaultBranch: defaultBranch,
dbFactoryUrl: doltdb.LocalDirDoltDB,
dbLocations: dbLocations,
databases: dbs,
functions: funcs,
externalProcedures: externalProcedures,
mu: &sync.RWMutex{},
fs: fs,
defaultBranch: defaultBranch,
dbFactoryUrl: doltdb.LocalDirDoltDB,
}, nil
}

Expand Down Expand Up @@ -360,7 +370,7 @@ func createRemote(ctx *sql.Context, remoteName, remoteUrl string, params map[str
return r, ddb, nil
}

func (p DoltDatabaseProvider) DropDatabase(ctx *sql.Context, name string) error {
func (p DoltDatabaseProvider) DropDatabase(_ *sql.Context, name string) error {
p.mu.Lock()
defer p.mu.Unlock()

Expand Down Expand Up @@ -512,7 +522,7 @@ func (p DoltDatabaseProvider) RevisionDbState(ctx *sql.Context, revDB string) (d
}

// DropRevisionDb implements RevisionDatabaseProvider
func (p DoltDatabaseProvider) DropRevisionDb(ctx *sql.Context, revDB string) error {
func (p DoltDatabaseProvider) DropRevisionDb(_ *sql.Context, revDB string) error {
p.mu.Lock()
defer p.mu.Unlock()

Expand All @@ -539,8 +549,18 @@ func (p DoltDatabaseProvider) Function(_ *sql.Context, name string) (sql.Functio
return fn, nil
}

// TableFunction implements the TableFunctionProvider interface
func (p DoltDatabaseProvider) TableFunction(ctx *sql.Context, name string) (sql.TableFunction, error) {
// ExternalStoredProcedure implements the sql.ExternalStoredProcedureProvider interface
func (p DoltDatabaseProvider) ExternalStoredProcedure(_ *sql.Context, name string, numOfParams int) (*sql.ExternalStoredProcedureDetails, error) {
return p.externalProcedures.LookupByNameAndParamCount(name, numOfParams)
}

// ExternalStoredProcedures implements the sql.ExternalStoredProcedureProvider interface
func (p DoltDatabaseProvider) ExternalStoredProcedures(_ *sql.Context, name string) ([]sql.ExternalStoredProcedureDetails, error) {
return p.externalProcedures.LookupByName(name)
}

// TableFunction implements the sql.TableFunctionProvider interface
func (p DoltDatabaseProvider) TableFunction(_ *sql.Context, name string) (sql.TableFunction, error) {
// currently, only one table function is supported, if we extend this, we should clean this up
// and store table functions in a map, similar to regular functions.
if strings.ToLower(name) == "dolt_diff" {
Expand Down
3 changes: 3 additions & 0 deletions go/libraries/doltcore/sqle/dprocedures/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ var DoltProcedures = []sql.ExternalStoredProcedureDetails{
{Name: "dolt_revert", Schema: int64Schema("status"), Function: doltRevert},
{Name: "dolt_tag", Schema: int64Schema("status"), Function: doltTag},
{Name: "dolt_verify_constraints", Schema: int64Schema("violations"), Function: doltVerifyConstraints},

// Dolt stored procedure aliases
{Name: "dadd", Schema: int64Schema("status"), Function: doltAdd},
{Name: "dbranch", Schema: int64Schema("status"), Function: doltBranch},
{Name: "dcheckout", Schema: int64Schema("status"), Function: doltCheckout},
{Name: "dclean", Schema: int64Schema("status"), Function: doltClean},
{Name: "dclone", Schema: int64Schema("status"), Function: doltClone},
{Name: "dcommit", Schema: stringSchema("hash"), Function: doltCommit},
{Name: "dfetch", Schema: int64Schema("success"), Function: doltFetch},
{Name: "dmerge", Schema: int64Schema("fast_forward", "conflicts"), Function: doltMerge},
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/enginetest/dolt_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ var DoltScripts = []queries.ScriptTest{
{
"dolt_checkout",
"",
"CREATE PROCEDURE dolt_checkout() SELECT 'External stored procedure defined by mydb';",
"CREATE PROCEDURE dolt_checkout() SELECT 'External stored procedure';",
"utf8mb4",
"utf8mb4_0900_bin",
"utf8mb4_0900_bin",
Expand Down

0 comments on commit 2a14823

Please sign in to comment.