forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
79 lines (65 loc) · 2.11 KB
/
conn.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed 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 driver
import (
"context"
"database/sql/driver"
"github.com/Sndav/go-mysql-server/sql"
)
// Conn is a connection to a database.
type Conn struct {
options Options
catalog *catalog
session sql.Session
contexts ContextBuilder
indexes *sql.IndexRegistry
views *sql.ViewRegistry
}
// Catalog returns the SQL catalog.
func (c *Conn) Catalog() *sql.Catalog { return c.catalog.engine.Catalog }
// Session returns the SQL session.
func (c *Conn) Session() sql.Session { return c.session }
// Prepare validates the query and returns a statement.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
ctx, err := c.newContextWithQuery(context.Background(), query)
if err != nil {
return nil, err
}
// validate the query
_, err = c.catalog.engine.AnalyzeQuery(ctx, query)
if err != nil {
return nil, err
}
return &Stmt{c, query}, nil
}
// Close does nothing.
func (c *Conn) Close() error {
return nil
}
// Begin returns a fake transaction.
func (c *Conn) Begin() (driver.Tx, error) {
return fakeTransaction{}, nil
}
func (c *Conn) newContextWithQuery(ctx context.Context, query string) (*sql.Context, error) {
return c.contexts.NewContext(ctx, c,
sql.WithSession(c.session),
sql.WithQuery(query),
sql.WithPid(c.catalog.nextProcessID()),
sql.WithMemoryManager(c.catalog.engine.Catalog.MemoryManager),
sql.WithIndexRegistry(c.indexes),
sql.WithViewRegistry(c.views))
}
type fakeTransaction struct{}
func (fakeTransaction) Commit() error { return nil }
func (fakeTransaction) Rollback() error { return nil }