Browse Source

Basic methods for all engines

Volodymyr Tkach 2 years ago
parent
commit
754e480723

+ 15 - 0
Makefile

@@ -0,0 +1,15 @@
+default: test
+
+clean:
+	go clean -testcache ./...
+
+test:
+	go test ./...
+
+lint:
+	golangci-lint run
+
+tidy:
+	go mod tidy
+
+.PHONY: default clean test lint tidy

+ 10 - 0
db/migrations/20220527233113_test_migration.sql

@@ -0,0 +1,10 @@
+-- migrate:up
+create table users (
+  id integer,
+  name varchar(255)
+);
+insert into users (id, name) values (1, 'alice');
+insert into users (id, name) values (2, 'bob');
+
+-- migrate:down
+drop table users;

+ 27 - 0
go.mod

@@ -0,0 +1,27 @@
+module github.com/vladimirok5959/golang-sql
+
+go 1.18
+
+require (
+	github.com/amacneil/dbmate v1.15.0
+	github.com/onsi/ginkgo v1.16.5
+	github.com/onsi/gomega v1.19.0
+)
+
+require (
+	github.com/go-sql-driver/mysql v1.6.0 // indirect
+	github.com/lib/pq v1.10.4 // indirect
+	github.com/mattn/go-sqlite3 v1.14.12 // indirect
+)
+
+require (
+	github.com/fsnotify/fsnotify v1.4.9 // indirect
+	github.com/nxadm/tail v1.4.8 // indirect
+	github.com/onsi/ginkgo/v2 v2.1.4 // indirect
+	golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
+	golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
+	golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
+	golang.org/x/text v0.3.7 // indirect
+	gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
+	gopkg.in/yaml.v2 v2.4.0 // indirect
+)

+ 103 - 0
go.sum

@@ -0,0 +1,103 @@
+github.com/amacneil/dbmate v1.15.0 h1:DbZ4AULjqkzJ08+064rmNwlO+kkFNTN/P5y4U8PgKGA=
+github.com/amacneil/dbmate v1.15.0/go.mod h1:w0m/bKnGEC5ho3C3T7Br6vtqgwYGfi5OGM/EWrya0Gw=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+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/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
+github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
+github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
+github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
+github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
+github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
+github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
+github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
+github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
+github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
+github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
+github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0=
+github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
+github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
+github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
+github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
+github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
+github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
+github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
+github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
+github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
+github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
+github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
+github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
+github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
+github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
+github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
+github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
+github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
+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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
+golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
+golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
+golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
+golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
+google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
+google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
+google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
+google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
+google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
+gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
+gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

+ 67 - 0
gosql/common/common.go

@@ -0,0 +1,67 @@
+package common
+
+import (
+	"context"
+	"database/sql"
+	"fmt"
+	"io"
+	"net/url"
+
+	"github.com/amacneil/dbmate/pkg/dbmate"
+	_ "github.com/amacneil/dbmate/pkg/driver/mysql"
+	_ "github.com/amacneil/dbmate/pkg/driver/postgres"
+	_ "github.com/amacneil/dbmate/pkg/driver/sqlite"
+	"golang.org/x/exp/slices"
+)
+
+type Engine interface {
+	Close() error
+	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
+	Ping(context.Context) error
+	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
+	QueryRow(ctx context.Context, query string, args ...any) *sql.Row
+}
+
+func ParseUrl(dbURL string) (*url.URL, error) {
+	databaseURL, err := url.Parse(dbURL)
+	if err != nil {
+		return nil, fmt.Errorf("unable to parse URL: %w", err)
+	}
+
+	if databaseURL.Scheme == "" {
+		return nil, fmt.Errorf("protocol scheme is not defined")
+	}
+
+	protocols := []string{"mysql", "postgres", "postgresql", "sqlite", "sqlite3"}
+	if !slices.Contains(protocols, databaseURL.Scheme) {
+		return nil, fmt.Errorf("unsupported protocol scheme: %s", databaseURL.Scheme)
+	}
+
+	return databaseURL, nil
+}
+
+func OpenDB(databaseURL *url.URL, migrationsDir string) (*sql.DB, error) {
+	mate := dbmate.New(databaseURL)
+
+	mate.AutoDumpSchema = false
+	mate.Log = io.Discard
+	if migrationsDir != "" {
+		mate.MigrationsDir = migrationsDir
+	}
+
+	driver, err := mate.GetDriver()
+	if err != nil {
+		return nil, fmt.Errorf("DB get driver error: %w", err)
+	}
+
+	if err := mate.CreateAndMigrate(); err != nil {
+		return nil, fmt.Errorf("DB migration error: %w", err)
+	}
+
+	db, err := driver.Open()
+	if err != nil {
+		return nil, fmt.Errorf("DB open error: %w", err)
+	}
+
+	return db, nil
+}

+ 146 - 0
gosql/common/common_test.go

@@ -0,0 +1,146 @@
+package common_test
+
+import (
+	"io/ioutil"
+	"net/url"
+	"path/filepath"
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-sql/gosql/common"
+)
+
+var _ = Describe("common", func() {
+	Context("ParseUrl", func() {
+		Context("Success", func() {
+			It("for MySQL", func() {
+				// mysql://username:password@127.0.0.1:3306/database?parseTime=true
+				// mysql://username:password@/database?socket=/var/run/mysqld/mysqld.sock
+
+				url := "mysql://username:password@127.0.0.1:3306/database?parseTime=true"
+				result, err := common.ParseUrl(url)
+
+				Expect(err).To(Succeed())
+				Expect(result.Scheme).To(Equal("mysql"))
+				Expect(result.User.Username()).To(Equal("username"))
+
+				password, whether := result.User.Password()
+				Expect(password).To(Equal("password"))
+				Expect(whether).To(BeTrue())
+
+				Expect(result.Host).To(Equal("127.0.0.1:3306"))
+				Expect(result.Path).To(Equal("/database"))
+				Expect(result.RawQuery).To(Equal("parseTime=true"))
+			})
+
+			It("for PostgreSQL", func() {
+				// postgres://username:password@127.0.0.1:5432/database?sslmode=disable
+				// postgresql://username:password@127.0.0.1:5432/database?sslmode=disable
+
+				url := "postgres://username:password@127.0.0.1:5432/database?sslmode=disable"
+				result, err := common.ParseUrl(url)
+
+				Expect(err).To(Succeed())
+				Expect(result.Scheme).To(Equal("postgres"))
+				Expect(result.User.Username()).To(Equal("username"))
+
+				password, whether := result.User.Password()
+				Expect(password).To(Equal("password"))
+				Expect(whether).To(BeTrue())
+
+				Expect(result.Host).To(Equal("127.0.0.1:5432"))
+				Expect(result.Path).To(Equal("/database"))
+				Expect(result.RawQuery).To(Equal("sslmode=disable"))
+			})
+
+			It("for SQLite", func() {
+				// sqlite:///data/database.sqlite
+				// sqlite3:///data/database.sqlite
+
+				url := "sqlite:///data/database.sqlite"
+				result, err := common.ParseUrl(url)
+
+				Expect(err).To(Succeed())
+				Expect(result.Scheme).To(Equal("sqlite"))
+				Expect(result.Host).To(Equal(""))
+				Expect(result.Path).To(Equal("/data/database.sqlite"))
+			})
+		})
+
+		Context("Fail", func() {
+			It("for empty", func() {
+				_, err := common.ParseUrl("")
+
+				Expect(err).NotTo(Succeed())
+				Expect(err.Error()).To(Equal("protocol scheme is not defined"))
+			})
+
+			It("for else", func() {
+				url := "12345"
+				_, err := common.ParseUrl(url)
+
+				Expect(err).NotTo(Succeed())
+				Expect(err.Error()).To(Equal("protocol scheme is not defined"))
+			})
+
+			It("for not supported", func() {
+				url := "example:///some-else"
+				_, err := common.ParseUrl(url)
+
+				Expect(err).NotTo(Succeed())
+				Expect(err.Error()).To(Equal("unsupported protocol scheme: example"))
+			})
+		})
+	})
+
+	Context("OpenDB", func() {
+		var migrationsDir string
+
+		BeforeEach(func() {
+			var err error
+			migrationsDir, err = filepath.Abs("../../db/migrations")
+			Expect(err).To(Succeed())
+		})
+
+		Context("Success", func() {
+			// // Note: you need to up MySQL server for this test case
+			// It("for MySQL", func() {
+			// 	databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
+			// 	Expect(err).To(Succeed())
+
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir)
+			// 	Expect(err).To(Succeed())
+			// 	Expect(db.Close()).To(Succeed())
+			// })
+
+			// // Note: you need to up PostgreSQL server for this test case
+			// It("for PostgreSQL", func() {
+			// 	databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
+			// 	Expect(err).To(Succeed())
+
+			// 	db, err := common.OpenDB(databaseURL, migrationsDir)
+			// 	Expect(err).To(Succeed())
+			// 	Expect(db.Close()).To(Succeed())
+			// })
+
+			It("for SQLite", func() {
+				f, err := ioutil.TempFile("", "go-sqlite3-test-")
+				Expect(err).To(Succeed())
+				f.Close()
+
+				databaseURL, err := url.Parse("sqlite://" + f.Name())
+				Expect(err).To(Succeed())
+
+				db, err := common.OpenDB(databaseURL, migrationsDir)
+				Expect(err).To(Succeed())
+				Expect(db.Close()).To(Succeed())
+			})
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "gosql/common")
+}

+ 38 - 0
gosql/common/dbmethods.go

@@ -0,0 +1,38 @@
+package common
+
+import (
+	"context"
+	"database/sql"
+	"regexp"
+)
+
+type DBMethods struct {
+	*sql.DB
+
+	Driver string
+}
+
+var r = regexp.MustCompile(`\$\d+`)
+
+func (db *DBMethods) fixQuery(query string) string {
+	if db.Driver == "mysql" {
+		return r.ReplaceAllString(query, "?")
+	}
+	return query
+}
+
+func (db *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
+	return db.DB.ExecContext(ctx, db.fixQuery(query), args...)
+}
+
+func (db *DBMethods) Ping(ctx context.Context) error {
+	return db.PingContext(ctx)
+}
+
+func (db *DBMethods) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
+	return db.DB.QueryContext(ctx, db.fixQuery(query), args...)
+}
+
+func (db *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *sql.Row {
+	return db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
+}

+ 64 - 0
gosql/engine/engine.go

@@ -0,0 +1,64 @@
+package engine
+
+import (
+	"net/url"
+
+	"github.com/vladimirok5959/golang-sql/gosql/common"
+)
+
+// ----------------------------------------------------------------------------
+
+type mysql struct {
+	*common.DBMethods
+}
+
+func NewMySQL(dbURL *url.URL, migrationsDir string) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir)
+	if err != nil {
+		return nil, err
+	}
+
+	return &mysql{
+		DBMethods: &common.DBMethods{
+			DB: db, Driver: dbURL.Scheme,
+		},
+	}, nil
+}
+
+// ----------------------------------------------------------------------------
+
+type postgresql struct {
+	*common.DBMethods
+}
+
+func NewPostgreSQL(dbURL *url.URL, migrationsDir string) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir)
+	if err != nil {
+		return nil, err
+	}
+
+	return &postgresql{
+		DBMethods: &common.DBMethods{
+			DB: db, Driver: dbURL.Scheme,
+		},
+	}, nil
+}
+
+// ----------------------------------------------------------------------------
+
+type sqlite struct {
+	*common.DBMethods
+}
+
+func NewSQLite(dbURL *url.URL, migrationsDir string) (common.Engine, error) {
+	db, err := common.OpenDB(dbURL, migrationsDir)
+	if err != nil {
+		return nil, err
+	}
+
+	return &sqlite{
+		DBMethods: &common.DBMethods{
+			DB: db, Driver: dbURL.Scheme,
+		},
+	}, nil
+}

+ 17 - 0
gosql/engine/engine_test.go

@@ -0,0 +1,17 @@
+package engine_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+)
+
+var _ = Describe("engine", func() {
+	// Nothing to test here
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "gosql/engine")
+}

+ 26 - 0
gosql/gosql.go

@@ -0,0 +1,26 @@
+package gosql
+
+import (
+	"fmt"
+
+	"github.com/vladimirok5959/golang-sql/gosql/common"
+	"github.com/vladimirok5959/golang-sql/gosql/engine"
+)
+
+func Open(dbURL, migrationsDir string) (common.Engine, error) {
+	databaseURL, err := common.ParseUrl(dbURL)
+	if err != nil {
+		return nil, err
+	}
+
+	switch databaseURL.Scheme {
+	case "mysql":
+		return engine.NewMySQL(databaseURL, migrationsDir)
+	case "postgres", "postgresql":
+		return engine.NewPostgreSQL(databaseURL, migrationsDir)
+	case "sqlite", "sqlite3":
+		return engine.NewSQLite(databaseURL, migrationsDir)
+	default:
+		return nil, fmt.Errorf("DB open error")
+	}
+}

+ 99 - 0
gosql/gosql_test.go

@@ -0,0 +1,99 @@
+package gosql_test
+
+import (
+	"context"
+	"io/ioutil"
+	"path/filepath"
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-sql/gosql"
+)
+
+var _ = Describe("gosql", func() {
+	Context("Open", func() {
+		var migrationsDir string
+		var ctx = context.Background()
+		var sql = "select id, name from users where id=$1"
+
+		var (
+			id   int
+			name string
+		)
+
+		BeforeEach(func() {
+			var err error
+			migrationsDir, err = filepath.Abs("../db/migrations")
+			Expect(err).To(Succeed())
+		})
+
+		// // Note: you need to up MySQL server for this test case
+		// Context("for MySQL", func() {
+		// 	It("open connection, migrate and select data", func() {
+		// 		db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir)
+		// 		Expect(err).To(Succeed())
+
+		// 		err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(id).To(Equal(1))
+		// 		Expect(name).To(Equal("alice"))
+
+		// 		err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(id).To(Equal(2))
+		// 		Expect(name).To(Equal("bob"))
+
+		// 		Expect(db.Close()).To(Succeed())
+		// 	})
+		// })
+
+		// // Note: you need to up PostgreSQL server for this test case
+		// Context("for PostgreSQL", func() {
+		// 	It("open connection, migrate and select data", func() {
+		// 		db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir)
+		// 		Expect(err).To(Succeed())
+
+		// 		err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(id).To(Equal(1))
+		// 		Expect(name).To(Equal("alice"))
+
+		// 		err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
+		// 		Expect(err).To(Succeed())
+		// 		Expect(id).To(Equal(2))
+		// 		Expect(name).To(Equal("bob"))
+
+		// 		Expect(db.Close()).To(Succeed())
+		// 	})
+		// })
+
+		Context("for SQLite", func() {
+			It("open connection, migrate and select data", func() {
+				f, err := ioutil.TempFile("", "go-sqlite-test-")
+				Expect(err).To(Succeed())
+				f.Close()
+
+				db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir)
+				Expect(err).To(Succeed())
+
+				err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
+				Expect(err).To(Succeed())
+				Expect(id).To(Equal(1))
+				Expect(name).To(Equal("alice"))
+
+				err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
+				Expect(err).To(Succeed())
+				Expect(id).To(Equal(2))
+				Expect(name).To(Equal("bob"))
+
+				Expect(db.Close()).To(Succeed())
+			})
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "gosql")
+}