github.com/searKing/golang/go@v1.2.117/database/sql/placeholder.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sql
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	strings_ "github.com/searKing/golang/go/strings"
    12  )
    13  
    14  // Placeholders behaves like strings.Join([]string{"?",...,"?"}, ",")
    15  func Placeholders(n int) string {
    16  	return strings_.JoinRepeat("?", ",", n)
    17  }
    18  
    19  // Pagination returns the "LIMIT %d, OFFSET %d"
    20  // query := Pagination(0, 0)
    21  // // "LIMIT 0, OFFSET 0"
    22  func Pagination(limit, offset int) string {
    23  	if limit < 0 || offset < 0 {
    24  		return ""
    25  	}
    26  	return fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
    27  }
    28  
    29  // ExpandAsColumns expand columns with alias AS
    30  // query := ExpandAsColumns("table.foo", "bar")
    31  // // []string{"table.foo AS table_foo", "bar AS bar"}
    32  func ExpandAsColumns(cols ...string) []string {
    33  	cols = strings_.SliceTrimEmpty(cols...)
    34  	var params []string
    35  	for _, col := range cols {
    36  		params = append(params, fmt.Sprintf("%[1]s AS %[2]s", col, CompliantName(col)))
    37  	}
    38  	return params
    39  }
    40  
    41  // TableColumns returns the []string{table.value1, table.value2 ...}
    42  // query := Columns("table", "foo", "bar")
    43  // // []string{"table.foo", "table.bar"}
    44  func TableColumns(table string, cols ...string) []string {
    45  	cols = strings_.SliceTrimEmpty(cols...)
    46  	var namedCols []string
    47  	for _, col := range cols {
    48  		if table == "" {
    49  			namedCols = append(namedCols, col)
    50  		} else {
    51  			namedCols = append(namedCols, fmt.Sprintf("%s.%s", table, col))
    52  		}
    53  	}
    54  	return namedCols
    55  }
    56  
    57  // TableValues returns the []string{:value1, :value2 ...}
    58  // query := TableValues("foo", "bar")
    59  // // []string{"?", "?"}
    60  func TableValues(cols ...string) []string {
    61  	cols = strings_.SliceTrimEmpty(cols...)
    62  
    63  	var namedCols []string
    64  	for range cols {
    65  		namedCols = append(namedCols, "?")
    66  	}
    67  	return namedCols
    68  }
    69  
    70  // TableColumnsValues returns the []string{table.value1=:value1, table.value2=:value2 ...}
    71  // query := ColumnsValues("table", "foo", "bar")
    72  // // []string{"table.foo=?", "table.bar=?"}
    73  func TableColumnsValues(cmp string, table string, cols ...string) []string {
    74  	cols = strings_.SliceTrimEmpty(cols...)
    75  	var namedCols []string
    76  	for _, col := range cols {
    77  		if table == "" {
    78  			namedCols = append(namedCols, fmt.Sprintf("%[1]s %[2]s ?", col, cmp))
    79  		} else {
    80  			namedCols = append(namedCols, fmt.Sprintf("%[1]s.%[2]s %[3]s ?", table, col, cmp))
    81  		}
    82  	}
    83  	return namedCols
    84  }
    85  
    86  // JoinTableColumns concatenates the elements of cols to column1, column2, ...
    87  // query := JoinTableColumns("table", "foo", "bar")
    88  // // "table.foo, table.bar"
    89  func JoinTableColumns(table string, cols ...string) string {
    90  	//cols = strings_.SliceTrimEmpty(cols...)
    91  	return strings.Join(TableColumns(table, cols...), ",")
    92  }
    93  
    94  // JoinTableColumnsWithAs concatenates the elements of cols to column1, column2, ...
    95  // query := JoinTableColumnsWithAs("table", "foo", "bar")
    96  // // "table.foo AS table.foo, table.bar AS table.bar"
    97  func JoinTableColumnsWithAs(table string, cols ...string) string {
    98  	//cols = strings_.SliceTrimEmpty(cols...)
    99  	return strings.Join(ExpandAsColumns(TableColumns(table, cols...)...), ",")
   100  }
   101  
   102  // JoinColumns concatenates the elements of cols to column1, column2, ...
   103  // query := JoinColumns("foo", "bar")
   104  // // "foo,bar"
   105  func JoinColumns(cols ...string) string {
   106  	return JoinTableColumns("", cols...)
   107  }
   108  
   109  // JoinColumnsWithAs concatenates the elements of cols to column1, column2, ...
   110  // query := JoinColumnsWithAs("foo", "bar")
   111  // // "foo AS foo,bar AS bar"
   112  func JoinColumnsWithAs(cols ...string) string {
   113  	return JoinTableColumnsWithAs("", cols...)
   114  }
   115  
   116  // JoinTableValues concatenates the elements of values to :value1, :value2, ...
   117  // query := JoinTableValues("foo", "bar")
   118  // // "?,?"
   119  // query := JoinTableValues()
   120  // // ""
   121  func JoinTableValues(cols ...string) string {
   122  	cols = strings_.SliceTrimEmpty(cols...)
   123  	if len(cols) == 0 {
   124  		// https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html
   125  		// DEFAULT
   126  		return ""
   127  	}
   128  	return strings.Join(TableValues(cols...), ",")
   129  }
   130  
   131  // JoinTableColumnsValues concatenates the elements of values to table.value1=:value1, table.value2=:value2 ...
   132  // query := JoinTableColumnsValues("table", "foo", "bar")
   133  // // "table.foo=?, table.bar=?"
   134  func JoinTableColumnsValues(cmp string, table string, cols ...string) string {
   135  	//cols = strings_.SliceTrimEmpty(cols...)
   136  	return strings.Join(TableColumnsValues(cmp, table, cols...), ",")
   137  }