github.com/dolthub/go-mysql-server@v0.18.0/sql/in_mem_table/editors.go (about)

     1  // Copyright 2023 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 in_mem_table
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  )
    22  
    23  var _ sql.TableEditor = StatementLockingTableEditor{}
    24  
    25  type StatementLockingTableEditor struct {
    26  	L sync.Locker
    27  	E sql.TableEditor
    28  }
    29  
    30  // StatementBegin implements the sql.TableEditor interface.
    31  func (e StatementLockingTableEditor) StatementBegin(ctx *sql.Context) {
    32  	e.L.Lock()
    33  }
    34  
    35  // DiscardChanges implements the sql.TableEditor interface.
    36  func (e StatementLockingTableEditor) DiscardChanges(ctx *sql.Context, errorEncountered error) error {
    37  	defer e.L.Unlock()
    38  	return e.E.DiscardChanges(ctx, errorEncountered)
    39  }
    40  
    41  // StatementComplete implements the sql.TableEditor interface.
    42  func (e StatementLockingTableEditor) StatementComplete(ctx *sql.Context) error {
    43  	defer e.L.Unlock()
    44  	return e.E.StatementComplete(ctx)
    45  }
    46  
    47  // Insert implements the sql.RowInserter interface.
    48  func (e StatementLockingTableEditor) Insert(ctx *sql.Context, row sql.Row) error {
    49  	return e.E.Insert(ctx, row)
    50  }
    51  
    52  // Update implements the sql.RowUpdater interface.
    53  func (e StatementLockingTableEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row) error {
    54  	return e.E.Update(ctx, old, new)
    55  }
    56  
    57  // Delete implements the sql.RowDeleter interface.
    58  func (e StatementLockingTableEditor) Delete(ctx *sql.Context, row sql.Row) error {
    59  	return e.E.Delete(ctx, row)
    60  }
    61  
    62  // Close implements the sql.Closer interface.
    63  func (e StatementLockingTableEditor) Close(ctx *sql.Context) error {
    64  	return e.E.Close(ctx)
    65  }
    66  
    67  type OperationLockingTableEditor struct {
    68  	L sync.Locker
    69  	E sql.TableEditor
    70  }
    71  
    72  // StatementBegin implements the sql.TableEditor interface.
    73  func (e OperationLockingTableEditor) StatementBegin(ctx *sql.Context) {
    74  	e.E.StatementBegin(ctx)
    75  }
    76  
    77  // DiscardChanges implements the sql.TableEditor interface.
    78  func (e OperationLockingTableEditor) DiscardChanges(ctx *sql.Context, errorEncountered error) error {
    79  	return e.E.DiscardChanges(ctx, errorEncountered)
    80  }
    81  
    82  // OperationComplete implements the sql.TableEditor interface.
    83  func (e OperationLockingTableEditor) StatementComplete(ctx *sql.Context) error {
    84  	return e.E.StatementComplete(ctx)
    85  }
    86  
    87  // Insert implements the sql.RowInserter interface.
    88  func (e OperationLockingTableEditor) Insert(ctx *sql.Context, row sql.Row) error {
    89  	e.L.Lock()
    90  	defer e.L.Unlock()
    91  	return e.E.Insert(ctx, row)
    92  }
    93  
    94  // Update implements the sql.RowUpdater interface.
    95  func (e OperationLockingTableEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row) error {
    96  	e.L.Lock()
    97  	defer e.L.Unlock()
    98  	return e.E.Update(ctx, old, new)
    99  }
   100  
   101  // Delete implements the sql.RowDeleter interface.
   102  func (e OperationLockingTableEditor) Delete(ctx *sql.Context, row sql.Row) error {
   103  	e.L.Lock()
   104  	defer e.L.Unlock()
   105  	return e.E.Delete(ctx, row)
   106  }
   107  
   108  // Close implements the sql.Closer interface.
   109  func (e OperationLockingTableEditor) Close(ctx *sql.Context) error {
   110  	return e.E.Close(ctx)
   111  }