Skip to content

Commit 122f152

Browse files
committed
finishing basic sql connections and with amp connections
1 parent f0ae7f3 commit 122f152

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Overview
2+
libpostgres is a collection of postgresql connections from various sources such as making connections with amp, elastic, etc.
3+
4+
# Constribute
5+
Please welcome .

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/jiharal/libpostgres
2+
3+
go 1.14
4+
5+
require (
6+
github.com/lib/pq v1.0.0
7+
github.com/stretchr/testify v1.5.1
8+
go.elastic.co/apm/module/apmsql v1.7.2
9+
)

go.sum

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
2+
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
3+
github.com/cucumber/godog v0.8.1/go.mod h1:vSh3r/lM+psC1BPXvdkSEuNjmXfpVqrMGYAElF6hxnA=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/elastic/go-sysinfo v1.1.1 h1:ZVlaLDyhVkDfjwPGU55CQRCRolNpc7P0BbyhhQZQmMI=
8+
github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0=
9+
github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
10+
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
11+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
12+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
13+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
14+
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
15+
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
16+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
17+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
18+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
19+
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
20+
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
21+
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
22+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
23+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
24+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
25+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
26+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
27+
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
28+
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
29+
github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4=
30+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
31+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
32+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
33+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
34+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
35+
go.elastic.co/apm v1.7.2 h1:0nwzVIPp4PDBXSYYtN19+1W5V+sj+C25UjqxDVoKcA8=
36+
go.elastic.co/apm v1.7.2/go.mod h1:tCw6CkOJgkWnzEthFN9HUP1uL3Gjc/Ur6m7gRPLaoH0=
37+
go.elastic.co/apm/module/apmsql v1.7.2 h1:gKtSmY03scRoOkcfPLFLp6H7UC3Fu5vg+wz5SebQ1R0=
38+
go.elastic.co/apm/module/apmsql v1.7.2/go.mod h1:T4Kdfo+urreLSeGHWq9t+kVnEOVDGKmiw6NTke/7C8w=
39+
go.elastic.co/fastjson v1.0.0 h1:ooXV/ABvf+tBul26jcVViPT3sBir0PvXgibYB1IQQzg=
40+
go.elastic.co/fastjson v1.0.0/go.mod h1:PmeUOMMtLHQr9ZS9J9owrAVg0FkaZDRZJEFTTGHtchs=
41+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
42+
golang.org/x/sys v0.0.0-20191025021431-6c3a3bfe00ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43+
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e h1:9vRrk9YW2BTzLP0VCB9ZDjU4cPqkg+IDWL7XgxA1yxQ=
44+
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
46+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
47+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
48+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
49+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
50+
howett.net/plist v0.0.0-20181124034731-591f970eefbb h1:jhnBjNi9UFpfpl8YZhA9CrOqpnJdvzuiHsl/dnxl11M=
51+
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=

libpostgres.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package libpostgres
2+
3+
import (
4+
"database/sql"
5+
"errors"
6+
"fmt"
7+
8+
_ "github.com/lib/pq"
9+
_ "go.elastic.co/apm/module/apmsql/pq"
10+
11+
"go.elastic.co/apm/module/apmsql"
12+
)
13+
14+
// Options is a struct
15+
type Options struct {
16+
Host string
17+
Port int
18+
Username string
19+
Password string
20+
DBName string
21+
ConnectTimeout int
22+
SSLCert string
23+
SSLKey string
24+
SSLRootCert string
25+
SSLMode string
26+
}
27+
28+
var sslModes []string = []string{"disable", "allow", "prefer", "require", "verify-ca", "verify-full"}
29+
30+
func isValidSSLMode(sslMode string) bool {
31+
for _, v := range sslModes {
32+
if sslMode == v {
33+
return true
34+
}
35+
}
36+
37+
return false
38+
}
39+
40+
// Connect is ..
41+
func Connect(options Options) (*sql.DB, error) {
42+
sslMode := "sslmode=disabled"
43+
44+
if options.SSLMode != "" && options.SSLMode != "disabled" {
45+
if !isValidSSLMode(options.SSLMode) {
46+
return nil, errors.New("invalid ssl mode")
47+
}
48+
49+
sslMode = fmt.Sprintf("sslmode=%s&sslrootcert=%s&sslcert=%s&sslkey=%s",
50+
options.SSLMode,
51+
options.SSLRootCert,
52+
options.SSLCert,
53+
options.SSLKey)
54+
}
55+
56+
dbConfig := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?%s",
57+
options.Username,
58+
options.Password,
59+
options.Host,
60+
options.Port,
61+
options.DBName,
62+
sslMode)
63+
64+
db, err := sql.Open("postgres", dbConfig)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
err = db.Ping()
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
return db, nil
75+
}
76+
77+
// ConnectWithAMP is ...
78+
func ConnectWithAMP(options Options) (*sql.DB, error) {
79+
sslMode := "sslmode=disabled"
80+
81+
if options.SSLMode != "" && options.SSLMode != "disabled" {
82+
if !isValidSSLMode(options.SSLMode) {
83+
return nil, errors.New("invalid ssl mode")
84+
}
85+
86+
sslMode = fmt.Sprintf("sslmode=%s&sslrootcert=%s&sslcert=%s&sslkey=%s",
87+
options.SSLMode,
88+
options.SSLRootCert,
89+
options.SSLCert,
90+
options.SSLKey)
91+
}
92+
93+
dbConfig := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?%s",
94+
options.Username,
95+
options.Password,
96+
options.Host,
97+
options.Port,
98+
options.DBName,
99+
sslMode)
100+
101+
db, err := apmsql.Open("postgres", dbConfig)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
err = db.Ping()
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
return db, nil
112+
}

libpostgres_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package libpostgres
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestLibpostgres(t *testing.T) {
10+
opt := Options{
11+
Host: "localhost",
12+
Port: 5432,
13+
Username: "postgres",
14+
Password: "",
15+
DBName: "hello_db",
16+
SSLMode: "disable",
17+
}
18+
db, err := ConnectWithAMP(opt)
19+
if err != nil {
20+
require.NoError(t, err)
21+
}
22+
defer db.Close()
23+
var name string
24+
err = db.QueryRow("select first_name from customer limit 1").Scan(&name)
25+
require.NoError(t, err)
26+
t.Log(name)
27+
28+
db1, err := Connect(opt)
29+
if err != nil {
30+
require.NoError(t, err)
31+
}
32+
defer db1.Close()
33+
var name1 string
34+
err = db1.QueryRow("select first_name from customer limit 1").Scan(&name1)
35+
require.NoError(t, err)
36+
t.Log(name1)
37+
38+
}

0 commit comments

Comments
 (0)