github.com/aacfactory/fns-contrib/databases/sql@v1.2.84/dac/table.go (about)

     1  package dac
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  type TableInfoOptions struct {
     8  	schema    string
     9  	conflicts []string
    10  }
    11  
    12  type TableInfoOption func(options *TableInfoOptions)
    13  
    14  func Schema(schema string) TableInfoOption {
    15  	return func(options *TableInfoOptions) {
    16  		options.schema = strings.TrimSpace(schema)
    17  	}
    18  }
    19  
    20  // Conflicts
    21  // params are field not column
    22  func Conflicts(conflicts ...string) TableInfoOption {
    23  	return func(options *TableInfoOptions) {
    24  		for _, conflict := range conflicts {
    25  			conflict = strings.TrimSpace(conflict)
    26  			if conflict == "" {
    27  				continue
    28  			}
    29  			options.conflicts = append(options.conflicts, conflict)
    30  		}
    31  	}
    32  }
    33  
    34  func Info(name string, options ...TableInfoOption) TableInfo {
    35  	opt := TableInfoOptions{}
    36  	for _, option := range options {
    37  		option(&opt)
    38  	}
    39  	return TableInfo{
    40  		name:      strings.TrimSpace(name),
    41  		schema:    opt.schema,
    42  		conflicts: opt.conflicts,
    43  	}
    44  }
    45  
    46  type TableInfo struct {
    47  	name      string
    48  	schema    string
    49  	conflicts []string
    50  }
    51  
    52  func (info TableInfo) Schema() string {
    53  	return info.schema
    54  }
    55  
    56  func (info TableInfo) Name() string {
    57  	return info.name
    58  }
    59  
    60  func (info TableInfo) Conflicts() []string {
    61  	return info.conflicts
    62  }
    63  
    64  // Table
    65  // the recv of TableInfo method must be value, can not be ptr
    66  type Table interface {
    67  	TableInfo() TableInfo
    68  }
    69  
    70  type ViewInfo struct {
    71  	pure   bool
    72  	name   string
    73  	schema string
    74  	base   Table
    75  }
    76  
    77  func (info ViewInfo) Pure() (string, string, bool) {
    78  	return info.schema, info.name, info.pure
    79  }
    80  
    81  func (info ViewInfo) Base() Table {
    82  	return info.base
    83  }
    84  
    85  func TableView(table Table) ViewInfo {
    86  	return ViewInfo{
    87  		pure:   false,
    88  		name:   "",
    89  		schema: "",
    90  		base:   table,
    91  	}
    92  }
    93  
    94  func PureView(name string, schema ...string) ViewInfo {
    95  	s := ""
    96  	if len(schema) > 0 {
    97  		s = schema[0]
    98  	}
    99  	return ViewInfo{
   100  		pure:   true,
   101  		name:   strings.TrimSpace(name),
   102  		schema: s,
   103  		base:   nil,
   104  	}
   105  }
   106  
   107  type View interface {
   108  	ViewInfo() ViewInfo
   109  }