forked from src-d/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
57 lines (46 loc) · 1.18 KB
/
database.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
50
51
52
53
54
55
56
package memory
import (
"github.com/src-d/go-mysql-server/sql"
)
// Database is an in-memory database.
type Database struct {
name string
tables map[string]sql.Table
}
// NewDatabase creates a new database with the given name.
func NewDatabase(name string) *Database {
return &Database{
name: name,
tables: map[string]sql.Table{},
}
}
// Name returns the database name.
func (d *Database) Name() string {
return d.name
}
// Tables returns all tables in the database.
func (d *Database) Tables() map[string]sql.Table {
return d.tables
}
// AddTable adds a new table to the database.
func (d *Database) AddTable(name string, t sql.Table) {
d.tables[name] = t
}
// CreateTable creates a table with the given name and schema
func (d *Database) CreateTable(ctx *sql.Context, name string, schema sql.Schema) error {
_, ok := d.tables[name]
if ok {
return sql.ErrTableAlreadyExists.New(name)
}
d.tables[name] = NewTable(name, schema)
return nil
}
// DropTable drops the table with the given name
func (d *Database) DropTable(ctx *sql.Context, name string) error {
_, ok := d.tables[name]
if !ok {
return sql.ErrTableNotFound.New(name)
}
delete(d.tables, name)
return nil
}