forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (51 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"database/sql/driver"
"fmt"
"io"
"os"
go_ora "github.com/sijms/go-ora"
)
func dieOnError(msg string, err error) {
if err != nil {
fmt.Println(msg, err)
os.Exit(1)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("\nhello_ora")
fmt.Println("\thello_ora check if it can connect to the given oracle server, then print server banner.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println("\thello_ora oracle://user:pass@server/service_name")
fmt.Println()
os.Exit(1)
}
connStr := os.ExpandEnv(os.Args[1])
conn, err := go_ora.NewConnection(connStr)
dieOnError("Can't create connection:", err)
err = conn.Open()
dieOnError("Can't open connection:", err)
defer conn.Close()
err = conn.Ping(context.Background())
dieOnError("Can't ping connection:", err)
fmt.Println("\nSuccessfully connected.\n")
stmt := go_ora.NewStmt("SELECT * FROM v$version", conn)
defer stmt.Close()
rows, err := stmt.Query(nil)
dieOnError("Can't create query:", err)
defer rows.Close()
values := make([]driver.Value, 1)
for {
err = rows.Next(values)
if err != nil {
break
}
fmt.Println(values[0])
}
if err != nil && err != io.EOF {
dieOnError("Can't fetch row:", err)
}
}