code.gitea.io/gitea@v1.21.7/models/db/common.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package db
     5  
     6  import (
     7  	"strings"
     8  
     9  	"code.gitea.io/gitea/modules/setting"
    10  	"code.gitea.io/gitea/modules/util"
    11  
    12  	"xorm.io/builder"
    13  )
    14  
    15  // BuildCaseInsensitiveLike returns a condition to check if the given value is like the given key case-insensitively.
    16  // Handles especially SQLite correctly as UPPER there only transforms ASCII letters.
    17  func BuildCaseInsensitiveLike(key, value string) builder.Cond {
    18  	if setting.Database.Type.IsSQLite3() {
    19  		return builder.Like{"UPPER(" + key + ")", util.ToUpperASCII(value)}
    20  	}
    21  	return builder.Like{"UPPER(" + key + ")", strings.ToUpper(value)}
    22  }
    23  
    24  // BuildCaseInsensitiveIn returns a condition to check if the given value is in the given values case-insensitively.
    25  // Handles especially SQLite correctly as UPPER there only transforms ASCII letters.
    26  func BuildCaseInsensitiveIn(key string, values []string) builder.Cond {
    27  	uppers := make([]string, 0, len(values))
    28  	if setting.Database.Type.IsSQLite3() {
    29  		for _, value := range values {
    30  			uppers = append(uppers, util.ToUpperASCII(value))
    31  		}
    32  	} else {
    33  		for _, value := range values {
    34  			uppers = append(uppers, strings.ToUpper(value))
    35  		}
    36  	}
    37  
    38  	return builder.In("UPPER("+key+")", uppers)
    39  }
    40  
    41  // BuilderDialect returns the xorm.Builder dialect of the engine
    42  func BuilderDialect() string {
    43  	switch {
    44  	case setting.Database.Type.IsMySQL():
    45  		return builder.MYSQL
    46  	case setting.Database.Type.IsSQLite3():
    47  		return builder.SQLITE
    48  	case setting.Database.Type.IsPostgreSQL():
    49  		return builder.POSTGRES
    50  	case setting.Database.Type.IsMSSQL():
    51  		return builder.MSSQL
    52  	default:
    53  		return ""
    54  	}
    55  }