/
catalog.go
49 lines (40 loc) · 1000 Bytes
/
catalog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package sql
import (
"fmt"
)
// Catalog holds databases, tables and functions.
type Catalog struct {
Databases
FunctionRegistry
}
// NewCatalog returns a new empty Catalog.
func NewCatalog() *Catalog {
return &Catalog{
Databases: Databases{},
FunctionRegistry: NewFunctionRegistry(),
}
}
// Databases is a collection of Database.
type Databases []Database
// Database returns the Database with the given name if it exists.
func (d Databases) Database(name string) (Database, error) {
for _, db := range d {
if db.Name() == name {
return db, nil
}
}
return nil, fmt.Errorf("database not found: %s", name)
}
// Table returns the Table with the given name if it exists.
func (d Databases) Table(dbName string, tableName string) (Table, error) {
db, err := d.Database(dbName)
if err != nil {
return nil, err
}
tables := db.Tables()
table, found := tables[tableName]
if !found {
return nil, fmt.Errorf("table not found: %s", tableName)
}
return table, nil
}