diff --git a/.gitignore b/.gitignore index 66fd13c..bbfd2cb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +.idea \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..29ed7d5 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/Selly-Modules/postgresql + +go 1.16 + +require ( + github.com/Masterminds/squirrel v1.5.0 + github.com/jmoiron/sqlx v1.3.4 + github.com/lib/pq v1.10.2 + github.com/logrusorgru/aurora v2.0.3+incompatible +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e1a62ac --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/Masterminds/squirrel v1.5.0 h1:JukIZisrUXadA9pl3rMkjhiamxiB0cXiu+HGp/Y8cY8= +github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= +github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= +github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/postgresql.go b/postgresql.go new file mode 100644 index 0000000..56bb30d --- /dev/null +++ b/postgresql.go @@ -0,0 +1,48 @@ +package postgresql + +import ( + "fmt" + "log" + "time" + + + "github.com/jmoiron/sqlx" + _ "github.com/lib/pq" // For postgres dialect + "github.com/logrusorgru/aurora" +) + +var ( + sqlxClient *sqlx.DB +) + +// Connect to postgres database +func Connect(host, user, password, dbname, port, sslmode string) error { + // Connect string + dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=UTC", + host, user, password, dbname, port, sslmode, + ) + + db, err := sqlx.Connect("postgres", dsn) + if err != nil { + log.Fatalln(err) + } + + fmt.Println(aurora.Green("*** CONNECTED TO POSTGRESQL - SQLX: " + dsn)) + + // Config connection pool + sqlDB := db.DB + sqlDB.SetMaxOpenConns(100) + sqlDB.SetMaxIdleConns(20) + sqlDB.SetConnMaxLifetime(time.Minute * 5) + + // Assign client + sqlxClient = db + + return nil +} + +// GetSqlxInstance ... +func GetSqlxInstance() *sqlx.DB { + return sqlxClient +} + diff --git a/stm_builder.go b/stm_builder.go new file mode 100644 index 0000000..37cf555 --- /dev/null +++ b/stm_builder.go @@ -0,0 +1,10 @@ +package postgresql + +import ( + "github.com/Masterminds/squirrel" +) + +// GetStmBuilder ... +func GetStmBuilder() squirrel.StatementBuilderType { + return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) +}