Documentation ¶
Index ¶
- Constants
- func BuildConnectionString(dialect SqlDialect, host string, port int, dbName string, dbUser string, ...) string
- func CheckDb(dialect SqlDialect, dbConnStr string, options *g.Config) bool
- func CloseDb(db *g.DB) bool
- func CreateRandomDb(dialect SqlDialect, host string, port int, dbUser string, password string, ...) (*g.DB, string)
- func DropDb(dialect SqlDialect, connStr string, options *g.Config) bool
- func DropDb2(dialect SqlDialect, systemDbConnStr string, dbName string, options *g.Config) bool
- func GetNextTableId(db *gorm.DB, table string) uint
- func OpenDb(dialect SqlDialect, host string, port int, dbName string, dbUser string, ...) *g.DB
- func OpenDb2(dialect SqlDialect, connStr string, create bool, check bool, options *g.Config, ...) *g.DB
- func Paginate(page int, size int) func(db *gorm.DB) *gorm.DB
- type Collation
- type SqlDialect
Constants ¶
View Source
const ( Postgres SqlDialect = "postgres" Mysql = "mysql" Mssql = "mssql" Sqlite = "sqlite" )
Variables ¶
This section is empty.
Functions ¶
func BuildConnectionString ¶
func BuildConnectionString(dialect SqlDialect, host string, port int, dbName string, dbUser string, password string, useSsl string) string
BuildConnectionString
Function that builds connection string from individual parameters to use in OpenDb2 * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - host - ip address / hostname of machine where database server is located * - port - integer value representing server tcp port (typically 5432 for postgres, 3306 for mysql and 1433 for mssql) * - dbName - database/catalog/schema name * - dbUser - user that is using for perform operations on dbName * - password - dbUser password * - useSsl - is a string value that currently is using with Postgres Sql Only (allowed options are: disable, and others for enable) * - collation a set of charset / collation options for database creation * Returns database connection string
func CheckDb ¶
func CheckDb(dialect SqlDialect, dbConnStr string, options *g.Config) bool
CheckDb
Functions that checks if database exists or not * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - connStr - full connection string * Returns true if database exists otherwise false
func CloseDb ¶
CloseDb
Function that close connection to database * Parameters: * - db - address of database context object * Returns true on success
func CreateRandomDb ¶ added in v1.2.4
func CreateRandomDb(dialect SqlDialect, host string, port int, dbUser string, password string, useSsl string, options *g.Config, collation *Collation) (*g.DB, string)
CreateRandomDb Function that Create and Open database with random name this function should be used for testing purposes create temporary database for test
Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - host - ip address / hostname of machine where database server is located * - port - integer value representing server tcp port (typically 5432 for postgres, 3306 for mysql and 1433 for mssql) * - dbUser - user that is using for perform operations on dbName * - password - dbUser password * - options - gorm config (from gorm.io/gorm NOT from github.com/jinzhu/gorm) * - collation a set of charset / collation options for database creation * Returns tuple og gorm.DB address of database context object and connStr
func DropDb ¶
func DropDb(dialect SqlDialect, connStr string, options *g.Config) bool
DropDb
Function that drops database from server * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - connStr - full connection string
func DropDb2 ¶
DropDb2
Function that drop database from server using system database and dropping database name * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - systemDbConnStr - connection string to system database (in mysql - mysql, in sqlserver - master, * in postgres - postgres) * - dbName - name of database that should be deleted * Returns true if database was deleted / dropped
func GetNextTableId ¶
GetNextTableId
Function for getting next free id (uint) for model that have Model field with ID * Parameters: * - db - gorm.DB address of database context object * - table - table name * Returns MAX(ID) + 1
func OpenDb ¶
func OpenDb(dialect SqlDialect, host string, port int, dbName string, dbUser string, password string, useSsl string, create bool, check bool, options *g.Config, collation *Collation) *g.DB
OpenDb
Function that Open or Create and Open database * If you are using MSSQL Do not forget to switch on TCP connections for sql server, otherwise you wil get following error: * Unable to open tcp connection with host '127.0.0.1:1433': dial tcp 127.0.0.1:1433: connectex: No connection could * be made because the target machine actively refused it. * Ensure that You allowed port usage by Sql Server Connection Manager * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - host - ip address / hostname of machine where database server is located * - port - integer value representing server tcp port (typically 5432 for postgres, 3306 for mysql and 1433 for mssql) * - dbName - database/catalog/schema name * - dbUser - user that is using for perform operations on dbName * - password - dbUser password * - create - if true we should create database if it does not exist * - check - if true existence of database is checking otherwise not (we sure that this is a random database and to save * some time we could omit existence check) * - options - gorm config (from gorm.io/gorm NOT from github.com/jinzhu/gorm) * - collation a set of charset / collation options for database creation * Returns gorm.DB address of database context object
func OpenDb2 ¶
func OpenDb2(dialect SqlDialect, connStr string, create bool, check bool, options *g.Config, collation *Collation) *g.DB
OpenDb2
Function that Open or Create and Open database * This function does same as OpenDb but there is only one difference in parameters: for this function we pass connection string * instead of a lot of individual parameters * Parameters: * - dialect - string that represent using db driver inside gorm (see enum above) * - connStr - full connection string * - create - if true we should create database if it does not exist * - check - if true existence of database is checking otherwise not (we sure that this is a random database and to save * some time we could omit existence check) * - options - gorm config (from gorm.io/gorm NOT from github.com/jinzhu/gorm) * - collation a set of charset / collation options for database creation
Types ¶
type Collation ¶ added in v1.4.0
type Collation struct { // Encoding represents string encoding type /* 1. For Postgres most common options are: ENCODING 'UTF8' LC_COLLATE = 'american_usa' LC_CTYPE = 'american_usa' * Therefore LC_CTYPE, LC_COLLATE & others similar will be stored in Parameters * 2. For Mysql i.e. CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; * Therefore CHARACTER SET storing in Encoding, Collate in Parameters * 3. For Mssql i.e. CREATE DATABASE MyOptionsTest COLLATE Latin1_General_100_CS_AS_SC; * Therefore COLLATE stores in Encoding */ Encoding string Parameters map[string]string }
Collation is a struct that could be used for different databases therefore it attempts to merge all options, see comments
For Postgres we should pass Collation as Collation {Encoding: "UTF8", Params: map[string]string {"LC_COLLATE": "en_US.utf8", * "LC_CTYPE": "en_US.utf8"} * For Mysql we should pass Collation {Encoding: "utf8mb4", Params: map[string]string {"COLLATE": "utf8mb4_unicode_ci"} * For Mssql we should pass Collation {Encoding: "utf8", Params: map[string]string{}
type SqlDialect ¶
type SqlDialect string
Click to show internal directories.
Click to hide internal directories.