github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/table.go (about)

     1  // Copyright 2021 ecodeclub
     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 eorm
    16  
    17  type TableReference interface {
    18  	getAlias() string
    19  }
    20  
    21  // Table 普通表
    22  type Table struct {
    23  	entity any
    24  	alias  string
    25  }
    26  
    27  // TableOf 创建一个 Table 代表一个表
    28  // alias 是指该表的别名
    29  // 例如 SELECT * FROM user_tab AS t1,t1 就是别名
    30  func TableOf(entity any, alias string) Table {
    31  	return Table{
    32  		entity: entity,
    33  		alias:  alias,
    34  	}
    35  }
    36  
    37  func (t Table) getAlias() string {
    38  	return t.alias
    39  }
    40  
    41  // Join 查询
    42  func (t Table) Join(right TableReference) *JoinBuilder {
    43  	return &JoinBuilder{
    44  		left:  t,
    45  		right: right,
    46  		typ:   "JOIN",
    47  	}
    48  }
    49  
    50  func (t Table) LeftJoin(right TableReference) *JoinBuilder {
    51  	return &JoinBuilder{
    52  		left:  t,
    53  		right: right,
    54  		typ:   "LEFT JOIN",
    55  	}
    56  }
    57  
    58  func (t Table) RightJoin(right TableReference) *JoinBuilder {
    59  	return &JoinBuilder{
    60  		left:  t,
    61  		right: right,
    62  		typ:   "RIGHT JOIN",
    63  	}
    64  }
    65  
    66  func (t Table) C(name string) Column {
    67  	return Column{
    68  		name:  name,
    69  		table: t,
    70  	}
    71  }
    72  
    73  // Max represents MAX
    74  func (t Table) Max(c string) Aggregate {
    75  	return Aggregate{
    76  		fn:    "MAX",
    77  		arg:   c,
    78  		table: t,
    79  	}
    80  }
    81  
    82  // Avg represents AVG
    83  func (t Table) Avg(c string) Aggregate {
    84  	return Aggregate{
    85  		fn:    "AVG",
    86  		arg:   c,
    87  		table: t,
    88  	}
    89  }
    90  
    91  // Min represents MIN
    92  func (t Table) Min(c string) Aggregate {
    93  	return Aggregate{
    94  		fn:    "MIN",
    95  		arg:   c,
    96  		table: t,
    97  	}
    98  }
    99  
   100  // Count represents COUNT
   101  func (t Table) Count(c string) Aggregate {
   102  	return Aggregate{
   103  		fn:    "COUNT",
   104  		arg:   c,
   105  		table: t,
   106  	}
   107  }
   108  
   109  // Sum represents SUM
   110  func (t Table) Sum(c string) Aggregate {
   111  	return Aggregate{
   112  		fn:    "SUM",
   113  		arg:   c,
   114  		table: t,
   115  	}
   116  }
   117  
   118  func (t Table) AllColumns() RawExpr {
   119  	return Raw("`" + t.alias + "`.*")
   120  }
   121  
   122  type Join struct {
   123  	left  TableReference
   124  	right TableReference
   125  	on    []Predicate
   126  	using []string
   127  	typ   string
   128  }
   129  
   130  func (Join) getAlias() string {
   131  	return ""
   132  }
   133  
   134  func (j Join) Join(reference TableReference) *JoinBuilder {
   135  	return &JoinBuilder{
   136  		left:  j,
   137  		right: reference,
   138  		typ:   "JOIN",
   139  	}
   140  }
   141  
   142  func (j Join) LeftJoin(reference TableReference) *JoinBuilder {
   143  	return &JoinBuilder{
   144  		left:  j,
   145  		right: reference,
   146  		typ:   "LEFT JOIN",
   147  	}
   148  }
   149  
   150  func (j Join) RightJoin(reference TableReference) *JoinBuilder {
   151  	return &JoinBuilder{
   152  		left:  j,
   153  		right: reference,
   154  		typ:   "RIGHT JOIN",
   155  	}
   156  }
   157  
   158  type JoinBuilder struct {
   159  	left  TableReference
   160  	right TableReference
   161  	typ   string
   162  }
   163  
   164  func (j *JoinBuilder) On(ps ...Predicate) Join {
   165  	return Join{
   166  		left:  j.left,
   167  		right: j.right,
   168  		typ:   j.typ,
   169  		on:    ps,
   170  	}
   171  }
   172  
   173  func (j *JoinBuilder) Using(cols ...string) Join {
   174  	return Join{
   175  		left:  j.left,
   176  		right: j.right,
   177  		typ:   j.typ,
   178  		using: cols,
   179  	}
   180  }
   181  
   182  type Subquery struct {
   183  	entity  TableReference
   184  	q       QueryBuilder
   185  	alias   string
   186  	columns []Selectable
   187  }
   188  
   189  var _ TableReference = Subquery{}
   190  
   191  func (s Subquery) getAlias() string {
   192  	return s.alias
   193  }
   194  
   195  func (Subquery) expr() (string, error) {
   196  	panic("implement me")
   197  }
   198  
   199  func (s Subquery) C(name string) Column {
   200  	return Column{
   201  		table: s.entity,
   202  		name:  name,
   203  	}
   204  }
   205  
   206  func (s Subquery) Join(target TableReference) *JoinBuilder {
   207  	return &JoinBuilder{
   208  		left:  s,
   209  		right: target,
   210  		typ:   "JOIN",
   211  	}
   212  }
   213  
   214  func (s Subquery) LeftJoin(target TableReference) *JoinBuilder {
   215  	return &JoinBuilder{
   216  		left:  s,
   217  		right: target,
   218  		typ:   "LEFT JOIN",
   219  	}
   220  }
   221  
   222  func (s Subquery) RightJoin(target TableReference) *JoinBuilder {
   223  	return &JoinBuilder{
   224  		left:  s,
   225  		right: target,
   226  		typ:   "RIGHT JOIN",
   227  	}
   228  }