github.com/dolthub/go-mysql-server@v0.18.0/driver/conn.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package driver
    16  
    17  import (
    18  	"context"
    19  	"database/sql/driver"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  )
    23  
    24  // Conn is a connection to a database.
    25  type Conn struct {
    26  	dsn      string
    27  	options  *Options
    28  	dbConn   *dbConn
    29  	session  sql.Session
    30  	contexts ContextBuilder
    31  	indexes  *sql.IndexRegistry
    32  	views    *sql.ViewRegistry
    33  }
    34  
    35  // DSN returns the driver connection string.
    36  func (c *Conn) DSN() string { return c.dsn }
    37  
    38  // Session returns the SQL session.
    39  func (c *Conn) Session() sql.Session { return c.session }
    40  
    41  // Prepare validates the query and returns a statement.
    42  func (c *Conn) Prepare(query string) (driver.Stmt, error) {
    43  	return c.newStmt(context.Background(), query)
    44  }
    45  
    46  // newStmt builds a new statement with the query.
    47  func (c *Conn) newStmt(ctx context.Context, query string) (*Stmt, error) {
    48  	sctx, err := c.newContextWithQuery(ctx, query)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	// validate the query
    54  	_, err = c.dbConn.engine.PrepareQuery(sctx, query)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return &Stmt{c, query}, nil
    60  }
    61  
    62  // Close does nothing.
    63  func (c *Conn) Close() error {
    64  	return nil
    65  }
    66  
    67  // Begin returns a fake transaction.
    68  func (c *Conn) Begin() (driver.Tx, error) {
    69  	return fakeTransaction{}, nil
    70  }
    71  
    72  // Exec executes a query that doesn't return rows.
    73  func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error) {
    74  	stmt, err := c.newStmt(context.Background(), query)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	defer stmt.Close()
    79  
    80  	rows, err := stmt.Exec(args)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return rows, nil
    86  }
    87  
    88  // ExecContext executes a query that doesn't return rows.
    89  func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
    90  	stmt, err := c.newStmt(ctx, query)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	defer stmt.Close()
    95  
    96  	res, err := stmt.ExecContext(ctx, args)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	return res, nil
   102  }
   103  
   104  // Query executes a query that may return rows.
   105  func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) {
   106  	stmt, err := c.newStmt(context.Background(), query)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	defer stmt.Close()
   111  
   112  	rows, err := stmt.Query(args)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return rows, nil
   118  }
   119  
   120  // QueryContext executes a query that may return rows.
   121  func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
   122  	stmt, err := c.newStmt(ctx, query)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	defer stmt.Close()
   127  
   128  	rows, err := stmt.QueryContext(ctx, args)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	return rows, nil
   134  }
   135  
   136  func (c *Conn) newContextWithQuery(ctx context.Context, query string) (*sql.Context, error) {
   137  	// TODO(cjs): parse the dsn at c.dsn and set sql.WithInitialDatabase(parseDbName(c.dsn)) ?
   138  	return c.contexts.NewContext(ctx, c,
   139  		sql.WithSession(c.session),
   140  		sql.WithQuery(query),
   141  		sql.WithPid(c.dbConn.nextProcessID()),
   142  		sql.WithMemoryManager(c.dbConn.engine.MemoryManager),
   143  		sql.WithProcessList(c.dbConn.engine.ProcessList))
   144  }
   145  
   146  type fakeTransaction struct{}
   147  
   148  func (fakeTransaction) Commit() error   { return nil }
   149  func (fakeTransaction) Rollback() error { return nil }
   150  
   151  // _ is a type assertion
   152  var (
   153  	_ driver.Conn           = ((*Conn)(nil))
   154  	_ driver.Execer         = ((*Conn)(nil))
   155  	_ driver.ExecerContext  = ((*Conn)(nil))
   156  	_ driver.Queryer        = ((*Conn)(nil))
   157  	_ driver.QueryerContext = ((*Conn)(nil))
   158  )