forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvp.go
151 lines (130 loc) · 3.27 KB
/
tvp.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/Melarian/go-mssqldb"
"log"
)
var (
debug = flag.Bool("debug", false, "enable debugging")
password = flag.String("password", "", "the database password")
port = flag.Int("port", 1433, "the database port")
server = flag.String("server", "", "the database server")
user = flag.String("user", "", "the database user")
)
type TvpExample struct {
MessageWithoutAnyTag string
MessageWithJSONTag string `json:"message"`
MessageWithTVPTag string `tvp:"message"`
MessageJSONSkipWithTVPTag string `json:"-" tvp:"message"`
OmitFieldJSONTag string `json:"-"`
OmitFieldTVPTag string `json:"any" tvp:"-"`
OmitFieldTVPTag2 string `tvp:"-"`
}
const (
crateSchema = `create schema TestTVPSchema;`
dropSchema = `drop schema TestTVPSchema;`
createTVP = `
CREATE TYPE TestTVPSchema.exampleTVP AS TABLE
(
message1 NVARCHAR(100),
message2 NVARCHAR(100),
message3 NVARCHAR(100),
message4 NVARCHAR(100)
)`
dropTVP = `DROP TYPE TestTVPSchema.exampleTVP;`
procedureWithTVP = `
CREATE PROCEDURE ExecTVP
@param1 TestTVPSchema.exampleTVP READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM @param1;
END;
`
dropProcedure = `drop PROCEDURE ExecTVP`
execTvp = `exec ExecTVP @param1;`
)
func main() {
flag.Parse()
if *debug {
fmt.Printf(" password:%s\n", *password)
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *user)
}
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
conn, err := sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()
_, err = conn.Exec(crateSchema)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropSchema)
_, err = conn.Exec(createTVP)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropTVP)
_, err = conn.Exec(procedureWithTVP)
if err != nil {
log.Println(err)
return
}
defer conn.Exec(dropProcedure)
exampleData := []TvpExample{
{
MessageWithoutAnyTag: "Hello1",
MessageWithJSONTag: "Hello2",
MessageWithTVPTag: "Hello3",
MessageJSONSkipWithTVPTag: "Hello4",
OmitFieldJSONTag: "Hello5",
OmitFieldTVPTag: "Hello6",
OmitFieldTVPTag2: "Hello7",
},
{
MessageWithoutAnyTag: "World1",
MessageWithJSONTag: "World2",
MessageWithTVPTag: "World3",
MessageJSONSkipWithTVPTag: "World4",
OmitFieldJSONTag: "World5",
OmitFieldTVPTag: "World6",
OmitFieldTVPTag2: "World7",
},
}
tvpType := mssql.TVP{
TypeName: "TestTVPSchema.exampleTVP",
Value: exampleData,
}
rows, err := conn.Query(execTvp,
sql.Named("param1", tvpType),
)
if err != nil {
log.Println(err)
return
}
tvpResult := make([]TvpExample, 0)
for rows.Next() {
tvpExample := TvpExample{}
err = rows.Scan(&tvpExample.MessageWithoutAnyTag,
&tvpExample.MessageWithJSONTag,
&tvpExample.MessageWithTVPTag,
&tvpExample.MessageJSONSkipWithTVPTag,
)
if err != nil {
log.Println(err)
return
}
tvpResult = append(tvpResult, tvpExample)
}
fmt.Println(tvpResult)
}