...

Source file src/github.com/doug-martin/goqu/v9/exp/ident.go

Documentation: github.com/doug-martin/goqu/v9/exp

     1  package exp
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  type (
     8  	identifier struct {
     9  		schema string
    10  		table  string
    11  		col    interface{}
    12  	}
    13  )
    14  
    15  var (
    16  	tableAndColumnParts                 = 2
    17  	schemaTableAndColumnIdentifierParts = 3
    18  )
    19  
    20  func ParseIdentifier(ident string) IdentifierExpression {
    21  	parts := strings.Split(ident, ".")
    22  	switch len(parts) {
    23  	case tableAndColumnParts:
    24  		return NewIdentifierExpression("", parts[0], parts[1])
    25  	case schemaTableAndColumnIdentifierParts:
    26  		return NewIdentifierExpression(parts[0], parts[1], parts[2])
    27  	}
    28  	return NewIdentifierExpression("", "", ident)
    29  }
    30  
    31  func NewIdentifierExpression(schema, table string, col interface{}) IdentifierExpression {
    32  	return identifier{}.Schema(schema).Table(table).Col(col)
    33  }
    34  
    35  func (i identifier) clone() identifier {
    36  	return identifier{schema: i.schema, table: i.table, col: i.col}
    37  }
    38  
    39  func (i identifier) Clone() Expression {
    40  	return i.clone()
    41  }
    42  
    43  func (i identifier) IsQualified() bool {
    44  	schema, table, col := i.schema, i.table, i.col
    45  	switch c := col.(type) {
    46  	case string:
    47  		if c != "" {
    48  			return len(table) > 0 || len(schema) > 0
    49  		}
    50  	default:
    51  		if c != nil {
    52  			return len(table) > 0 || len(schema) > 0
    53  		}
    54  	}
    55  	if len(table) > 0 {
    56  		return len(schema) > 0
    57  	}
    58  	return false
    59  }
    60  
    61  // Sets the table on the current identifier
    62  //  I("col").Table("table") -> "table"."col" //postgres
    63  //  I("col").Table("table") -> `table`.`col` //mysql
    64  //  I("col").Table("table") -> `table`.`col` //sqlite3
    65  func (i identifier) Table(table string) IdentifierExpression {
    66  	i.table = table
    67  	return i
    68  }
    69  
    70  func (i identifier) GetTable() string {
    71  	return i.table
    72  }
    73  
    74  // Sets the table on the current identifier
    75  //  I("table").Schema("schema") -> "schema"."table" //postgres
    76  //  I("col").Schema("table") -> `schema`.`table` //mysql
    77  //  I("col").Schema("table") -> `schema`.`table` //sqlite3
    78  func (i identifier) Schema(schema string) IdentifierExpression {
    79  	i.schema = schema
    80  	return i
    81  }
    82  
    83  func (i identifier) GetSchema() string {
    84  	return i.schema
    85  }
    86  
    87  // Sets the table on the current identifier
    88  //  I("table").Col("col") -> "table"."col" //postgres
    89  //  I("table").Schema("col") -> `table`.`col` //mysql
    90  //  I("table").Schema("col") -> `table`.`col` //sqlite3
    91  func (i identifier) Col(col interface{}) IdentifierExpression {
    92  	if col == "*" {
    93  		i.col = Star()
    94  	} else {
    95  		i.col = col
    96  	}
    97  	return i
    98  }
    99  
   100  func (i identifier) Expression() Expression { return i }
   101  
   102  // Qualifies the epression with a * literal (e.g. "table".*)
   103  func (i identifier) All() IdentifierExpression { return i.Col("*") }
   104  
   105  func (i identifier) IsEmpty() bool {
   106  	isEmpty := i.schema == "" && i.table == ""
   107  	if isEmpty {
   108  		switch t := i.col.(type) {
   109  		case nil:
   110  			return true
   111  		case string:
   112  			return t == ""
   113  		default:
   114  			return false
   115  		}
   116  	}
   117  	return isEmpty
   118  }
   119  
   120  // Gets the column identifier
   121  func (i identifier) GetCol() interface{} { return i.col }
   122  
   123  // Used within updates to set a column value
   124  func (i identifier) Set(val interface{}) UpdateExpression { return set(i, val) }
   125  
   126  // Alias an identifier (e.g "my_col" AS "other_col")
   127  func (i identifier) As(val interface{}) AliasedExpression {
   128  	if v, ok := val.(string); ok {
   129  		ident := ParseIdentifier(v)
   130  		if i.col != nil && i.col != "" {
   131  			return NewAliasExpression(i, ident)
   132  		}
   133  		aliasCol := ident.GetCol()
   134  		if i.table != "" {
   135  			return NewAliasExpression(i, NewIdentifierExpression("", aliasCol.(string), nil))
   136  		} else if i.schema != "" {
   137  			return NewAliasExpression(i, NewIdentifierExpression(aliasCol.(string), "", nil))
   138  		}
   139  	}
   140  	return NewAliasExpression(i, val)
   141  }
   142  
   143  // Returns a BooleanExpression for equality (e.g "my_col" = 1)
   144  func (i identifier) Eq(val interface{}) BooleanExpression { return eq(i, val) }
   145  
   146  // Returns a BooleanExpression for in equality (e.g "my_col" != 1)
   147  func (i identifier) Neq(val interface{}) BooleanExpression { return neq(i, val) }
   148  
   149  // Returns a BooleanExpression for checking that a identifier is greater than another value (e.g "my_col" > 1)
   150  func (i identifier) Gt(val interface{}) BooleanExpression { return gt(i, val) }
   151  
   152  // Returns a BooleanExpression for checking that a identifier is greater than or equal to another value
   153  // (e.g "my_col" >= 1)
   154  func (i identifier) Gte(val interface{}) BooleanExpression { return gte(i, val) }
   155  
   156  // Returns a BooleanExpression for checking that a identifier is less than another value (e.g "my_col" < 1)
   157  func (i identifier) Lt(val interface{}) BooleanExpression { return lt(i, val) }
   158  
   159  // Returns a BooleanExpression for checking that a identifier is less than or equal to another value
   160  // (e.g "my_col" <= 1)
   161  func (i identifier) Lte(val interface{}) BooleanExpression { return lte(i, val) }
   162  
   163  // Returns a BooleanExpression for bit inversion (e.g ~ "my_col")
   164  func (i identifier) BitwiseInversion() BitwiseExpression { return bitwiseInversion(i) }
   165  
   166  // Returns a BooleanExpression for bit OR (e.g "my_col" | 1)
   167  func (i identifier) BitwiseOr(val interface{}) BitwiseExpression { return bitwiseOr(i, val) }
   168  
   169  // Returns a BooleanExpression for bit AND (e.g "my_col" & 1)
   170  func (i identifier) BitwiseAnd(val interface{}) BitwiseExpression { return bitwiseAnd(i, val) }
   171  
   172  // Returns a BooleanExpression for bit XOR (e.g "my_col" ^ 1)
   173  func (i identifier) BitwiseXor(val interface{}) BitwiseExpression { return bitwiseXor(i, val) }
   174  
   175  // Returns a BooleanExpression for bit LEFT shift (e.g "my_col" << 1)
   176  func (i identifier) BitwiseLeftShift(val interface{}) BitwiseExpression {
   177  	return bitwiseLeftShift(i, val)
   178  }
   179  
   180  // Returns a BooleanExpression for bit RIGHT shift (e.g "my_col" >> 1)
   181  func (i identifier) BitwiseRightShift(val interface{}) BitwiseExpression {
   182  	return bitwiseRightShift(i, val)
   183  }
   184  
   185  // Returns a BooleanExpression for checking that a identifier is in a list of values or  (e.g "my_col" > 1)
   186  func (i identifier) In(vals ...interface{}) BooleanExpression         { return in(i, vals...) }
   187  func (i identifier) NotIn(vals ...interface{}) BooleanExpression      { return notIn(i, vals...) }
   188  func (i identifier) Like(val interface{}) BooleanExpression           { return like(i, val) }
   189  func (i identifier) NotLike(val interface{}) BooleanExpression        { return notLike(i, val) }
   190  func (i identifier) ILike(val interface{}) BooleanExpression          { return iLike(i, val) }
   191  func (i identifier) NotILike(val interface{}) BooleanExpression       { return notILike(i, val) }
   192  func (i identifier) RegexpLike(val interface{}) BooleanExpression     { return regexpLike(i, val) }
   193  func (i identifier) RegexpNotLike(val interface{}) BooleanExpression  { return regexpNotLike(i, val) }
   194  func (i identifier) RegexpILike(val interface{}) BooleanExpression    { return regexpILike(i, val) }
   195  func (i identifier) RegexpNotILike(val interface{}) BooleanExpression { return regexpNotILike(i, val) }
   196  func (i identifier) Is(val interface{}) BooleanExpression             { return is(i, val) }
   197  func (i identifier) IsNot(val interface{}) BooleanExpression          { return isNot(i, val) }
   198  func (i identifier) IsNull() BooleanExpression                        { return is(i, nil) }
   199  func (i identifier) IsNotNull() BooleanExpression                     { return isNot(i, nil) }
   200  func (i identifier) IsTrue() BooleanExpression                        { return is(i, true) }
   201  func (i identifier) IsNotTrue() BooleanExpression                     { return isNot(i, true) }
   202  func (i identifier) IsFalse() BooleanExpression                       { return is(i, false) }
   203  func (i identifier) IsNotFalse() BooleanExpression                    { return isNot(i, false) }
   204  func (i identifier) Asc() OrderedExpression                           { return asc(i) }
   205  func (i identifier) Desc() OrderedExpression                          { return desc(i) }
   206  func (i identifier) Distinct() SQLFunctionExpression                  { return NewSQLFunctionExpression("DISTINCT", i) }
   207  func (i identifier) Cast(t string) CastExpression                     { return NewCastExpression(i, t) }
   208  
   209  // Returns a RangeExpression for checking that a identifier is between two values (e.g "my_col" BETWEEN 1 AND 10)
   210  func (i identifier) Between(val RangeVal) RangeExpression { return between(i, val) }
   211  
   212  // Returns a RangeExpression for checking that a identifier is between two values (e.g "my_col" BETWEEN 1 AND 10)
   213  func (i identifier) NotBetween(val RangeVal) RangeExpression { return notBetween(i, val) }
   214  

View as plain text