Debian Codegraph @

bulkcopy_test.go

raw symbols SCIP
1 package mssql
2
3 import (
4 "database/sql"
5 "encoding/hex"
6 "log"
7 "math"
8 "reflect"
9 "strings"
10 "testing"
11 "time"
12 )
13
14 func TestBulkcopy(t *testing.T) {
15 // TDS level Bulk Insert is not supported on Azure SQL Server.
16 if dsn := makeConnStr(t); strings.HasSuffix(strings.Split(dsn.Host, ":")[0], ".database.windows.net") {
17 t.Skip("TDS level bulk copy is not supported on Azure SQL Server")
18 }
19 type testValue struct {
20 colname string
21 val interface{}
22 }
23 tableName := "#table_test"
24 geom, _ := hex.DecodeString("E6100000010C00000000000034400000000000004440")
25 testValues := []testValue{
26
27 {"test_nvarchar", "ab©ĎéⒻghïjklmnopqЯ☀tuvwxyz"},
28 {"test_varchar", "abcdefg"},
29 {"test_char", "abcdefg "},
30 {"test_nchar", "abcdefg "},
31 {"test_text", "abcdefg"},
32 {"test_ntext", "abcdefg"},
33 {"test_float", 1234.56},
34 {"test_floatn", 1234.56},
35 {"test_real", 1234.56},
36 {"test_realn", 1234.56},
37 {"test_bit", true},
38 {"test_bitn", nil},
39 {"test_smalldatetime", time.Date(2010, 11, 12, 13, 14, 0, 0, time.UTC)},
40 {"test_smalldatetimen", time.Date(2010, 11, 12, 13, 14, 0, 0, time.UTC)},
41 {"test_datetime", time.Date(2010, 11, 12, 13, 14, 15, 120000000, time.UTC)},
42 {"test_datetimen", time.Date(2010, 11, 12, 13, 14, 15, 120000000, time.UTC)},
43 {"test_datetime2_1", time.Date(2010, 11, 12, 13, 14, 15, 0, time.UTC)},
44 {"test_datetime2_3", time.Date(2010, 11, 12, 13, 14, 15, 123000000, time.UTC)},
45 {"test_datetime2_7", time.Date(2010, 11, 12, 13, 14, 15, 123000000, time.UTC)},
46 {"test_date", time.Date(2010, 11, 12, 00, 00, 00, 0, time.UTC)},
47 {"test_tinyint", 255},
48 {"test_smallint", 32767},
49 {"test_smallintn", nil},
50 {"test_int", 2147483647},
51 {"test_bigint", 9223372036854775807},
52 {"test_bigintn", nil},
53 {"test_geom", geom},
54 //{"test_smallmoney", nil},
55 //{"test_money", nil},
56 //{"test_decimal_18_0", nil},
57 //{"test_decimal_9_2", nil},
58 //{"test_decimal_18_0", nil},
59 }
60
61 columns := make([]string, len(testValues))
62 for i, val := range testValues {
63 columns[i] = val.colname
64 }
65
66 values := make([]interface{}, len(testValues))
67 for i, val := range testValues {
68 values[i] = val.val
69 }
70
71 conn := open(t)
72 defer conn.Close()
73
74 setupTable(conn, tableName)
75
76 stmt, err := conn.Prepare(CopyIn(tableName, MssqlBulkOptions{}, columns...))
77
78 for i := 0; i < 10; i++ {
79 _, err = stmt.Exec(values...)
80 if err != nil {
81 t.Error("AddRow failed: ", err.Error())
82 return
83 }
84 }
85
86 result, err := stmt.Exec()
87 if err != nil {
88 t.Fatal("bulkcopy failed: ", err.Error())
89 }
90
91 insertedRowCount, _ := result.RowsAffected()
92 if insertedRowCount == 0 {
93 t.Fatal("0 row inserted!")
94 }
95
96 //check that all rows are present
97 var rowCount int
98 err = conn.QueryRow("select count(*) c from " + tableName).Scan(&rowCount)
99
100 if rowCount != 10 {
101 t.Errorf("unexpected row count %d", rowCount)
102 }
103
104 //data verification
105 rows, err := conn.Query("select " + strings.Join(columns, ",") + " from " + tableName)
106 if err != nil {
107 log.Fatal(err)
108 }
109 defer rows.Close()
110 for rows.Next() {
111
112 ptrs := make([]interface{}, len(columns))
113 container := make([]interface{}, len(columns))
114 for i, _ := range ptrs {
115 ptrs[i] = &container[i]
116 }
117 if err := rows.Scan(ptrs...); err != nil {
118 t.Fatal(err)
119 }
120 for i, c := range testValues {
121 if !compareValue(container[i], c.val) {
122 t.Errorf("columns %s : %s != %s\n", c.colname, container[i], c.val)
123 }
124 }
125 }
126 if err := rows.Err(); err != nil {
127 t.Error(err)
128 }
129 }
130
131 func compareValue(a interface{}, expected interface{}) bool {
132 switch expected := expected.(type) {
133 case int:
134 return int64(expected) == a
135 case int32:
136 return int64(expected) == a
137 case int64:
138 return int64(expected) == a
139 case float64:
140 return math.Abs(expected-a.(float64)) < 0.0001
141 default:
142 return reflect.DeepEqual(expected, a)
143 }
144 }
145 func setupTable(conn *sql.DB, tableName string) {
146
147 tablesql := `CREATE TABLE ` + tableName + ` (
148 [id] [int] IDENTITY(1,1) NOT NULL,
149 [test_nvarchar] [nvarchar](50) NULL,
150 [test_varchar] [varchar](50) NULL,
151 [test_char] [char](10) NULL,
152 [test_nchar] [nchar](10) NULL,
153 [test_text] [text] NULL,
154 [test_ntext] [ntext] NULL,
155 [test_float] [float] NOT NULL,
156 [test_floatn] [float] NULL,
157 [test_real] [real] NULL,
158 [test_realn] [real] NULL,
159 [test_bit] [bit] NOT NULL,
160 [test_bitn] [bit] NULL,
161 [test_smalldatetime] [smalldatetime] NOT NULL,
162 [test_smalldatetimen] [smalldatetime] NULL,
163 [test_datetime] [datetime] NOT NULL,
164 [test_datetimen] [datetime] NULL,
165 [test_datetime2_1] [datetime2](1) NULL,
166 [test_datetime2_3] [datetime2](3) NULL,
167 [test_datetime2_7] [datetime2](7) NULL,
168 [test_date] [date] NULL,
169 [test_smallmoney] [smallmoney] NULL,
170 [test_money] [money] NULL,
171 [test_tinyint] [tinyint] NULL,
172 [test_smallint] [smallint] NOT NULL,
173 [test_smallintn] [smallint] NULL,
174 [test_int] [int] NULL,
175 [test_bigint] [bigint] NOT NULL,
176 [test_bigintn] [bigint] NULL,
177 [test_geom] [geometry] NULL,
178 [test_geog] [geography] NULL,
179 [text_xml] [xml] NULL,
180 [test_uniqueidentifier] [uniqueidentifier] NULL,
181 [test_decimal_18_0] [decimal](18, 0) NULL,
182 [test_decimal_9_2] [decimal](9, 2) NULL,
183 [test_decimal_20_0] [decimal](20, 0) NULL,
184 CONSTRAINT [PK_` + tableName + `_id] PRIMARY KEY CLUSTERED
185 (
186 [id] ASC
187 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
188 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];`
189 _, err := conn.Exec(tablesql)
190 if err != nil {
191 log.Fatal("tablesql failed:", err)
192 }
193
194 }