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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package mssql
import (
"database/sql"
"encoding/hex"
"log"
"math"
"reflect"
"strings"
"testing"
"time"
)
func TestBulkcopy(t *testing.T) {
// TDS level Bulk Insert is not supported on Azure SQL Server.
if dsn := makeConnStr(t); strings.HasSuffix(strings.Split(dsn.Host, ":")[0], ".database.windows.net") {
t.Skip("TDS level bulk copy is not supported on Azure SQL Server")
}
type testValue struct {
colname string
val interface{}
}
tableName := "#table_test"
geom, _ := hex.DecodeString("E6100000010C00000000000034400000000000004440")
testValues := []testValue{
{"test_nvarchar", "ab©ĎéⒻghïjklmnopqЯ☀tuvwxyz"},
{"test_varchar", "abcdefg"},
{"test_char", "abcdefg "},
{"test_nchar", "abcdefg "},
{"test_text", "abcdefg"},
{"test_ntext", "abcdefg"},
{"test_float", 1234.56},
{"test_floatn", 1234.56},
{"test_real", 1234.56},
{"test_realn", 1234.56},
{"test_bit", true},
{"test_bitn", nil},
{"test_smalldatetime", time.Date(2010, 11, 12, 13, 14, 0, 0, time.UTC)},
{"test_smalldatetimen", time.Date(2010, 11, 12, 13, 14, 0, 0, time.UTC)},
{"test_datetime", time.Date(2010, 11, 12, 13, 14, 15, 120000000, time.UTC)},
{"test_datetimen", time.Date(2010, 11, 12, 13, 14, 15, 120000000, time.UTC)},
{"test_datetime2_1", time.Date(2010, 11, 12, 13, 14, 15, 0, time.UTC)},
{"test_datetime2_3", time.Date(2010, 11, 12, 13, 14, 15, 123000000, time.UTC)},
{"test_datetime2_7", time.Date(2010, 11, 12, 13, 14, 15, 123000000, time.UTC)},
{"test_date", time.Date(2010, 11, 12, 00, 00, 00, 0, time.UTC)},
{"test_tinyint", 255},
{"test_smallint", 32767},
{"test_smallintn", nil},
{"test_int", 2147483647},
{"test_bigint", 9223372036854775807},
{"test_bigintn", nil},
{"test_geom", geom},
//{"test_smallmoney", nil},
//{"test_money", nil},
//{"test_decimal_18_0", nil},
//{"test_decimal_9_2", nil},
//{"test_decimal_18_0", nil},
}
columns := make([]string, len(testValues))
for i, val := range testValues {
columns[i] = val.colname
}
values := make([]interface{}, len(testValues))
for i, val := range testValues {
values[i] = val.val
}
conn := open(t)
defer conn.Close()
setupTable(conn, tableName)
stmt, err := conn.Prepare(CopyIn(tableName, MssqlBulkOptions{}, columns...))
for i := 0; i < 10; i++ {
_, err = stmt.Exec(values...)
if err != nil {
t.Error("AddRow failed: ", err.Error())
return
}
}
result, err := stmt.Exec()
if err != nil {
t.Fatal("bulkcopy failed: ", err.Error())
}
insertedRowCount, _ := result.RowsAffected()
if insertedRowCount == 0 {
t.Fatal("0 row inserted!")
}
//check that all rows are present
var rowCount int
err = conn.QueryRow("select count(*) c from " + tableName).Scan(&rowCount)
if rowCount != 10 {
t.Errorf("unexpected row count %d", rowCount)
}
//data verification
rows, err := conn.Query("select " + strings.Join(columns, ",") + " from " + tableName)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
ptrs := make([]interface{}, len(columns))
container := make([]interface{}, len(columns))
for i, _ := range ptrs {
ptrs[i] = &container[i]
}
if err := rows.Scan(ptrs...); err != nil {
t.Fatal(err)
}
for i, c := range testValues {
if !compareValue(container[i], c.val) {
t.Errorf("columns %s : %s != %s\n", c.colname, container[i], c.val)
}
}
}
if err := rows.Err(); err != nil {
t.Error(err)
}
}
func compareValue(a interface{}, expected interface{}) bool {
switch expected := expected.(type) {
case int:
return int64(expected) == a
case int32:
return int64(expected) == a
case int64:
return int64(expected) == a
case float64:
return math.Abs(expected-a.(float64)) < 0.0001
default:
return reflect.DeepEqual(expected, a)
}
}
func setupTable(conn *sql.DB, tableName string) {
tablesql := `CREATE TABLE ` + tableName + ` (
[id] [int] IDENTITY(1,1) NOT NULL,
[test_nvarchar] [nvarchar](50) NULL,
[test_varchar] [varchar](50) NULL,
[test_char] [char](10) NULL,
[test_nchar] [nchar](10) NULL,
[test_text] [text] NULL,
[test_ntext] [ntext] NULL,
[test_float] [float] NOT NULL,
[test_floatn] [float] NULL,
[test_real] [real] NULL,
[test_realn] [real] NULL,
[test_bit] [bit] NOT NULL,
[test_bitn] [bit] NULL,
[test_smalldatetime] [smalldatetime] NOT NULL,
[test_smalldatetimen] [smalldatetime] NULL,
[test_datetime] [datetime] NOT NULL,
[test_datetimen] [datetime] NULL,
[test_datetime2_1] [datetime2](1) NULL,
[test_datetime2_3] [datetime2](3) NULL,
[test_datetime2_7] [datetime2](7) NULL,
[test_date] [date] NULL,
[test_smallmoney] [smallmoney] NULL,
[test_money] [money] NULL,
[test_tinyint] [tinyint] NULL,
[test_smallint] [smallint] NOT NULL,
[test_smallintn] [smallint] NULL,
[test_int] [int] NULL,
[test_bigint] [bigint] NOT NULL,
[test_bigintn] [bigint] NULL,
[test_geom] [geometry] NULL,
[test_geog] [geography] NULL,
[text_xml] [xml] NULL,
[test_uniqueidentifier] [uniqueidentifier] NULL,
[test_decimal_18_0] [decimal](18, 0) NULL,
[test_decimal_9_2] [decimal](9, 2) NULL,
[test_decimal_20_0] [decimal](20, 0) NULL,
CONSTRAINT [PK_` + tableName + `_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];`
_, err := conn.Exec(tablesql)
if err != nil {
log.Fatal("tablesql failed:", err)
}
}
|