forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (76 loc) · 1.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"fmt"
go_ora "github.com/chenyb888/go-ora/v2"
"os"
)
//func dieOnError(msg string, err error) {
// if err != nil {
// fmt.Println(msg, err)
// os.Exit(1)
// }
//}
func usage() {
fmt.Println()
fmt.Println("exec")
fmt.Println(" execute DML oracle statement.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` exec -server server_url sql_text`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` exec -server "oracle://user:pass@server/service_name" "update my_table set col_1 = val1 where id = val2"`)
fmt.Println()
}
func main() {
var (
server string
sqlText string
)
flag.StringVar(&server, "server", "", "Server's URL, oracle://user:pass@server/service_name")
flag.Parse()
if len(flag.Args()) < 1 {
fmt.Println("Missing sql text")
usage()
os.Exit(1)
}
sqlText = flag.Arg(0)
connStr := os.ExpandEnv(server)
if connStr == "" {
fmt.Println("Missing -server option")
usage()
os.Exit(1)
}
DB, err := go_ora.NewConnection(connStr)
if err != nil {
fmt.Println("Can't create new connection", err)
return
}
err = DB.Open()
if err != nil {
fmt.Println("Can't open the connection:", err)
return
}
defer func() {
err = DB.Close()
if err != nil {
fmt.Println("Can't close connection", err)
}
}()
stmt := go_ora.NewStmt(sqlText, DB)
defer func() {
err = stmt.Close()
if err != nil {
fmt.Println("Can't close stmt", err)
}
}()
result, err := stmt.Exec(nil)
if err != nil {
fmt.Println("Can't execute sql", err)
return
}
rowsAffected, _ := result.RowsAffected()
fmt.Println("rows affected: ", rowsAffected)
}