forked from mattn/go-oci8
/
main.go
71 lines (61 loc) · 1.37 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
package main
import (
"database/sql"
"fmt"
"os"
"github.com/mattn/go-oci8"
)
type ID string
func (id ID) Scan(src interface{}) error {
fmt.Println(src)
return nil
}
func getDSN() string {
var dsn string
if len(os.Args) > 1 {
dsn = os.Args[1]
if dsn != "" {
return dsn
}
}
dsn = os.Getenv("GO_OCI8_CONNECT_STRING")
if dsn != "" {
return dsn
}
fmt.Fprintln(os.Stderr, `Please specifiy connection parameter in GO_OCI8_CONNECT_STRING environment variable,
or as the first argument! (The format is user/name@host:port/sid)`)
return "scott/tiger@XE"
}
func main() {
os.Setenv("NLS_LANG", "")
db, err := sql.Open("oci8", getDSN())
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
db.Exec("drop table lastinsertid_example")
_, err = db.Exec("create table lastinsertid_example(id varchar2(256) not null primary key, data varchar2(256))")
if err != nil {
fmt.Println(err)
return
}
res, err := db.Exec("insert into lastinsertid_example(id, data) values(:1, :2)", "001", "こんにちわ世界")
if err != nil {
fmt.Println(err)
return
}
lastInsertId, err := res.LastInsertId()
if err != nil {
fmt.Println(err)
return
}
rowID := oci8.GetLastInsertId(lastInsertId)
var id string
err = db.QueryRow("select id from lastinsertid_example where rowid = :1", rowID).Scan(&id)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(id)
}