zeroshade commented on code in PR #246: URL: https://github.com/apache/iceberg-go/pull/246#discussion_r1934571592
########## catalog/sql/sql.go: ########## @@ -0,0 +1,721 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package sql + +import ( + "context" + "database/sql" + "errors" + "fmt" + "maps" + "path" + "slices" + "strings" + "sync" + _ "unsafe" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/catalog" + "github.com/apache/iceberg-go/catalog/internal" + "github.com/apache/iceberg-go/io" + "github.com/apache/iceberg-go/table" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/feature" + "github.com/uptrace/bun/dialect/mssqldialect" + "github.com/uptrace/bun/dialect/mysqldialect" + "github.com/uptrace/bun/dialect/oracledialect" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/dialect/sqlitedialect" + "github.com/uptrace/bun/extra/bundebug" + "github.com/uptrace/bun/schema" +) + +type SupportedDialect string + +const ( + Postgres SupportedDialect = "postgres" + MySQL SupportedDialect = "mysql" + SQLite SupportedDialect = "sqlite" + MSSQL SupportedDialect = "mssql" + Oracle SupportedDialect = "oracle" +) + +const ( + DialectKey = "sql.dialect" + DriverKey = "sql.driver" + initCatalogTablesKey = "init_catalog_tables" +) + +func init() { + catalog.Register("sql", catalog.RegistrarFunc(func(name string, p iceberg.Properties) (c catalog.Catalog, err error) { + driver, ok := p[DriverKey] + if !ok { + return nil, errors.New("must provide driver to pass to sql.Open") + } + + dialect := strings.ToLower(p[DialectKey]) + if dialect == "" { + return nil, errors.New("must provide sql dialect to use") + } + + uri := strings.TrimPrefix(p.Get("uri", ""), "sql://") + sqldb, err := sql.Open(driver, uri) + if err != nil { + return nil, err + } + + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("failed to create SQL catalog: %v", r) + } + }() + + return NewCatalog(p.Get(name, "sql"), sqldb, SupportedDialect(dialect), p) + })) +} + +var ( + _ catalog.Catalog = (*Catalog)(nil) +) + +var ( + minimalNamespaceProps = iceberg.Properties{"exists": "true"} + + dialects = map[SupportedDialect]schema.Dialect{} + dialectMx sync.Mutex +) + +func createDialect(d SupportedDialect) schema.Dialect { + switch d { + case Postgres: + return pgdialect.New() + case MySQL: + return mysqldialect.New() + case SQLite: + return sqlitedialect.New() + case MSSQL: + return mssqldialect.New() + case Oracle: + return oracledialect.New() + default: + panic("unsupported sql dialect") + } +} + +func getDialect(d SupportedDialect) schema.Dialect { + dialectMx.Lock() + defer dialectMx.Unlock() + ret, ok := dialects[d] + if !ok { + ret = createDialect(d) + dialects[d] = ret + } + return ret +} + +type sqlIcebergTable struct { + bun.BaseModel `bun:"table:iceberg_tables"` + + CatalogName string `bun:",pk"` + TableNamespace string `bun:",pk"` + TableName string `bun:",pk"` + MetadataLocation sql.NullString + PreviousMetadataLocation sql.NullString +} + +type sqlIcebergNamespaceProps struct { + bun.BaseModel `bun:"table:iceberg_namespace_properties"` + + CatalogName string `bun:",pk"` + Namespace string `bun:",pk"` + PropertyKey string `bun:",pk"` + PropertyValue sql.NullString +} + +func withReadTx[R any](ctx context.Context, db *bun.DB, fn func(context.Context, bun.Tx) (R, error)) (result R, err error) { + db.RunInTx(ctx, &sql.TxOptions{ReadOnly: true}, func(ctx context.Context, tx bun.Tx) error { + result, err = fn(ctx, tx) + return err + }) + return +} + +func withWriteTx(ctx context.Context, db *bun.DB, fn func(context.Context, bun.Tx) error) error { + return db.RunInTx(ctx, &sql.TxOptions{Isolation: sql.LevelLinearizable}, func(ctx context.Context, tx bun.Tx) error { Review Comment: It's what made the most sense to me, unless you think I should just use `LevelDefault` and let the underlying DB decide? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org