forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkimport_example_test.go
116 lines (100 loc) · 2.53 KB
/
bulkimport_example_test.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
// +build go1.10
package mssql_test
import (
"database/sql"
"flag"
"fmt"
"log"
"strings"
"unicode/utf8"
"github.com/denisenkom/go-mssqldb"
)
const (
createTestTable = `CREATE TABLE test_table(
id int IDENTITY(1,1) NOT NULL,
test_nvarchar nvarchar(50) NULL,
test_varchar varchar(50) NULL,
test_float float NULL,
test_datetime2_3 datetime2(3) NULL,
test_bitn bit NULL,
test_bigint bigint NOT NULL,
test_geom geometry NULL,
CONSTRAINT PK_table_test_id PRIMARY KEY CLUSTERED
(
id ASC
) ON [PRIMARY]);`
dropTestTable = "IF OBJECT_ID('test_table', 'U') IS NOT NULL DROP TABLE test_table;"
)
// This example shows how to perform bulk imports
func ExampleCopyIn() {
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 := makeConnURL().String()
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
db, err := sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer db.Close()
txn, err := db.Begin()
if err != nil {
log.Fatal(err)
}
// Create table
_, err = db.Exec(createTestTable)
if err != nil {
log.Fatal(err)
}
defer db.Exec(dropTestTable)
// mssqldb.CopyIn creates string to be consumed by Prepare
stmt, err := txn.Prepare(mssql.CopyIn("test_table", mssql.BulkOptions{}, "test_varchar", "test_nvarchar", "test_float", "test_bigint"))
if err != nil {
log.Fatal(err.Error())
}
for i := 0; i < 10; i++ {
_, err = stmt.Exec(generateString(0, 30), generateStringUnicode(0, 30), i, i)
if err != nil {
log.Fatal(err.Error())
}
}
result, err := stmt.Exec()
if err != nil {
log.Fatal(err)
}
err = stmt.Close()
if err != nil {
log.Fatal(err)
}
err = txn.Commit()
if err != nil {
log.Fatal(err)
}
rowCount, _ := result.RowsAffected()
log.Printf("%d row copied\n", rowCount)
log.Printf("bye\n")
}
func generateString(x int, n int) string {
letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, n)
for i := range b {
b[i] = letters[(x+i)%len(letters)]
}
return string(b)
}
func generateStringUnicode(x int, n int) string {
letters := []byte("ab©💾é?ghïjklmnopqЯ☀tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := &strings.Builder{}
for i := 0; i < n; i++ {
r, sz := utf8.DecodeRune(letters[x%len(letters):])
x += sz
b.WriteRune(r)
}
return b.String()
}