github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/base/base.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package base
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/uber-go/dosa"
    27  )
    28  
    29  const name = "base"
    30  
    31  // ErrNoMoreConnector is used when there is no more next connector
    32  type ErrNoMoreConnector struct {
    33  }
    34  
    35  // Error satisfies the error interface.
    36  func (e ErrNoMoreConnector) Error() string {
    37  	return "no more connectors"
    38  }
    39  
    40  // Connector always calls Next Connector in all the functions
    41  type Connector struct {
    42  	Next dosa.Connector
    43  }
    44  
    45  // NewConnector creates new base Connector
    46  func NewConnector(next dosa.Connector) dosa.Connector {
    47  	return &Connector{Next: next}
    48  }
    49  
    50  // CreateIfNotExists calls Next
    51  func (c *Connector) CreateIfNotExists(ctx context.Context, ei *dosa.EntityInfo, values map[string]dosa.FieldValue) error {
    52  	if c.Next == nil {
    53  		return ErrNoMoreConnector{}
    54  	}
    55  	return c.Next.CreateIfNotExists(ctx, ei, values)
    56  }
    57  
    58  // Read calls Next
    59  func (c *Connector) Read(ctx context.Context, ei *dosa.EntityInfo, values map[string]dosa.FieldValue, minimumFields []string) (map[string]dosa.FieldValue, error) {
    60  	if c.Next == nil {
    61  		return nil, ErrNoMoreConnector{}
    62  	}
    63  	return c.Next.Read(ctx, ei, values, minimumFields)
    64  }
    65  
    66  // MultiRead calls Next
    67  func (c *Connector) MultiRead(ctx context.Context, ei *dosa.EntityInfo, values []map[string]dosa.FieldValue, minimumFields []string) ([]*dosa.FieldValuesOrError, error) {
    68  	if c.Next == nil {
    69  		return nil, ErrNoMoreConnector{}
    70  	}
    71  	return c.Next.MultiRead(ctx, ei, values, minimumFields)
    72  }
    73  
    74  // Upsert calls Next
    75  func (c *Connector) Upsert(ctx context.Context, ei *dosa.EntityInfo, values map[string]dosa.FieldValue) error {
    76  	if c.Next == nil {
    77  		return ErrNoMoreConnector{}
    78  	}
    79  	return c.Next.Upsert(ctx, ei, values)
    80  }
    81  
    82  // MultiUpsert calls Next
    83  func (c *Connector) MultiUpsert(ctx context.Context, ei *dosa.EntityInfo, values []map[string]dosa.FieldValue) ([]error, error) {
    84  	if c.Next == nil {
    85  		return nil, ErrNoMoreConnector{}
    86  	}
    87  	return c.Next.MultiUpsert(ctx, ei, values)
    88  }
    89  
    90  // Remove calls Next
    91  func (c *Connector) Remove(ctx context.Context, ei *dosa.EntityInfo, values map[string]dosa.FieldValue) error {
    92  	if c.Next == nil {
    93  		return ErrNoMoreConnector{}
    94  	}
    95  	return c.Next.Remove(ctx, ei, values)
    96  }
    97  
    98  // RemoveRange calls Next.
    99  func (c *Connector) RemoveRange(ctx context.Context, ei *dosa.EntityInfo, columnConditions map[string][]*dosa.Condition) error {
   100  	if c.Next == nil {
   101  		return ErrNoMoreConnector{}
   102  	}
   103  	return c.Next.RemoveRange(ctx, ei, columnConditions)
   104  }
   105  
   106  // MultiRemove calls Next
   107  func (c *Connector) MultiRemove(ctx context.Context, ei *dosa.EntityInfo, multiValues []map[string]dosa.FieldValue) ([]error, error) {
   108  	if c.Next == nil {
   109  		return nil, ErrNoMoreConnector{}
   110  	}
   111  	return c.Next.MultiRemove(ctx, ei, multiValues)
   112  }
   113  
   114  // Range calls Next
   115  func (c *Connector) Range(ctx context.Context, ei *dosa.EntityInfo, columnConditions map[string][]*dosa.Condition, minimumFields []string, token string, limit int) ([]map[string]dosa.FieldValue, string, error) {
   116  	if c.Next == nil {
   117  		return nil, "", ErrNoMoreConnector{}
   118  	}
   119  	return c.Next.Range(ctx, ei, columnConditions, minimumFields, token, limit)
   120  }
   121  
   122  // Scan calls Next
   123  func (c *Connector) Scan(ctx context.Context, ei *dosa.EntityInfo, minimumFields []string, token string, limit int) ([]map[string]dosa.FieldValue, string, error) {
   124  	if c.Next == nil {
   125  		return nil, "", ErrNoMoreConnector{}
   126  	}
   127  	return c.Next.Scan(ctx, ei, minimumFields, token, limit)
   128  }
   129  
   130  // CheckSchema calls Next
   131  func (c *Connector) CheckSchema(ctx context.Context, scope, namePrefix string, ed []*dosa.EntityDefinition) (int32, error) {
   132  	if c.Next == nil {
   133  		return dosa.InvalidVersion, ErrNoMoreConnector{}
   134  	}
   135  	return c.Next.CheckSchema(ctx, scope, namePrefix, ed)
   136  }
   137  
   138  // UpsertSchema calls Next
   139  func (c *Connector) UpsertSchema(ctx context.Context, scope, namePrefix string, ed []*dosa.EntityDefinition) (*dosa.SchemaStatus, error) {
   140  	if c.Next == nil {
   141  		return nil, ErrNoMoreConnector{}
   142  	}
   143  	return c.Next.UpsertSchema(ctx, scope, namePrefix, ed)
   144  }
   145  
   146  // CheckSchemaStatus calls Next
   147  func (c *Connector) CheckSchemaStatus(ctx context.Context, scope string, namePrefix string, version int32) (*dosa.SchemaStatus, error) {
   148  	if c.Next == nil {
   149  		return nil, ErrNoMoreConnector{}
   150  	}
   151  	return c.Next.CheckSchemaStatus(ctx, scope, namePrefix, version)
   152  }
   153  
   154  // CreateScope calls Next
   155  func (c *Connector) CreateScope(ctx context.Context, scope string) error {
   156  	if c.Next == nil {
   157  		return ErrNoMoreConnector{}
   158  	}
   159  	return c.Next.CreateScope(ctx, scope)
   160  }
   161  
   162  // TruncateScope calls Next
   163  func (c *Connector) TruncateScope(ctx context.Context, scope string) error {
   164  	if c.Next == nil {
   165  		return ErrNoMoreConnector{}
   166  	}
   167  	return c.Next.TruncateScope(ctx, scope)
   168  }
   169  
   170  // DropScope calls Next
   171  func (c *Connector) DropScope(ctx context.Context, scope string) error {
   172  	if c.Next == nil {
   173  		return ErrNoMoreConnector{}
   174  	}
   175  	return c.Next.DropScope(ctx, scope)
   176  }
   177  
   178  // ScopeExists calls Next
   179  func (c *Connector) ScopeExists(ctx context.Context, scope string) (bool, error) {
   180  	if c.Next == nil {
   181  		return false, ErrNoMoreConnector{}
   182  	}
   183  	return c.Next.ScopeExists(ctx, scope)
   184  }
   185  
   186  // Shutdown always returns nil
   187  func (c *Connector) Shutdown() error {
   188  	if c.Next == nil {
   189  		return ErrNoMoreConnector{}
   190  	}
   191  	return c.Next.Shutdown()
   192  }
   193  
   194  // Name returns the name of the connector
   195  func Name() string {
   196  	return name
   197  }