| 1 |
package mssql |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"math" |
| 9 |
"strconv" |
| 10 |
"time" |
| 11 |
"reflect" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
const ( |
| 17 |
typeNull = 0x1f |
| 18 |
typeInt1 = 0x30 |
| 19 |
typeBit = 0x32 |
| 20 |
typeInt2 = 0x34 |
| 21 |
typeInt4 = 0x38 |
| 22 |
typeDateTim4 = 0x3a |
| 23 |
typeFlt4 = 0x3b |
| 24 |
typeMoney = 0x3c |
| 25 |
typeDateTime = 0x3d |
| 26 |
typeFlt8 = 0x3e |
| 27 |
typeMoney4 = 0x7a |
| 28 |
typeInt8 = 0x7f |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const ( |
| 34 |
|
| 35 |
typeGuid = 0x24 |
| 36 |
typeIntN = 0x26 |
| 37 |
typeDecimal = 0x37 |
| 38 |
typeNumeric = 0x3f |
| 39 |
typeBitN = 0x68 |
| 40 |
typeDecimalN = 0x6a |
| 41 |
typeNumericN = 0x6c |
| 42 |
typeFltN = 0x6d |
| 43 |
typeMoneyN = 0x6e |
| 44 |
typeDateTimeN = 0x6f |
| 45 |
typeDateN = 0x28 |
| 46 |
typeTimeN = 0x29 |
| 47 |
typeDateTime2N = 0x2a |
| 48 |
typeDateTimeOffsetN = 0x2b |
| 49 |
typeChar = 0x2f |
| 50 |
typeVarChar = 0x27 |
| 51 |
typeBinary = 0x2d |
| 52 |
typeVarBinary = 0x25 |
| 53 |
|
| 54 |
|
| 55 |
typeBigVarBin = 0xa5 |
| 56 |
typeBigVarChar = 0xa7 |
| 57 |
typeBigBinary = 0xad |
| 58 |
typeBigChar = 0xaf |
| 59 |
typeNVarChar = 0xe7 |
| 60 |
typeNChar = 0xef |
| 61 |
typeXml = 0xf1 |
| 62 |
typeUdt = 0xf0 |
| 63 |
|
| 64 |
|
| 65 |
typeText = 0x23 |
| 66 |
typeImage = 0x22 |
| 67 |
typeNText = 0x63 |
| 68 |
typeVariant = 0x62 |
| 69 |
) |
| 70 |
const PLP_NULL = 0xFFFFFFFFFFFFFFFF |
| 71 |
const UNKNOWN_PLP_LEN = 0xFFFFFFFFFFFFFFFE |
| 72 |
const PLP_TERMINATOR = 0x00000000 |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
type typeInfo struct { |
| 77 |
TypeId uint8 |
| 78 |
Size int |
| 79 |
Scale uint8 |
| 80 |
Prec uint8 |
| 81 |
Buffer []byte |
| 82 |
Collation collation |
| 83 |
UdtInfo udtInfo |
| 84 |
XmlInfo xmlInfo |
| 85 |
Reader func(ti *typeInfo, r *tdsBuffer) (res interface{}) |
| 86 |
Writer func(w io.Writer, ti typeInfo, buf []byte) (err error) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
type udtInfo struct { |
| 92 |
|
| 93 |
DBName string |
| 94 |
SchemaName string |
| 95 |
TypeName string |
| 96 |
AssemblyQualifiedName string |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
type xmlInfo struct { |
| 102 |
SchemaPresent uint8 |
| 103 |
DBName string |
| 104 |
OwningSchema string |
| 105 |
XmlSchemaCollection string |
| 106 |
} |
| 107 |
|
| 108 |
func readTypeInfo(r *tdsBuffer) (res typeInfo) { |
| 109 |
res.TypeId = r.byte() |
| 110 |
switch res.TypeId { |
| 111 |
case typeNull, typeInt1, typeBit, typeInt2, typeInt4, typeDateTim4, |
| 112 |
typeFlt4, typeMoney, typeDateTime, typeFlt8, typeMoney4, typeInt8: |
| 113 |
|
| 114 |
switch res.TypeId { |
| 115 |
case typeNull: |
| 116 |
res.Size = 0 |
| 117 |
case typeInt1, typeBit: |
| 118 |
res.Size = 1 |
| 119 |
case typeInt2: |
| 120 |
res.Size = 2 |
| 121 |
case typeInt4, typeDateTim4, typeFlt4, typeMoney4: |
| 122 |
res.Size = 4 |
| 123 |
case typeMoney, typeDateTime, typeFlt8, typeInt8: |
| 124 |
res.Size = 8 |
| 125 |
} |
| 126 |
res.Reader = readFixedType |
| 127 |
res.Buffer = make([]byte, res.Size) |
| 128 |
default: |
| 129 |
readVarLen(&res, r) |
| 130 |
} |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
func writeTypeInfo(w io.Writer, ti *typeInfo) (err error) { |
| 135 |
err = binary.Write(w, binary.LittleEndian, ti.TypeId) |
| 136 |
if err != nil { |
| 137 |
return |
| 138 |
} |
| 139 |
switch ti.TypeId { |
| 140 |
case typeNull, typeInt1, typeBit, typeInt2, typeInt4, typeDateTim4, |
| 141 |
typeFlt4, typeMoney, typeDateTime, typeFlt8, typeMoney4, typeInt8: |
| 142 |
|
| 143 |
ti.Writer = writeFixedType |
| 144 |
default: |
| 145 |
err = writeVarLen(w, ti) |
| 146 |
if err != nil { |
| 147 |
return |
| 148 |
} |
| 149 |
} |
| 150 |
return |
| 151 |
} |
| 152 |
|
| 153 |
func writeFixedType(w io.Writer, ti typeInfo, buf []byte) (err error) { |
| 154 |
_, err = w.Write(buf) |
| 155 |
return |
| 156 |
} |
| 157 |
|
| 158 |
func writeVarLen(w io.Writer, ti *typeInfo) (err error) { |
| 159 |
switch ti.TypeId { |
| 160 |
case typeDateN: |
| 161 |
ti.Writer = writeByteLenType |
| 162 |
case typeTimeN, typeDateTime2N, typeDateTimeOffsetN: |
| 163 |
if err = binary.Write(w, binary.LittleEndian, ti.Scale); err != nil { |
| 164 |
return |
| 165 |
} |
| 166 |
ti.Writer = writeByteLenType |
| 167 |
case typeIntN, typeDecimal, typeNumeric, |
| 168 |
typeBitN, typeDecimalN, typeNumericN, typeFltN, |
| 169 |
typeMoneyN, typeDateTimeN, typeChar, |
| 170 |
typeVarChar, typeBinary, typeVarBinary: |
| 171 |
|
| 172 |
|
| 173 |
if ti.Size > 0xff { |
| 174 |
panic("Invalid size for BYLELEN_TYPE") |
| 175 |
} |
| 176 |
if err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)); err != nil { |
| 177 |
return |
| 178 |
} |
| 179 |
switch ti.TypeId { |
| 180 |
case typeDecimal, typeNumeric, typeDecimalN, typeNumericN: |
| 181 |
err = binary.Write(w, binary.LittleEndian, ti.Prec) |
| 182 |
if err != nil { |
| 183 |
return |
| 184 |
} |
| 185 |
err = binary.Write(w, binary.LittleEndian, ti.Scale) |
| 186 |
if err != nil { |
| 187 |
return |
| 188 |
} |
| 189 |
} |
| 190 |
ti.Writer = writeByteLenType |
| 191 |
case typeGuid: |
| 192 |
if !(ti.Size == 0x10 || ti.Size == 0x00) { |
| 193 |
panic("Invalid size for BYLELEN_TYPE") |
| 194 |
} |
| 195 |
if err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)); err != nil { |
| 196 |
return |
| 197 |
} |
| 198 |
ti.Writer = writeByteLenType |
| 199 |
case typeBigVarBin, typeBigVarChar, typeBigBinary, typeBigChar, |
| 200 |
typeNVarChar, typeNChar, typeXml, typeUdt: |
| 201 |
|
| 202 |
if ti.Size > 8000 || ti.Size == 0 { |
| 203 |
if err = binary.Write(w, binary.LittleEndian, uint16(0xffff)); err != nil { |
| 204 |
return |
| 205 |
} |
| 206 |
ti.Writer = writePLPType |
| 207 |
} else { |
| 208 |
if err = binary.Write(w, binary.LittleEndian, uint16(ti.Size)); err != nil { |
| 209 |
return |
| 210 |
} |
| 211 |
ti.Writer = writeShortLenType |
| 212 |
} |
| 213 |
switch ti.TypeId { |
| 214 |
case typeBigVarChar, typeBigChar, typeNVarChar, typeNChar: |
| 215 |
if err = writeCollation(w, ti.Collation); err != nil { |
| 216 |
return |
| 217 |
} |
| 218 |
case typeXml: |
| 219 |
if err = binary.Write(w, binary.LittleEndian, ti.XmlInfo.SchemaPresent); err != nil { |
| 220 |
return |
| 221 |
} |
| 222 |
} |
| 223 |
case typeText, typeImage, typeNText, typeVariant: |
| 224 |
|
| 225 |
if err = binary.Write(w, binary.LittleEndian, uint32(ti.Size)); err != nil { |
| 226 |
return |
| 227 |
} |
| 228 |
if err = writeCollation(w, ti.Collation); err != nil { |
| 229 |
return |
| 230 |
} |
| 231 |
ti.Writer = writeLongLenType |
| 232 |
default: |
| 233 |
panic("Invalid type") |
| 234 |
} |
| 235 |
return |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
func decodeDateTim4(buf []byte) time.Time { |
| 240 |
days := binary.LittleEndian.Uint16(buf) |
| 241 |
mins := binary.LittleEndian.Uint16(buf[2:]) |
| 242 |
return time.Date(1900, 1, 1+int(days), |
| 243 |
0, int(mins), 0, 0, time.UTC) |
| 244 |
} |
| 245 |
|
| 246 |
func decodeDateTime(buf []byte) time.Time { |
| 247 |
days := int32(binary.LittleEndian.Uint32(buf)) |
| 248 |
tm := binary.LittleEndian.Uint32(buf[4:]) |
| 249 |
ns := int(math.Trunc(float64(tm%300)/0.3+0.5)) * 1000000 |
| 250 |
secs := int(tm / 300) |
| 251 |
return time.Date(1900, 1, 1+int(days), |
| 252 |
0, 0, secs, ns, time.UTC) |
| 253 |
} |
| 254 |
|
| 255 |
func readFixedType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 256 |
r.ReadFull(ti.Buffer) |
| 257 |
buf := ti.Buffer |
| 258 |
switch ti.TypeId { |
| 259 |
case typeNull: |
| 260 |
return nil |
| 261 |
case typeInt1: |
| 262 |
return int64(buf[0]) |
| 263 |
case typeBit: |
| 264 |
return buf[0] != 0 |
| 265 |
case typeInt2: |
| 266 |
return int64(int16(binary.LittleEndian.Uint16(buf))) |
| 267 |
case typeInt4: |
| 268 |
return int64(int32(binary.LittleEndian.Uint32(buf))) |
| 269 |
case typeDateTim4: |
| 270 |
return decodeDateTim4(buf) |
| 271 |
case typeFlt4: |
| 272 |
return math.Float32frombits(binary.LittleEndian.Uint32(buf)) |
| 273 |
case typeMoney4: |
| 274 |
return decodeMoney4(buf) |
| 275 |
case typeMoney: |
| 276 |
return decodeMoney(buf) |
| 277 |
case typeDateTime: |
| 278 |
return decodeDateTime(buf) |
| 279 |
case typeFlt8: |
| 280 |
return math.Float64frombits(binary.LittleEndian.Uint64(buf)) |
| 281 |
case typeInt8: |
| 282 |
return int64(binary.LittleEndian.Uint64(buf)) |
| 283 |
default: |
| 284 |
badStreamPanicf("Invalid typeid") |
| 285 |
} |
| 286 |
panic("shoulnd't get here") |
| 287 |
} |
| 288 |
|
| 289 |
func readByteLenType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 290 |
size := r.byte() |
| 291 |
if size == 0 { |
| 292 |
return nil |
| 293 |
} |
| 294 |
r.ReadFull(ti.Buffer[:size]) |
| 295 |
buf := ti.Buffer[:size] |
| 296 |
switch ti.TypeId { |
| 297 |
case typeDateN: |
| 298 |
if len(buf) != 3 { |
| 299 |
badStreamPanicf("Invalid size for DATENTYPE") |
| 300 |
} |
| 301 |
return decodeDate(buf) |
| 302 |
case typeTimeN: |
| 303 |
return decodeTime(ti.Scale, buf) |
| 304 |
case typeDateTime2N: |
| 305 |
return decodeDateTime2(ti.Scale, buf) |
| 306 |
case typeDateTimeOffsetN: |
| 307 |
return decodeDateTimeOffset(ti.Scale, buf) |
| 308 |
case typeGuid: |
| 309 |
return decodeGuid(buf) |
| 310 |
case typeIntN: |
| 311 |
switch len(buf) { |
| 312 |
case 1: |
| 313 |
return int64(buf[0]) |
| 314 |
case 2: |
| 315 |
return int64(int16((binary.LittleEndian.Uint16(buf)))) |
| 316 |
case 4: |
| 317 |
return int64(int32(binary.LittleEndian.Uint32(buf))) |
| 318 |
case 8: |
| 319 |
return int64(binary.LittleEndian.Uint64(buf)) |
| 320 |
default: |
| 321 |
badStreamPanicf("Invalid size for INTNTYPE") |
| 322 |
} |
| 323 |
case typeDecimal, typeNumeric, typeDecimalN, typeNumericN: |
| 324 |
return decodeDecimal(ti.Prec, ti.Scale, buf) |
| 325 |
case typeBitN: |
| 326 |
if len(buf) != 1 { |
| 327 |
badStreamPanicf("Invalid size for BITNTYPE") |
| 328 |
} |
| 329 |
return buf[0] != 0 |
| 330 |
case typeFltN: |
| 331 |
switch len(buf) { |
| 332 |
case 4: |
| 333 |
return float64(math.Float32frombits(binary.LittleEndian.Uint32(buf))) |
| 334 |
case 8: |
| 335 |
return math.Float64frombits(binary.LittleEndian.Uint64(buf)) |
| 336 |
default: |
| 337 |
badStreamPanicf("Invalid size for FLTNTYPE") |
| 338 |
} |
| 339 |
case typeMoneyN: |
| 340 |
switch len(buf) { |
| 341 |
case 4: |
| 342 |
return decodeMoney4(buf) |
| 343 |
case 8: |
| 344 |
return decodeMoney(buf) |
| 345 |
default: |
| 346 |
badStreamPanicf("Invalid size for MONEYNTYPE") |
| 347 |
} |
| 348 |
case typeDateTimeN: |
| 349 |
switch len(buf) { |
| 350 |
case 4: |
| 351 |
return decodeDateTim4(buf) |
| 352 |
case 8: |
| 353 |
return decodeDateTime(buf) |
| 354 |
default: |
| 355 |
badStreamPanicf("Invalid size for DATETIMENTYPE") |
| 356 |
} |
| 357 |
case typeChar, typeVarChar: |
| 358 |
return decodeChar(ti.Collation, buf) |
| 359 |
case typeBinary, typeVarBinary: |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
cpy := make([]byte, len(buf)) |
| 364 |
copy(cpy, buf) |
| 365 |
return cpy |
| 366 |
default: |
| 367 |
badStreamPanicf("Invalid typeid") |
| 368 |
} |
| 369 |
panic("shoulnd't get here") |
| 370 |
} |
| 371 |
|
| 372 |
func writeByteLenType(w io.Writer, ti typeInfo, buf []byte) (err error) { |
| 373 |
if ti.Size > 0xff { |
| 374 |
panic("Invalid size for BYTELEN_TYPE") |
| 375 |
} |
| 376 |
err = binary.Write(w, binary.LittleEndian, uint8(ti.Size)) |
| 377 |
if err != nil { |
| 378 |
return |
| 379 |
} |
| 380 |
_, err = w.Write(buf) |
| 381 |
return |
| 382 |
} |
| 383 |
|
| 384 |
func readShortLenType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 385 |
size := r.uint16() |
| 386 |
if size == 0xffff { |
| 387 |
return nil |
| 388 |
} |
| 389 |
r.ReadFull(ti.Buffer[:size]) |
| 390 |
buf := ti.Buffer[:size] |
| 391 |
switch ti.TypeId { |
| 392 |
case typeBigVarChar, typeBigChar: |
| 393 |
return decodeChar(ti.Collation, buf) |
| 394 |
case typeBigVarBin, typeBigBinary: |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
cpy := make([]byte, len(buf)) |
| 399 |
copy(cpy, buf) |
| 400 |
return cpy |
| 401 |
case typeNVarChar, typeNChar: |
| 402 |
return decodeNChar(buf) |
| 403 |
case typeUdt: |
| 404 |
return decodeUdt(*ti, buf) |
| 405 |
default: |
| 406 |
badStreamPanicf("Invalid typeid") |
| 407 |
} |
| 408 |
panic("shoulnd't get here") |
| 409 |
} |
| 410 |
|
| 411 |
func writeShortLenType(w io.Writer, ti typeInfo, buf []byte) (err error) { |
| 412 |
if buf == nil { |
| 413 |
err = binary.Write(w, binary.LittleEndian, uint16(0xffff)) |
| 414 |
return |
| 415 |
} |
| 416 |
if ti.Size > 0xfffe { |
| 417 |
panic("Invalid size for USHORTLEN_TYPE") |
| 418 |
} |
| 419 |
err = binary.Write(w, binary.LittleEndian, uint16(ti.Size)) |
| 420 |
if err != nil { |
| 421 |
return |
| 422 |
} |
| 423 |
_, err = w.Write(buf) |
| 424 |
return |
| 425 |
} |
| 426 |
|
| 427 |
func readLongLenType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
textptrsize := int(r.byte()) |
| 433 |
if textptrsize == 0 { |
| 434 |
return nil |
| 435 |
} |
| 436 |
textptr := make([]byte, textptrsize) |
| 437 |
r.ReadFull(textptr) |
| 438 |
timestamp := r.uint64() |
| 439 |
_ = timestamp |
| 440 |
size := r.int32() |
| 441 |
if size == -1 { |
| 442 |
return nil |
| 443 |
} |
| 444 |
buf := make([]byte, size) |
| 445 |
r.ReadFull(buf) |
| 446 |
switch ti.TypeId { |
| 447 |
case typeText: |
| 448 |
return decodeChar(ti.Collation, buf) |
| 449 |
case typeImage: |
| 450 |
return buf |
| 451 |
case typeNText: |
| 452 |
return decodeNChar(buf) |
| 453 |
default: |
| 454 |
badStreamPanicf("Invalid typeid") |
| 455 |
} |
| 456 |
panic("shoulnd't get here") |
| 457 |
} |
| 458 |
func writeLongLenType(w io.Writer, ti typeInfo, buf []byte) (err error) { |
| 459 |
|
| 460 |
err = binary.Write(w, binary.LittleEndian, byte(0x10)) |
| 461 |
if err != nil { |
| 462 |
return |
| 463 |
} |
| 464 |
err = binary.Write(w, binary.LittleEndian, uint64(0xFFFFFFFFFFFFFFFF)) |
| 465 |
if err != nil { |
| 466 |
return |
| 467 |
} |
| 468 |
err = binary.Write(w, binary.LittleEndian, uint64(0xFFFFFFFFFFFFFFFF)) |
| 469 |
if err != nil { |
| 470 |
return |
| 471 |
} |
| 472 |
|
| 473 |
err = binary.Write(w, binary.LittleEndian, uint64(0xFFFFFFFFFFFFFFFF)) |
| 474 |
if err != nil { |
| 475 |
return |
| 476 |
} |
| 477 |
|
| 478 |
err = binary.Write(w, binary.LittleEndian, uint32(ti.Size)) |
| 479 |
if err != nil { |
| 480 |
return |
| 481 |
} |
| 482 |
_, err = w.Write(buf) |
| 483 |
return |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
func readVariantType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 489 |
size := r.int32() |
| 490 |
if size == 0 { |
| 491 |
return nil |
| 492 |
} |
| 493 |
vartype := r.byte() |
| 494 |
propbytes := int32(r.byte()) |
| 495 |
switch vartype { |
| 496 |
case typeGuid: |
| 497 |
buf := make([]byte, size-2-propbytes) |
| 498 |
r.ReadFull(buf) |
| 499 |
return buf |
| 500 |
case typeBit: |
| 501 |
return r.byte() != 0 |
| 502 |
case typeInt1: |
| 503 |
return int64(r.byte()) |
| 504 |
case typeInt2: |
| 505 |
return int64(int16(r.uint16())) |
| 506 |
case typeInt4: |
| 507 |
return int64(r.int32()) |
| 508 |
case typeInt8: |
| 509 |
return int64(r.uint64()) |
| 510 |
case typeDateTime: |
| 511 |
buf := make([]byte, size-2-propbytes) |
| 512 |
r.ReadFull(buf) |
| 513 |
return decodeDateTime(buf) |
| 514 |
case typeDateTim4: |
| 515 |
buf := make([]byte, size-2-propbytes) |
| 516 |
r.ReadFull(buf) |
| 517 |
return decodeDateTim4(buf) |
| 518 |
case typeFlt4: |
| 519 |
return float64(math.Float32frombits(r.uint32())) |
| 520 |
case typeFlt8: |
| 521 |
return math.Float64frombits(r.uint64()) |
| 522 |
case typeMoney4: |
| 523 |
buf := make([]byte, size-2-propbytes) |
| 524 |
r.ReadFull(buf) |
| 525 |
return decodeMoney4(buf) |
| 526 |
case typeMoney: |
| 527 |
buf := make([]byte, size-2-propbytes) |
| 528 |
r.ReadFull(buf) |
| 529 |
return decodeMoney(buf) |
| 530 |
case typeDateN: |
| 531 |
buf := make([]byte, size-2-propbytes) |
| 532 |
r.ReadFull(buf) |
| 533 |
return decodeDate(buf) |
| 534 |
case typeTimeN: |
| 535 |
scale := r.byte() |
| 536 |
buf := make([]byte, size-2-propbytes) |
| 537 |
r.ReadFull(buf) |
| 538 |
return decodeTime(scale, buf) |
| 539 |
case typeDateTime2N: |
| 540 |
scale := r.byte() |
| 541 |
buf := make([]byte, size-2-propbytes) |
| 542 |
r.ReadFull(buf) |
| 543 |
return decodeDateTime2(scale, buf) |
| 544 |
case typeDateTimeOffsetN: |
| 545 |
scale := r.byte() |
| 546 |
buf := make([]byte, size-2-propbytes) |
| 547 |
r.ReadFull(buf) |
| 548 |
return decodeDateTimeOffset(scale, buf) |
| 549 |
case typeBigVarBin, typeBigBinary: |
| 550 |
r.uint16() |
| 551 |
buf := make([]byte, size-2-propbytes) |
| 552 |
r.ReadFull(buf) |
| 553 |
return buf |
| 554 |
case typeDecimalN, typeNumericN: |
| 555 |
prec := r.byte() |
| 556 |
scale := r.byte() |
| 557 |
buf := make([]byte, size-2-propbytes) |
| 558 |
r.ReadFull(buf) |
| 559 |
return decodeDecimal(prec, scale, buf) |
| 560 |
case typeBigVarChar, typeBigChar: |
| 561 |
col := readCollation(r) |
| 562 |
r.uint16() |
| 563 |
buf := make([]byte, size-2-propbytes) |
| 564 |
r.ReadFull(buf) |
| 565 |
return decodeChar(col, buf) |
| 566 |
case typeNVarChar, typeNChar: |
| 567 |
_ = readCollation(r) |
| 568 |
r.uint16() |
| 569 |
buf := make([]byte, size-2-propbytes) |
| 570 |
r.ReadFull(buf) |
| 571 |
return decodeNChar(buf) |
| 572 |
default: |
| 573 |
badStreamPanicf("Invalid variant typeid") |
| 574 |
} |
| 575 |
panic("shoulnd't get here") |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
func readPLPType(ti *typeInfo, r *tdsBuffer) (interface{}) { |
| 581 |
size := r.uint64() |
| 582 |
var buf *bytes.Buffer |
| 583 |
switch size { |
| 584 |
case PLP_NULL: |
| 585 |
|
| 586 |
return nil |
| 587 |
case UNKNOWN_PLP_LEN: |
| 588 |
|
| 589 |
buf = bytes.NewBuffer(make([]byte, 0, 1000)) |
| 590 |
default: |
| 591 |
buf = bytes.NewBuffer(make([]byte, 0, size)) |
| 592 |
} |
| 593 |
for true { |
| 594 |
chunksize := r.uint32() |
| 595 |
if chunksize == 0 { |
| 596 |
break |
| 597 |
} |
| 598 |
if _, err := io.CopyN(buf, r, int64(chunksize)); err != nil { |
| 599 |
badStreamPanicf("Reading PLP type failed: %s", err.Error()) |
| 600 |
} |
| 601 |
} |
| 602 |
switch ti.TypeId { |
| 603 |
case typeXml: |
| 604 |
return decodeXml(*ti, buf.Bytes()) |
| 605 |
case typeBigVarChar, typeBigChar, typeText: |
| 606 |
return decodeChar(ti.Collation, buf.Bytes()) |
| 607 |
case typeBigVarBin, typeBigBinary, typeImage: |
| 608 |
return buf.Bytes() |
| 609 |
case typeNVarChar, typeNChar, typeNText: |
| 610 |
return decodeNChar(buf.Bytes()) |
| 611 |
case typeUdt: |
| 612 |
return decodeUdt(*ti, buf.Bytes()) |
| 613 |
} |
| 614 |
panic("shoulnd't get here") |
| 615 |
} |
| 616 |
|
| 617 |
func writePLPType(w io.Writer, ti typeInfo, buf []byte) (err error) { |
| 618 |
if err = binary.Write(w, binary.LittleEndian, uint64(UNKNOWN_PLP_LEN)); err != nil { |
| 619 |
return |
| 620 |
} |
| 621 |
for { |
| 622 |
chunksize := uint32(len(buf)) |
| 623 |
if chunksize == 0 { |
| 624 |
err = binary.Write(w, binary.LittleEndian, uint32(PLP_TERMINATOR)) |
| 625 |
return |
| 626 |
} |
| 627 |
if err = binary.Write(w, binary.LittleEndian, chunksize); err != nil { |
| 628 |
return |
| 629 |
} |
| 630 |
if _, err = w.Write(buf[:chunksize]); err != nil { |
| 631 |
return |
| 632 |
} |
| 633 |
buf = buf[chunksize:] |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
func readVarLen(ti *typeInfo, r *tdsBuffer) { |
| 638 |
switch ti.TypeId { |
| 639 |
case typeDateN: |
| 640 |
ti.Size = 3 |
| 641 |
ti.Reader = readByteLenType |
| 642 |
ti.Buffer = make([]byte, ti.Size) |
| 643 |
case typeTimeN, typeDateTime2N, typeDateTimeOffsetN: |
| 644 |
ti.Scale = r.byte() |
| 645 |
switch ti.Scale { |
| 646 |
case 0, 1, 2: |
| 647 |
ti.Size = 3 |
| 648 |
case 3, 4: |
| 649 |
ti.Size = 4 |
| 650 |
case 5, 6, 7: |
| 651 |
ti.Size = 5 |
| 652 |
default: |
| 653 |
badStreamPanicf("Invalid scale for TIME/DATETIME2/DATETIMEOFFSET type") |
| 654 |
} |
| 655 |
switch ti.TypeId { |
| 656 |
case typeDateTime2N: |
| 657 |
ti.Size += 3 |
| 658 |
case typeDateTimeOffsetN: |
| 659 |
ti.Size += 5 |
| 660 |
} |
| 661 |
ti.Reader = readByteLenType |
| 662 |
ti.Buffer = make([]byte, ti.Size) |
| 663 |
case typeGuid, typeIntN, typeDecimal, typeNumeric, |
| 664 |
typeBitN, typeDecimalN, typeNumericN, typeFltN, |
| 665 |
typeMoneyN, typeDateTimeN, typeChar, |
| 666 |
typeVarChar, typeBinary, typeVarBinary: |
| 667 |
|
| 668 |
ti.Size = int(r.byte()) |
| 669 |
ti.Buffer = make([]byte, ti.Size) |
| 670 |
switch ti.TypeId { |
| 671 |
case typeDecimal, typeNumeric, typeDecimalN, typeNumericN: |
| 672 |
ti.Prec = r.byte() |
| 673 |
ti.Scale = r.byte() |
| 674 |
} |
| 675 |
ti.Reader = readByteLenType |
| 676 |
case typeXml: |
| 677 |
ti.XmlInfo.SchemaPresent = r.byte() |
| 678 |
if ti.XmlInfo.SchemaPresent != 0 { |
| 679 |
|
| 680 |
ti.XmlInfo.DBName = r.BVarChar() |
| 681 |
|
| 682 |
ti.XmlInfo.OwningSchema = r.BVarChar() |
| 683 |
|
| 684 |
ti.XmlInfo.XmlSchemaCollection = r.UsVarChar() |
| 685 |
} |
| 686 |
ti.Reader = readPLPType |
| 687 |
case typeUdt: |
| 688 |
ti.Size = int(r.uint16()) |
| 689 |
ti.UdtInfo.DBName = r.BVarChar() |
| 690 |
ti.UdtInfo.SchemaName = r.BVarChar() |
| 691 |
ti.UdtInfo.TypeName = r.BVarChar() |
| 692 |
ti.UdtInfo.AssemblyQualifiedName = r.UsVarChar() |
| 693 |
|
| 694 |
ti.Buffer = make([]byte, ti.Size) |
| 695 |
ti.Reader = readPLPType |
| 696 |
case typeBigVarBin, typeBigVarChar, typeBigBinary, typeBigChar, |
| 697 |
typeNVarChar, typeNChar: |
| 698 |
|
| 699 |
ti.Size = int(r.uint16()) |
| 700 |
switch ti.TypeId { |
| 701 |
case typeBigVarChar, typeBigChar, typeNVarChar, typeNChar: |
| 702 |
ti.Collation = readCollation(r) |
| 703 |
} |
| 704 |
if ti.Size == 0xffff { |
| 705 |
ti.Reader = readPLPType |
| 706 |
} else { |
| 707 |
ti.Buffer = make([]byte, ti.Size) |
| 708 |
ti.Reader = readShortLenType |
| 709 |
} |
| 710 |
case typeText, typeImage, typeNText, typeVariant: |
| 711 |
|
| 712 |
ti.Size = int(r.int32()) |
| 713 |
switch ti.TypeId { |
| 714 |
case typeText, typeNText: |
| 715 |
ti.Collation = readCollation(r) |
| 716 |
|
| 717 |
numparts := int(r.byte()) |
| 718 |
for i := 0; i < numparts; i++ { |
| 719 |
r.UsVarChar() |
| 720 |
} |
| 721 |
ti.Reader = readLongLenType |
| 722 |
case typeImage: |
| 723 |
|
| 724 |
numparts := int(r.byte()) |
| 725 |
for i := 0; i < numparts; i++ { |
| 726 |
r.UsVarChar() |
| 727 |
} |
| 728 |
ti.Reader = readLongLenType |
| 729 |
case typeVariant: |
| 730 |
ti.Reader = readVariantType |
| 731 |
} |
| 732 |
default: |
| 733 |
badStreamPanicf("Invalid type %d", ti.TypeId) |
| 734 |
} |
| 735 |
return |
| 736 |
} |
| 737 |
|
| 738 |
func decodeMoney(buf []byte) []byte { |
| 739 |
money := int64(uint64(buf[4]) | |
| 740 |
uint64(buf[5])<<8 | |
| 741 |
uint64(buf[6])<<16 | |
| 742 |
uint64(buf[7])<<24 | |
| 743 |
uint64(buf[0])<<32 | |
| 744 |
uint64(buf[1])<<40 | |
| 745 |
uint64(buf[2])<<48 | |
| 746 |
uint64(buf[3])<<56) |
| 747 |
return scaleBytes(strconv.FormatInt(money, 10), 4) |
| 748 |
} |
| 749 |
|
| 750 |
func decodeMoney4(buf []byte) []byte { |
| 751 |
money := int32(binary.LittleEndian.Uint32(buf[0:4])) |
| 752 |
return scaleBytes(strconv.FormatInt(int64(money), 10), 4) |
| 753 |
} |
| 754 |
|
| 755 |
func decodeGuid(buf []byte) []byte { |
| 756 |
res := make([]byte, 16) |
| 757 |
copy(res, buf) |
| 758 |
return res |
| 759 |
} |
| 760 |
|
| 761 |
func decodeDecimal(prec uint8, scale uint8, buf []byte) []byte { |
| 762 |
var sign uint8 |
| 763 |
sign = buf[0] |
| 764 |
dec := Decimal{ |
| 765 |
positive: sign != 0, |
| 766 |
prec: prec, |
| 767 |
scale: scale, |
| 768 |
} |
| 769 |
buf = buf[1:] |
| 770 |
l := len(buf) / 4 |
| 771 |
for i := 0; i < l; i++ { |
| 772 |
dec.integer[i] = binary.LittleEndian.Uint32(buf[0:4]) |
| 773 |
buf = buf[4:] |
| 774 |
} |
| 775 |
return dec.Bytes() |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
func decodeDateInt(buf []byte) (days int) { |
| 780 |
days = int(buf[0]) + int(buf[1])*256 + int(buf[2])*256*256 |
| 781 |
return |
| 782 |
} |
| 783 |
|
| 784 |
func decodeDate(buf []byte) time.Time { |
| 785 |
return time.Date(1, 1, 1+decodeDateInt(buf), 0, 0, 0, 0, time.UTC) |
| 786 |
} |
| 787 |
|
| 788 |
func decodeTimeInt(scale uint8, buf []byte) (sec int, ns int) { |
| 789 |
var acc uint64 = 0 |
| 790 |
for i := len(buf) - 1; i >= 0; i-- { |
| 791 |
acc <<= 8 |
| 792 |
acc |= uint64(buf[i]) |
| 793 |
} |
| 794 |
for i := 0; i < 7-int(scale); i++ { |
| 795 |
acc *= 10 |
| 796 |
} |
| 797 |
nsbig := acc * 100 |
| 798 |
sec = int(nsbig / 1000000000) |
| 799 |
ns = int(nsbig % 1000000000) |
| 800 |
return |
| 801 |
} |
| 802 |
|
| 803 |
func decodeTime(scale uint8, buf []byte) time.Time { |
| 804 |
sec, ns := decodeTimeInt(scale, buf) |
| 805 |
return time.Date(1, 1, 1, 0, 0, sec, ns, time.UTC) |
| 806 |
} |
| 807 |
|
| 808 |
func decodeDateTime2(scale uint8, buf []byte) time.Time { |
| 809 |
timesize := len(buf) - 3 |
| 810 |
sec, ns := decodeTimeInt(scale, buf[:timesize]) |
| 811 |
days := decodeDateInt(buf[timesize:]) |
| 812 |
return time.Date(1, 1, 1+days, 0, 0, sec, ns, time.UTC) |
| 813 |
} |
| 814 |
|
| 815 |
func decodeDateTimeOffset(scale uint8, buf []byte) time.Time { |
| 816 |
timesize := len(buf) - 3 - 2 |
| 817 |
sec, ns := decodeTimeInt(scale, buf[:timesize]) |
| 818 |
buf = buf[timesize:] |
| 819 |
days := decodeDateInt(buf[:3]) |
| 820 |
buf = buf[3:] |
| 821 |
offset := int(int16(binary.LittleEndian.Uint16(buf))) |
| 822 |
return time.Date(1, 1, 1+days, 0, 0, sec+offset*60, ns, |
| 823 |
time.FixedZone("", offset*60)) |
| 824 |
} |
| 825 |
|
| 826 |
func divFloor(x int64, y int64) int64 { |
| 827 |
q := x / y |
| 828 |
r := x % y |
| 829 |
if r != 0 && ((r < 0) != (y < 0)) { |
| 830 |
q-- |
| 831 |
} |
| 832 |
return q |
| 833 |
} |
| 834 |
|
| 835 |
func dateTime2(t time.Time) (days int32, ns int64) { |
| 836 |
|
| 837 |
days64 := divFloor(t.Unix(), 24*60*60) |
| 838 |
|
| 839 |
days = int32(days64) + 1969*365 + 1969/4 - 1969/100 + 1969/400 |
| 840 |
|
| 841 |
secs := t.Unix() - days64*24*60*60 |
| 842 |
|
| 843 |
ns = secs*1e9 + int64(t.Nanosecond()) |
| 844 |
return |
| 845 |
} |
| 846 |
|
| 847 |
func decodeChar(col collation, buf []byte) string { |
| 848 |
return charset2utf8(col, buf) |
| 849 |
} |
| 850 |
|
| 851 |
func decodeUcs2(buf []byte) string { |
| 852 |
res, err := ucs22str(buf) |
| 853 |
if err != nil { |
| 854 |
badStreamPanicf("Invalid UCS2 encoding: %s", err.Error()) |
| 855 |
} |
| 856 |
return res |
| 857 |
} |
| 858 |
|
| 859 |
func decodeNChar(buf []byte) string { |
| 860 |
return decodeUcs2(buf) |
| 861 |
} |
| 862 |
|
| 863 |
func decodeXml(ti typeInfo, buf []byte) string { |
| 864 |
return decodeUcs2(buf) |
| 865 |
} |
| 866 |
|
| 867 |
func decodeUdt(ti typeInfo, buf []byte) []byte { |
| 868 |
return buf |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
func makeGoLangScanType(ti typeInfo) reflect.Type { |
| 876 |
switch ti.TypeId { |
| 877 |
case typeInt4: |
| 878 |
return reflect.TypeOf(int64(0)) |
| 879 |
case typeInt8: |
| 880 |
return reflect.TypeOf(int64(0)) |
| 881 |
case typeFlt4: |
| 882 |
return reflect.TypeOf(float64(0)) |
| 883 |
case typeIntN: |
| 884 |
switch ti.Size { |
| 885 |
case 1: |
| 886 |
return reflect.TypeOf(int64(0)) |
| 887 |
case 2: |
| 888 |
return reflect.TypeOf(int64(0)) |
| 889 |
case 4: |
| 890 |
return reflect.TypeOf(int64(0)) |
| 891 |
case 8: |
| 892 |
return reflect.TypeOf(int64(0)) |
| 893 |
default: |
| 894 |
panic("invalid size of INTNTYPE") |
| 895 |
} |
| 896 |
case typeFlt8: |
| 897 |
return reflect.TypeOf(float64(0)) |
| 898 |
case typeFltN: |
| 899 |
switch ti.Size { |
| 900 |
case 4: |
| 901 |
return reflect.TypeOf(float64(0)) |
| 902 |
case 8: |
| 903 |
return reflect.TypeOf(float64(0)) |
| 904 |
default: |
| 905 |
panic("invalid size of FLNNTYPE") |
| 906 |
} |
| 907 |
case typeBigVarBin: |
| 908 |
return reflect.TypeOf([]byte{}) |
| 909 |
case typeVarChar: |
| 910 |
return reflect.TypeOf("") |
| 911 |
case typeNVarChar: |
| 912 |
return reflect.TypeOf("") |
| 913 |
case typeBit, typeBitN: |
| 914 |
return reflect.TypeOf(true) |
| 915 |
case typeDecimalN, typeNumericN: |
| 916 |
return reflect.TypeOf([]byte{}) |
| 917 |
case typeMoneyN: |
| 918 |
switch ti.Size { |
| 919 |
case 4: |
| 920 |
return reflect.TypeOf([]byte{}) |
| 921 |
case 8: |
| 922 |
return reflect.TypeOf([]byte{}) |
| 923 |
default: |
| 924 |
panic("invalid size of MONEYN") |
| 925 |
} |
| 926 |
case typeDateTim4: |
| 927 |
return reflect.TypeOf(time.Time{}) |
| 928 |
case typeDateTime: |
| 929 |
return reflect.TypeOf(time.Time{}) |
| 930 |
case typeDateTimeN: |
| 931 |
switch ti.Size { |
| 932 |
case 4: |
| 933 |
return reflect.TypeOf(time.Time{}) |
| 934 |
case 8: |
| 935 |
return reflect.TypeOf(time.Time{}) |
| 936 |
default: |
| 937 |
panic("invalid size of DATETIMEN") |
| 938 |
} |
| 939 |
case typeDateTime2N: |
| 940 |
return reflect.TypeOf(time.Time{}) |
| 941 |
case typeDateN: |
| 942 |
return reflect.TypeOf(time.Time{}) |
| 943 |
case typeTimeN: |
| 944 |
return reflect.TypeOf(time.Time{}) |
| 945 |
case typeDateTimeOffsetN: |
| 946 |
return reflect.TypeOf(time.Time{}) |
| 947 |
case typeBigVarChar: |
| 948 |
return reflect.TypeOf("") |
| 949 |
case typeBigChar: |
| 950 |
return reflect.TypeOf("") |
| 951 |
case typeNChar: |
| 952 |
return reflect.TypeOf("") |
| 953 |
case typeGuid: |
| 954 |
return reflect.TypeOf([]byte{}) |
| 955 |
case typeXml: |
| 956 |
return reflect.TypeOf("") |
| 957 |
case typeText: |
| 958 |
return reflect.TypeOf("") |
| 959 |
case typeNText: |
| 960 |
return reflect.TypeOf("") |
| 961 |
case typeImage: |
| 962 |
return reflect.TypeOf([]byte{}) |
| 963 |
case typeVariant: |
| 964 |
return reflect.TypeOf(nil) |
| 965 |
default: |
| 966 |
panic(fmt.Sprintf("not implemented makeDecl for type %d", ti.TypeId)) |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
func makeDecl(ti typeInfo) string { |
| 971 |
switch ti.TypeId { |
| 972 |
case typeInt1: |
| 973 |
return "tinyint" |
| 974 |
case typeInt2: |
| 975 |
return "smallint" |
| 976 |
case typeInt4: |
| 977 |
return "int" |
| 978 |
case typeInt8: |
| 979 |
return "bigint" |
| 980 |
case typeFlt4: |
| 981 |
return "real" |
| 982 |
case typeIntN: |
| 983 |
switch ti.Size { |
| 984 |
case 1: |
| 985 |
return "tinyint" |
| 986 |
case 2: |
| 987 |
return "smallint" |
| 988 |
case 4: |
| 989 |
return "int" |
| 990 |
case 8: |
| 991 |
return "bigint" |
| 992 |
default: |
| 993 |
panic("invalid size of INTNTYPE") |
| 994 |
} |
| 995 |
case typeFlt8: |
| 996 |
return "float" |
| 997 |
case typeFltN: |
| 998 |
switch ti.Size { |
| 999 |
case 4: |
| 1000 |
return "real" |
| 1001 |
case 8: |
| 1002 |
return "float" |
| 1003 |
default: |
| 1004 |
panic("invalid size of FLNNTYPE") |
| 1005 |
} |
| 1006 |
case typeDecimal, typeDecimalN: |
| 1007 |
return fmt.Sprintf("decimal(%d, %d)", ti.Prec, ti.Scale) |
| 1008 |
case typeMoney4: |
| 1009 |
return "smallmoney" |
| 1010 |
case typeMoney: |
| 1011 |
return "money" |
| 1012 |
case typeMoneyN: |
| 1013 |
switch ti.Size { |
| 1014 |
case 4: |
| 1015 |
return "smallmoney" |
| 1016 |
case 8: |
| 1017 |
return "money" |
| 1018 |
default: |
| 1019 |
panic("invalid size of MONEYNTYPE") |
| 1020 |
} |
| 1021 |
case typeBigVarBin: |
| 1022 |
if ti.Size > 8000 || ti.Size == 0 { |
| 1023 |
return "varbinary(max)" |
| 1024 |
} else { |
| 1025 |
return fmt.Sprintf("varbinary(%d)", ti.Size) |
| 1026 |
} |
| 1027 |
case typeNChar: |
| 1028 |
return fmt.Sprintf("nchar(%d)", ti.Size/2) |
| 1029 |
case typeBigChar, typeChar: |
| 1030 |
return fmt.Sprintf("char(%d)", ti.Size) |
| 1031 |
case typeBigVarChar, typeVarChar: |
| 1032 |
if ti.Size > 4000 || ti.Size == 0 { |
| 1033 |
return fmt.Sprintf("varchar(max)") |
| 1034 |
} else { |
| 1035 |
return fmt.Sprintf("varchar(%d)", ti.Size) |
| 1036 |
} |
| 1037 |
case typeNVarChar: |
| 1038 |
if ti.Size > 8000 || ti.Size == 0 { |
| 1039 |
return "nvarchar(max)" |
| 1040 |
} else { |
| 1041 |
return fmt.Sprintf("nvarchar(%d)", ti.Size/2) |
| 1042 |
} |
| 1043 |
case typeBit, typeBitN: |
| 1044 |
return "bit" |
| 1045 |
case typeDateTim4: |
| 1046 |
return "smalldatetime" |
| 1047 |
case typeDateN: |
| 1048 |
return "date" |
| 1049 |
case typeDateTime: |
| 1050 |
return "datetime" |
| 1051 |
case typeDateTimeN: |
| 1052 |
switch ti.Size { |
| 1053 |
case 4: |
| 1054 |
return "smalldatetime" |
| 1055 |
case 8: |
| 1056 |
return "datetime" |
| 1057 |
default: |
| 1058 |
panic("invalid size of DATETIMNTYPE") |
| 1059 |
} |
| 1060 |
case typeDateTime2N: |
| 1061 |
return fmt.Sprintf("datetime2(%d)", ti.Scale) |
| 1062 |
case typeDateTimeOffsetN: |
| 1063 |
return fmt.Sprintf("datetimeoffset(%d)", ti.Scale) |
| 1064 |
case typeText: |
| 1065 |
return "text" |
| 1066 |
case typeNText: |
| 1067 |
return "ntext" |
| 1068 |
case typeUdt: |
| 1069 |
return ti.UdtInfo.TypeName |
| 1070 |
default: |
| 1071 |
panic(fmt.Sprintf("not implemented makeDecl for type %#x", ti.TypeId)) |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
func makeGoLangTypeName(ti typeInfo) string { |
| 1082 |
switch ti.TypeId { |
| 1083 |
case typeInt4: |
| 1084 |
return "INT" |
| 1085 |
case typeInt8: |
| 1086 |
return "BIGINT" |
| 1087 |
case typeFlt4: |
| 1088 |
return "REAL" |
| 1089 |
case typeIntN: |
| 1090 |
switch ti.Size { |
| 1091 |
case 1: |
| 1092 |
return "TINYINT" |
| 1093 |
case 2: |
| 1094 |
return "SMALLINT" |
| 1095 |
case 4: |
| 1096 |
return "INT" |
| 1097 |
case 8: |
| 1098 |
return "BIGINT" |
| 1099 |
default: |
| 1100 |
panic("invalid size of INTNTYPE") |
| 1101 |
} |
| 1102 |
case typeFlt8: |
| 1103 |
return "FLOAT" |
| 1104 |
case typeFltN: |
| 1105 |
switch ti.Size { |
| 1106 |
case 4: |
| 1107 |
return "REAL" |
| 1108 |
case 8: |
| 1109 |
return "FLOAT" |
| 1110 |
default: |
| 1111 |
panic("invalid size of FLNNTYPE") |
| 1112 |
} |
| 1113 |
case typeBigVarBin: |
| 1114 |
return "VARBINARY" |
| 1115 |
case typeVarChar: |
| 1116 |
return "VARCHAR" |
| 1117 |
case typeNVarChar: |
| 1118 |
return "NVARCHAR" |
| 1119 |
case typeBit, typeBitN: |
| 1120 |
return "BIT" |
| 1121 |
case typeDecimalN, typeNumericN: |
| 1122 |
return "DECIMAL" |
| 1123 |
case typeMoneyN: |
| 1124 |
switch ti.Size { |
| 1125 |
case 4: |
| 1126 |
return "SMALLMONEY" |
| 1127 |
case 8: |
| 1128 |
return "MONEY" |
| 1129 |
default: |
| 1130 |
panic("invalid size of MONEYN") |
| 1131 |
} |
| 1132 |
case typeDateTimeN: |
| 1133 |
switch ti.Size { |
| 1134 |
case 4: |
| 1135 |
return "SMALLDATETIME" |
| 1136 |
case 8: |
| 1137 |
return "DATETIME" |
| 1138 |
default: |
| 1139 |
panic("invalid size of DATETIMEN") |
| 1140 |
} |
| 1141 |
case typeDateTime2N: |
| 1142 |
return "DATETIME2" |
| 1143 |
case typeDateN: |
| 1144 |
return "DATE" |
| 1145 |
case typeTimeN: |
| 1146 |
return "TIME" |
| 1147 |
case typeDateTimeOffsetN: |
| 1148 |
return "DATETIMEOFFSET" |
| 1149 |
case typeBigVarChar: |
| 1150 |
return "VARCHAR" |
| 1151 |
case typeBigChar: |
| 1152 |
return "CHAR" |
| 1153 |
case typeNChar: |
| 1154 |
return "NCHAR" |
| 1155 |
case typeGuid: |
| 1156 |
return "UNIQUEIDENTIFIER" |
| 1157 |
case typeXml: |
| 1158 |
return "XML" |
| 1159 |
case typeText: |
| 1160 |
return "TEXT" |
| 1161 |
case typeNText: |
| 1162 |
return "NTEXT" |
| 1163 |
case typeImage: |
| 1164 |
return "IMAGE" |
| 1165 |
case typeVariant: |
| 1166 |
return "SQL_VARIANT" |
| 1167 |
default: |
| 1168 |
panic(fmt.Sprintf("not implemented makeDecl for type %d", ti.TypeId)) |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
func makeGoLangTypeLength(ti typeInfo) (int64, bool) { |
| 1185 |
switch ti.TypeId { |
| 1186 |
case typeInt4: |
| 1187 |
return 0, false |
| 1188 |
case typeInt8: |
| 1189 |
return 0, false |
| 1190 |
case typeFlt4: |
| 1191 |
return 0, false |
| 1192 |
case typeIntN: |
| 1193 |
switch ti.Size { |
| 1194 |
case 1: |
| 1195 |
return 0, false |
| 1196 |
case 2: |
| 1197 |
return 0, false |
| 1198 |
case 4: |
| 1199 |
return 0, false |
| 1200 |
case 8: |
| 1201 |
return 0, false |
| 1202 |
default: |
| 1203 |
panic("invalid size of INTNTYPE") |
| 1204 |
} |
| 1205 |
case typeFlt8: |
| 1206 |
return 0, false |
| 1207 |
case typeFltN: |
| 1208 |
switch ti.Size { |
| 1209 |
case 4: |
| 1210 |
return 0, false |
| 1211 |
case 8: |
| 1212 |
return 0, false |
| 1213 |
default: |
| 1214 |
panic("invalid size of FLNNTYPE") |
| 1215 |
} |
| 1216 |
case typeBit, typeBitN: |
| 1217 |
return 0, false |
| 1218 |
case typeDecimalN, typeNumericN: |
| 1219 |
return 0, false |
| 1220 |
case typeMoneyN: |
| 1221 |
switch ti.Size { |
| 1222 |
case 4: |
| 1223 |
return 0, false |
| 1224 |
case 8: |
| 1225 |
return 0, false |
| 1226 |
default: |
| 1227 |
panic("invalid size of MONEYN") |
| 1228 |
} |
| 1229 |
case typeDateTimeN: |
| 1230 |
switch ti.Size { |
| 1231 |
case 4: |
| 1232 |
return 0, false |
| 1233 |
case 8: |
| 1234 |
return 0, false |
| 1235 |
default: |
| 1236 |
panic("invalid size of DATETIMEN") |
| 1237 |
} |
| 1238 |
case typeDateTime2N: |
| 1239 |
return 0, false |
| 1240 |
case typeDateN: |
| 1241 |
return 0, false |
| 1242 |
case typeTimeN: |
| 1243 |
return 0, false |
| 1244 |
case typeDateTimeOffsetN: |
| 1245 |
return 0, false |
| 1246 |
case typeBigVarBin: |
| 1247 |
if ti.Size == 0xffff { |
| 1248 |
return 2147483645, true |
| 1249 |
} else { |
| 1250 |
return int64(ti.Size), true |
| 1251 |
} |
| 1252 |
case typeVarChar: |
| 1253 |
return int64(ti.Size), true |
| 1254 |
case typeBigVarChar: |
| 1255 |
if ti.Size == 0xffff { |
| 1256 |
return 2147483645, true |
| 1257 |
} else { |
| 1258 |
return int64(ti.Size), true |
| 1259 |
} |
| 1260 |
case typeBigChar: |
| 1261 |
return int64(ti.Size), true |
| 1262 |
case typeNVarChar: |
| 1263 |
if ti.Size == 0xffff { |
| 1264 |
return 2147483645 / 2, true |
| 1265 |
} else { |
| 1266 |
return int64(ti.Size) / 2, true |
| 1267 |
} |
| 1268 |
case typeNChar: |
| 1269 |
return int64(ti.Size) / 2, true |
| 1270 |
case typeGuid: |
| 1271 |
return 0, false |
| 1272 |
case typeXml: |
| 1273 |
return 1073741822, true |
| 1274 |
case typeText: |
| 1275 |
return 2147483647, true |
| 1276 |
case typeNText: |
| 1277 |
return 1073741823, true |
| 1278 |
case typeImage: |
| 1279 |
return 2147483647, true |
| 1280 |
case typeVariant: |
| 1281 |
return 0, false |
| 1282 |
default: |
| 1283 |
panic(fmt.Sprintf("not implemented makeDecl for type %d", ti.TypeId)) |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
func makeGoLangTypePrecisionScale(ti typeInfo) (int64, int64, bool) { |
| 1300 |
switch ti.TypeId { |
| 1301 |
case typeInt4: |
| 1302 |
return 0, 0, false |
| 1303 |
case typeInt8: |
| 1304 |
return 0, 0, false |
| 1305 |
case typeFlt4: |
| 1306 |
return 0, 0, false |
| 1307 |
case typeIntN: |
| 1308 |
switch ti.Size { |
| 1309 |
case 1: |
| 1310 |
return 0, 0, false |
| 1311 |
case 2: |
| 1312 |
return 0, 0, false |
| 1313 |
case 4: |
| 1314 |
return 0, 0, false |
| 1315 |
case 8: |
| 1316 |
return 0, 0, false |
| 1317 |
default: |
| 1318 |
panic("invalid size of INTNTYPE") |
| 1319 |
} |
| 1320 |
case typeFlt8: |
| 1321 |
return 0, 0, false |
| 1322 |
case typeFltN: |
| 1323 |
switch ti.Size { |
| 1324 |
case 4: |
| 1325 |
return 0, 0, false |
| 1326 |
case 8: |
| 1327 |
return 0, 0, false |
| 1328 |
default: |
| 1329 |
panic("invalid size of FLNNTYPE") |
| 1330 |
} |
| 1331 |
case typeBit, typeBitN: |
| 1332 |
return 0, 0, false |
| 1333 |
case typeDecimalN, typeNumericN: |
| 1334 |
return int64(ti.Prec), int64(ti.Scale), true |
| 1335 |
case typeMoneyN: |
| 1336 |
switch ti.Size { |
| 1337 |
case 4: |
| 1338 |
return 0, 0, false |
| 1339 |
case 8: |
| 1340 |
return 0, 0, false |
| 1341 |
default: |
| 1342 |
panic("invalid size of MONEYN") |
| 1343 |
} |
| 1344 |
case typeDateTimeN: |
| 1345 |
switch ti.Size { |
| 1346 |
case 4: |
| 1347 |
return 0, 0, false |
| 1348 |
case 8: |
| 1349 |
return 0, 0, false |
| 1350 |
default: |
| 1351 |
panic("invalid size of DATETIMEN") |
| 1352 |
} |
| 1353 |
case typeDateTime2N: |
| 1354 |
return 0, 0, false |
| 1355 |
case typeDateN: |
| 1356 |
return 0, 0, false |
| 1357 |
case typeTimeN: |
| 1358 |
return 0, 0, false |
| 1359 |
case typeDateTimeOffsetN: |
| 1360 |
return 0, 0, false |
| 1361 |
case typeBigVarBin: |
| 1362 |
return 0, 0, false |
| 1363 |
case typeVarChar: |
| 1364 |
return 0, 0, false |
| 1365 |
case typeBigVarChar: |
| 1366 |
return 0, 0, false |
| 1367 |
case typeBigChar: |
| 1368 |
return 0, 0, false |
| 1369 |
case typeNVarChar: |
| 1370 |
return 0, 0, false |
| 1371 |
case typeNChar: |
| 1372 |
return 0, 0, false |
| 1373 |
case typeGuid: |
| 1374 |
return 0, 0, false |
| 1375 |
case typeXml: |
| 1376 |
return 0, 0, false |
| 1377 |
case typeText: |
| 1378 |
return 0, 0, false |
| 1379 |
case typeNText: |
| 1380 |
return 0, 0, false |
| 1381 |
case typeImage: |
| 1382 |
return 0, 0, false |
| 1383 |
case typeVariant: |
| 1384 |
return 0, 0, false |
| 1385 |
default: |
| 1386 |
panic(fmt.Sprintf("not implemented makeDecl for type %d", ti.TypeId)) |
| 1387 |
} |
| 1388 |
} |