utils

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2025 License: Apache-2.0 Imports: 18 Imported by: 8

Documentation

Index

Constants

View Source
const (
	PGX_DRIVER_PROTOCOL   = "postgresql"
	MYSQL_DRIVER_PROTOCOL = "mysql"
	NET_PROP_KEY          = "net"
)
View Source
const (
	CONN_PREPARE                        = "Conn.Prepare"
	CONN_PREPARE_CONTEXT                = "Conn.PrepareContext"
	CONN_CLOSE                          = "Conn.Close"
	CONN_BEGIN                          = "Conn.Begin"
	CONN_BEGIN_TX                       = "Conn.BeginTx"
	CONN_QUERY_CONTEXT                  = "Conn.QueryContext"
	CONN_EXEC_CONTEXT                   = "Conn.ExecContext"
	CONN_PING                           = "Conn.Ping"
	CONN_IS_VALID                       = "Conn.IsValid"
	CONN_RESET_SESSION                  = "Conn.ResetSession"
	CONN_CHECK_NAMED_VALUE              = "Conn.CheckNamedValue"
	STMT_CLOSE                          = "Stmt.Close"
	STMT_EXEC                           = "Stmt.Exec"
	STMT_EXEC_CONTEXT                   = "Stmt.ExecContext"
	STMT_NUM_INPUT                      = "Stmt.NumInput"
	STMT_QUERY                          = "Stmt.Query"
	STMT_QUERY_CONTEXT                  = "Stmt.QueryContext"
	STMT_CHECK_NAMED_VALUE              = "Stmt.CheckNamedValue"
	RESULT_LAST_INSERT_ID               = "Result.LastInsertId"
	RESULT_ROWS_AFFECTED                = "Result.RowsAffected"
	TX_COMMIT                           = "Tx.Commit"
	TX_ROLLBACK                         = "Tx.Rollback"
	ROWS_CLOSE                          = "Rows.Close"
	ROWS_COLUMNS                        = "Rows.Columns"
	ROWS_NEXT                           = "Rows.Next"
	ROWS_COLUMN_TYPE_PRECISION_SCALE    = "Rows.ColumnTypePrecisionScale"
	ROWS_COLUMN_TYPE_DATABASE_TYPE_NAME = "Rows.ColumnTypeDatabaseTypeName"
	ROWS_COLUMN_TYPE_LENGTH             = "Rows.ColumnTypeLength"
	ROWS_HAS_NEXT_RESULT_SET            = "Rows.HasNextResultSet"
	ROWS_NEXT_RESULT_SET                = "Rows.NextResultSet"
	ROWS_COLUMN_TYPE_SCAN_TYPE          = "Rows.ColumnTypeScanType"
	ROWS_COLUMN_TYPE_NULLABLE           = "Rows.ColumnTypeNullable"
)
View Source
const (
	INSTANCE_GROUP = "instance"
	DNS_GROUP      = "dns"
	DOMAIN_GROUP   = "domain"
	REGION_GROUP   = "region"
)
View Source
const SET_AUTOCOMMIT_0 = "set autocommit = 0"

Variables

View Source
var (
	AURORA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.rds\\.amazonaws\\.com\\.?)$")

	AURORA_CHINA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.rds\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.amazonaws\\.com\\.cn\\.?)$")

	AURORA_OLD_CHINA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.rds\\.amazonaws\\.com\\.cn\\.?)$")

	AURORA_GOV_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.rds\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.(amazonaws\\.com\\.?|c2s\\.ic\\.gov\\.?|sc2s\\.sgov\\.gov\\.?))$")

	IP_V4_REGEXP = regexp.MustCompile(
		"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" +
			"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}" +
			"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")

	IP_V6_REGEXP = regexp.MustCompile("^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$")

	IP_V6_COMPRESSED_REGEXP = regexp.MustCompile("^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" +
		"::(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$")
)
View Source
var CleanupIntervalNanos time.Duration = 10 * time.Minute
View Source
var SENSITIVE_PROPERTIES = map[string]struct{}{
	property_util.PASSWORD.Name:     {},
	property_util.IDP_PASSWORD.Name: {},
}

Functions

func AllKeys

func AllKeys[T comparable, V any](mapOfKeysAndValues map[T]V) []T

func CheckIdpCredentialsWithFallback

func CheckIdpCredentialsWithFallback(
	idpUserNameProperty property_util.AwsWrapperProperty,
	idpPasswordProperty property_util.AwsWrapperProperty,
	props map[string]string)

func CombineMaps

func CombineMaps[K comparable, V any](mapToCopy map[K]V, mapToAdd map[K]V) map[K]V

func ConvertDriverValueToString

func ConvertDriverValueToString(value driver.Value) (string, bool)

func CreateMapCopy

func CreateMapCopy[K comparable, V any](mapToCopy map[K]V) map[K]V

func DoesCloseTransaction

func DoesCloseTransaction(methodName string, methodArgs ...any) bool

func DoesOpenTransaction

func DoesOpenTransaction(methodName string, methodArgs ...any) bool

func ExecQueryDirectly

func ExecQueryDirectly(conn driver.Conn, query string) error

func FilterSlice

func FilterSlice[T any](slice []T, filter func(T) bool) []T

func FindHostInTopology

func FindHostInTopology(hosts []*host_info_util.HostInfo, hostNames ...string) *host_info_util.HostInfo

func FindRegisteredDriver

func FindRegisteredDriver(dialectCode string) bool

func GetFirstRowFromQuery

func GetFirstRowFromQuery(conn driver.Conn, query string) []driver.Value

Directly executes query on conn, and returns the first row. Returns nil if unable to obtain a row.

func GetFirstRowFromQueryAsString

func GetFirstRowFromQueryAsString(conn driver.Conn, query string) []string

Directly executes query on conn and converts all possible values in the first row to strings. Any values that cannot be converted are returned as "". Returns nil if unable to obtain a row.

func GetHostNameFromEndpoint

func GetHostNameFromEndpoint(endpoint string) string

func GetHostsFromDsn

func GetHostsFromDsn(dsn string, isSingleWriterDsn bool) (hostInfoList []*host_info_util.HostInfo, err error)

func GetPreparedHost

func GetPreparedHost(host string) string

func GetProtocol

func GetProtocol(dsn string) (string, error)

func GetQueryFromSqlOrMethodArgs

func GetQueryFromSqlOrMethodArgs(sql string, methodArgs ...any) string

func GetRdsClusterHostUrl

func GetRdsClusterHostUrl(host string) string

func GetRdsInstanceHostPattern

func GetRdsInstanceHostPattern(host string) string

func GetRdsRegion

func GetRdsRegion(host string) string

func GetSeparateSqlStatements

func GetSeparateSqlStatements(query string) []string

func GetStructName

func GetStructName(v any) string

func IndexOf

func IndexOf[T any](slice []T, item T, compareFunc func(T, T) bool) int

func IsIPV6

func IsIPV6(host string) bool

func IsIPv4

func IsIPv4(host string) bool

func IsLimitlessDbShardGroupDns

func IsLimitlessDbShardGroupDns(host string) bool

func IsRdsClusterDns

func IsRdsClusterDns(host string) bool

func IsRdsCustomClusterDns

func IsRdsCustomClusterDns(host string) bool

func IsRdsDns

func IsRdsDns(host string) bool

func IsRdsProxyDns

func IsRdsProxyDns(host string) bool

func IsReachable

func IsReachable(conn driver.Conn, ctx context.Context) bool

func IsReaderClusterDns

func IsReaderClusterDns(host string) bool

func IsWriterClusterDns

func IsWriterClusterDns(host string) bool

func LengthOfSyncMap

func LengthOfSyncMap(syncMap *sync.Map) int

func LogTopology

func LogTopology(hosts []*host_info_util.HostInfo, msgPrefix string) string

func MaskProperties

func MaskProperties(props map[string]string) map[string]string

func MaskSensitiveInfoFromDsn

func MaskSensitiveInfoFromDsn(dsn string) string

func ParseDatabaseFromDsn

func ParseDatabaseFromDsn(dsn string) (string, error)

func ParseDsn

func ParseDsn(dsn string) (map[string]string, error)

func ParseHostPortPair

func ParseHostPortPair(instanceClusterTemplate string, defaultPort int) (*host_info_util.HostInfo, error)

func ParsePasswordFromDsn

func ParsePasswordFromDsn(dsn string) (string, error)

func ParseUserFromDsn

func ParseUserFromDsn(dsn string) (string, error)

func RemoveFromSlice

func RemoveFromSlice[T any](slice []T, item T, compareFunc func(T, T) bool) []T

func ResetPreparedHostFunc

func ResetPreparedHostFunc()

func Rollback

func Rollback(conn driver.Conn, currentTx driver.Tx)

func SetPreparedHostFunc

func SetPreparedHostFunc(newPrepareHostFunc func(string) string)

func SliceAndMapHaveCommonElement

func SliceAndMapHaveCommonElement[T comparable, V any](sliceA []T, mapOfKeysAndValues map[T]V) bool

func ValidateUrl

func ValidateUrl(url string) error

Types

type CacheMap

type CacheMap[T any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[T any]() *CacheMap[T]

func (*CacheMap[T]) CleanUp

func (c *CacheMap[T]) CleanUp()

func (*CacheMap[T]) Clear

func (c *CacheMap[T]) Clear()

func (*CacheMap[T]) ComputeIfAbsent

func (c *CacheMap[T]) ComputeIfAbsent(key string, computeFunc func() T, itemExpiration time.Duration) T

func (*CacheMap[T]) Get

func (c *CacheMap[T]) Get(key string) (T, bool)

func (*CacheMap[T]) GetAllEntries

func (c *CacheMap[T]) GetAllEntries() map[string]T

Get a map copy of all entries in the cache, including expired entries.

func (*CacheMap[T]) Put

func (c *CacheMap[T]) Put(key string, value T, expiration time.Duration)

func (*CacheMap[T]) PutIfAbsent

func (c *CacheMap[T]) PutIfAbsent(key string, value T, expiration time.Duration)

func (*CacheMap[T]) Remove

func (c *CacheMap[T]) Remove(key string)

func (*CacheMap[T]) Size

func (c *CacheMap[T]) Size() int

type DisposalFunc

type DisposalFunc[T any] func(T) bool

type RdsUrlType

type RdsUrlType struct {
	Id           int
	IsRds        bool
	IsRdsCluster bool
}
var (
	OTHER                               RdsUrlType = RdsUrlType{0, false, false}
	RDS_WRITER_CLUSTER                  RdsUrlType = RdsUrlType{1, true, true}
	RDS_READER_CLUSTER                  RdsUrlType = RdsUrlType{2, true, true}
	RDS_CUSTOM_CLUSTER                  RdsUrlType = RdsUrlType{3, true, true}
	RDS_PROXY                           RdsUrlType = RdsUrlType{4, true, false}
	RDS_INSTANCE                        RdsUrlType = RdsUrlType{5, true, false}
	RDS_AURORA_LIMITLESS_DB_SHARD_GROUP RdsUrlType = RdsUrlType{6, true, false}
	IP_ADDRESS                          RdsUrlType = RdsUrlType{7, false, false}
)

func IdentifyRdsUrlType

func IdentifyRdsUrlType(host string) RdsUrlType

type SlidingExpirationCache

type SlidingExpirationCache[T any] struct {
	// contains filtered or unexported fields
}

func NewSlidingExpirationCache

func NewSlidingExpirationCache[T any](id string, funcs ...DisposalFunc[T]) *SlidingExpirationCache[T]

func (*SlidingExpirationCache[T]) CleanUp

func (c *SlidingExpirationCache[T]) CleanUp()

func (*SlidingExpirationCache[T]) Clear

func (c *SlidingExpirationCache[T]) Clear()

func (*SlidingExpirationCache[T]) ComputeIfAbsent

func (c *SlidingExpirationCache[T]) ComputeIfAbsent(key string, computeFunc func() T, itemExpiration time.Duration) T

func (*SlidingExpirationCache[T]) Get

func (c *SlidingExpirationCache[T]) Get(key string, itemExpiration time.Duration) (T, bool)

func (*SlidingExpirationCache[T]) GetAllEntries

func (c *SlidingExpirationCache[T]) GetAllEntries() map[string]T

Get a map copy of all entries in the cache, including expired entries.

func (*SlidingExpirationCache[T]) Put

func (c *SlidingExpirationCache[T]) Put(key string, value T, itemExpiration time.Duration)

func (*SlidingExpirationCache[T]) PutIfAbsent

func (c *SlidingExpirationCache[T]) PutIfAbsent(key string, value T, expiration time.Duration)

func (*SlidingExpirationCache[T]) Remove

func (c *SlidingExpirationCache[T]) Remove(key string)

func (*SlidingExpirationCache[T]) SetCleanupIntervalNanos

func (c *SlidingExpirationCache[T]) SetCleanupIntervalNanos(newIntervalNanos time.Duration)

func (*SlidingExpirationCache[T]) Size

func (c *SlidingExpirationCache[T]) Size() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL